blob: 679a65a439f8beefa95f32da70b33036768f41e2 [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.*;
74
Doug Feltcb3791202011-07-07 11:57:48 -070075import com.android.internal.R;
76import com.android.internal.util.Predicate;
77import com.android.internal.view.menu.MenuBuilder;
78
Christopher Tatea0374192010-10-05 13:06:41 -070079import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070080import java.lang.reflect.InvocationTargetException;
81import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
83import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070084import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080085import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * <p>
89 * This class represents the basic building block for user interface components. A View
90 * occupies a rectangular area on the screen and is responsible for drawing and
91 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070092 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
94 * are invisible containers that hold other Views (or other ViewGroups) and define
95 * their layout properties.
96 * </p>
97 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070098 * <div class="special reference">
99 * <h3>Developer Guides</h3>
100 * <p>For information about using this class to develop your application's user interface,
101 * 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 -0800102 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * <a name="Using"></a>
105 * <h3>Using Views</h3>
106 * <p>
107 * All of the views in a window are arranged in a single tree. You can add views
108 * either from code or by specifying a tree of views in one or more XML layout
109 * files. There are many specialized subclasses of views that act as controls or
110 * are capable of displaying text, images, or other content.
111 * </p>
112 * <p>
113 * Once you have created a tree of views, there are typically a few types of
114 * common operations you may wish to perform:
115 * <ul>
116 * <li><strong>Set properties:</strong> for example setting the text of a
117 * {@link android.widget.TextView}. The available properties and the methods
118 * that set them will vary among the different subclasses of views. Note that
119 * properties that are known at build time can be set in the XML layout
120 * files.</li>
121 * <li><strong>Set focus:</strong> The framework will handled moving focus in
122 * response to user input. To force focus to a specific view, call
123 * {@link #requestFocus}.</li>
124 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
125 * that will be notified when something interesting happens to the view. For
126 * example, all views will let you set a listener to be notified when the view
127 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700128 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
129 * Other view subclasses offer more specialized listeners. For example, a Button
130 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * </ul>
134 * </p>
135 * <p><em>
136 * Note: The Android framework is responsible for measuring, laying out and
137 * drawing views. You should not call methods that perform these actions on
138 * views yourself unless you are actually implementing a
139 * {@link android.view.ViewGroup}.
140 * </em></p>
141 *
142 * <a name="Lifecycle"></a>
143 * <h3>Implementing a Custom View</h3>
144 *
145 * <p>
146 * To implement a custom view, you will usually begin by providing overrides for
147 * some of the standard methods that the framework calls on all views. You do
148 * not need to override all of these methods. In fact, you can start by just
149 * overriding {@link #onDraw(android.graphics.Canvas)}.
150 * <table border="2" width="85%" align="center" cellpadding="5">
151 * <thead>
152 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
153 * </thead>
154 *
155 * <tbody>
156 * <tr>
157 * <td rowspan="2">Creation</td>
158 * <td>Constructors</td>
159 * <td>There is a form of the constructor that are called when the view
160 * is created from code and a form that is called when the view is
161 * inflated from a layout file. The second form should parse and apply
162 * any attributes defined in the layout file.
163 * </td>
164 * </tr>
165 * <tr>
166 * <td><code>{@link #onFinishInflate()}</code></td>
167 * <td>Called after a view and all of its children has been inflated
168 * from XML.</td>
169 * </tr>
170 *
171 * <tr>
172 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700173 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * <td>Called to determine the size requirements for this view and all
175 * of its children.
176 * </td>
177 * </tr>
178 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700179 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <td>Called when this view should assign a size and position to all
181 * of its children.
182 * </td>
183 * </tr>
184 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700185 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * <td>Called when the size of this view has changed.
187 * </td>
188 * </tr>
189 *
190 * <tr>
191 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the view should render its content.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when a new key event occurs.
201 * </td>
202 * </tr>
203 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700204 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * <td>Called when a key up event occurs.
206 * </td>
207 * </tr>
208 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700209 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <td>Called when a trackball motion event occurs.
211 * </td>
212 * </tr>
213 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * <td>Called when a touch screen motion event occurs.
216 * </td>
217 * </tr>
218 *
219 * <tr>
220 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when the view gains or loses focus.
223 * </td>
224 * </tr>
225 *
226 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700227 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <td>Called when the window containing the view gains or loses focus.
229 * </td>
230 * </tr>
231 *
232 * <tr>
233 * <td rowspan="3">Attaching</td>
234 * <td><code>{@link #onAttachedToWindow()}</code></td>
235 * <td>Called when the view is attached to a window.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td><code>{@link #onDetachedFromWindow}</code></td>
241 * <td>Called when the view is detached from its window.
242 * </td>
243 * </tr>
244 *
245 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700246 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <td>Called when the visibility of the window containing the view
248 * has changed.
249 * </td>
250 * </tr>
251 * </tbody>
252 *
253 * </table>
254 * </p>
255 *
256 * <a name="IDs"></a>
257 * <h3>IDs</h3>
258 * Views may have an integer id associated with them. These ids are typically
259 * assigned in the layout XML files, and are used to find specific views within
260 * the view tree. A common pattern is to:
261 * <ul>
262 * <li>Define a Button in the layout file and assign it a unique ID.
263 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700264 * &lt;Button
265 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * android:layout_width="wrap_content"
267 * android:layout_height="wrap_content"
268 * android:text="@string/my_button_text"/&gt;
269 * </pre></li>
270 * <li>From the onCreate method of an Activity, find the Button
271 * <pre class="prettyprint">
272 * Button myButton = (Button) findViewById(R.id.my_button);
273 * </pre></li>
274 * </ul>
275 * <p>
276 * View IDs need not be unique throughout the tree, but it is good practice to
277 * ensure that they are at least unique within the part of the tree you are
278 * searching.
279 * </p>
280 *
281 * <a name="Position"></a>
282 * <h3>Position</h3>
283 * <p>
284 * The geometry of a view is that of a rectangle. A view has a location,
285 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
286 * two dimensions, expressed as a width and a height. The unit for location
287 * and dimensions is the pixel.
288 * </p>
289 *
290 * <p>
291 * It is possible to retrieve the location of a view by invoking the methods
292 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
293 * coordinate of the rectangle representing the view. The latter returns the
294 * top, or Y, coordinate of the rectangle representing the view. These methods
295 * both return the location of the view relative to its parent. For instance,
296 * when getLeft() returns 20, that means the view is located 20 pixels to the
297 * right of the left edge of its direct parent.
298 * </p>
299 *
300 * <p>
301 * In addition, several convenience methods are offered to avoid unnecessary
302 * computations, namely {@link #getRight()} and {@link #getBottom()}.
303 * These methods return the coordinates of the right and bottom edges of the
304 * rectangle representing the view. For instance, calling {@link #getRight()}
305 * is similar to the following computation: <code>getLeft() + getWidth()</code>
306 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
307 * </p>
308 *
309 * <a name="SizePaddingMargins"></a>
310 * <h3>Size, padding and margins</h3>
311 * <p>
312 * The size of a view is expressed with a width and a height. A view actually
313 * possess two pairs of width and height values.
314 * </p>
315 *
316 * <p>
317 * The first pair is known as <em>measured width</em> and
318 * <em>measured height</em>. These dimensions define how big a view wants to be
319 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
320 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
321 * and {@link #getMeasuredHeight()}.
322 * </p>
323 *
324 * <p>
325 * The second pair is simply known as <em>width</em> and <em>height</em>, or
326 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
327 * dimensions define the actual size of the view on screen, at drawing time and
328 * after layout. These values may, but do not have to, be different from the
329 * measured width and height. The width and height can be obtained by calling
330 * {@link #getWidth()} and {@link #getHeight()}.
331 * </p>
332 *
333 * <p>
334 * To measure its dimensions, a view takes into account its padding. The padding
335 * is expressed in pixels for the left, top, right and bottom parts of the view.
336 * Padding can be used to offset the content of the view by a specific amount of
337 * pixels. For instance, a left padding of 2 will push the view's content by
338 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700339 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
340 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
341 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
342 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 * </p>
344 *
345 * <p>
346 * Even though a view can define a padding, it does not provide any support for
347 * margins. However, view groups provide such a support. Refer to
348 * {@link android.view.ViewGroup} and
349 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
350 * </p>
351 *
352 * <a name="Layout"></a>
353 * <h3>Layout</h3>
354 * <p>
355 * Layout is a two pass process: a measure pass and a layout pass. The measuring
356 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
357 * of the view tree. Each view pushes dimension specifications down the tree
358 * during the recursion. At the end of the measure pass, every view has stored
359 * its measurements. The second pass happens in
360 * {@link #layout(int,int,int,int)} and is also top-down. During
361 * this pass each parent is responsible for positioning all of its children
362 * using the sizes computed in the measure pass.
363 * </p>
364 *
365 * <p>
366 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
367 * {@link #getMeasuredHeight()} values must be set, along with those for all of
368 * that view's descendants. A view's measured width and measured height values
369 * must respect the constraints imposed by the view's parents. This guarantees
370 * that at the end of the measure pass, all parents accept all of their
371 * children's measurements. A parent view may call measure() more than once on
372 * its children. For example, the parent may measure each child once with
373 * unspecified dimensions to find out how big they want to be, then call
374 * measure() on them again with actual numbers if the sum of all the children's
375 * unconstrained sizes is too big or too small.
376 * </p>
377 *
378 * <p>
379 * The measure pass uses two classes to communicate dimensions. The
380 * {@link MeasureSpec} class is used by views to tell their parents how they
381 * want to be measured and positioned. The base LayoutParams class just
382 * describes how big the view wants to be for both width and height. For each
383 * dimension, it can specify one of:
384 * <ul>
385 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800386 * <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 -0800387 * (minus padding)
388 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
389 * enclose its content (plus padding).
390 * </ul>
391 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
392 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
393 * an X and Y value.
394 * </p>
395 *
396 * <p>
397 * MeasureSpecs are used to push requirements down the tree from parent to
398 * child. A MeasureSpec can be in one of three modes:
399 * <ul>
400 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
401 * of a child view. For example, a LinearLayout may call measure() on its child
402 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
403 * tall the child view wants to be given a width of 240 pixels.
404 * <li>EXACTLY: This is used by the parent to impose an exact size on the
405 * child. The child must use this size, and guarantee that all of its
406 * descendants will fit within this size.
407 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
408 * child. The child must gurantee that it and all of its descendants will fit
409 * within this size.
410 * </ul>
411 * </p>
412 *
413 * <p>
414 * To intiate a layout, call {@link #requestLayout}. This method is typically
415 * called by a view on itself when it believes that is can no longer fit within
416 * its current bounds.
417 * </p>
418 *
419 * <a name="Drawing"></a>
420 * <h3>Drawing</h3>
421 * <p>
422 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700423 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 * this means that parents will draw before (i.e., behind) their children, with
425 * siblings drawn in the order they appear in the tree.
426 * If you set a background drawable for a View, then the View will draw it for you
427 * before calling back to its <code>onDraw()</code> method.
428 * </p>
429 *
430 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700431 * 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 -0800432 * </p>
433 *
434 * <p>
435 * To force a view to draw, call {@link #invalidate()}.
436 * </p>
437 *
438 * <a name="EventHandlingThreading"></a>
439 * <h3>Event Handling and Threading</h3>
440 * <p>
441 * The basic cycle of a view is as follows:
442 * <ol>
443 * <li>An event comes in and is dispatched to the appropriate view. The view
444 * handles the event and notifies any listeners.</li>
445 * <li>If in the course of processing the event, the view's bounds may need
446 * to be changed, the view will call {@link #requestLayout()}.</li>
447 * <li>Similarly, if in the course of processing the event the view's appearance
448 * may need to be changed, the view will call {@link #invalidate()}.</li>
449 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
450 * the framework will take care of measuring, laying out, and drawing the tree
451 * as appropriate.</li>
452 * </ol>
453 * </p>
454 *
455 * <p><em>Note: The entire view tree is single threaded. You must always be on
456 * the UI thread when calling any method on any view.</em>
457 * If you are doing work on other threads and want to update the state of a view
458 * from that thread, you should use a {@link Handler}.
459 * </p>
460 *
461 * <a name="FocusHandling"></a>
462 * <h3>Focus Handling</h3>
463 * <p>
464 * The framework will handle routine focus movement in response to user input.
465 * This includes changing the focus as views are removed or hidden, or as new
466 * views become available. Views indicate their willingness to take focus
467 * through the {@link #isFocusable} method. To change whether a view can take
468 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
469 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
470 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
471 * </p>
472 * <p>
473 * Focus movement is based on an algorithm which finds the nearest neighbor in a
474 * given direction. In rare cases, the default algorithm may not match the
475 * intended behavior of the developer. In these situations, you can provide
476 * explicit overrides by using these XML attributes in the layout file:
477 * <pre>
478 * nextFocusDown
479 * nextFocusLeft
480 * nextFocusRight
481 * nextFocusUp
482 * </pre>
483 * </p>
484 *
485 *
486 * <p>
487 * To get a particular view to take focus, call {@link #requestFocus()}.
488 * </p>
489 *
490 * <a name="TouchMode"></a>
491 * <h3>Touch Mode</h3>
492 * <p>
493 * When a user is navigating a user interface via directional keys such as a D-pad, it is
494 * necessary to give focus to actionable items such as buttons so the user can see
495 * what will take input. If the device has touch capabilities, however, and the user
496 * begins interacting with the interface by touching it, it is no longer necessary to
497 * always highlight, or give focus to, a particular view. This motivates a mode
498 * for interaction named 'touch mode'.
499 * </p>
500 * <p>
501 * For a touch capable device, once the user touches the screen, the device
502 * will enter touch mode. From this point onward, only views for which
503 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
504 * Other views that are touchable, like buttons, will not take focus when touched; they will
505 * only fire the on click listeners.
506 * </p>
507 * <p>
508 * Any time a user hits a directional key, such as a D-pad direction, the view device will
509 * exit touch mode, and find a view to take focus, so that the user may resume interacting
510 * with the user interface without touching the screen again.
511 * </p>
512 * <p>
513 * The touch mode state is maintained across {@link android.app.Activity}s. Call
514 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
515 * </p>
516 *
517 * <a name="Scrolling"></a>
518 * <h3>Scrolling</h3>
519 * <p>
520 * The framework provides basic support for views that wish to internally
521 * scroll their content. This includes keeping track of the X and Y scroll
522 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800523 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700524 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * </p>
526 *
527 * <a name="Tags"></a>
528 * <h3>Tags</h3>
529 * <p>
530 * Unlike IDs, tags are not used to identify views. Tags are essentially an
531 * extra piece of information that can be associated with a view. They are most
532 * often used as a convenience to store data related to views in the views
533 * themselves rather than by putting them in a separate structure.
534 * </p>
535 *
536 * <a name="Animation"></a>
537 * <h3>Animation</h3>
538 * <p>
539 * You can attach an {@link Animation} object to a view using
540 * {@link #setAnimation(Animation)} or
541 * {@link #startAnimation(Animation)}. The animation can alter the scale,
542 * rotation, translation and alpha of a view over time. If the animation is
543 * attached to a view that has children, the animation will affect the entire
544 * subtree rooted by that node. When an animation is started, the framework will
545 * take care of redrawing the appropriate views until the animation completes.
546 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800547 * <p>
548 * Starting with Android 3.0, the preferred way of animating views is to use the
549 * {@link android.animation} package APIs.
550 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 *
Jeff Brown85a31762010-09-01 17:01:00 -0700552 * <a name="Security"></a>
553 * <h3>Security</h3>
554 * <p>
555 * Sometimes it is essential that an application be able to verify that an action
556 * is being performed with the full knowledge and consent of the user, such as
557 * granting a permission request, making a purchase or clicking on an advertisement.
558 * Unfortunately, a malicious application could try to spoof the user into
559 * performing these actions, unaware, by concealing the intended purpose of the view.
560 * As a remedy, the framework offers a touch filtering mechanism that can be used to
561 * improve the security of views that provide access to sensitive functionality.
562 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700563 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800564 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700565 * will discard touches that are received whenever the view's window is obscured by
566 * another visible window. As a result, the view will not receive touches whenever a
567 * toast, dialog or other window appears above the view's window.
568 * </p><p>
569 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700570 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
571 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700572 * </p>
573 *
Romain Guy171c5922011-01-06 10:04:23 -0800574 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700575 * @attr ref android.R.styleable#View_background
576 * @attr ref android.R.styleable#View_clickable
577 * @attr ref android.R.styleable#View_contentDescription
578 * @attr ref android.R.styleable#View_drawingCacheQuality
579 * @attr ref android.R.styleable#View_duplicateParentState
580 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700581 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700582 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700583 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_isScrollContainer
586 * @attr ref android.R.styleable#View_focusable
587 * @attr ref android.R.styleable#View_focusableInTouchMode
588 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
589 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800590 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_longClickable
592 * @attr ref android.R.styleable#View_minHeight
593 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * @attr ref android.R.styleable#View_nextFocusDown
595 * @attr ref android.R.styleable#View_nextFocusLeft
596 * @attr ref android.R.styleable#View_nextFocusRight
597 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700598 * @attr ref android.R.styleable#View_onClick
599 * @attr ref android.R.styleable#View_padding
600 * @attr ref android.R.styleable#View_paddingBottom
601 * @attr ref android.R.styleable#View_paddingLeft
602 * @attr ref android.R.styleable#View_paddingRight
603 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800604 * @attr ref android.R.styleable#View_paddingStart
605 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700635public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
636 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 private static final boolean DBG = false;
638
639 /**
640 * The logging tag used by this class with android.util.Log.
641 */
642 protected static final String VIEW_LOG_TAG = "View";
643
644 /**
645 * Used to mark a View that has no ID.
646 */
647 public static final int NO_ID = -1;
648
649 /**
650 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
651 * calling setFlags.
652 */
653 private static final int NOT_FOCUSABLE = 0x00000000;
654
655 /**
656 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
657 * setFlags.
658 */
659 private static final int FOCUSABLE = 0x00000001;
660
661 /**
662 * Mask for use with setFlags indicating bits used for focus.
663 */
664 private static final int FOCUSABLE_MASK = 0x00000001;
665
666 /**
667 * This view will adjust its padding to fit sytem windows (e.g. status bar)
668 */
669 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
670
671 /**
Scott Main812634c22011-07-27 13:22:35 -0700672 * This view is visible.
673 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
674 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 */
676 public static final int VISIBLE = 0x00000000;
677
678 /**
679 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700680 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
681 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 public static final int INVISIBLE = 0x00000004;
684
685 /**
686 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700687 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
688 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 */
690 public static final int GONE = 0x00000008;
691
692 /**
693 * Mask for use with setFlags indicating bits used for visibility.
694 * {@hide}
695 */
696 static final int VISIBILITY_MASK = 0x0000000C;
697
698 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
699
700 /**
701 * This view is enabled. Intrepretation varies by subclass.
702 * Use with ENABLED_MASK when calling setFlags.
703 * {@hide}
704 */
705 static final int ENABLED = 0x00000000;
706
707 /**
708 * This view is disabled. Intrepretation varies by subclass.
709 * Use with ENABLED_MASK when calling setFlags.
710 * {@hide}
711 */
712 static final int DISABLED = 0x00000020;
713
714 /**
715 * Mask for use with setFlags indicating bits used for indicating whether
716 * this view is enabled
717 * {@hide}
718 */
719 static final int ENABLED_MASK = 0x00000020;
720
721 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700722 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
723 * called and further optimizations will be performed. It is okay to have
724 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 * {@hide}
726 */
727 static final int WILL_NOT_DRAW = 0x00000080;
728
729 /**
730 * Mask for use with setFlags indicating bits used for indicating whether
731 * this view is will draw
732 * {@hide}
733 */
734 static final int DRAW_MASK = 0x00000080;
735
736 /**
737 * <p>This view doesn't show scrollbars.</p>
738 * {@hide}
739 */
740 static final int SCROLLBARS_NONE = 0x00000000;
741
742 /**
743 * <p>This view shows horizontal scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
747
748 /**
749 * <p>This view shows vertical scrollbars.</p>
750 * {@hide}
751 */
752 static final int SCROLLBARS_VERTICAL = 0x00000200;
753
754 /**
755 * <p>Mask for use with setFlags indicating bits used for indicating which
756 * scrollbars are enabled.</p>
757 * {@hide}
758 */
759 static final int SCROLLBARS_MASK = 0x00000300;
760
Jeff Brown85a31762010-09-01 17:01:00 -0700761 /**
762 * Indicates that the view should filter touches when its window is obscured.
763 * Refer to the class comments for more information about this security feature.
764 * {@hide}
765 */
766 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
767
768 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
770 /**
771 * <p>This view doesn't show fading edges.</p>
772 * {@hide}
773 */
774 static final int FADING_EDGE_NONE = 0x00000000;
775
776 /**
777 * <p>This view shows horizontal fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
781
782 /**
783 * <p>This view shows vertical fading edges.</p>
784 * {@hide}
785 */
786 static final int FADING_EDGE_VERTICAL = 0x00002000;
787
788 /**
789 * <p>Mask for use with setFlags indicating bits used for indicating which
790 * fading edges are enabled.</p>
791 * {@hide}
792 */
793 static final int FADING_EDGE_MASK = 0x00003000;
794
795 /**
796 * <p>Indicates this view can be clicked. When clickable, a View reacts
797 * to clicks by notifying the OnClickListener.<p>
798 * {@hide}
799 */
800 static final int CLICKABLE = 0x00004000;
801
802 /**
803 * <p>Indicates this view is caching its drawing into a bitmap.</p>
804 * {@hide}
805 */
806 static final int DRAWING_CACHE_ENABLED = 0x00008000;
807
808 /**
809 * <p>Indicates that no icicle should be saved for this view.<p>
810 * {@hide}
811 */
812 static final int SAVE_DISABLED = 0x000010000;
813
814 /**
815 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
816 * property.</p>
817 * {@hide}
818 */
819 static final int SAVE_DISABLED_MASK = 0x000010000;
820
821 /**
822 * <p>Indicates that no drawing cache should ever be created for this view.<p>
823 * {@hide}
824 */
825 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
826
827 /**
828 * <p>Indicates this view can take / keep focus when int touch mode.</p>
829 * {@hide}
830 */
831 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
832
833 /**
834 * <p>Enables low quality mode for the drawing cache.</p>
835 */
836 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
837
838 /**
839 * <p>Enables high quality mode for the drawing cache.</p>
840 */
841 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
842
843 /**
844 * <p>Enables automatic quality mode for the drawing cache.</p>
845 */
846 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
847
848 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
849 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
850 };
851
852 /**
853 * <p>Mask for use with setFlags indicating bits used for the cache
854 * quality property.</p>
855 * {@hide}
856 */
857 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
858
859 /**
860 * <p>
861 * Indicates this view can be long clicked. When long clickable, a View
862 * reacts to long clicks by notifying the OnLongClickListener or showing a
863 * context menu.
864 * </p>
865 * {@hide}
866 */
867 static final int LONG_CLICKABLE = 0x00200000;
868
869 /**
870 * <p>Indicates that this view gets its drawable states from its direct parent
871 * and ignores its original internal states.</p>
872 *
873 * @hide
874 */
875 static final int DUPLICATE_PARENT_STATE = 0x00400000;
876
877 /**
878 * The scrollbar style to display the scrollbars inside the content area,
879 * without increasing the padding. The scrollbars will be overlaid with
880 * translucency on the view's content.
881 */
882 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
883
884 /**
885 * The scrollbar style to display the scrollbars inside the padded area,
886 * increasing the padding of the view. The scrollbars will not overlap the
887 * content area of the view.
888 */
889 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
890
891 /**
892 * The scrollbar style to display the scrollbars at the edge of the view,
893 * without increasing the padding. The scrollbars will be overlaid with
894 * translucency.
895 */
896 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
897
898 /**
899 * The scrollbar style to display the scrollbars at the edge of the view,
900 * increasing the padding of the view. The scrollbars will only overlap the
901 * background, if any.
902 */
903 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
904
905 /**
906 * Mask to check if the scrollbar style is overlay or inset.
907 * {@hide}
908 */
909 static final int SCROLLBARS_INSET_MASK = 0x01000000;
910
911 /**
912 * Mask to check if the scrollbar style is inside or outside.
913 * {@hide}
914 */
915 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
916
917 /**
918 * Mask for scrollbar style.
919 * {@hide}
920 */
921 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
922
923 /**
924 * View flag indicating that the screen should remain on while the
925 * window containing this view is visible to the user. This effectively
926 * takes care of automatically setting the WindowManager's
927 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
928 */
929 public static final int KEEP_SCREEN_ON = 0x04000000;
930
931 /**
932 * View flag indicating whether this view should have sound effects enabled
933 * for events such as clicking and touching.
934 */
935 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
936
937 /**
938 * View flag indicating whether this view should have haptic feedback
939 * enabled for events such as long presses.
940 */
941 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
942
943 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700944 * <p>Indicates that the view hierarchy should stop saving state when
945 * it reaches this view. If state saving is initiated immediately at
946 * the view, it will be allowed.
947 * {@hide}
948 */
949 static final int PARENT_SAVE_DISABLED = 0x20000000;
950
951 /**
952 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
953 * {@hide}
954 */
955 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
956
957 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800958 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700959 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800960 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700961 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800962
963 /**
964 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700965 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800966 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700967 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800968
969 /**
970 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700971 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800972 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700973 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800974
975 /**
976 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700977 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800978 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700979 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800980
981 /**
982 * Mask for use with setFlags indicating bits used for horizontalDirection.
983 * {@hide}
984 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700985 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800986
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700987 /*
988 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
989 * flag value.
990 * {@hide}
991 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700992 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
993 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800994
995 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700996 * Default horizontalDirection.
997 * {@hide}
998 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700999 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001000
1001 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001002 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1003 * should add all focusable Views regardless if they are focusable in touch mode.
1004 */
1005 public static final int FOCUSABLES_ALL = 0x00000000;
1006
1007 /**
1008 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1009 * should add only Views focusable in touch mode.
1010 */
1011 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
1045 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001046 * Bits of {@link #getMeasuredWidthAndState()} and
1047 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1048 */
1049 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1050
1051 /**
1052 * Bits of {@link #getMeasuredWidthAndState()} and
1053 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1054 */
1055 public static final int MEASURED_STATE_MASK = 0xff000000;
1056
1057 /**
1058 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1059 * for functions that combine both width and height into a single int,
1060 * such as {@link #getMeasuredState()} and the childState argument of
1061 * {@link #resolveSizeAndState(int, int, int)}.
1062 */
1063 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1064
1065 /**
1066 * Bit of {@link #getMeasuredWidthAndState()} and
1067 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1068 * is smaller that the space the view would like to have.
1069 */
1070 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1071
1072 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 * Base View state sets
1074 */
1075 // Singles
1076 /**
1077 * Indicates the view has no states set. States are used with
1078 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1079 * view depending on its state.
1080 *
1081 * @see android.graphics.drawable.Drawable
1082 * @see #getDrawableState()
1083 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001084 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 /**
1086 * Indicates the view is enabled. 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[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 /**
1095 * Indicates the view is focused. States are used with
1096 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1097 * view depending on its state.
1098 *
1099 * @see android.graphics.drawable.Drawable
1100 * @see #getDrawableState()
1101 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001102 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 /**
1104 * Indicates the view is selected. States are used with
1105 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1106 * view depending on its state.
1107 *
1108 * @see android.graphics.drawable.Drawable
1109 * @see #getDrawableState()
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view is pressed. States are used with
1114 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1115 * view depending on its state.
1116 *
1117 * @see android.graphics.drawable.Drawable
1118 * @see #getDrawableState()
1119 * @hide
1120 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001121 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 /**
1123 * Indicates the view's window has focus. States are used with
1124 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1125 * view depending on its state.
1126 *
1127 * @see android.graphics.drawable.Drawable
1128 * @see #getDrawableState()
1129 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001130 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 // Doubles
1132 /**
1133 * Indicates the view is enabled and has the focus.
1134 *
1135 * @see #ENABLED_STATE_SET
1136 * @see #FOCUSED_STATE_SET
1137 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001138 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 /**
1140 * Indicates the view is enabled and selected.
1141 *
1142 * @see #ENABLED_STATE_SET
1143 * @see #SELECTED_STATE_SET
1144 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001145 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 /**
1147 * Indicates the view is enabled and that its window has focus.
1148 *
1149 * @see #ENABLED_STATE_SET
1150 * @see #WINDOW_FOCUSED_STATE_SET
1151 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001152 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 /**
1154 * Indicates the view is focused and selected.
1155 *
1156 * @see #FOCUSED_STATE_SET
1157 * @see #SELECTED_STATE_SET
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view has the focus and that its window has the focus.
1162 *
1163 * @see #FOCUSED_STATE_SET
1164 * @see #WINDOW_FOCUSED_STATE_SET
1165 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001166 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 /**
1168 * Indicates the view is selected and that its window has the focus.
1169 *
1170 * @see #SELECTED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 // Triples
1175 /**
1176 * Indicates the view is enabled, focused and selected.
1177 *
1178 * @see #ENABLED_STATE_SET
1179 * @see #FOCUSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is enabled, focused and its window has the focus.
1185 *
1186 * @see #ENABLED_STATE_SET
1187 * @see #FOCUSED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is enabled, selected and its window has the focus.
1193 *
1194 * @see #ENABLED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 * @see #WINDOW_FOCUSED_STATE_SET
1197 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001198 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 /**
1200 * Indicates the view is focused, selected and its window has the focus.
1201 *
1202 * @see #FOCUSED_STATE_SET
1203 * @see #SELECTED_STATE_SET
1204 * @see #WINDOW_FOCUSED_STATE_SET
1205 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001206 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 /**
1208 * Indicates the view is enabled, focused, selected and its window
1209 * has the focus.
1210 *
1211 * @see #ENABLED_STATE_SET
1212 * @see #FOCUSED_STATE_SET
1213 * @see #SELECTED_STATE_SET
1214 * @see #WINDOW_FOCUSED_STATE_SET
1215 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001216 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 /**
1218 * Indicates the view is pressed and its window has the focus.
1219 *
1220 * @see #PRESSED_STATE_SET
1221 * @see #WINDOW_FOCUSED_STATE_SET
1222 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001223 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 /**
1225 * Indicates the view is pressed and selected.
1226 *
1227 * @see #PRESSED_STATE_SET
1228 * @see #SELECTED_STATE_SET
1229 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001230 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 /**
1232 * Indicates the view is pressed, selected and its window has the focus.
1233 *
1234 * @see #PRESSED_STATE_SET
1235 * @see #SELECTED_STATE_SET
1236 * @see #WINDOW_FOCUSED_STATE_SET
1237 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001238 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 /**
1240 * Indicates the view is pressed and focused.
1241 *
1242 * @see #PRESSED_STATE_SET
1243 * @see #FOCUSED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, focused and its window has the focus.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #WINDOW_FOCUSED_STATE_SET
1252 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001253 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 /**
1255 * Indicates the view is pressed, focused and selected.
1256 *
1257 * @see #PRESSED_STATE_SET
1258 * @see #SELECTED_STATE_SET
1259 * @see #FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed, focused, selected and its window has the focus.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #FOCUSED_STATE_SET
1267 * @see #SELECTED_STATE_SET
1268 * @see #WINDOW_FOCUSED_STATE_SET
1269 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001270 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 /**
1272 * Indicates the view is pressed and enabled.
1273 *
1274 * @see #PRESSED_STATE_SET
1275 * @see #ENABLED_STATE_SET
1276 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001277 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 /**
1279 * Indicates the view is pressed, enabled and its window has the focus.
1280 *
1281 * @see #PRESSED_STATE_SET
1282 * @see #ENABLED_STATE_SET
1283 * @see #WINDOW_FOCUSED_STATE_SET
1284 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001285 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 /**
1287 * Indicates the view is pressed, enabled and selected.
1288 *
1289 * @see #PRESSED_STATE_SET
1290 * @see #ENABLED_STATE_SET
1291 * @see #SELECTED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 /**
1295 * Indicates the view is pressed, enabled, selected and its window has the
1296 * focus.
1297 *
1298 * @see #PRESSED_STATE_SET
1299 * @see #ENABLED_STATE_SET
1300 * @see #SELECTED_STATE_SET
1301 * @see #WINDOW_FOCUSED_STATE_SET
1302 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001303 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 /**
1305 * Indicates the view is pressed, enabled and focused.
1306 *
1307 * @see #PRESSED_STATE_SET
1308 * @see #ENABLED_STATE_SET
1309 * @see #FOCUSED_STATE_SET
1310 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001311 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 /**
1313 * Indicates the view is pressed, enabled, focused and its window has the
1314 * focus.
1315 *
1316 * @see #PRESSED_STATE_SET
1317 * @see #ENABLED_STATE_SET
1318 * @see #FOCUSED_STATE_SET
1319 * @see #WINDOW_FOCUSED_STATE_SET
1320 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001321 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 /**
1323 * Indicates the view is pressed, enabled, focused and selected.
1324 *
1325 * @see #PRESSED_STATE_SET
1326 * @see #ENABLED_STATE_SET
1327 * @see #SELECTED_STATE_SET
1328 * @see #FOCUSED_STATE_SET
1329 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001330 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 /**
1332 * Indicates the view is pressed, enabled, focused, selected and its window
1333 * has the focus.
1334 *
1335 * @see #PRESSED_STATE_SET
1336 * @see #ENABLED_STATE_SET
1337 * @see #SELECTED_STATE_SET
1338 * @see #FOCUSED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342
1343 /**
1344 * The order here is very important to {@link #getDrawableState()}
1345 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 private static final int[][] VIEW_STATE_SETS;
1347
Romain Guyb051e892010-09-28 19:09:36 -07001348 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1349 static final int VIEW_STATE_SELECTED = 1 << 1;
1350 static final int VIEW_STATE_FOCUSED = 1 << 2;
1351 static final int VIEW_STATE_ENABLED = 1 << 3;
1352 static final int VIEW_STATE_PRESSED = 1 << 4;
1353 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001354 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001355 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001356 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1357 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001358
1359 static final int[] VIEW_STATE_IDS = new int[] {
1360 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1361 R.attr.state_selected, VIEW_STATE_SELECTED,
1362 R.attr.state_focused, VIEW_STATE_FOCUSED,
1363 R.attr.state_enabled, VIEW_STATE_ENABLED,
1364 R.attr.state_pressed, VIEW_STATE_PRESSED,
1365 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001366 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001367 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001368 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1369 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 };
1371
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001372 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001373 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1374 throw new IllegalStateException(
1375 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1376 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001378 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001380 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001382 orderedIds[i * 2] = viewState;
1383 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 }
1385 }
1386 }
Romain Guyb051e892010-09-28 19:09:36 -07001387 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1388 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1389 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001390 int numBits = Integer.bitCount(i);
1391 int[] set = new int[numBits];
1392 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001393 for (int j = 0; j < orderedIds.length; j += 2) {
1394 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001395 set[pos++] = orderedIds[j];
1396 }
1397 }
1398 VIEW_STATE_SETS[i] = set;
1399 }
1400
1401 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1402 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1403 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1404 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1406 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1407 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1409 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1411 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1413 | VIEW_STATE_FOCUSED];
1414 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1415 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1417 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1419 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1421 | VIEW_STATE_ENABLED];
1422 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1424 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1426 | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1429 | VIEW_STATE_ENABLED];
1430 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1432 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1433
1434 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1435 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1437 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1439 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1441 | VIEW_STATE_PRESSED];
1442 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1444 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1449 | VIEW_STATE_PRESSED];
1450 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1452 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1453 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1455 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1457 | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1460 | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1463 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1466 | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1469 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1472 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1475 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1476 | VIEW_STATE_PRESSED];
1477 }
1478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001480 * Accessibility event types that are dispatched for text population.
1481 */
1482 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1483 AccessibilityEvent.TYPE_VIEW_CLICKED
1484 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1485 | AccessibilityEvent.TYPE_VIEW_SELECTED
1486 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1487 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1488 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001489 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001490 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1491 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001492
1493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 * Temporary Rect currently for use in setBackground(). This will probably
1495 * be extended in the future to hold our own class with more than just
1496 * a Rect. :)
1497 */
1498 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001499
1500 /**
1501 * Map used to store views' tags.
1502 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001503 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001506 * The next available accessiiblity id.
1507 */
1508 private static int sNextAccessibilityViewId;
1509
1510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 * The animation currently associated with this view.
1512 * @hide
1513 */
1514 protected Animation mCurrentAnimation = null;
1515
1516 /**
1517 * Width as measured during measure pass.
1518 * {@hide}
1519 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001520 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001521 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
1523 /**
1524 * Height as measured during measure pass.
1525 * {@hide}
1526 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001527 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001528 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529
1530 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001531 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1532 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1533 * its display list. This flag, used only when hw accelerated, allows us to clear the
1534 * flag while retaining this information until it's needed (at getDisplayList() time and
1535 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1536 *
1537 * {@hide}
1538 */
1539 boolean mRecreateDisplayList = false;
1540
1541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 * The view's identifier.
1543 * {@hide}
1544 *
1545 * @see #setId(int)
1546 * @see #getId()
1547 */
1548 @ViewDebug.ExportedProperty(resolveId = true)
1549 int mID = NO_ID;
1550
1551 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001552 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001553 */
1554 int mAccessibilityViewId = NO_ID;
1555
1556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 * The view's tag.
1558 * {@hide}
1559 *
1560 * @see #setTag(Object)
1561 * @see #getTag()
1562 */
1563 protected Object mTag;
1564
1565 // for mPrivateFlags:
1566 /** {@hide} */
1567 static final int WANTS_FOCUS = 0x00000001;
1568 /** {@hide} */
1569 static final int FOCUSED = 0x00000002;
1570 /** {@hide} */
1571 static final int SELECTED = 0x00000004;
1572 /** {@hide} */
1573 static final int IS_ROOT_NAMESPACE = 0x00000008;
1574 /** {@hide} */
1575 static final int HAS_BOUNDS = 0x00000010;
1576 /** {@hide} */
1577 static final int DRAWN = 0x00000020;
1578 /**
1579 * When this flag is set, this view is running an animation on behalf of its
1580 * children and should therefore not cancel invalidate requests, even if they
1581 * lie outside of this view's bounds.
1582 *
1583 * {@hide}
1584 */
1585 static final int DRAW_ANIMATION = 0x00000040;
1586 /** {@hide} */
1587 static final int SKIP_DRAW = 0x00000080;
1588 /** {@hide} */
1589 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1590 /** {@hide} */
1591 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1592 /** {@hide} */
1593 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1594 /** {@hide} */
1595 static final int MEASURED_DIMENSION_SET = 0x00000800;
1596 /** {@hide} */
1597 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001598 /** {@hide} */
1599 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600
1601 private static final int PRESSED = 0x00004000;
1602
1603 /** {@hide} */
1604 static final int DRAWING_CACHE_VALID = 0x00008000;
1605 /**
1606 * Flag used to indicate that this view should be drawn once more (and only once
1607 * more) after its animation has completed.
1608 * {@hide}
1609 */
1610 static final int ANIMATION_STARTED = 0x00010000;
1611
1612 private static final int SAVE_STATE_CALLED = 0x00020000;
1613
1614 /**
1615 * Indicates that the View returned true when onSetAlpha() was called and that
1616 * the alpha must be restored.
1617 * {@hide}
1618 */
1619 static final int ALPHA_SET = 0x00040000;
1620
1621 /**
1622 * Set by {@link #setScrollContainer(boolean)}.
1623 */
1624 static final int SCROLL_CONTAINER = 0x00080000;
1625
1626 /**
1627 * Set by {@link #setScrollContainer(boolean)}.
1628 */
1629 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1630
1631 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001632 * View flag indicating whether this view was invalidated (fully or partially.)
1633 *
1634 * @hide
1635 */
1636 static final int DIRTY = 0x00200000;
1637
1638 /**
1639 * View flag indicating whether this view was invalidated by an opaque
1640 * invalidate request.
1641 *
1642 * @hide
1643 */
1644 static final int DIRTY_OPAQUE = 0x00400000;
1645
1646 /**
1647 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1648 *
1649 * @hide
1650 */
1651 static final int DIRTY_MASK = 0x00600000;
1652
1653 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001654 * Indicates whether the background is opaque.
1655 *
1656 * @hide
1657 */
1658 static final int OPAQUE_BACKGROUND = 0x00800000;
1659
1660 /**
1661 * Indicates whether the scrollbars are opaque.
1662 *
1663 * @hide
1664 */
1665 static final int OPAQUE_SCROLLBARS = 0x01000000;
1666
1667 /**
1668 * Indicates whether the view is opaque.
1669 *
1670 * @hide
1671 */
1672 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001673
Adam Powelle14579b2009-12-16 18:39:52 -08001674 /**
1675 * Indicates a prepressed state;
1676 * the short time between ACTION_DOWN and recognizing
1677 * a 'real' press. Prepressed is used to recognize quick taps
1678 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001679 *
Adam Powelle14579b2009-12-16 18:39:52 -08001680 * @hide
1681 */
1682 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001683
Adam Powellc9fbaab2010-02-16 17:16:19 -08001684 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001685 * Indicates whether the view is temporarily detached.
1686 *
1687 * @hide
1688 */
1689 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001690
Adam Powell8568c3a2010-04-19 14:26:11 -07001691 /**
1692 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001693 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001694 * @hide
1695 */
1696 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001697
1698 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001699 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1700 * @hide
1701 */
1702 private static final int HOVERED = 0x10000000;
1703
1704 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001705 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1706 * for transform operations
1707 *
1708 * @hide
1709 */
Adam Powellf37df072010-09-17 16:22:49 -07001710 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001711
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001712 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001713 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001714
Chet Haasefd2b0022010-08-06 13:08:56 -07001715 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001716 * Indicates that this view was specifically invalidated, not just dirtied because some
1717 * child view was invalidated. The flag is used to determine when we need to recreate
1718 * a view's display list (as opposed to just returning a reference to its existing
1719 * display list).
1720 *
1721 * @hide
1722 */
1723 static final int INVALIDATED = 0x80000000;
1724
Christopher Tate3d4bf172011-03-28 16:16:46 -07001725 /* Masks for mPrivateFlags2 */
1726
1727 /**
1728 * Indicates that this view has reported that it can accept the current drag's content.
1729 * Cleared when the drag operation concludes.
1730 * @hide
1731 */
1732 static final int DRAG_CAN_ACCEPT = 0x00000001;
1733
1734 /**
1735 * Indicates that this view is currently directly under the drag location in a
1736 * drag-and-drop operation involving content that it can accept. Cleared when
1737 * the drag exits the view, or when the drag operation concludes.
1738 * @hide
1739 */
1740 static final int DRAG_HOVERED = 0x00000002;
1741
Cibu Johny86666632010-02-22 13:01:02 -08001742 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001743 * Indicates whether the view layout direction has been resolved and drawn to the
1744 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001745 *
1746 * @hide
1747 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001748 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1749
1750 /**
1751 * Indicates whether the view layout direction has been resolved.
1752 *
1753 * @hide
1754 */
1755 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1756
Cibu Johny86666632010-02-22 13:01:02 -08001757
Adam Powell539ee872012-02-03 19:00:49 -08001758 /**
1759 * Indicates that the view is tracking some sort of transient state
1760 * that the app should not need to be aware of, but that the framework
1761 * should take special care to preserve.
1762 *
1763 * @hide
1764 */
1765 static final int HAS_TRANSIENT_STATE = 0x00000010;
1766
1767
Christopher Tate3d4bf172011-03-28 16:16:46 -07001768 /* End of masks for mPrivateFlags2 */
1769
1770 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1771
Chet Haasedaf98e92011-01-10 14:10:36 -08001772 /**
Adam Powell637d3372010-08-25 14:37:03 -07001773 * Always allow a user to over-scroll this view, provided it is a
1774 * view that can scroll.
1775 *
1776 * @see #getOverScrollMode()
1777 * @see #setOverScrollMode(int)
1778 */
1779 public static final int OVER_SCROLL_ALWAYS = 0;
1780
1781 /**
1782 * Allow a user to over-scroll this view only if the content is large
1783 * enough to meaningfully scroll, provided it is a view that can scroll.
1784 *
1785 * @see #getOverScrollMode()
1786 * @see #setOverScrollMode(int)
1787 */
1788 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1789
1790 /**
1791 * Never allow a user to over-scroll this view.
1792 *
1793 * @see #getOverScrollMode()
1794 * @see #setOverScrollMode(int)
1795 */
1796 public static final int OVER_SCROLL_NEVER = 2;
1797
1798 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001799 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001800 *
Joe Malin32736f02011-01-19 16:14:20 -08001801 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001802 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001803 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001804
1805 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001806 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1807 *
1808 * This is for use in games, book readers, video players, or any other "immersive" application
1809 * where the usual system chrome is deemed too distracting.
1810 *
1811 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001812 *
Joe Malin32736f02011-01-19 16:14:20 -08001813 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001814 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001815 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1816
1817 /**
1818 * View has requested that the system navigation be temporarily hidden.
1819 *
1820 * This is an even less obtrusive state than that called for by
1821 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1822 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1823 * those to disappear. This is useful (in conjunction with the
1824 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1825 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1826 * window flags) for displaying content using every last pixel on the display.
1827 *
1828 * There is a limitation: because navigation controls are so important, the least user
1829 * interaction will cause them to reappear immediately.
1830 *
1831 * @see #setSystemUiVisibility(int)
1832 */
1833 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1834
1835 /**
1836 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1837 */
1838 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1839
1840 /**
1841 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1842 */
1843 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001844
1845 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001846 * @hide
1847 *
1848 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1849 * out of the public fields to keep the undefined bits out of the developer's way.
1850 *
1851 * Flag to make the status bar not expandable. Unless you also
1852 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1853 */
1854 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1855
1856 /**
1857 * @hide
1858 *
1859 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1860 * out of the public fields to keep the undefined bits out of the developer's way.
1861 *
1862 * Flag to hide notification icons and scrolling ticker text.
1863 */
1864 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1865
1866 /**
1867 * @hide
1868 *
1869 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1870 * out of the public fields to keep the undefined bits out of the developer's way.
1871 *
1872 * Flag to disable incoming notification alerts. This will not block
1873 * icons, but it will block sound, vibrating and other visual or aural notifications.
1874 */
1875 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1876
1877 /**
1878 * @hide
1879 *
1880 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1881 * out of the public fields to keep the undefined bits out of the developer's way.
1882 *
1883 * Flag to hide only the scrolling ticker. Note that
1884 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1885 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1886 */
1887 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1888
1889 /**
1890 * @hide
1891 *
1892 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1893 * out of the public fields to keep the undefined bits out of the developer's way.
1894 *
1895 * Flag to hide the center system info area.
1896 */
1897 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1898
1899 /**
1900 * @hide
1901 *
1902 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1903 * out of the public fields to keep the undefined bits out of the developer's way.
1904 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001905 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001906 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1907 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001908 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001909
1910 /**
1911 * @hide
1912 *
1913 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1914 * out of the public fields to keep the undefined bits out of the developer's way.
1915 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001916 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001917 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1918 */
1919 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1920
1921 /**
1922 * @hide
1923 *
1924 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1925 * out of the public fields to keep the undefined bits out of the developer's way.
1926 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001927 * Flag to hide only the clock. You might use this if your activity has
1928 * its own clock making the status bar's clock redundant.
1929 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001930 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1931
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001932 /**
1933 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001934 *
1935 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1936 * out of the public fields to keep the undefined bits out of the developer's way.
1937 *
1938 * Flag to hide only the recent apps button. Don't use this
1939 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1940 */
1941 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1942
1943 /**
1944 * @hide
1945 *
1946 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1947 *
1948 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1949 */
1950 @Deprecated
1951 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1952 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1953
1954 /**
1955 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001956 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001957 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001958
1959 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001960 * These are the system UI flags that can be cleared by events outside
1961 * of an application. Currently this is just the ability to tap on the
1962 * screen while hiding the navigation bar to have it return.
1963 * @hide
1964 */
1965 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1966 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1967
1968 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001969 * Find views that render the specified text.
1970 *
1971 * @see #findViewsWithText(ArrayList, CharSequence, int)
1972 */
1973 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1974
1975 /**
1976 * Find find views that contain the specified content description.
1977 *
1978 * @see #findViewsWithText(ArrayList, CharSequence, int)
1979 */
1980 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1981
1982 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07001983 * Find views that contain {@link AccessibilityNodeProvider}. Such
1984 * a View is a root of virtual view hierarchy and may contain the searched
1985 * text. If this flag is set Views with providers are automatically
1986 * added and it is a responsibility of the client to call the APIs of
1987 * the provider to determine whether the virtual tree rooted at this View
1988 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
1989 * represeting the virtual views with this text.
1990 *
1991 * @see #findViewsWithText(ArrayList, CharSequence, int)
1992 *
1993 * @hide
1994 */
1995 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
1996
1997 /**
Romain Guybb9908b2012-03-08 11:14:07 -08001998 * Indicates that the screen has changed state and is now off.
1999 *
2000 * @see #onScreenStateChanged(int)
2001 */
2002 public static final int SCREEN_STATE_OFF = 0x0;
2003
2004 /**
2005 * Indicates that the screen has changed state and is now on.
2006 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002007 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002008 */
2009 public static final int SCREEN_STATE_ON = 0x1;
2010
2011 /**
Adam Powell637d3372010-08-25 14:37:03 -07002012 * Controls the over-scroll mode for this view.
2013 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2014 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2015 * and {@link #OVER_SCROLL_NEVER}.
2016 */
2017 private int mOverScrollMode;
2018
2019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 * The parent this view is attached to.
2021 * {@hide}
2022 *
2023 * @see #getParent()
2024 */
2025 protected ViewParent mParent;
2026
2027 /**
2028 * {@hide}
2029 */
2030 AttachInfo mAttachInfo;
2031
2032 /**
2033 * {@hide}
2034 */
Romain Guy809a7f62009-05-14 15:44:42 -07002035 @ViewDebug.ExportedProperty(flagMapping = {
2036 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2037 name = "FORCE_LAYOUT"),
2038 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2039 name = "LAYOUT_REQUIRED"),
2040 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002041 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002042 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2043 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2044 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2045 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2046 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002048 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049
2050 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002051 * This view's request for the visibility of the status bar.
2052 * @hide
2053 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002054 @ViewDebug.ExportedProperty(flagMapping = {
2055 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2056 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2057 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2058 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2059 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2060 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2061 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2062 equals = SYSTEM_UI_FLAG_VISIBLE,
2063 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2064 })
Joe Onorato664644d2011-01-23 17:53:23 -08002065 int mSystemUiVisibility;
2066
2067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 * Count of how many windows this view has been attached to.
2069 */
2070 int mWindowAttachCount;
2071
2072 /**
2073 * The layout parameters associated with this view and used by the parent
2074 * {@link android.view.ViewGroup} to determine how this view should be
2075 * laid out.
2076 * {@hide}
2077 */
2078 protected ViewGroup.LayoutParams mLayoutParams;
2079
2080 /**
2081 * The view flags hold various views states.
2082 * {@hide}
2083 */
2084 @ViewDebug.ExportedProperty
2085 int mViewFlags;
2086
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002087 static class TransformationInfo {
2088 /**
2089 * The transform matrix for the View. This transform is calculated internally
2090 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2091 * is used by default. Do *not* use this variable directly; instead call
2092 * getMatrix(), which will automatically recalculate the matrix if necessary
2093 * to get the correct matrix based on the latest rotation and scale properties.
2094 */
2095 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002096
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002097 /**
2098 * The transform matrix for the View. This transform is calculated internally
2099 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2100 * is used by default. Do *not* use this variable directly; instead call
2101 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2102 * to get the correct matrix based on the latest rotation and scale properties.
2103 */
2104 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002105
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002106 /**
2107 * An internal variable that tracks whether we need to recalculate the
2108 * transform matrix, based on whether the rotation or scaleX/Y properties
2109 * have changed since the matrix was last calculated.
2110 */
2111 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002112
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002113 /**
2114 * An internal variable that tracks whether we need to recalculate the
2115 * transform matrix, based on whether the rotation or scaleX/Y properties
2116 * have changed since the matrix was last calculated.
2117 */
2118 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002119
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002120 /**
2121 * A variable that tracks whether we need to recalculate the
2122 * transform matrix, based on whether the rotation or scaleX/Y properties
2123 * have changed since the matrix was last calculated. This variable
2124 * is only valid after a call to updateMatrix() or to a function that
2125 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2126 */
2127 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002128
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002129 /**
2130 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2131 */
2132 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002133
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002134 /**
2135 * This matrix is used when computing the matrix for 3D rotations.
2136 */
2137 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002138
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002139 /**
2140 * These prev values are used to recalculate a centered pivot point when necessary. The
2141 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2142 * set), so thes values are only used then as well.
2143 */
2144 private int mPrevWidth = -1;
2145 private int mPrevHeight = -1;
2146
2147 /**
2148 * The degrees rotation around the vertical axis through the pivot point.
2149 */
2150 @ViewDebug.ExportedProperty
2151 float mRotationY = 0f;
2152
2153 /**
2154 * The degrees rotation around the horizontal axis through the pivot point.
2155 */
2156 @ViewDebug.ExportedProperty
2157 float mRotationX = 0f;
2158
2159 /**
2160 * The degrees rotation around the pivot point.
2161 */
2162 @ViewDebug.ExportedProperty
2163 float mRotation = 0f;
2164
2165 /**
2166 * The amount of translation of the object away from its left property (post-layout).
2167 */
2168 @ViewDebug.ExportedProperty
2169 float mTranslationX = 0f;
2170
2171 /**
2172 * The amount of translation of the object away from its top property (post-layout).
2173 */
2174 @ViewDebug.ExportedProperty
2175 float mTranslationY = 0f;
2176
2177 /**
2178 * The amount of scale in the x direction around the pivot point. A
2179 * value of 1 means no scaling is applied.
2180 */
2181 @ViewDebug.ExportedProperty
2182 float mScaleX = 1f;
2183
2184 /**
2185 * The amount of scale in the y direction around the pivot point. A
2186 * value of 1 means no scaling is applied.
2187 */
2188 @ViewDebug.ExportedProperty
2189 float mScaleY = 1f;
2190
2191 /**
Chet Haasea33de552012-02-03 16:28:24 -08002192 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002193 */
2194 @ViewDebug.ExportedProperty
2195 float mPivotX = 0f;
2196
2197 /**
Chet Haasea33de552012-02-03 16:28:24 -08002198 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002199 */
2200 @ViewDebug.ExportedProperty
2201 float mPivotY = 0f;
2202
2203 /**
2204 * The opacity of the View. This is a value from 0 to 1, where 0 means
2205 * completely transparent and 1 means completely opaque.
2206 */
2207 @ViewDebug.ExportedProperty
2208 float mAlpha = 1f;
2209 }
2210
2211 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002212
Joe Malin32736f02011-01-19 16:14:20 -08002213 private boolean mLastIsOpaque;
2214
Chet Haasefd2b0022010-08-06 13:08:56 -07002215 /**
2216 * Convenience value to check for float values that are close enough to zero to be considered
2217 * zero.
2218 */
Romain Guy2542d192010-08-18 11:47:12 -07002219 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002220
2221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 * The distance in pixels from the left edge of this view's parent
2223 * to the left edge of this view.
2224 * {@hide}
2225 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002226 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 protected int mLeft;
2228 /**
2229 * The distance in pixels from the left edge of this view's parent
2230 * to the right edge of this view.
2231 * {@hide}
2232 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002233 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 protected int mRight;
2235 /**
2236 * The distance in pixels from the top edge of this view's parent
2237 * to the top edge of this view.
2238 * {@hide}
2239 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002240 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 protected int mTop;
2242 /**
2243 * The distance in pixels from the top edge of this view's parent
2244 * to the bottom edge of this view.
2245 * {@hide}
2246 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002247 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 protected int mBottom;
2249
2250 /**
2251 * The offset, in pixels, by which the content of this view is scrolled
2252 * horizontally.
2253 * {@hide}
2254 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002255 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 protected int mScrollX;
2257 /**
2258 * The offset, in pixels, by which the content of this view is scrolled
2259 * vertically.
2260 * {@hide}
2261 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002262 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 protected int mScrollY;
2264
2265 /**
2266 * The left padding in pixels, that is the distance in pixels between the
2267 * left edge of this view and the left edge of its content.
2268 * {@hide}
2269 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002270 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 protected int mPaddingLeft;
2272 /**
2273 * The right padding in pixels, that is the distance in pixels between the
2274 * right edge of this view and the right edge of its content.
2275 * {@hide}
2276 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002277 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 protected int mPaddingRight;
2279 /**
2280 * The top padding in pixels, that is the distance in pixels between the
2281 * top edge of this view and the top edge of its content.
2282 * {@hide}
2283 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002284 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 protected int mPaddingTop;
2286 /**
2287 * The bottom padding in pixels, that is the distance in pixels between the
2288 * bottom edge of this view and the bottom edge of its content.
2289 * {@hide}
2290 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002291 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 protected int mPaddingBottom;
2293
2294 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002295 * Briefly describes the view and is primarily used for accessibility support.
2296 */
2297 private CharSequence mContentDescription;
2298
2299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002301 *
2302 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002304 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002305 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306
2307 /**
2308 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002309 *
2310 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002312 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002313 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002315 /**
Adam Powell20232d02010-12-08 21:08:53 -08002316 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002317 *
2318 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002319 */
2320 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002321 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002322
2323 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002324 * Cache if the user padding is relative.
2325 *
2326 */
2327 @ViewDebug.ExportedProperty(category = "padding")
2328 boolean mUserPaddingRelative;
2329
2330 /**
2331 * Cache the paddingStart set by the user to append to the scrollbar's size.
2332 *
2333 */
2334 @ViewDebug.ExportedProperty(category = "padding")
2335 int mUserPaddingStart;
2336
2337 /**
2338 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2339 *
2340 */
2341 @ViewDebug.ExportedProperty(category = "padding")
2342 int mUserPaddingEnd;
2343
2344 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002345 * @hide
2346 */
2347 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2348 /**
2349 * @hide
2350 */
2351 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 private Drawable mBGDrawable;
2354
2355 private int mBackgroundResource;
2356 private boolean mBackgroundSizeChanged;
2357
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002358 static class ListenerInfo {
2359 /**
2360 * Listener used to dispatch focus change events.
2361 * This field should be made private, so it is hidden from the SDK.
2362 * {@hide}
2363 */
2364 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002366 /**
2367 * Listeners for layout change events.
2368 */
2369 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002370
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002371 /**
2372 * Listeners for attach events.
2373 */
2374 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002375
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002376 /**
2377 * Listener used to dispatch click events.
2378 * This field should be made private, so it is hidden from the SDK.
2379 * {@hide}
2380 */
2381 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002383 /**
2384 * Listener used to dispatch long click events.
2385 * This field should be made private, so it is hidden from the SDK.
2386 * {@hide}
2387 */
2388 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002390 /**
2391 * Listener used to build the context menu.
2392 * This field should be made private, so it is hidden from the SDK.
2393 * {@hide}
2394 */
2395 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002397 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002399 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002401 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002402
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002403 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002404
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002405 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002406
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002407 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2408 }
2409
2410 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 /**
2413 * The application environment this view lives in.
2414 * This field should be made private, so it is hidden from the SDK.
2415 * {@hide}
2416 */
2417 protected Context mContext;
2418
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002419 private final Resources mResources;
2420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 private ScrollabilityCache mScrollCache;
2422
2423 private int[] mDrawableState = null;
2424
Romain Guy0211a0a2011-02-14 16:34:59 -08002425 /**
2426 * Set to true when drawing cache is enabled and cannot be created.
2427 *
2428 * @hide
2429 */
2430 public boolean mCachingFailed;
2431
Romain Guy02890fd2010-08-06 17:58:44 -07002432 private Bitmap mDrawingCache;
2433 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002434 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002435 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436
2437 /**
2438 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2439 * the user may specify which view to go to next.
2440 */
2441 private int mNextFocusLeftId = View.NO_ID;
2442
2443 /**
2444 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2445 * the user may specify which view to go to next.
2446 */
2447 private int mNextFocusRightId = View.NO_ID;
2448
2449 /**
2450 * When this view has focus and the next focus is {@link #FOCUS_UP},
2451 * the user may specify which view to go to next.
2452 */
2453 private int mNextFocusUpId = View.NO_ID;
2454
2455 /**
2456 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2457 * the user may specify which view to go to next.
2458 */
2459 private int mNextFocusDownId = View.NO_ID;
2460
Jeff Brown4e6319b2010-12-13 10:36:51 -08002461 /**
2462 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2463 * the user may specify which view to go to next.
2464 */
2465 int mNextFocusForwardId = View.NO_ID;
2466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002468 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002469 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002470 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 private UnsetPressedState mUnsetPressedState;
2473
2474 /**
2475 * Whether the long press's action has been invoked. The tap's action is invoked on the
2476 * up event while a long press is invoked as soon as the long press duration is reached, so
2477 * a long press could be performed before the tap is checked, in which case the tap's action
2478 * should not be invoked.
2479 */
2480 private boolean mHasPerformedLongPress;
2481
2482 /**
2483 * The minimum height of the view. We'll try our best to have the height
2484 * of this view to at least this amount.
2485 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002486 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 private int mMinHeight;
2488
2489 /**
2490 * The minimum width of the view. We'll try our best to have the width
2491 * of this view to at least this amount.
2492 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002493 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 private int mMinWidth;
2495
2496 /**
2497 * The delegate to handle touch events that are physically in this view
2498 * but should be handled by another view.
2499 */
2500 private TouchDelegate mTouchDelegate = null;
2501
2502 /**
2503 * Solid color to use as a background when creating the drawing cache. Enables
2504 * the cache to use 16 bit bitmaps instead of 32 bit.
2505 */
2506 private int mDrawingCacheBackgroundColor = 0;
2507
2508 /**
2509 * Special tree observer used when mAttachInfo is null.
2510 */
2511 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002512
Adam Powelle14579b2009-12-16 18:39:52 -08002513 /**
2514 * Cache the touch slop from the context that created the view.
2515 */
2516 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002519 * Object that handles automatic animation of view properties.
2520 */
2521 private ViewPropertyAnimator mAnimator = null;
2522
2523 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002524 * Flag indicating that a drag can cross window boundaries. When
2525 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2526 * with this flag set, all visible applications will be able to participate
2527 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002528 *
2529 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002530 */
2531 public static final int DRAG_FLAG_GLOBAL = 1;
2532
2533 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002534 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2535 */
2536 private float mVerticalScrollFactor;
2537
2538 /**
Adam Powell20232d02010-12-08 21:08:53 -08002539 * Position of the vertical scroll bar.
2540 */
2541 private int mVerticalScrollbarPosition;
2542
2543 /**
2544 * Position the scroll bar at the default position as determined by the system.
2545 */
2546 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2547
2548 /**
2549 * Position the scroll bar along the left edge.
2550 */
2551 public static final int SCROLLBAR_POSITION_LEFT = 1;
2552
2553 /**
2554 * Position the scroll bar along the right edge.
2555 */
2556 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2557
2558 /**
Romain Guy171c5922011-01-06 10:04:23 -08002559 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002560 *
2561 * @see #getLayerType()
2562 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002563 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002564 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002565 */
2566 public static final int LAYER_TYPE_NONE = 0;
2567
2568 /**
2569 * <p>Indicates that the view has a software layer. A software layer is backed
2570 * by a bitmap and causes the view to be rendered using Android's software
2571 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002572 *
Romain Guy171c5922011-01-06 10:04:23 -08002573 * <p>Software layers have various usages:</p>
2574 * <p>When the application is not using hardware acceleration, a software layer
2575 * is useful to apply a specific color filter and/or blending mode and/or
2576 * translucency to a view and all its children.</p>
2577 * <p>When the application is using hardware acceleration, a software layer
2578 * is useful to render drawing primitives not supported by the hardware
2579 * accelerated pipeline. It can also be used to cache a complex view tree
2580 * into a texture and reduce the complexity of drawing operations. For instance,
2581 * when animating a complex view tree with a translation, a software layer can
2582 * be used to render the view tree only once.</p>
2583 * <p>Software layers should be avoided when the affected view tree updates
2584 * often. Every update will require to re-render the software layer, which can
2585 * potentially be slow (particularly when hardware acceleration is turned on
2586 * since the layer will have to be uploaded into a hardware texture after every
2587 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002588 *
2589 * @see #getLayerType()
2590 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002591 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002592 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002593 */
2594 public static final int LAYER_TYPE_SOFTWARE = 1;
2595
2596 /**
2597 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2598 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2599 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2600 * rendering pipeline, but only if hardware acceleration is turned on for the
2601 * view hierarchy. When hardware acceleration is turned off, hardware layers
2602 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002603 *
Romain Guy171c5922011-01-06 10:04:23 -08002604 * <p>A hardware layer is useful to apply a specific color filter and/or
2605 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002606 * <p>A hardware layer can be used to cache a complex view tree into a
2607 * texture and reduce the complexity of drawing operations. For instance,
2608 * when animating a complex view tree with a translation, a hardware layer can
2609 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002610 * <p>A hardware layer can also be used to increase the rendering quality when
2611 * rotation transformations are applied on a view. It can also be used to
2612 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002613 *
2614 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002615 * @see #setLayerType(int, android.graphics.Paint)
2616 * @see #LAYER_TYPE_NONE
2617 * @see #LAYER_TYPE_SOFTWARE
2618 */
2619 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002620
Romain Guy3aaff3a2011-01-12 14:18:47 -08002621 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2622 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2623 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2624 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2625 })
Romain Guy171c5922011-01-06 10:04:23 -08002626 int mLayerType = LAYER_TYPE_NONE;
2627 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002628 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002629
2630 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002631 * Set to true when the view is sending hover accessibility events because it
2632 * is the innermost hovered view.
2633 */
2634 private boolean mSendingHoverAccessibilityEvents;
2635
2636 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002637 * Delegate for injecting accessiblity functionality.
2638 */
2639 AccessibilityDelegate mAccessibilityDelegate;
2640
2641 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002642 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002643 */
2644 public static final int TEXT_DIRECTION_INHERIT = 0;
2645
2646 /**
2647 * Text direction is using "first strong algorithm". The first strong directional character
2648 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002649 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002650 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002651 */
2652 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2653
2654 /**
2655 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2656 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002657 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002658 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002659 */
2660 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2661
2662 /**
2663 * Text direction is forced to LTR.
2664 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002665 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002666 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002667
2668 /**
2669 * Text direction is forced to RTL.
2670 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002671 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002672 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002673
2674 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002675 * Text direction is coming from the system Locale.
2676 *
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002677 */
2678 public static final int TEXT_DIRECTION_LOCALE = 5;
2679
2680 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002681 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002682 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002683 */
2684 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2685
2686 /**
2687 * The text direction that has been defined by {@link #setTextDirection(int)}.
2688 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002689 */
2690 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002691 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2692 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2693 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2694 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002695 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2696 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002697 })
Doug Feltcb3791202011-07-07 11:57:48 -07002698 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002699
2700 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002701 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002702 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002703 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2704 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002705 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002706 */
2707 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002708 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2709 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2710 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002711 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002712 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2713 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002714 })
Doug Feltcb3791202011-07-07 11:57:48 -07002715 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002716
2717 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002718 * Consistency verifier for debugging purposes.
2719 * @hide
2720 */
2721 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2722 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2723 new InputEventConsistencyVerifier(this, 0) : null;
2724
2725 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 * Simple constructor to use when creating a view from code.
2727 *
2728 * @param context The Context the view is running in, through which it can
2729 * access the current theme, resources, etc.
2730 */
2731 public View(Context context) {
2732 mContext = context;
2733 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002734 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002735 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002736 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002737 mUserPaddingStart = -1;
2738 mUserPaddingEnd = -1;
2739 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 }
2741
2742 /**
2743 * Constructor that is called when inflating a view from XML. This is called
2744 * when a view is being constructed from an XML file, supplying attributes
2745 * that were specified in the XML file. This version uses a default style of
2746 * 0, so the only attribute values applied are those in the Context's Theme
2747 * and the given AttributeSet.
2748 *
2749 * <p>
2750 * The method onFinishInflate() will be called after all children have been
2751 * added.
2752 *
2753 * @param context The Context the view is running in, through which it can
2754 * access the current theme, resources, etc.
2755 * @param attrs The attributes of the XML tag that is inflating the view.
2756 * @see #View(Context, AttributeSet, int)
2757 */
2758 public View(Context context, AttributeSet attrs) {
2759 this(context, attrs, 0);
2760 }
2761
2762 /**
2763 * Perform inflation from XML and apply a class-specific base style. This
2764 * constructor of View allows subclasses to use their own base style when
2765 * they are inflating. For example, a Button class's constructor would call
2766 * this version of the super class constructor and supply
2767 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2768 * the theme's button style to modify all of the base view attributes (in
2769 * particular its background) as well as the Button class's attributes.
2770 *
2771 * @param context The Context the view is running in, through which it can
2772 * access the current theme, resources, etc.
2773 * @param attrs The attributes of the XML tag that is inflating the view.
2774 * @param defStyle The default style to apply to this view. If 0, no style
2775 * will be applied (beyond what is included in the theme). This may
2776 * either be an attribute resource, whose value will be retrieved
2777 * from the current theme, or an explicit style resource.
2778 * @see #View(Context, AttributeSet)
2779 */
2780 public View(Context context, AttributeSet attrs, int defStyle) {
2781 this(context);
2782
2783 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2784 defStyle, 0);
2785
2786 Drawable background = null;
2787
2788 int leftPadding = -1;
2789 int topPadding = -1;
2790 int rightPadding = -1;
2791 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002792 int startPadding = -1;
2793 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794
2795 int padding = -1;
2796
2797 int viewFlagValues = 0;
2798 int viewFlagMasks = 0;
2799
2800 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 int x = 0;
2803 int y = 0;
2804
Chet Haase73066682010-11-29 15:55:32 -08002805 float tx = 0;
2806 float ty = 0;
2807 float rotation = 0;
2808 float rotationX = 0;
2809 float rotationY = 0;
2810 float sx = 1f;
2811 float sy = 1f;
2812 boolean transformSet = false;
2813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2815
Adam Powell637d3372010-08-25 14:37:03 -07002816 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 final int N = a.getIndexCount();
2818 for (int i = 0; i < N; i++) {
2819 int attr = a.getIndex(i);
2820 switch (attr) {
2821 case com.android.internal.R.styleable.View_background:
2822 background = a.getDrawable(attr);
2823 break;
2824 case com.android.internal.R.styleable.View_padding:
2825 padding = a.getDimensionPixelSize(attr, -1);
2826 break;
2827 case com.android.internal.R.styleable.View_paddingLeft:
2828 leftPadding = a.getDimensionPixelSize(attr, -1);
2829 break;
2830 case com.android.internal.R.styleable.View_paddingTop:
2831 topPadding = a.getDimensionPixelSize(attr, -1);
2832 break;
2833 case com.android.internal.R.styleable.View_paddingRight:
2834 rightPadding = a.getDimensionPixelSize(attr, -1);
2835 break;
2836 case com.android.internal.R.styleable.View_paddingBottom:
2837 bottomPadding = a.getDimensionPixelSize(attr, -1);
2838 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002839 case com.android.internal.R.styleable.View_paddingStart:
2840 startPadding = a.getDimensionPixelSize(attr, -1);
2841 break;
2842 case com.android.internal.R.styleable.View_paddingEnd:
2843 endPadding = a.getDimensionPixelSize(attr, -1);
2844 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 case com.android.internal.R.styleable.View_scrollX:
2846 x = a.getDimensionPixelOffset(attr, 0);
2847 break;
2848 case com.android.internal.R.styleable.View_scrollY:
2849 y = a.getDimensionPixelOffset(attr, 0);
2850 break;
Chet Haase73066682010-11-29 15:55:32 -08002851 case com.android.internal.R.styleable.View_alpha:
2852 setAlpha(a.getFloat(attr, 1f));
2853 break;
2854 case com.android.internal.R.styleable.View_transformPivotX:
2855 setPivotX(a.getDimensionPixelOffset(attr, 0));
2856 break;
2857 case com.android.internal.R.styleable.View_transformPivotY:
2858 setPivotY(a.getDimensionPixelOffset(attr, 0));
2859 break;
2860 case com.android.internal.R.styleable.View_translationX:
2861 tx = a.getDimensionPixelOffset(attr, 0);
2862 transformSet = true;
2863 break;
2864 case com.android.internal.R.styleable.View_translationY:
2865 ty = a.getDimensionPixelOffset(attr, 0);
2866 transformSet = true;
2867 break;
2868 case com.android.internal.R.styleable.View_rotation:
2869 rotation = a.getFloat(attr, 0);
2870 transformSet = true;
2871 break;
2872 case com.android.internal.R.styleable.View_rotationX:
2873 rotationX = a.getFloat(attr, 0);
2874 transformSet = true;
2875 break;
2876 case com.android.internal.R.styleable.View_rotationY:
2877 rotationY = a.getFloat(attr, 0);
2878 transformSet = true;
2879 break;
2880 case com.android.internal.R.styleable.View_scaleX:
2881 sx = a.getFloat(attr, 1f);
2882 transformSet = true;
2883 break;
2884 case com.android.internal.R.styleable.View_scaleY:
2885 sy = a.getFloat(attr, 1f);
2886 transformSet = true;
2887 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 case com.android.internal.R.styleable.View_id:
2889 mID = a.getResourceId(attr, NO_ID);
2890 break;
2891 case com.android.internal.R.styleable.View_tag:
2892 mTag = a.getText(attr);
2893 break;
2894 case com.android.internal.R.styleable.View_fitsSystemWindows:
2895 if (a.getBoolean(attr, false)) {
2896 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2897 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2898 }
2899 break;
2900 case com.android.internal.R.styleable.View_focusable:
2901 if (a.getBoolean(attr, false)) {
2902 viewFlagValues |= FOCUSABLE;
2903 viewFlagMasks |= FOCUSABLE_MASK;
2904 }
2905 break;
2906 case com.android.internal.R.styleable.View_focusableInTouchMode:
2907 if (a.getBoolean(attr, false)) {
2908 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2909 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2910 }
2911 break;
2912 case com.android.internal.R.styleable.View_clickable:
2913 if (a.getBoolean(attr, false)) {
2914 viewFlagValues |= CLICKABLE;
2915 viewFlagMasks |= CLICKABLE;
2916 }
2917 break;
2918 case com.android.internal.R.styleable.View_longClickable:
2919 if (a.getBoolean(attr, false)) {
2920 viewFlagValues |= LONG_CLICKABLE;
2921 viewFlagMasks |= LONG_CLICKABLE;
2922 }
2923 break;
2924 case com.android.internal.R.styleable.View_saveEnabled:
2925 if (!a.getBoolean(attr, true)) {
2926 viewFlagValues |= SAVE_DISABLED;
2927 viewFlagMasks |= SAVE_DISABLED_MASK;
2928 }
2929 break;
2930 case com.android.internal.R.styleable.View_duplicateParentState:
2931 if (a.getBoolean(attr, false)) {
2932 viewFlagValues |= DUPLICATE_PARENT_STATE;
2933 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2934 }
2935 break;
2936 case com.android.internal.R.styleable.View_visibility:
2937 final int visibility = a.getInt(attr, 0);
2938 if (visibility != 0) {
2939 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2940 viewFlagMasks |= VISIBILITY_MASK;
2941 }
2942 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002943 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002944 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002945 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002946 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002947 final int layoutDirection = a.getInt(attr, -1);
2948 if (layoutDirection != -1) {
2949 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002950 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002951 // Set to default (LAYOUT_DIRECTION_INHERIT)
2952 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002953 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002954 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002955 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 case com.android.internal.R.styleable.View_drawingCacheQuality:
2957 final int cacheQuality = a.getInt(attr, 0);
2958 if (cacheQuality != 0) {
2959 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2960 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2961 }
2962 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002963 case com.android.internal.R.styleable.View_contentDescription:
2964 mContentDescription = a.getString(attr);
2965 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2967 if (!a.getBoolean(attr, true)) {
2968 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2969 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2970 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002971 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2973 if (!a.getBoolean(attr, true)) {
2974 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2975 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2976 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002977 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 case R.styleable.View_scrollbars:
2979 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2980 if (scrollbars != SCROLLBARS_NONE) {
2981 viewFlagValues |= scrollbars;
2982 viewFlagMasks |= SCROLLBARS_MASK;
2983 initializeScrollbars(a);
2984 }
2985 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002986 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002988 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2989 // Ignore the attribute starting with ICS
2990 break;
2991 }
2992 // With builds < ICS, fall through and apply fading edges
2993 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2995 if (fadingEdge != FADING_EDGE_NONE) {
2996 viewFlagValues |= fadingEdge;
2997 viewFlagMasks |= FADING_EDGE_MASK;
2998 initializeFadingEdge(a);
2999 }
3000 break;
3001 case R.styleable.View_scrollbarStyle:
3002 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3003 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3004 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3005 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3006 }
3007 break;
3008 case R.styleable.View_isScrollContainer:
3009 setScrollContainer = true;
3010 if (a.getBoolean(attr, false)) {
3011 setScrollContainer(true);
3012 }
3013 break;
3014 case com.android.internal.R.styleable.View_keepScreenOn:
3015 if (a.getBoolean(attr, false)) {
3016 viewFlagValues |= KEEP_SCREEN_ON;
3017 viewFlagMasks |= KEEP_SCREEN_ON;
3018 }
3019 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003020 case R.styleable.View_filterTouchesWhenObscured:
3021 if (a.getBoolean(attr, false)) {
3022 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3023 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3024 }
3025 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 case R.styleable.View_nextFocusLeft:
3027 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3028 break;
3029 case R.styleable.View_nextFocusRight:
3030 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3031 break;
3032 case R.styleable.View_nextFocusUp:
3033 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3034 break;
3035 case R.styleable.View_nextFocusDown:
3036 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3037 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003038 case R.styleable.View_nextFocusForward:
3039 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3040 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 case R.styleable.View_minWidth:
3042 mMinWidth = a.getDimensionPixelSize(attr, 0);
3043 break;
3044 case R.styleable.View_minHeight:
3045 mMinHeight = a.getDimensionPixelSize(attr, 0);
3046 break;
Romain Guy9a817362009-05-01 10:57:14 -07003047 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003048 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003049 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003050 + "be used within a restricted context");
3051 }
3052
Romain Guy9a817362009-05-01 10:57:14 -07003053 final String handlerName = a.getString(attr);
3054 if (handlerName != null) {
3055 setOnClickListener(new OnClickListener() {
3056 private Method mHandler;
3057
3058 public void onClick(View v) {
3059 if (mHandler == null) {
3060 try {
3061 mHandler = getContext().getClass().getMethod(handlerName,
3062 View.class);
3063 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003064 int id = getId();
3065 String idText = id == NO_ID ? "" : " with id '"
3066 + getContext().getResources().getResourceEntryName(
3067 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003068 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003069 handlerName + "(View) in the activity "
3070 + getContext().getClass() + " for onClick handler"
3071 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003072 }
3073 }
3074
3075 try {
3076 mHandler.invoke(getContext(), View.this);
3077 } catch (IllegalAccessException e) {
3078 throw new IllegalStateException("Could not execute non "
3079 + "public method of the activity", e);
3080 } catch (InvocationTargetException e) {
3081 throw new IllegalStateException("Could not execute "
3082 + "method of the activity", e);
3083 }
3084 }
3085 });
3086 }
3087 break;
Adam Powell637d3372010-08-25 14:37:03 -07003088 case R.styleable.View_overScrollMode:
3089 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3090 break;
Adam Powell20232d02010-12-08 21:08:53 -08003091 case R.styleable.View_verticalScrollbarPosition:
3092 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3093 break;
Romain Guy171c5922011-01-06 10:04:23 -08003094 case R.styleable.View_layerType:
3095 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3096 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003097 case R.styleable.View_textDirection:
3098 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3099 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 }
3101 }
3102
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003103 a.recycle();
3104
Adam Powell637d3372010-08-25 14:37:03 -07003105 setOverScrollMode(overScrollMode);
3106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 if (background != null) {
3108 setBackgroundDrawable(background);
3109 }
3110
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003111 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3112 // layout direction). Those cached values will be used later during padding resolution.
3113 mUserPaddingStart = startPadding;
3114 mUserPaddingEnd = endPadding;
3115
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003116 updateUserPaddingRelative();
3117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 if (padding >= 0) {
3119 leftPadding = padding;
3120 topPadding = padding;
3121 rightPadding = padding;
3122 bottomPadding = padding;
3123 }
3124
3125 // If the user specified the padding (either with android:padding or
3126 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3127 // use the default padding or the padding from the background drawable
3128 // (stored at this point in mPadding*)
3129 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3130 topPadding >= 0 ? topPadding : mPaddingTop,
3131 rightPadding >= 0 ? rightPadding : mPaddingRight,
3132 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3133
3134 if (viewFlagMasks != 0) {
3135 setFlags(viewFlagValues, viewFlagMasks);
3136 }
3137
3138 // Needs to be called after mViewFlags is set
3139 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3140 recomputePadding();
3141 }
3142
3143 if (x != 0 || y != 0) {
3144 scrollTo(x, y);
3145 }
3146
Chet Haase73066682010-11-29 15:55:32 -08003147 if (transformSet) {
3148 setTranslationX(tx);
3149 setTranslationY(ty);
3150 setRotation(rotation);
3151 setRotationX(rotationX);
3152 setRotationY(rotationY);
3153 setScaleX(sx);
3154 setScaleY(sy);
3155 }
3156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3158 setScrollContainer(true);
3159 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003160
3161 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 }
3163
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003164 private void updateUserPaddingRelative() {
3165 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3166 }
3167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 /**
3169 * Non-public constructor for use in testing
3170 */
3171 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003172 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 }
3174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 /**
3176 * <p>
3177 * Initializes the fading edges from a given set of styled attributes. This
3178 * method should be called by subclasses that need fading edges and when an
3179 * instance of these subclasses is created programmatically rather than
3180 * being inflated from XML. This method is automatically called when the XML
3181 * is inflated.
3182 * </p>
3183 *
3184 * @param a the styled attributes set to initialize the fading edges from
3185 */
3186 protected void initializeFadingEdge(TypedArray a) {
3187 initScrollCache();
3188
3189 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3190 R.styleable.View_fadingEdgeLength,
3191 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3192 }
3193
3194 /**
3195 * Returns the size of the vertical faded edges used to indicate that more
3196 * content in this view is visible.
3197 *
3198 * @return The size in pixels of the vertical faded edge or 0 if vertical
3199 * faded edges are not enabled for this view.
3200 * @attr ref android.R.styleable#View_fadingEdgeLength
3201 */
3202 public int getVerticalFadingEdgeLength() {
3203 if (isVerticalFadingEdgeEnabled()) {
3204 ScrollabilityCache cache = mScrollCache;
3205 if (cache != null) {
3206 return cache.fadingEdgeLength;
3207 }
3208 }
3209 return 0;
3210 }
3211
3212 /**
3213 * Set the size of the faded edge used to indicate that more content in this
3214 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003215 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3216 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3217 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 *
3219 * @param length The size in pixels of the faded edge used to indicate that more
3220 * content in this view is visible.
3221 */
3222 public void setFadingEdgeLength(int length) {
3223 initScrollCache();
3224 mScrollCache.fadingEdgeLength = length;
3225 }
3226
3227 /**
3228 * Returns the size of the horizontal faded edges used to indicate that more
3229 * content in this view is visible.
3230 *
3231 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3232 * faded edges are not enabled for this view.
3233 * @attr ref android.R.styleable#View_fadingEdgeLength
3234 */
3235 public int getHorizontalFadingEdgeLength() {
3236 if (isHorizontalFadingEdgeEnabled()) {
3237 ScrollabilityCache cache = mScrollCache;
3238 if (cache != null) {
3239 return cache.fadingEdgeLength;
3240 }
3241 }
3242 return 0;
3243 }
3244
3245 /**
3246 * Returns the width of the vertical scrollbar.
3247 *
3248 * @return The width in pixels of the vertical scrollbar or 0 if there
3249 * is no vertical scrollbar.
3250 */
3251 public int getVerticalScrollbarWidth() {
3252 ScrollabilityCache cache = mScrollCache;
3253 if (cache != null) {
3254 ScrollBarDrawable scrollBar = cache.scrollBar;
3255 if (scrollBar != null) {
3256 int size = scrollBar.getSize(true);
3257 if (size <= 0) {
3258 size = cache.scrollBarSize;
3259 }
3260 return size;
3261 }
3262 return 0;
3263 }
3264 return 0;
3265 }
3266
3267 /**
3268 * Returns the height of the horizontal scrollbar.
3269 *
3270 * @return The height in pixels of the horizontal scrollbar or 0 if
3271 * there is no horizontal scrollbar.
3272 */
3273 protected int getHorizontalScrollbarHeight() {
3274 ScrollabilityCache cache = mScrollCache;
3275 if (cache != null) {
3276 ScrollBarDrawable scrollBar = cache.scrollBar;
3277 if (scrollBar != null) {
3278 int size = scrollBar.getSize(false);
3279 if (size <= 0) {
3280 size = cache.scrollBarSize;
3281 }
3282 return size;
3283 }
3284 return 0;
3285 }
3286 return 0;
3287 }
3288
3289 /**
3290 * <p>
3291 * Initializes the scrollbars from a given set of styled attributes. This
3292 * method should be called by subclasses that need scrollbars and when an
3293 * instance of these subclasses is created programmatically rather than
3294 * being inflated from XML. This method is automatically called when the XML
3295 * is inflated.
3296 * </p>
3297 *
3298 * @param a the styled attributes set to initialize the scrollbars from
3299 */
3300 protected void initializeScrollbars(TypedArray a) {
3301 initScrollCache();
3302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003304
Mike Cleronf116bf82009-09-27 19:14:12 -07003305 if (scrollabilityCache.scrollBar == null) {
3306 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3307 }
Joe Malin32736f02011-01-19 16:14:20 -08003308
Romain Guy8bda2482010-03-02 11:42:11 -08003309 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310
Mike Cleronf116bf82009-09-27 19:14:12 -07003311 if (!fadeScrollbars) {
3312 scrollabilityCache.state = ScrollabilityCache.ON;
3313 }
3314 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003315
3316
Mike Cleronf116bf82009-09-27 19:14:12 -07003317 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3318 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3319 .getScrollBarFadeDuration());
3320 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3321 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3322 ViewConfiguration.getScrollDefaultDelay());
3323
Joe Malin32736f02011-01-19 16:14:20 -08003324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3326 com.android.internal.R.styleable.View_scrollbarSize,
3327 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3328
3329 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3330 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3331
3332 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3333 if (thumb != null) {
3334 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3335 }
3336
3337 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3338 false);
3339 if (alwaysDraw) {
3340 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3341 }
3342
3343 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3344 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3345
3346 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3347 if (thumb != null) {
3348 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3349 }
3350
3351 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3352 false);
3353 if (alwaysDraw) {
3354 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3355 }
3356
3357 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003358 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 }
3360
3361 /**
3362 * <p>
3363 * Initalizes the scrollability cache if necessary.
3364 * </p>
3365 */
3366 private void initScrollCache() {
3367 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003368 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 }
3370 }
3371
3372 /**
Adam Powell20232d02010-12-08 21:08:53 -08003373 * Set the position of the vertical scroll bar. Should be one of
3374 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3375 * {@link #SCROLLBAR_POSITION_RIGHT}.
3376 *
3377 * @param position Where the vertical scroll bar should be positioned.
3378 */
3379 public void setVerticalScrollbarPosition(int position) {
3380 if (mVerticalScrollbarPosition != position) {
3381 mVerticalScrollbarPosition = position;
3382 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003383 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003384 }
3385 }
3386
3387 /**
3388 * @return The position where the vertical scroll bar will show, if applicable.
3389 * @see #setVerticalScrollbarPosition(int)
3390 */
3391 public int getVerticalScrollbarPosition() {
3392 return mVerticalScrollbarPosition;
3393 }
3394
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003395 ListenerInfo getListenerInfo() {
3396 if (mListenerInfo != null) {
3397 return mListenerInfo;
3398 }
3399 mListenerInfo = new ListenerInfo();
3400 return mListenerInfo;
3401 }
3402
Adam Powell20232d02010-12-08 21:08:53 -08003403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 * Register a callback to be invoked when focus of this view changed.
3405 *
3406 * @param l The callback that will run.
3407 */
3408 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003409 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 }
3411
3412 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003413 * Add a listener that will be called when the bounds of the view change due to
3414 * layout processing.
3415 *
3416 * @param listener The listener that will be called when layout bounds change.
3417 */
3418 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003419 ListenerInfo li = getListenerInfo();
3420 if (li.mOnLayoutChangeListeners == null) {
3421 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003422 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003423 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3424 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003425 }
Chet Haase21cd1382010-09-01 17:42:29 -07003426 }
3427
3428 /**
3429 * Remove a listener for layout changes.
3430 *
3431 * @param listener The listener for layout bounds change.
3432 */
3433 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003434 ListenerInfo li = mListenerInfo;
3435 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003436 return;
3437 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003438 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003439 }
3440
3441 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003442 * Add a listener for attach state changes.
3443 *
3444 * This listener will be called whenever this view is attached or detached
3445 * from a window. Remove the listener using
3446 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3447 *
3448 * @param listener Listener to attach
3449 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3450 */
3451 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003452 ListenerInfo li = getListenerInfo();
3453 if (li.mOnAttachStateChangeListeners == null) {
3454 li.mOnAttachStateChangeListeners
3455 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003456 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003457 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003458 }
3459
3460 /**
3461 * Remove a listener for attach state changes. The listener will receive no further
3462 * notification of window attach/detach events.
3463 *
3464 * @param listener Listener to remove
3465 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3466 */
3467 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003468 ListenerInfo li = mListenerInfo;
3469 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003470 return;
3471 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003472 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003473 }
3474
3475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 * Returns the focus-change callback registered for this view.
3477 *
3478 * @return The callback, or null if one is not registered.
3479 */
3480 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003481 ListenerInfo li = mListenerInfo;
3482 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 }
3484
3485 /**
3486 * Register a callback to be invoked when this view is clicked. If this view is not
3487 * clickable, it becomes clickable.
3488 *
3489 * @param l The callback that will run
3490 *
3491 * @see #setClickable(boolean)
3492 */
3493 public void setOnClickListener(OnClickListener l) {
3494 if (!isClickable()) {
3495 setClickable(true);
3496 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003497 getListenerInfo().mOnClickListener = l;
3498 }
3499
3500 /**
3501 * Return whether this view has an attached OnClickListener. Returns
3502 * true if there is a listener, false if there is none.
3503 */
3504 public boolean hasOnClickListeners() {
3505 ListenerInfo li = mListenerInfo;
3506 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 }
3508
3509 /**
3510 * Register a callback to be invoked when this view is clicked and held. If this view is not
3511 * long clickable, it becomes long clickable.
3512 *
3513 * @param l The callback that will run
3514 *
3515 * @see #setLongClickable(boolean)
3516 */
3517 public void setOnLongClickListener(OnLongClickListener l) {
3518 if (!isLongClickable()) {
3519 setLongClickable(true);
3520 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003521 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 }
3523
3524 /**
3525 * Register a callback to be invoked when the context menu for this view is
3526 * being built. If this view is not long clickable, it becomes long clickable.
3527 *
3528 * @param l The callback that will run
3529 *
3530 */
3531 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3532 if (!isLongClickable()) {
3533 setLongClickable(true);
3534 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003535 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 }
3537
3538 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003539 * Call this view's OnClickListener, if it is defined. Performs all normal
3540 * actions associated with clicking: reporting accessibility event, playing
3541 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 *
3543 * @return True there was an assigned OnClickListener that was called, false
3544 * otherwise is returned.
3545 */
3546 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003547 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3548
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003549 ListenerInfo li = mListenerInfo;
3550 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003552 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 return true;
3554 }
3555
3556 return false;
3557 }
3558
3559 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003560 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3561 * this only calls the listener, and does not do any associated clicking
3562 * actions like reporting an accessibility event.
3563 *
3564 * @return True there was an assigned OnClickListener that was called, false
3565 * otherwise is returned.
3566 */
3567 public boolean callOnClick() {
3568 ListenerInfo li = mListenerInfo;
3569 if (li != null && li.mOnClickListener != null) {
3570 li.mOnClickListener.onClick(this);
3571 return true;
3572 }
3573 return false;
3574 }
3575
3576 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003577 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3578 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003580 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 */
3582 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003583 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003586 ListenerInfo li = mListenerInfo;
3587 if (li != null && li.mOnLongClickListener != null) {
3588 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 }
3590 if (!handled) {
3591 handled = showContextMenu();
3592 }
3593 if (handled) {
3594 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3595 }
3596 return handled;
3597 }
3598
3599 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003600 * Performs button-related actions during a touch down event.
3601 *
3602 * @param event The event.
3603 * @return True if the down was consumed.
3604 *
3605 * @hide
3606 */
3607 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3608 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3609 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3610 return true;
3611 }
3612 }
3613 return false;
3614 }
3615
3616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 * Bring up the context menu for this view.
3618 *
3619 * @return Whether a context menu was displayed.
3620 */
3621 public boolean showContextMenu() {
3622 return getParent().showContextMenuForChild(this);
3623 }
3624
3625 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003626 * Bring up the context menu for this view, referring to the item under the specified point.
3627 *
3628 * @param x The referenced x coordinate.
3629 * @param y The referenced y coordinate.
3630 * @param metaState The keyboard modifiers that were pressed.
3631 * @return Whether a context menu was displayed.
3632 *
3633 * @hide
3634 */
3635 public boolean showContextMenu(float x, float y, int metaState) {
3636 return showContextMenu();
3637 }
3638
3639 /**
Adam Powell6e346362010-07-23 10:18:23 -07003640 * Start an action mode.
3641 *
3642 * @param callback Callback that will control the lifecycle of the action mode
3643 * @return The new action mode if it is started, null otherwise
3644 *
3645 * @see ActionMode
3646 */
3647 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003648 ViewParent parent = getParent();
3649 if (parent == null) return null;
3650 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003651 }
3652
3653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 * Register a callback to be invoked when a key is pressed in this view.
3655 * @param l the key listener to attach to this view
3656 */
3657 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003658 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 }
3660
3661 /**
3662 * Register a callback to be invoked when a touch event is sent to this view.
3663 * @param l the touch listener to attach to this view
3664 */
3665 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003666 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 }
3668
3669 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003670 * Register a callback to be invoked when a generic motion event is sent to this view.
3671 * @param l the generic motion listener to attach to this view
3672 */
3673 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003674 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003675 }
3676
3677 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003678 * Register a callback to be invoked when a hover event is sent to this view.
3679 * @param l the hover listener to attach to this view
3680 */
3681 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003682 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003683 }
3684
3685 /**
Joe Malin32736f02011-01-19 16:14:20 -08003686 * Register a drag event listener callback object for this View. The parameter is
3687 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3688 * View, the system calls the
3689 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3690 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003691 */
3692 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003693 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003694 }
3695
3696 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003697 * Give this view focus. This will cause
3698 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 *
3700 * Note: this does not check whether this {@link View} should get focus, it just
3701 * gives it focus no matter what. It should only be called internally by framework
3702 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3703 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003704 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3705 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 * focus moved when requestFocus() is called. It may not always
3707 * apply, in which case use the default View.FOCUS_DOWN.
3708 * @param previouslyFocusedRect The rectangle of the view that had focus
3709 * prior in this View's coordinate system.
3710 */
3711 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3712 if (DBG) {
3713 System.out.println(this + " requestFocus()");
3714 }
3715
3716 if ((mPrivateFlags & FOCUSED) == 0) {
3717 mPrivateFlags |= FOCUSED;
3718
3719 if (mParent != null) {
3720 mParent.requestChildFocus(this, this);
3721 }
3722
3723 onFocusChanged(true, direction, previouslyFocusedRect);
3724 refreshDrawableState();
3725 }
3726 }
3727
3728 /**
3729 * Request that a rectangle of this view be visible on the screen,
3730 * scrolling if necessary just enough.
3731 *
3732 * <p>A View should call this if it maintains some notion of which part
3733 * of its content is interesting. For example, a text editing view
3734 * should call this when its cursor moves.
3735 *
3736 * @param rectangle The rectangle.
3737 * @return Whether any parent scrolled.
3738 */
3739 public boolean requestRectangleOnScreen(Rect rectangle) {
3740 return requestRectangleOnScreen(rectangle, false);
3741 }
3742
3743 /**
3744 * Request that a rectangle of this view be visible on the screen,
3745 * scrolling if necessary just enough.
3746 *
3747 * <p>A View should call this if it maintains some notion of which part
3748 * of its content is interesting. For example, a text editing view
3749 * should call this when its cursor moves.
3750 *
3751 * <p>When <code>immediate</code> is set to true, scrolling will not be
3752 * animated.
3753 *
3754 * @param rectangle The rectangle.
3755 * @param immediate True to forbid animated scrolling, false otherwise
3756 * @return Whether any parent scrolled.
3757 */
3758 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3759 View child = this;
3760 ViewParent parent = mParent;
3761 boolean scrolled = false;
3762 while (parent != null) {
3763 scrolled |= parent.requestChildRectangleOnScreen(child,
3764 rectangle, immediate);
3765
3766 // offset rect so next call has the rectangle in the
3767 // coordinate system of its direct child.
3768 rectangle.offset(child.getLeft(), child.getTop());
3769 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3770
3771 if (!(parent instanceof View)) {
3772 break;
3773 }
Romain Guy8506ab42009-06-11 17:35:47 -07003774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 child = (View) parent;
3776 parent = child.getParent();
3777 }
3778 return scrolled;
3779 }
3780
3781 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003782 * Called when this view wants to give up focus. If focus is cleared
3783 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3784 * <p>
3785 * <strong>Note:</strong> When a View clears focus the framework is trying
3786 * to give focus to the first focusable View from the top. Hence, if this
3787 * View is the first from the top that can take focus, then its focus will
3788 * not be cleared nor will the focus change callback be invoked.
3789 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 */
3791 public void clearFocus() {
3792 if (DBG) {
3793 System.out.println(this + " clearFocus()");
3794 }
3795
3796 if ((mPrivateFlags & FOCUSED) != 0) {
3797 mPrivateFlags &= ~FOCUSED;
3798
3799 if (mParent != null) {
3800 mParent.clearChildFocus(this);
3801 }
3802
3803 onFocusChanged(false, 0, null);
3804 refreshDrawableState();
3805 }
3806 }
3807
3808 /**
3809 * Called to clear the focus of a view that is about to be removed.
3810 * Doesn't call clearChildFocus, which prevents this view from taking
3811 * focus again before it has been removed from the parent
3812 */
3813 void clearFocusForRemoval() {
3814 if ((mPrivateFlags & FOCUSED) != 0) {
3815 mPrivateFlags &= ~FOCUSED;
3816
3817 onFocusChanged(false, 0, null);
3818 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003819
3820 // The view cleared focus and invoked the callbacks, so now is the
3821 // time to give focus to the the first focusable from the top to
3822 // ensure that the gain focus is announced after clear focus.
3823 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 }
3825 }
3826
3827 /**
3828 * Called internally by the view system when a new view is getting focus.
3829 * This is what clears the old focus.
3830 */
3831 void unFocus() {
3832 if (DBG) {
3833 System.out.println(this + " unFocus()");
3834 }
3835
3836 if ((mPrivateFlags & FOCUSED) != 0) {
3837 mPrivateFlags &= ~FOCUSED;
3838
3839 onFocusChanged(false, 0, null);
3840 refreshDrawableState();
3841 }
3842 }
3843
3844 /**
3845 * Returns true if this view has focus iteself, or is the ancestor of the
3846 * view that has focus.
3847 *
3848 * @return True if this view has or contains focus, false otherwise.
3849 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003850 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 public boolean hasFocus() {
3852 return (mPrivateFlags & FOCUSED) != 0;
3853 }
3854
3855 /**
3856 * Returns true if this view is focusable or if it contains a reachable View
3857 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3858 * is a View whose parents do not block descendants focus.
3859 *
3860 * Only {@link #VISIBLE} views are considered focusable.
3861 *
3862 * @return True if the view is focusable or if the view contains a focusable
3863 * View, false otherwise.
3864 *
3865 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3866 */
3867 public boolean hasFocusable() {
3868 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3869 }
3870
3871 /**
3872 * Called by the view system when the focus state of this view changes.
3873 * When the focus change event is caused by directional navigation, direction
3874 * and previouslyFocusedRect provide insight into where the focus is coming from.
3875 * When overriding, be sure to call up through to the super class so that
3876 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003877 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 * @param gainFocus True if the View has focus; false otherwise.
3879 * @param direction The direction focus has moved when requestFocus()
3880 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003881 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3882 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3883 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3885 * system, of the previously focused view. If applicable, this will be
3886 * passed in as finer grained information about where the focus is coming
3887 * from (in addition to direction). Will be <code>null</code> otherwise.
3888 */
3889 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003890 if (gainFocus) {
3891 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3892 }
3893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 InputMethodManager imm = InputMethodManager.peekInstance();
3895 if (!gainFocus) {
3896 if (isPressed()) {
3897 setPressed(false);
3898 }
3899 if (imm != null && mAttachInfo != null
3900 && mAttachInfo.mHasWindowFocus) {
3901 imm.focusOut(this);
3902 }
Romain Guya2431d02009-04-30 16:30:00 -07003903 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 } else if (imm != null && mAttachInfo != null
3905 && mAttachInfo.mHasWindowFocus) {
3906 imm.focusIn(this);
3907 }
Romain Guy8506ab42009-06-11 17:35:47 -07003908
Romain Guy0fd89bf2011-01-26 15:41:30 -08003909 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003910 ListenerInfo li = mListenerInfo;
3911 if (li != null && li.mOnFocusChangeListener != null) {
3912 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 }
Joe Malin32736f02011-01-19 16:14:20 -08003914
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003915 if (mAttachInfo != null) {
3916 mAttachInfo.mKeyDispatchState.reset(this);
3917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 }
3919
3920 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003921 * Sends an accessibility event of the given type. If accessiiblity is
3922 * not enabled this method has no effect. The default implementation calls
3923 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3924 * to populate information about the event source (this View), then calls
3925 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3926 * populate the text content of the event source including its descendants,
3927 * and last calls
3928 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3929 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003930 * <p>
3931 * If an {@link AccessibilityDelegate} has been specified via calling
3932 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3933 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3934 * responsible for handling this call.
3935 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003936 *
Scott Mainb303d832011-10-12 16:45:18 -07003937 * @param eventType The type of the event to send, as defined by several types from
3938 * {@link android.view.accessibility.AccessibilityEvent}, such as
3939 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3940 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003941 *
3942 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3943 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3944 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003945 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003946 */
3947 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003948 if (mAccessibilityDelegate != null) {
3949 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3950 } else {
3951 sendAccessibilityEventInternal(eventType);
3952 }
3953 }
3954
3955 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08003956 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
3957 * {@link AccessibilityEvent} to make an announcement which is related to some
3958 * sort of a context change for which none of the events representing UI transitions
3959 * is a good fit. For example, announcing a new page in a book. If accessibility
3960 * is not enabled this method does nothing.
3961 *
3962 * @param text The announcement text.
3963 */
3964 public void announceForAccessibility(CharSequence text) {
3965 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3966 AccessibilityEvent event = AccessibilityEvent.obtain(
3967 AccessibilityEvent.TYPE_ANNOUNCEMENT);
3968 event.getText().add(text);
3969 sendAccessibilityEventUnchecked(event);
3970 }
3971 }
3972
3973 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003974 * @see #sendAccessibilityEvent(int)
3975 *
3976 * Note: Called from the default {@link AccessibilityDelegate}.
3977 */
3978 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003979 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3980 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3981 }
3982 }
3983
3984 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003985 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3986 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003987 * perform a check whether accessibility is enabled.
3988 * <p>
3989 * If an {@link AccessibilityDelegate} has been specified via calling
3990 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3991 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3992 * is responsible for handling this call.
3993 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003994 *
3995 * @param event The event to send.
3996 *
3997 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003998 */
3999 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004000 if (mAccessibilityDelegate != null) {
4001 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4002 } else {
4003 sendAccessibilityEventUncheckedInternal(event);
4004 }
4005 }
4006
4007 /**
4008 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4009 *
4010 * Note: Called from the default {@link AccessibilityDelegate}.
4011 */
4012 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004013 if (!isShown()) {
4014 return;
4015 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004016 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004017 // Only a subset of accessibility events populates text content.
4018 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4019 dispatchPopulateAccessibilityEvent(event);
4020 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004021 // In the beginning we called #isShown(), so we know that getParent() is not null.
4022 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004023 }
4024
4025 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004026 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4027 * to its children for adding their text content to the event. Note that the
4028 * event text is populated in a separate dispatch path since we add to the
4029 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004030 * A typical implementation will call
4031 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4032 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4033 * on each child. Override this method if custom population of the event text
4034 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004035 * <p>
4036 * If an {@link AccessibilityDelegate} has been specified via calling
4037 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4038 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4039 * is responsible for handling this call.
4040 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004041 * <p>
4042 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4043 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4044 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004045 *
4046 * @param event The event.
4047 *
4048 * @return True if the event population was completed.
4049 */
4050 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004051 if (mAccessibilityDelegate != null) {
4052 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4053 } else {
4054 return dispatchPopulateAccessibilityEventInternal(event);
4055 }
4056 }
4057
4058 /**
4059 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4060 *
4061 * Note: Called from the default {@link AccessibilityDelegate}.
4062 */
4063 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004064 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004065 return false;
4066 }
4067
4068 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004069 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004070 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004071 * text content. While this method is free to modify event
4072 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004073 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4074 * <p>
4075 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004076 * to the text added by the super implementation:
4077 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004078 * super.onPopulateAccessibilityEvent(event);
4079 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4080 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4081 * mCurrentDate.getTimeInMillis(), flags);
4082 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004083 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004084 * <p>
4085 * If an {@link AccessibilityDelegate} has been specified via calling
4086 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4087 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4088 * is responsible for handling this call.
4089 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004090 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4091 * information to the event, in case the default implementation has basic information to add.
4092 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004093 *
4094 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004095 *
4096 * @see #sendAccessibilityEvent(int)
4097 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004098 */
4099 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004100 if (mAccessibilityDelegate != null) {
4101 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4102 } else {
4103 onPopulateAccessibilityEventInternal(event);
4104 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004105 }
4106
4107 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004108 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4109 *
4110 * Note: Called from the default {@link AccessibilityDelegate}.
4111 */
4112 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4113
4114 }
4115
4116 /**
4117 * Initializes an {@link AccessibilityEvent} with information about
4118 * this View which is the event source. In other words, the source of
4119 * an accessibility event is the view whose state change triggered firing
4120 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004121 * <p>
4122 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004123 * to properties set by the super implementation:
4124 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4125 * super.onInitializeAccessibilityEvent(event);
4126 * event.setPassword(true);
4127 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004128 * <p>
4129 * If an {@link AccessibilityDelegate} has been specified via calling
4130 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4131 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4132 * is responsible for handling this call.
4133 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004134 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4135 * information to the event, in case the default implementation has basic information to add.
4136 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004137 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004138 *
4139 * @see #sendAccessibilityEvent(int)
4140 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4141 */
4142 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004143 if (mAccessibilityDelegate != null) {
4144 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4145 } else {
4146 onInitializeAccessibilityEventInternal(event);
4147 }
4148 }
4149
4150 /**
4151 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4152 *
4153 * Note: Called from the default {@link AccessibilityDelegate}.
4154 */
4155 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004156 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004157 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004158 event.setPackageName(getContext().getPackageName());
4159 event.setEnabled(isEnabled());
4160 event.setContentDescription(mContentDescription);
4161
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004162 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4163 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4164 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4165 FOCUSABLES_ALL);
4166 event.setItemCount(focusablesTempList.size());
4167 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4168 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004169 }
4170 }
4171
4172 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004173 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4174 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4175 * This method is responsible for obtaining an accessibility node info from a
4176 * pool of reusable instances and calling
4177 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4178 * initialize the former.
4179 * <p>
4180 * Note: The client is responsible for recycling the obtained instance by calling
4181 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4182 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004183 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004184 * @return A populated {@link AccessibilityNodeInfo}.
4185 *
4186 * @see AccessibilityNodeInfo
4187 */
4188 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004189 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4190 if (provider != null) {
4191 return provider.createAccessibilityNodeInfo(View.NO_ID);
4192 } else {
4193 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4194 onInitializeAccessibilityNodeInfo(info);
4195 return info;
4196 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004197 }
4198
4199 /**
4200 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4201 * The base implementation sets:
4202 * <ul>
4203 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004204 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4205 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004206 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4207 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4208 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4209 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4210 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4211 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4212 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4213 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4214 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4215 * </ul>
4216 * <p>
4217 * Subclasses should override this method, call the super implementation,
4218 * and set additional attributes.
4219 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004220 * <p>
4221 * If an {@link AccessibilityDelegate} has been specified via calling
4222 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4223 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4224 * is responsible for handling this call.
4225 * </p>
4226 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004227 * @param info The instance to initialize.
4228 */
4229 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004230 if (mAccessibilityDelegate != null) {
4231 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4232 } else {
4233 onInitializeAccessibilityNodeInfoInternal(info);
4234 }
4235 }
4236
4237 /**
4238 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4239 *
4240 * Note: Called from the default {@link AccessibilityDelegate}.
4241 */
4242 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004243 Rect bounds = mAttachInfo.mTmpInvalRect;
4244 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004245 info.setBoundsInParent(bounds);
4246
4247 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4248 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004249 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004250 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4251 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004252
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004253 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4254 ViewParent parent = getParent();
4255 if (parent instanceof View) {
4256 View parentView = (View) parent;
4257 info.setParent(parentView);
4258 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004259 }
4260
4261 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004262 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004263 info.setContentDescription(getContentDescription());
4264
4265 info.setEnabled(isEnabled());
4266 info.setClickable(isClickable());
4267 info.setFocusable(isFocusable());
4268 info.setFocused(isFocused());
4269 info.setSelected(isSelected());
4270 info.setLongClickable(isLongClickable());
4271
4272 // TODO: These make sense only if we are in an AdapterView but all
4273 // views can be selected. Maybe from accessiiblity perspective
4274 // we should report as selectable view in an AdapterView.
4275 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4276 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4277
4278 if (isFocusable()) {
4279 if (isFocused()) {
4280 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4281 } else {
4282 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4283 }
4284 }
4285 }
4286
4287 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004288 * Sets a delegate for implementing accessibility support via compositon as
4289 * opposed to inheritance. The delegate's primary use is for implementing
4290 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4291 *
4292 * @param delegate The delegate instance.
4293 *
4294 * @see AccessibilityDelegate
4295 */
4296 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4297 mAccessibilityDelegate = delegate;
4298 }
4299
4300 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004301 * Gets the provider for managing a virtual view hierarchy rooted at this View
4302 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4303 * that explore the window content.
4304 * <p>
4305 * If this method returns an instance, this instance is responsible for managing
4306 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4307 * View including the one representing the View itself. Similarly the returned
4308 * instance is responsible for performing accessibility actions on any virtual
4309 * view or the root view itself.
4310 * </p>
4311 * <p>
4312 * If an {@link AccessibilityDelegate} has been specified via calling
4313 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4314 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4315 * is responsible for handling this call.
4316 * </p>
4317 *
4318 * @return The provider.
4319 *
4320 * @see AccessibilityNodeProvider
4321 */
4322 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4323 if (mAccessibilityDelegate != null) {
4324 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4325 } else {
4326 return null;
4327 }
4328 }
4329
4330 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004331 * Gets the unique identifier of this view on the screen for accessibility purposes.
4332 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4333 *
4334 * @return The view accessibility id.
4335 *
4336 * @hide
4337 */
4338 public int getAccessibilityViewId() {
4339 if (mAccessibilityViewId == NO_ID) {
4340 mAccessibilityViewId = sNextAccessibilityViewId++;
4341 }
4342 return mAccessibilityViewId;
4343 }
4344
4345 /**
4346 * Gets the unique identifier of the window in which this View reseides.
4347 *
4348 * @return The window accessibility id.
4349 *
4350 * @hide
4351 */
4352 public int getAccessibilityWindowId() {
4353 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4354 }
4355
4356 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004357 * Gets the {@link View} description. It briefly describes the view and is
4358 * primarily used for accessibility support. Set this property to enable
4359 * better accessibility support for your application. This is especially
4360 * true for views that do not have textual representation (For example,
4361 * ImageButton).
4362 *
4363 * @return The content descriptiopn.
4364 *
4365 * @attr ref android.R.styleable#View_contentDescription
4366 */
4367 public CharSequence getContentDescription() {
4368 return mContentDescription;
4369 }
4370
4371 /**
4372 * Sets the {@link View} description. It briefly describes the view and is
4373 * primarily used for accessibility support. Set this property to enable
4374 * better accessibility support for your application. This is especially
4375 * true for views that do not have textual representation (For example,
4376 * ImageButton).
4377 *
4378 * @param contentDescription The content description.
4379 *
4380 * @attr ref android.R.styleable#View_contentDescription
4381 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004382 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004383 public void setContentDescription(CharSequence contentDescription) {
4384 mContentDescription = contentDescription;
4385 }
4386
4387 /**
Romain Guya2431d02009-04-30 16:30:00 -07004388 * Invoked whenever this view loses focus, either by losing window focus or by losing
4389 * focus within its window. This method can be used to clear any state tied to the
4390 * focus. For instance, if a button is held pressed with the trackball and the window
4391 * loses focus, this method can be used to cancel the press.
4392 *
4393 * Subclasses of View overriding this method should always call super.onFocusLost().
4394 *
4395 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004396 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004397 *
4398 * @hide pending API council approval
4399 */
4400 protected void onFocusLost() {
4401 resetPressedState();
4402 }
4403
4404 private void resetPressedState() {
4405 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4406 return;
4407 }
4408
4409 if (isPressed()) {
4410 setPressed(false);
4411
4412 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004413 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004414 }
4415 }
4416 }
4417
4418 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 * Returns true if this view has focus
4420 *
4421 * @return True if this view has focus, false otherwise.
4422 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004423 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 public boolean isFocused() {
4425 return (mPrivateFlags & FOCUSED) != 0;
4426 }
4427
4428 /**
4429 * Find the view in the hierarchy rooted at this view that currently has
4430 * focus.
4431 *
4432 * @return The view that currently has focus, or null if no focused view can
4433 * be found.
4434 */
4435 public View findFocus() {
4436 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4437 }
4438
4439 /**
4440 * Change whether this view is one of the set of scrollable containers in
4441 * its window. This will be used to determine whether the window can
4442 * resize or must pan when a soft input area is open -- scrollable
4443 * containers allow the window to use resize mode since the container
4444 * will appropriately shrink.
4445 */
4446 public void setScrollContainer(boolean isScrollContainer) {
4447 if (isScrollContainer) {
4448 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4449 mAttachInfo.mScrollContainers.add(this);
4450 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4451 }
4452 mPrivateFlags |= SCROLL_CONTAINER;
4453 } else {
4454 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4455 mAttachInfo.mScrollContainers.remove(this);
4456 }
4457 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4458 }
4459 }
4460
4461 /**
4462 * Returns the quality of the drawing cache.
4463 *
4464 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4465 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4466 *
4467 * @see #setDrawingCacheQuality(int)
4468 * @see #setDrawingCacheEnabled(boolean)
4469 * @see #isDrawingCacheEnabled()
4470 *
4471 * @attr ref android.R.styleable#View_drawingCacheQuality
4472 */
4473 public int getDrawingCacheQuality() {
4474 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4475 }
4476
4477 /**
4478 * Set the drawing cache quality of this view. This value is used only when the
4479 * drawing cache is enabled
4480 *
4481 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4482 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4483 *
4484 * @see #getDrawingCacheQuality()
4485 * @see #setDrawingCacheEnabled(boolean)
4486 * @see #isDrawingCacheEnabled()
4487 *
4488 * @attr ref android.R.styleable#View_drawingCacheQuality
4489 */
4490 public void setDrawingCacheQuality(int quality) {
4491 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4492 }
4493
4494 /**
4495 * Returns whether the screen should remain on, corresponding to the current
4496 * value of {@link #KEEP_SCREEN_ON}.
4497 *
4498 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4499 *
4500 * @see #setKeepScreenOn(boolean)
4501 *
4502 * @attr ref android.R.styleable#View_keepScreenOn
4503 */
4504 public boolean getKeepScreenOn() {
4505 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4506 }
4507
4508 /**
4509 * Controls whether the screen should remain on, modifying the
4510 * value of {@link #KEEP_SCREEN_ON}.
4511 *
4512 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4513 *
4514 * @see #getKeepScreenOn()
4515 *
4516 * @attr ref android.R.styleable#View_keepScreenOn
4517 */
4518 public void setKeepScreenOn(boolean keepScreenOn) {
4519 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4520 }
4521
4522 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004523 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4524 * @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 -08004525 *
4526 * @attr ref android.R.styleable#View_nextFocusLeft
4527 */
4528 public int getNextFocusLeftId() {
4529 return mNextFocusLeftId;
4530 }
4531
4532 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004533 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4534 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4535 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 *
4537 * @attr ref android.R.styleable#View_nextFocusLeft
4538 */
4539 public void setNextFocusLeftId(int nextFocusLeftId) {
4540 mNextFocusLeftId = nextFocusLeftId;
4541 }
4542
4543 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004544 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4545 * @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 -08004546 *
4547 * @attr ref android.R.styleable#View_nextFocusRight
4548 */
4549 public int getNextFocusRightId() {
4550 return mNextFocusRightId;
4551 }
4552
4553 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004554 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4555 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4556 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 *
4558 * @attr ref android.R.styleable#View_nextFocusRight
4559 */
4560 public void setNextFocusRightId(int nextFocusRightId) {
4561 mNextFocusRightId = nextFocusRightId;
4562 }
4563
4564 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004565 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4566 * @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 -08004567 *
4568 * @attr ref android.R.styleable#View_nextFocusUp
4569 */
4570 public int getNextFocusUpId() {
4571 return mNextFocusUpId;
4572 }
4573
4574 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004575 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4576 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4577 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 *
4579 * @attr ref android.R.styleable#View_nextFocusUp
4580 */
4581 public void setNextFocusUpId(int nextFocusUpId) {
4582 mNextFocusUpId = nextFocusUpId;
4583 }
4584
4585 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004586 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4587 * @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 -08004588 *
4589 * @attr ref android.R.styleable#View_nextFocusDown
4590 */
4591 public int getNextFocusDownId() {
4592 return mNextFocusDownId;
4593 }
4594
4595 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004596 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4597 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4598 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 *
4600 * @attr ref android.R.styleable#View_nextFocusDown
4601 */
4602 public void setNextFocusDownId(int nextFocusDownId) {
4603 mNextFocusDownId = nextFocusDownId;
4604 }
4605
4606 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004607 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4608 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4609 *
4610 * @attr ref android.R.styleable#View_nextFocusForward
4611 */
4612 public int getNextFocusForwardId() {
4613 return mNextFocusForwardId;
4614 }
4615
4616 /**
4617 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4618 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4619 * decide automatically.
4620 *
4621 * @attr ref android.R.styleable#View_nextFocusForward
4622 */
4623 public void setNextFocusForwardId(int nextFocusForwardId) {
4624 mNextFocusForwardId = nextFocusForwardId;
4625 }
4626
4627 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628 * Returns the visibility of this view and all of its ancestors
4629 *
4630 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4631 */
4632 public boolean isShown() {
4633 View current = this;
4634 //noinspection ConstantConditions
4635 do {
4636 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4637 return false;
4638 }
4639 ViewParent parent = current.mParent;
4640 if (parent == null) {
4641 return false; // We are not attached to the view root
4642 }
4643 if (!(parent instanceof View)) {
4644 return true;
4645 }
4646 current = (View) parent;
4647 } while (current != null);
4648
4649 return false;
4650 }
4651
4652 /**
4653 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4654 * is set
4655 *
4656 * @param insets Insets for system windows
4657 *
4658 * @return True if this view applied the insets, false otherwise
4659 */
4660 protected boolean fitSystemWindows(Rect insets) {
4661 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4662 mPaddingLeft = insets.left;
4663 mPaddingTop = insets.top;
4664 mPaddingRight = insets.right;
4665 mPaddingBottom = insets.bottom;
4666 requestLayout();
4667 return true;
4668 }
4669 return false;
4670 }
4671
4672 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004673 * Set whether or not this view should account for system screen decorations
4674 * such as the status bar and inset its content. This allows this view to be
4675 * positioned in absolute screen coordinates and remain visible to the user.
4676 *
4677 * <p>This should only be used by top-level window decor views.
4678 *
4679 * @param fitSystemWindows true to inset content for system screen decorations, false for
4680 * default behavior.
4681 *
4682 * @attr ref android.R.styleable#View_fitsSystemWindows
4683 */
4684 public void setFitsSystemWindows(boolean fitSystemWindows) {
4685 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4686 }
4687
4688 /**
4689 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4690 * will account for system screen decorations such as the status bar and inset its
4691 * content. This allows the view to be positioned in absolute screen coordinates
4692 * and remain visible to the user.
4693 *
4694 * @return true if this view will adjust its content bounds for system screen decorations.
4695 *
4696 * @attr ref android.R.styleable#View_fitsSystemWindows
4697 */
4698 public boolean fitsSystemWindows() {
4699 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4700 }
4701
4702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 * Returns the visibility status for this view.
4704 *
4705 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4706 * @attr ref android.R.styleable#View_visibility
4707 */
4708 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004709 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4710 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4711 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004712 })
4713 public int getVisibility() {
4714 return mViewFlags & VISIBILITY_MASK;
4715 }
4716
4717 /**
4718 * Set the enabled state of this view.
4719 *
4720 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4721 * @attr ref android.R.styleable#View_visibility
4722 */
4723 @RemotableViewMethod
4724 public void setVisibility(int visibility) {
4725 setFlags(visibility, VISIBILITY_MASK);
4726 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4727 }
4728
4729 /**
4730 * Returns the enabled status for this view. The interpretation of the
4731 * enabled state varies by subclass.
4732 *
4733 * @return True if this view is enabled, false otherwise.
4734 */
4735 @ViewDebug.ExportedProperty
4736 public boolean isEnabled() {
4737 return (mViewFlags & ENABLED_MASK) == ENABLED;
4738 }
4739
4740 /**
4741 * Set the enabled state of this view. The interpretation of the enabled
4742 * state varies by subclass.
4743 *
4744 * @param enabled True if this view is enabled, false otherwise.
4745 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004746 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004748 if (enabled == isEnabled()) return;
4749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004750 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4751
4752 /*
4753 * The View most likely has to change its appearance, so refresh
4754 * the drawable state.
4755 */
4756 refreshDrawableState();
4757
4758 // Invalidate too, since the default behavior for views is to be
4759 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004760 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 }
4762
4763 /**
4764 * Set whether this view can receive the focus.
4765 *
4766 * Setting this to false will also ensure that this view is not focusable
4767 * in touch mode.
4768 *
4769 * @param focusable If true, this view can receive the focus.
4770 *
4771 * @see #setFocusableInTouchMode(boolean)
4772 * @attr ref android.R.styleable#View_focusable
4773 */
4774 public void setFocusable(boolean focusable) {
4775 if (!focusable) {
4776 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4777 }
4778 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4779 }
4780
4781 /**
4782 * Set whether this view can receive focus while in touch mode.
4783 *
4784 * Setting this to true will also ensure that this view is focusable.
4785 *
4786 * @param focusableInTouchMode If true, this view can receive the focus while
4787 * in touch mode.
4788 *
4789 * @see #setFocusable(boolean)
4790 * @attr ref android.R.styleable#View_focusableInTouchMode
4791 */
4792 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4793 // Focusable in touch mode should always be set before the focusable flag
4794 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4795 // which, in touch mode, will not successfully request focus on this view
4796 // because the focusable in touch mode flag is not set
4797 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4798 if (focusableInTouchMode) {
4799 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4800 }
4801 }
4802
4803 /**
4804 * Set whether this view should have sound effects enabled for events such as
4805 * clicking and touching.
4806 *
4807 * <p>You may wish to disable sound effects for a view if you already play sounds,
4808 * for instance, a dial key that plays dtmf tones.
4809 *
4810 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4811 * @see #isSoundEffectsEnabled()
4812 * @see #playSoundEffect(int)
4813 * @attr ref android.R.styleable#View_soundEffectsEnabled
4814 */
4815 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4816 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4817 }
4818
4819 /**
4820 * @return whether this view should have sound effects enabled for events such as
4821 * clicking and touching.
4822 *
4823 * @see #setSoundEffectsEnabled(boolean)
4824 * @see #playSoundEffect(int)
4825 * @attr ref android.R.styleable#View_soundEffectsEnabled
4826 */
4827 @ViewDebug.ExportedProperty
4828 public boolean isSoundEffectsEnabled() {
4829 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4830 }
4831
4832 /**
4833 * Set whether this view should have haptic feedback for events such as
4834 * long presses.
4835 *
4836 * <p>You may wish to disable haptic feedback if your view already controls
4837 * its own haptic feedback.
4838 *
4839 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4840 * @see #isHapticFeedbackEnabled()
4841 * @see #performHapticFeedback(int)
4842 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4843 */
4844 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4845 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4846 }
4847
4848 /**
4849 * @return whether this view should have haptic feedback enabled for events
4850 * long presses.
4851 *
4852 * @see #setHapticFeedbackEnabled(boolean)
4853 * @see #performHapticFeedback(int)
4854 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4855 */
4856 @ViewDebug.ExportedProperty
4857 public boolean isHapticFeedbackEnabled() {
4858 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4859 }
4860
4861 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004862 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004863 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004864 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4865 * {@link #LAYOUT_DIRECTION_RTL},
4866 * {@link #LAYOUT_DIRECTION_INHERIT} or
4867 * {@link #LAYOUT_DIRECTION_LOCALE}.
4868 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004869 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004870 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004871 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4872 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4873 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4874 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004875 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004876 public int getLayoutDirection() {
4877 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004878 }
4879
4880 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004881 * Set the layout direction for this view. This will propagate a reset of layout direction
4882 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004883 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004884 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4885 * {@link #LAYOUT_DIRECTION_RTL},
4886 * {@link #LAYOUT_DIRECTION_INHERIT} or
4887 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004888 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004889 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004890 */
4891 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004892 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004893 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004894 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004895 // Setting the flag will also request a layout.
4896 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4897 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004898 }
4899
4900 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004901 * Returns the resolved layout direction for this view.
4902 *
4903 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4904 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004905 */
4906 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4907 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4908 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4909 })
4910 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004911 resolveLayoutDirectionIfNeeded();
4912 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004913 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4914 }
4915
4916 /**
4917 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4918 * layout attribute and/or the inherited value from the parent.</p>
4919 *
4920 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004921 */
4922 @ViewDebug.ExportedProperty(category = "layout")
4923 public boolean isLayoutRtl() {
4924 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4925 }
4926
4927 /**
Adam Powell539ee872012-02-03 19:00:49 -08004928 * Indicates whether the view is currently tracking transient state that the
4929 * app should not need to concern itself with saving and restoring, but that
4930 * the framework should take special note to preserve when possible.
4931 *
4932 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004933 */
4934 @ViewDebug.ExportedProperty(category = "layout")
4935 public boolean hasTransientState() {
4936 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4937 }
4938
4939 /**
4940 * Set whether this view is currently tracking transient state that the
4941 * framework should attempt to preserve when possible.
4942 *
4943 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004944 */
4945 public void setHasTransientState(boolean hasTransientState) {
4946 if (hasTransientState() == hasTransientState) return;
4947
4948 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4949 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4950 if (mParent != null) {
4951 try {
4952 mParent.childHasTransientStateChanged(this, hasTransientState);
4953 } catch (AbstractMethodError e) {
4954 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
4955 " does not fully implement ViewParent", e);
4956 }
4957 }
4958 }
4959
4960 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004961 * If this view doesn't do any drawing on its own, set this flag to
4962 * allow further optimizations. By default, this flag is not set on
4963 * View, but could be set on some View subclasses such as ViewGroup.
4964 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004965 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4966 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004967 *
4968 * @param willNotDraw whether or not this View draw on its own
4969 */
4970 public void setWillNotDraw(boolean willNotDraw) {
4971 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4972 }
4973
4974 /**
4975 * Returns whether or not this View draws on its own.
4976 *
4977 * @return true if this view has nothing to draw, false otherwise
4978 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004979 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 public boolean willNotDraw() {
4981 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4982 }
4983
4984 /**
4985 * When a View's drawing cache is enabled, drawing is redirected to an
4986 * offscreen bitmap. Some views, like an ImageView, must be able to
4987 * bypass this mechanism if they already draw a single bitmap, to avoid
4988 * unnecessary usage of the memory.
4989 *
4990 * @param willNotCacheDrawing true if this view does not cache its
4991 * drawing, false otherwise
4992 */
4993 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4994 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4995 }
4996
4997 /**
4998 * Returns whether or not this View can cache its drawing or not.
4999 *
5000 * @return true if this view does not cache its drawing, false otherwise
5001 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005002 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005003 public boolean willNotCacheDrawing() {
5004 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5005 }
5006
5007 /**
5008 * Indicates whether this view reacts to click events or not.
5009 *
5010 * @return true if the view is clickable, false otherwise
5011 *
5012 * @see #setClickable(boolean)
5013 * @attr ref android.R.styleable#View_clickable
5014 */
5015 @ViewDebug.ExportedProperty
5016 public boolean isClickable() {
5017 return (mViewFlags & CLICKABLE) == CLICKABLE;
5018 }
5019
5020 /**
5021 * Enables or disables click events for this view. When a view
5022 * is clickable it will change its state to "pressed" on every click.
5023 * Subclasses should set the view clickable to visually react to
5024 * user's clicks.
5025 *
5026 * @param clickable true to make the view clickable, false otherwise
5027 *
5028 * @see #isClickable()
5029 * @attr ref android.R.styleable#View_clickable
5030 */
5031 public void setClickable(boolean clickable) {
5032 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5033 }
5034
5035 /**
5036 * Indicates whether this view reacts to long click events or not.
5037 *
5038 * @return true if the view is long clickable, false otherwise
5039 *
5040 * @see #setLongClickable(boolean)
5041 * @attr ref android.R.styleable#View_longClickable
5042 */
5043 public boolean isLongClickable() {
5044 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5045 }
5046
5047 /**
5048 * Enables or disables long click events for this view. When a view is long
5049 * clickable it reacts to the user holding down the button for a longer
5050 * duration than a tap. This event can either launch the listener or a
5051 * context menu.
5052 *
5053 * @param longClickable true to make the view long clickable, false otherwise
5054 * @see #isLongClickable()
5055 * @attr ref android.R.styleable#View_longClickable
5056 */
5057 public void setLongClickable(boolean longClickable) {
5058 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5059 }
5060
5061 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005062 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 *
5064 * @see #isClickable()
5065 * @see #setClickable(boolean)
5066 *
5067 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5068 * the View's internal state from a previously set "pressed" state.
5069 */
5070 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005071 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 if (pressed) {
5074 mPrivateFlags |= PRESSED;
5075 } else {
5076 mPrivateFlags &= ~PRESSED;
5077 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005078
5079 if (needsRefresh) {
5080 refreshDrawableState();
5081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 dispatchSetPressed(pressed);
5083 }
5084
5085 /**
5086 * Dispatch setPressed to all of this View's children.
5087 *
5088 * @see #setPressed(boolean)
5089 *
5090 * @param pressed The new pressed state
5091 */
5092 protected void dispatchSetPressed(boolean pressed) {
5093 }
5094
5095 /**
5096 * Indicates whether the view is currently in pressed state. Unless
5097 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5098 * the pressed state.
5099 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005100 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101 * @see #isClickable()
5102 * @see #setClickable(boolean)
5103 *
5104 * @return true if the view is currently pressed, false otherwise
5105 */
5106 public boolean isPressed() {
5107 return (mPrivateFlags & PRESSED) == PRESSED;
5108 }
5109
5110 /**
5111 * Indicates whether this view will save its state (that is,
5112 * whether its {@link #onSaveInstanceState} method will be called).
5113 *
5114 * @return Returns true if the view state saving is enabled, else false.
5115 *
5116 * @see #setSaveEnabled(boolean)
5117 * @attr ref android.R.styleable#View_saveEnabled
5118 */
5119 public boolean isSaveEnabled() {
5120 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5121 }
5122
5123 /**
5124 * Controls whether the saving of this view's state is
5125 * enabled (that is, whether its {@link #onSaveInstanceState} method
5126 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005127 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 * for its state to be saved. This flag can only disable the
5129 * saving of this view; any child views may still have their state saved.
5130 *
5131 * @param enabled Set to false to <em>disable</em> state saving, or true
5132 * (the default) to allow it.
5133 *
5134 * @see #isSaveEnabled()
5135 * @see #setId(int)
5136 * @see #onSaveInstanceState()
5137 * @attr ref android.R.styleable#View_saveEnabled
5138 */
5139 public void setSaveEnabled(boolean enabled) {
5140 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5141 }
5142
Jeff Brown85a31762010-09-01 17:01:00 -07005143 /**
5144 * Gets whether the framework should discard touches when the view's
5145 * window is obscured by another visible window.
5146 * Refer to the {@link View} security documentation for more details.
5147 *
5148 * @return True if touch filtering is enabled.
5149 *
5150 * @see #setFilterTouchesWhenObscured(boolean)
5151 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5152 */
5153 @ViewDebug.ExportedProperty
5154 public boolean getFilterTouchesWhenObscured() {
5155 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5156 }
5157
5158 /**
5159 * Sets whether the framework should discard touches when the view's
5160 * window is obscured by another visible window.
5161 * Refer to the {@link View} security documentation for more details.
5162 *
5163 * @param enabled True if touch filtering should be enabled.
5164 *
5165 * @see #getFilterTouchesWhenObscured
5166 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5167 */
5168 public void setFilterTouchesWhenObscured(boolean enabled) {
5169 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5170 FILTER_TOUCHES_WHEN_OBSCURED);
5171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172
5173 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005174 * Indicates whether the entire hierarchy under this view will save its
5175 * state when a state saving traversal occurs from its parent. The default
5176 * is true; if false, these views will not be saved unless
5177 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5178 *
5179 * @return Returns true if the view state saving from parent is enabled, else false.
5180 *
5181 * @see #setSaveFromParentEnabled(boolean)
5182 */
5183 public boolean isSaveFromParentEnabled() {
5184 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5185 }
5186
5187 /**
5188 * Controls whether the entire hierarchy under this view will save its
5189 * state when a state saving traversal occurs from its parent. The default
5190 * is true; if false, these views will not be saved unless
5191 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5192 *
5193 * @param enabled Set to false to <em>disable</em> state saving, or true
5194 * (the default) to allow it.
5195 *
5196 * @see #isSaveFromParentEnabled()
5197 * @see #setId(int)
5198 * @see #onSaveInstanceState()
5199 */
5200 public void setSaveFromParentEnabled(boolean enabled) {
5201 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5202 }
5203
5204
5205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 * Returns whether this View is able to take focus.
5207 *
5208 * @return True if this view can take focus, or false otherwise.
5209 * @attr ref android.R.styleable#View_focusable
5210 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005211 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 public final boolean isFocusable() {
5213 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5214 }
5215
5216 /**
5217 * When a view is focusable, it may not want to take focus when in touch mode.
5218 * For example, a button would like focus when the user is navigating via a D-pad
5219 * so that the user can click on it, but once the user starts touching the screen,
5220 * the button shouldn't take focus
5221 * @return Whether the view is focusable in touch mode.
5222 * @attr ref android.R.styleable#View_focusableInTouchMode
5223 */
5224 @ViewDebug.ExportedProperty
5225 public final boolean isFocusableInTouchMode() {
5226 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5227 }
5228
5229 /**
5230 * Find the nearest view in the specified direction that can take focus.
5231 * This does not actually give focus to that view.
5232 *
5233 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5234 *
5235 * @return The nearest focusable in the specified direction, or null if none
5236 * can be found.
5237 */
5238 public View focusSearch(int direction) {
5239 if (mParent != null) {
5240 return mParent.focusSearch(this, direction);
5241 } else {
5242 return null;
5243 }
5244 }
5245
5246 /**
5247 * This method is the last chance for the focused view and its ancestors to
5248 * respond to an arrow key. This is called when the focused view did not
5249 * consume the key internally, nor could the view system find a new view in
5250 * the requested direction to give focus to.
5251 *
5252 * @param focused The currently focused view.
5253 * @param direction The direction focus wants to move. One of FOCUS_UP,
5254 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5255 * @return True if the this view consumed this unhandled move.
5256 */
5257 public boolean dispatchUnhandledMove(View focused, int direction) {
5258 return false;
5259 }
5260
5261 /**
5262 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005263 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005265 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5266 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 * @return The user specified next view, or null if there is none.
5268 */
5269 View findUserSetNextFocus(View root, int direction) {
5270 switch (direction) {
5271 case FOCUS_LEFT:
5272 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005273 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 case FOCUS_RIGHT:
5275 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005276 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 case FOCUS_UP:
5278 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005279 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 case FOCUS_DOWN:
5281 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005282 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005283 case FOCUS_FORWARD:
5284 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005285 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005286 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005287 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005288 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005289 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005290 @Override
5291 public boolean apply(View t) {
5292 return t.mNextFocusForwardId == id;
5293 }
5294 });
5295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 }
5297 return null;
5298 }
5299
Jeff Brown4dfbec22011-08-15 14:55:37 -07005300 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5301 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5302 @Override
5303 public boolean apply(View t) {
5304 return t.mID == childViewId;
5305 }
5306 });
5307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 if (result == null) {
5309 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5310 + "by user for id " + childViewId);
5311 }
5312 return result;
5313 }
5314
5315 /**
5316 * Find and return all focusable views that are descendants of this view,
5317 * possibly including this view if it is focusable itself.
5318 *
5319 * @param direction The direction of the focus
5320 * @return A list of focusable views
5321 */
5322 public ArrayList<View> getFocusables(int direction) {
5323 ArrayList<View> result = new ArrayList<View>(24);
5324 addFocusables(result, direction);
5325 return result;
5326 }
5327
5328 /**
5329 * Add any focusable views that are descendants of this view (possibly
5330 * including this view if it is focusable itself) to views. If we are in touch mode,
5331 * only add views that are also focusable in touch mode.
5332 *
5333 * @param views Focusable views found so far
5334 * @param direction The direction of the focus
5335 */
5336 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005337 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339
svetoslavganov75986cf2009-05-14 22:28:01 -07005340 /**
5341 * Adds any focusable views that are descendants of this view (possibly
5342 * including this view if it is focusable itself) to views. This method
5343 * adds all focusable views regardless if we are in touch mode or
5344 * only views focusable in touch mode if we are in touch mode depending on
5345 * the focusable mode paramater.
5346 *
5347 * @param views Focusable views found so far or null if all we are interested is
5348 * the number of focusables.
5349 * @param direction The direction of the focus.
5350 * @param focusableMode The type of focusables to be added.
5351 *
5352 * @see #FOCUSABLES_ALL
5353 * @see #FOCUSABLES_TOUCH_MODE
5354 */
5355 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5356 if (!isFocusable()) {
5357 return;
5358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359
svetoslavganov75986cf2009-05-14 22:28:01 -07005360 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5361 isInTouchMode() && !isFocusableInTouchMode()) {
5362 return;
5363 }
5364
5365 if (views != null) {
5366 views.add(this);
5367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 }
5369
5370 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005371 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005372 * The search is performed by either the text that the View renders or the content
5373 * description that describes the view for accessibility purposes and the view does
5374 * not render or both. Clients can specify how the search is to be performed via
5375 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5376 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005377 *
5378 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005379 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005380 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005381 * @see #FIND_VIEWS_WITH_TEXT
5382 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5383 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005384 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005385 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005386 if (getAccessibilityNodeProvider() != null) {
5387 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5388 outViews.add(this);
5389 }
5390 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5391 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005392 String searchedLowerCase = searched.toString().toLowerCase();
5393 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5394 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5395 outViews.add(this);
5396 }
5397 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005398 }
5399
5400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 * Find and return all touchable views that are descendants of this view,
5402 * possibly including this view if it is touchable itself.
5403 *
5404 * @return A list of touchable views
5405 */
5406 public ArrayList<View> getTouchables() {
5407 ArrayList<View> result = new ArrayList<View>();
5408 addTouchables(result);
5409 return result;
5410 }
5411
5412 /**
5413 * Add any touchable views that are descendants of this view (possibly
5414 * including this view if it is touchable itself) to views.
5415 *
5416 * @param views Touchable views found so far
5417 */
5418 public void addTouchables(ArrayList<View> views) {
5419 final int viewFlags = mViewFlags;
5420
5421 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5422 && (viewFlags & ENABLED_MASK) == ENABLED) {
5423 views.add(this);
5424 }
5425 }
5426
5427 /**
5428 * Call this to try to give focus to a specific view or to one of its
5429 * descendants.
5430 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005431 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5432 * false), or if it is focusable and it is not focusable in touch mode
5433 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005435 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005436 * have focus, and you want your parent to look for the next one.
5437 *
5438 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5439 * {@link #FOCUS_DOWN} and <code>null</code>.
5440 *
5441 * @return Whether this view or one of its descendants actually took focus.
5442 */
5443 public final boolean requestFocus() {
5444 return requestFocus(View.FOCUS_DOWN);
5445 }
5446
5447
5448 /**
5449 * Call this to try to give focus to a specific view or to one of its
5450 * descendants and give it a hint about what direction focus is heading.
5451 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005452 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5453 * false), or if it is focusable and it is not focusable in touch mode
5454 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005455 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005456 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457 * have focus, and you want your parent to look for the next one.
5458 *
5459 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5460 * <code>null</code> set for the previously focused rectangle.
5461 *
5462 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5463 * @return Whether this view or one of its descendants actually took focus.
5464 */
5465 public final boolean requestFocus(int direction) {
5466 return requestFocus(direction, null);
5467 }
5468
5469 /**
5470 * Call this to try to give focus to a specific view or to one of its descendants
5471 * and give it hints about the direction and a specific rectangle that the focus
5472 * is coming from. The rectangle can help give larger views a finer grained hint
5473 * about where focus is coming from, and therefore, where to show selection, or
5474 * forward focus change internally.
5475 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005476 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5477 * false), or if it is focusable and it is not focusable in touch mode
5478 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 *
5480 * A View will not take focus if it is not visible.
5481 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005482 * A View will not take focus if one of its parents has
5483 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5484 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005486 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 * have focus, and you want your parent to look for the next one.
5488 *
5489 * You may wish to override this method if your custom {@link View} has an internal
5490 * {@link View} that it wishes to forward the request to.
5491 *
5492 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5493 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5494 * to give a finer grained hint about where focus is coming from. May be null
5495 * if there is no hint.
5496 * @return Whether this view or one of its descendants actually took focus.
5497 */
5498 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5499 // need to be focusable
5500 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5501 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5502 return false;
5503 }
5504
5505 // need to be focusable in touch mode if in touch mode
5506 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005507 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5508 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 }
5510
5511 // need to not have any parents blocking us
5512 if (hasAncestorThatBlocksDescendantFocus()) {
5513 return false;
5514 }
5515
5516 handleFocusGainInternal(direction, previouslyFocusedRect);
5517 return true;
5518 }
5519
5520 /**
5521 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5522 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5523 * touch mode to request focus when they are touched.
5524 *
5525 * @return Whether this view or one of its descendants actually took focus.
5526 *
5527 * @see #isInTouchMode()
5528 *
5529 */
5530 public final boolean requestFocusFromTouch() {
5531 // Leave touch mode if we need to
5532 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005533 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005534 if (viewRoot != null) {
5535 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 }
5537 }
5538 return requestFocus(View.FOCUS_DOWN);
5539 }
5540
5541 /**
5542 * @return Whether any ancestor of this view blocks descendant focus.
5543 */
5544 private boolean hasAncestorThatBlocksDescendantFocus() {
5545 ViewParent ancestor = mParent;
5546 while (ancestor instanceof ViewGroup) {
5547 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5548 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5549 return true;
5550 } else {
5551 ancestor = vgAncestor.getParent();
5552 }
5553 }
5554 return false;
5555 }
5556
5557 /**
Romain Guya440b002010-02-24 15:57:54 -08005558 * @hide
5559 */
5560 public void dispatchStartTemporaryDetach() {
5561 onStartTemporaryDetach();
5562 }
5563
5564 /**
5565 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5567 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005568 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 */
5570 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005571 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005572 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005573 }
5574
5575 /**
5576 * @hide
5577 */
5578 public void dispatchFinishTemporaryDetach() {
5579 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 }
Romain Guy8506ab42009-06-11 17:35:47 -07005581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 /**
5583 * Called after {@link #onStartTemporaryDetach} when the container is done
5584 * changing the view.
5585 */
5586 public void onFinishTemporaryDetach() {
5587 }
Romain Guy8506ab42009-06-11 17:35:47 -07005588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005590 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5591 * for this view's window. Returns null if the view is not currently attached
5592 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005593 * just use the standard high-level event callbacks like
5594 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005595 */
5596 public KeyEvent.DispatcherState getKeyDispatcherState() {
5597 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5598 }
Joe Malin32736f02011-01-19 16:14:20 -08005599
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005601 * Dispatch a key event before it is processed by any input method
5602 * associated with the view hierarchy. This can be used to intercept
5603 * key events in special situations before the IME consumes them; a
5604 * typical example would be handling the BACK key to update the application's
5605 * UI instead of allowing the IME to see it and close itself.
5606 *
5607 * @param event The key event to be dispatched.
5608 * @return True if the event was handled, false otherwise.
5609 */
5610 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5611 return onKeyPreIme(event.getKeyCode(), event);
5612 }
5613
5614 /**
5615 * Dispatch a key event to the next view on the focus path. This path runs
5616 * from the top of the view tree down to the currently focused view. If this
5617 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5618 * the next node down the focus path. This method also fires any key
5619 * listeners.
5620 *
5621 * @param event The key event to be dispatched.
5622 * @return True if the event was handled, false otherwise.
5623 */
5624 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005625 if (mInputEventConsistencyVerifier != null) {
5626 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005628
Jeff Brown21bc5c92011-02-28 18:27:14 -08005629 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005630 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005631 ListenerInfo li = mListenerInfo;
5632 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5633 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 return true;
5635 }
5636
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005637 if (event.dispatch(this, mAttachInfo != null
5638 ? mAttachInfo.mKeyDispatchState : null, this)) {
5639 return true;
5640 }
5641
5642 if (mInputEventConsistencyVerifier != null) {
5643 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5644 }
5645 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 }
5647
5648 /**
5649 * Dispatches a key shortcut event.
5650 *
5651 * @param event The key event to be dispatched.
5652 * @return True if the event was handled by the view, false otherwise.
5653 */
5654 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5655 return onKeyShortcut(event.getKeyCode(), event);
5656 }
5657
5658 /**
5659 * Pass the touch screen motion event down to the target view, or this
5660 * view if it is the target.
5661 *
5662 * @param event The motion event to be dispatched.
5663 * @return True if the event was handled by the view, false otherwise.
5664 */
5665 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005666 if (mInputEventConsistencyVerifier != null) {
5667 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5668 }
5669
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005670 if (onFilterTouchEventForSecurity(event)) {
5671 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005672 ListenerInfo li = mListenerInfo;
5673 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5674 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005675 return true;
5676 }
5677
5678 if (onTouchEvent(event)) {
5679 return true;
5680 }
Jeff Brown85a31762010-09-01 17:01:00 -07005681 }
5682
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005683 if (mInputEventConsistencyVerifier != null) {
5684 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005686 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 }
5688
5689 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005690 * Filter the touch event to apply security policies.
5691 *
5692 * @param event The motion event to be filtered.
5693 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005694 *
Jeff Brown85a31762010-09-01 17:01:00 -07005695 * @see #getFilterTouchesWhenObscured
5696 */
5697 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005698 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005699 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5700 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5701 // Window is obscured, drop this touch.
5702 return false;
5703 }
5704 return true;
5705 }
5706
5707 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 * Pass a trackball motion event down to the focused view.
5709 *
5710 * @param event The motion event to be dispatched.
5711 * @return True if the event was handled by the view, false otherwise.
5712 */
5713 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005714 if (mInputEventConsistencyVerifier != null) {
5715 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5716 }
5717
Romain Guy02ccac62011-06-24 13:20:23 -07005718 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719 }
5720
5721 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005722 * Dispatch a generic motion event.
5723 * <p>
5724 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5725 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005726 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005727 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005728 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005729 *
5730 * @param event The motion event to be dispatched.
5731 * @return True if the event was handled by the view, false otherwise.
5732 */
5733 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005734 if (mInputEventConsistencyVerifier != null) {
5735 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5736 }
5737
Jeff Browna032cc02011-03-07 16:56:21 -08005738 final int source = event.getSource();
5739 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5740 final int action = event.getAction();
5741 if (action == MotionEvent.ACTION_HOVER_ENTER
5742 || action == MotionEvent.ACTION_HOVER_MOVE
5743 || action == MotionEvent.ACTION_HOVER_EXIT) {
5744 if (dispatchHoverEvent(event)) {
5745 return true;
5746 }
5747 } else if (dispatchGenericPointerEvent(event)) {
5748 return true;
5749 }
5750 } else if (dispatchGenericFocusedEvent(event)) {
5751 return true;
5752 }
5753
Jeff Brown10b62902011-06-20 16:40:37 -07005754 if (dispatchGenericMotionEventInternal(event)) {
5755 return true;
5756 }
5757
5758 if (mInputEventConsistencyVerifier != null) {
5759 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5760 }
5761 return false;
5762 }
5763
5764 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005765 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005766 ListenerInfo li = mListenerInfo;
5767 if (li != null && li.mOnGenericMotionListener != null
5768 && (mViewFlags & ENABLED_MASK) == ENABLED
5769 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005770 return true;
5771 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005772
5773 if (onGenericMotionEvent(event)) {
5774 return true;
5775 }
5776
5777 if (mInputEventConsistencyVerifier != null) {
5778 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5779 }
5780 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005781 }
5782
5783 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005784 * Dispatch a hover event.
5785 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005786 * Do not call this method directly.
5787 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005788 * </p>
5789 *
5790 * @param event The motion event to be dispatched.
5791 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005792 */
5793 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005794 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005795 ListenerInfo li = mListenerInfo;
5796 if (li != null && li.mOnHoverListener != null
5797 && (mViewFlags & ENABLED_MASK) == ENABLED
5798 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005799 return true;
5800 }
5801
Jeff Browna032cc02011-03-07 16:56:21 -08005802 return onHoverEvent(event);
5803 }
5804
5805 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005806 * Returns true if the view has a child to which it has recently sent
5807 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5808 * it does not have a hovered child, then it must be the innermost hovered view.
5809 * @hide
5810 */
5811 protected boolean hasHoveredChild() {
5812 return false;
5813 }
5814
5815 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005816 * Dispatch a generic motion event to the view under the first pointer.
5817 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005818 * Do not call this method directly.
5819 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005820 * </p>
5821 *
5822 * @param event The motion event to be dispatched.
5823 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005824 */
5825 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5826 return false;
5827 }
5828
5829 /**
5830 * Dispatch a generic motion event to the currently focused view.
5831 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005832 * Do not call this method directly.
5833 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005834 * </p>
5835 *
5836 * @param event The motion event to be dispatched.
5837 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005838 */
5839 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5840 return false;
5841 }
5842
5843 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005844 * Dispatch a pointer event.
5845 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005846 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5847 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5848 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005849 * and should not be expected to handle other pointing device features.
5850 * </p>
5851 *
5852 * @param event The motion event to be dispatched.
5853 * @return True if the event was handled by the view, false otherwise.
5854 * @hide
5855 */
5856 public final boolean dispatchPointerEvent(MotionEvent event) {
5857 if (event.isTouchEvent()) {
5858 return dispatchTouchEvent(event);
5859 } else {
5860 return dispatchGenericMotionEvent(event);
5861 }
5862 }
5863
5864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 * Called when the window containing this view gains or loses window focus.
5866 * ViewGroups should override to route to their children.
5867 *
5868 * @param hasFocus True if the window containing this view now has focus,
5869 * false otherwise.
5870 */
5871 public void dispatchWindowFocusChanged(boolean hasFocus) {
5872 onWindowFocusChanged(hasFocus);
5873 }
5874
5875 /**
5876 * Called when the window containing this view gains or loses focus. Note
5877 * that this is separate from view focus: to receive key events, both
5878 * your view and its window must have focus. If a window is displayed
5879 * on top of yours that takes input focus, then your own window will lose
5880 * focus but the view focus will remain unchanged.
5881 *
5882 * @param hasWindowFocus True if the window containing this view now has
5883 * focus, false otherwise.
5884 */
5885 public void onWindowFocusChanged(boolean hasWindowFocus) {
5886 InputMethodManager imm = InputMethodManager.peekInstance();
5887 if (!hasWindowFocus) {
5888 if (isPressed()) {
5889 setPressed(false);
5890 }
5891 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5892 imm.focusOut(this);
5893 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005894 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005895 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005896 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5898 imm.focusIn(this);
5899 }
5900 refreshDrawableState();
5901 }
5902
5903 /**
5904 * Returns true if this view is in a window that currently has window focus.
5905 * Note that this is not the same as the view itself having focus.
5906 *
5907 * @return True if this view is in a window that currently has window focus.
5908 */
5909 public boolean hasWindowFocus() {
5910 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5911 }
5912
5913 /**
Adam Powell326d8082009-12-09 15:10:07 -08005914 * Dispatch a view visibility change down the view hierarchy.
5915 * ViewGroups should override to route to their children.
5916 * @param changedView The view whose visibility changed. Could be 'this' or
5917 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005918 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5919 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005920 */
5921 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5922 onVisibilityChanged(changedView, visibility);
5923 }
5924
5925 /**
5926 * Called when the visibility of the view or an ancestor of the view is changed.
5927 * @param changedView The view whose visibility changed. Could be 'this' or
5928 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005929 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5930 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005931 */
5932 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005933 if (visibility == VISIBLE) {
5934 if (mAttachInfo != null) {
5935 initialAwakenScrollBars();
5936 } else {
5937 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5938 }
5939 }
Adam Powell326d8082009-12-09 15:10:07 -08005940 }
5941
5942 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005943 * Dispatch a hint about whether this view is displayed. For instance, when
5944 * a View moves out of the screen, it might receives a display hint indicating
5945 * the view is not displayed. Applications should not <em>rely</em> on this hint
5946 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005947 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005948 * @param hint A hint about whether or not this view is displayed:
5949 * {@link #VISIBLE} or {@link #INVISIBLE}.
5950 */
5951 public void dispatchDisplayHint(int hint) {
5952 onDisplayHint(hint);
5953 }
5954
5955 /**
5956 * Gives this view a hint about whether is displayed or not. For instance, when
5957 * a View moves out of the screen, it might receives a display hint indicating
5958 * the view is not displayed. Applications should not <em>rely</em> on this hint
5959 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005960 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005961 * @param hint A hint about whether or not this view is displayed:
5962 * {@link #VISIBLE} or {@link #INVISIBLE}.
5963 */
5964 protected void onDisplayHint(int hint) {
5965 }
5966
5967 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 * Dispatch a window visibility change down the view hierarchy.
5969 * ViewGroups should override to route to their children.
5970 *
5971 * @param visibility The new visibility of the window.
5972 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005973 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 */
5975 public void dispatchWindowVisibilityChanged(int visibility) {
5976 onWindowVisibilityChanged(visibility);
5977 }
5978
5979 /**
5980 * Called when the window containing has change its visibility
5981 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5982 * that this tells you whether or not your window is being made visible
5983 * to the window manager; this does <em>not</em> tell you whether or not
5984 * your window is obscured by other windows on the screen, even if it
5985 * is itself visible.
5986 *
5987 * @param visibility The new visibility of the window.
5988 */
5989 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005990 if (visibility == VISIBLE) {
5991 initialAwakenScrollBars();
5992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 }
5994
5995 /**
5996 * Returns the current visibility of the window this view is attached to
5997 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5998 *
5999 * @return Returns the current visibility of the view's window.
6000 */
6001 public int getWindowVisibility() {
6002 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6003 }
6004
6005 /**
6006 * Retrieve the overall visible display size in which the window this view is
6007 * attached to has been positioned in. This takes into account screen
6008 * decorations above the window, for both cases where the window itself
6009 * is being position inside of them or the window is being placed under
6010 * then and covered insets are used for the window to position its content
6011 * inside. In effect, this tells you the available area where content can
6012 * be placed and remain visible to users.
6013 *
6014 * <p>This function requires an IPC back to the window manager to retrieve
6015 * the requested information, so should not be used in performance critical
6016 * code like drawing.
6017 *
6018 * @param outRect Filled in with the visible display frame. If the view
6019 * is not attached to a window, this is simply the raw display size.
6020 */
6021 public void getWindowVisibleDisplayFrame(Rect outRect) {
6022 if (mAttachInfo != null) {
6023 try {
6024 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6025 } catch (RemoteException e) {
6026 return;
6027 }
6028 // XXX This is really broken, and probably all needs to be done
6029 // in the window manager, and we need to know more about whether
6030 // we want the area behind or in front of the IME.
6031 final Rect insets = mAttachInfo.mVisibleInsets;
6032 outRect.left += insets.left;
6033 outRect.top += insets.top;
6034 outRect.right -= insets.right;
6035 outRect.bottom -= insets.bottom;
6036 return;
6037 }
6038 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006039 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006040 }
6041
6042 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006043 * Dispatch a notification about a resource configuration change down
6044 * the view hierarchy.
6045 * ViewGroups should override to route to their children.
6046 *
6047 * @param newConfig The new resource configuration.
6048 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006049 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006050 */
6051 public void dispatchConfigurationChanged(Configuration newConfig) {
6052 onConfigurationChanged(newConfig);
6053 }
6054
6055 /**
6056 * Called when the current configuration of the resources being used
6057 * by the application have changed. You can use this to decide when
6058 * to reload resources that can changed based on orientation and other
6059 * configuration characterstics. You only need to use this if you are
6060 * not relying on the normal {@link android.app.Activity} mechanism of
6061 * recreating the activity instance upon a configuration change.
6062 *
6063 * @param newConfig The new resource configuration.
6064 */
6065 protected void onConfigurationChanged(Configuration newConfig) {
6066 }
6067
6068 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 * Private function to aggregate all per-view attributes in to the view
6070 * root.
6071 */
6072 void dispatchCollectViewAttributes(int visibility) {
6073 performCollectViewAttributes(visibility);
6074 }
6075
6076 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006077 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006078 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6079 mAttachInfo.mKeepScreenOn = true;
6080 }
6081 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006082 ListenerInfo li = mListenerInfo;
6083 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006084 mAttachInfo.mHasSystemUiListeners = true;
6085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 }
6087 }
6088
6089 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006090 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006092 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6093 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 ai.mRecomputeGlobalAttributes = true;
6095 }
6096 }
6097 }
6098
6099 /**
6100 * Returns whether the device is currently in touch mode. Touch mode is entered
6101 * once the user begins interacting with the device by touch, and affects various
6102 * things like whether focus is always visible to the user.
6103 *
6104 * @return Whether the device is in touch mode.
6105 */
6106 @ViewDebug.ExportedProperty
6107 public boolean isInTouchMode() {
6108 if (mAttachInfo != null) {
6109 return mAttachInfo.mInTouchMode;
6110 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006111 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 }
6113 }
6114
6115 /**
6116 * Returns the context the view is running in, through which it can
6117 * access the current theme, resources, etc.
6118 *
6119 * @return The view's Context.
6120 */
6121 @ViewDebug.CapturedViewProperty
6122 public final Context getContext() {
6123 return mContext;
6124 }
6125
6126 /**
6127 * Handle a key event before it is processed by any input method
6128 * associated with the view hierarchy. This can be used to intercept
6129 * key events in special situations before the IME consumes them; a
6130 * typical example would be handling the BACK key to update the application's
6131 * UI instead of allowing the IME to see it and close itself.
6132 *
6133 * @param keyCode The value in event.getKeyCode().
6134 * @param event Description of the key event.
6135 * @return If you handled the event, return true. If you want to allow the
6136 * event to be handled by the next receiver, return false.
6137 */
6138 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6139 return false;
6140 }
6141
6142 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006143 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6144 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6146 * is released, if the view is enabled and clickable.
6147 *
6148 * @param keyCode A key code that represents the button pressed, from
6149 * {@link android.view.KeyEvent}.
6150 * @param event The KeyEvent object that defines the button action.
6151 */
6152 public boolean onKeyDown(int keyCode, KeyEvent event) {
6153 boolean result = false;
6154
6155 switch (keyCode) {
6156 case KeyEvent.KEYCODE_DPAD_CENTER:
6157 case KeyEvent.KEYCODE_ENTER: {
6158 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6159 return true;
6160 }
6161 // Long clickable items don't necessarily have to be clickable
6162 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6163 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6164 (event.getRepeatCount() == 0)) {
6165 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006166 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 return true;
6168 }
6169 break;
6170 }
6171 }
6172 return result;
6173 }
6174
6175 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006176 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6177 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6178 * the event).
6179 */
6180 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6181 return false;
6182 }
6183
6184 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006185 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6186 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6188 * {@link KeyEvent#KEYCODE_ENTER} is released.
6189 *
6190 * @param keyCode A key code that represents the button pressed, from
6191 * {@link android.view.KeyEvent}.
6192 * @param event The KeyEvent object that defines the button action.
6193 */
6194 public boolean onKeyUp(int keyCode, KeyEvent event) {
6195 boolean result = false;
6196
6197 switch (keyCode) {
6198 case KeyEvent.KEYCODE_DPAD_CENTER:
6199 case KeyEvent.KEYCODE_ENTER: {
6200 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6201 return true;
6202 }
6203 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6204 setPressed(false);
6205
6206 if (!mHasPerformedLongPress) {
6207 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006208 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209
6210 result = performClick();
6211 }
6212 }
6213 break;
6214 }
6215 }
6216 return result;
6217 }
6218
6219 /**
6220 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6221 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6222 * the event).
6223 *
6224 * @param keyCode A key code that represents the button pressed, from
6225 * {@link android.view.KeyEvent}.
6226 * @param repeatCount The number of times the action was made.
6227 * @param event The KeyEvent object that defines the button action.
6228 */
6229 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6230 return false;
6231 }
6232
6233 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006234 * Called on the focused view when a key shortcut event is not handled.
6235 * Override this method to implement local key shortcuts for the View.
6236 * Key shortcuts can also be implemented by setting the
6237 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 *
6239 * @param keyCode The value in event.getKeyCode().
6240 * @param event Description of the key event.
6241 * @return If you handled the event, return true. If you want to allow the
6242 * event to be handled by the next receiver, return false.
6243 */
6244 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6245 return false;
6246 }
6247
6248 /**
6249 * Check whether the called view is a text editor, in which case it
6250 * would make sense to automatically display a soft input window for
6251 * it. Subclasses should override this if they implement
6252 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006253 * a call on that method would return a non-null InputConnection, and
6254 * they are really a first-class editor that the user would normally
6255 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006256 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006257 * <p>The default implementation always returns false. This does
6258 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6259 * will not be called or the user can not otherwise perform edits on your
6260 * view; it is just a hint to the system that this is not the primary
6261 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006262 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006263 * @return Returns true if this view is a text editor, else false.
6264 */
6265 public boolean onCheckIsTextEditor() {
6266 return false;
6267 }
Romain Guy8506ab42009-06-11 17:35:47 -07006268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 /**
6270 * Create a new InputConnection for an InputMethod to interact
6271 * with the view. The default implementation returns null, since it doesn't
6272 * support input methods. You can override this to implement such support.
6273 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006275 * <p>When implementing this, you probably also want to implement
6276 * {@link #onCheckIsTextEditor()} to indicate you will return a
6277 * non-null InputConnection.
6278 *
6279 * @param outAttrs Fill in with attribute information about the connection.
6280 */
6281 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6282 return null;
6283 }
6284
6285 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006286 * Called by the {@link android.view.inputmethod.InputMethodManager}
6287 * when a view who is not the current
6288 * input connection target is trying to make a call on the manager. The
6289 * default implementation returns false; you can override this to return
6290 * true for certain views if you are performing InputConnection proxying
6291 * to them.
6292 * @param view The View that is making the InputMethodManager call.
6293 * @return Return true to allow the call, false to reject.
6294 */
6295 public boolean checkInputConnectionProxy(View view) {
6296 return false;
6297 }
Romain Guy8506ab42009-06-11 17:35:47 -07006298
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006300 * Show the context menu for this view. It is not safe to hold on to the
6301 * menu after returning from this method.
6302 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006303 * You should normally not overload this method. Overload
6304 * {@link #onCreateContextMenu(ContextMenu)} or define an
6305 * {@link OnCreateContextMenuListener} to add items to the context menu.
6306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006307 * @param menu The context menu to populate
6308 */
6309 public void createContextMenu(ContextMenu menu) {
6310 ContextMenuInfo menuInfo = getContextMenuInfo();
6311
6312 // Sets the current menu info so all items added to menu will have
6313 // my extra info set.
6314 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6315
6316 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006317 ListenerInfo li = mListenerInfo;
6318 if (li != null && li.mOnCreateContextMenuListener != null) {
6319 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320 }
6321
6322 // Clear the extra information so subsequent items that aren't mine don't
6323 // have my extra info.
6324 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6325
6326 if (mParent != null) {
6327 mParent.createContextMenu(menu);
6328 }
6329 }
6330
6331 /**
6332 * Views should implement this if they have extra information to associate
6333 * with the context menu. The return result is supplied as a parameter to
6334 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6335 * callback.
6336 *
6337 * @return Extra information about the item for which the context menu
6338 * should be shown. This information will vary across different
6339 * subclasses of View.
6340 */
6341 protected ContextMenuInfo getContextMenuInfo() {
6342 return null;
6343 }
6344
6345 /**
6346 * Views should implement this if the view itself is going to add items to
6347 * the context menu.
6348 *
6349 * @param menu the context menu to populate
6350 */
6351 protected void onCreateContextMenu(ContextMenu menu) {
6352 }
6353
6354 /**
6355 * Implement this method to handle trackball motion events. The
6356 * <em>relative</em> movement of the trackball since the last event
6357 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6358 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6359 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6360 * they will often be fractional values, representing the more fine-grained
6361 * movement information available from a trackball).
6362 *
6363 * @param event The motion event.
6364 * @return True if the event was handled, false otherwise.
6365 */
6366 public boolean onTrackballEvent(MotionEvent event) {
6367 return false;
6368 }
6369
6370 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006371 * Implement this method to handle generic motion events.
6372 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006373 * Generic motion events describe joystick movements, mouse hovers, track pad
6374 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006375 * {@link MotionEvent#getSource() source} of the motion event specifies
6376 * the class of input that was received. Implementations of this method
6377 * must examine the bits in the source before processing the event.
6378 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006379 * </p><p>
6380 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6381 * are delivered to the view under the pointer. All other generic motion events are
6382 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006383 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006384 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006385 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006386 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6387 * // process the joystick movement...
6388 * return true;
6389 * }
6390 * }
6391 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6392 * switch (event.getAction()) {
6393 * case MotionEvent.ACTION_HOVER_MOVE:
6394 * // process the mouse hover movement...
6395 * return true;
6396 * case MotionEvent.ACTION_SCROLL:
6397 * // process the scroll wheel movement...
6398 * return true;
6399 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006400 * }
6401 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006402 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006403 *
6404 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006405 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006406 */
6407 public boolean onGenericMotionEvent(MotionEvent event) {
6408 return false;
6409 }
6410
6411 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006412 * Implement this method to handle hover events.
6413 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006414 * This method is called whenever a pointer is hovering into, over, or out of the
6415 * bounds of a view and the view is not currently being touched.
6416 * Hover events are represented as pointer events with action
6417 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6418 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6419 * </p>
6420 * <ul>
6421 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6422 * when the pointer enters the bounds of the view.</li>
6423 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6424 * when the pointer has already entered the bounds of the view and has moved.</li>
6425 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6426 * when the pointer has exited the bounds of the view or when the pointer is
6427 * about to go down due to a button click, tap, or similar user action that
6428 * causes the view to be touched.</li>
6429 * </ul>
6430 * <p>
6431 * The view should implement this method to return true to indicate that it is
6432 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006433 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006434 * The default implementation calls {@link #setHovered} to update the hovered state
6435 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006436 * is enabled and is clickable. The default implementation also sends hover
6437 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006438 * </p>
6439 *
6440 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006441 * @return True if the view handled the hover event.
6442 *
6443 * @see #isHovered
6444 * @see #setHovered
6445 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006446 */
6447 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006448 // The root view may receive hover (or touch) events that are outside the bounds of
6449 // the window. This code ensures that we only send accessibility events for
6450 // hovers that are actually within the bounds of the root view.
6451 final int action = event.getAction();
6452 if (!mSendingHoverAccessibilityEvents) {
6453 if ((action == MotionEvent.ACTION_HOVER_ENTER
6454 || action == MotionEvent.ACTION_HOVER_MOVE)
6455 && !hasHoveredChild()
6456 && pointInView(event.getX(), event.getY())) {
6457 mSendingHoverAccessibilityEvents = true;
6458 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6459 }
6460 } else {
6461 if (action == MotionEvent.ACTION_HOVER_EXIT
6462 || (action == MotionEvent.ACTION_HOVER_MOVE
6463 && !pointInView(event.getX(), event.getY()))) {
6464 mSendingHoverAccessibilityEvents = false;
6465 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6466 }
Jeff Browna1b24182011-07-28 13:38:24 -07006467 }
6468
Jeff Brown87b7f802011-06-21 18:35:45 -07006469 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006470 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006471 case MotionEvent.ACTION_HOVER_ENTER:
6472 setHovered(true);
6473 break;
6474 case MotionEvent.ACTION_HOVER_EXIT:
6475 setHovered(false);
6476 break;
6477 }
Jeff Browna1b24182011-07-28 13:38:24 -07006478
6479 // Dispatch the event to onGenericMotionEvent before returning true.
6480 // This is to provide compatibility with existing applications that
6481 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6482 // break because of the new default handling for hoverable views
6483 // in onHoverEvent.
6484 // Note that onGenericMotionEvent will be called by default when
6485 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6486 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006487 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006488 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006489 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006490 }
6491
6492 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006493 * Returns true if the view should handle {@link #onHoverEvent}
6494 * by calling {@link #setHovered} to change its hovered state.
6495 *
6496 * @return True if the view is hoverable.
6497 */
6498 private boolean isHoverable() {
6499 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006500 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006501 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6502 return false;
6503 }
6504
6505 return (viewFlags & CLICKABLE) == CLICKABLE
6506 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6507 }
6508
6509 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006510 * Returns true if the view is currently hovered.
6511 *
6512 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006513 *
6514 * @see #setHovered
6515 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006516 */
Jeff Brown10b62902011-06-20 16:40:37 -07006517 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006518 public boolean isHovered() {
6519 return (mPrivateFlags & HOVERED) != 0;
6520 }
6521
6522 /**
6523 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006524 * <p>
6525 * Calling this method also changes the drawable state of the view. This
6526 * enables the view to react to hover by using different drawable resources
6527 * to change its appearance.
6528 * </p><p>
6529 * The {@link #onHoverChanged} method is called when the hovered state changes.
6530 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006531 *
6532 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006533 *
6534 * @see #isHovered
6535 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006536 */
6537 public void setHovered(boolean hovered) {
6538 if (hovered) {
6539 if ((mPrivateFlags & HOVERED) == 0) {
6540 mPrivateFlags |= HOVERED;
6541 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006542 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006543 }
6544 } else {
6545 if ((mPrivateFlags & HOVERED) != 0) {
6546 mPrivateFlags &= ~HOVERED;
6547 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006548 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006549 }
6550 }
6551 }
6552
6553 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006554 * Implement this method to handle hover state changes.
6555 * <p>
6556 * This method is called whenever the hover state changes as a result of a
6557 * call to {@link #setHovered}.
6558 * </p>
6559 *
6560 * @param hovered The current hover state, as returned by {@link #isHovered}.
6561 *
6562 * @see #isHovered
6563 * @see #setHovered
6564 */
6565 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006566 }
6567
6568 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 * Implement this method to handle touch screen motion events.
6570 *
6571 * @param event The motion event.
6572 * @return True if the event was handled, false otherwise.
6573 */
6574 public boolean onTouchEvent(MotionEvent event) {
6575 final int viewFlags = mViewFlags;
6576
6577 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006578 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006579 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 // A disabled view that is clickable still consumes the touch
6582 // events, it just doesn't respond to them.
6583 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6584 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6585 }
6586
6587 if (mTouchDelegate != null) {
6588 if (mTouchDelegate.onTouchEvent(event)) {
6589 return true;
6590 }
6591 }
6592
6593 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6594 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6595 switch (event.getAction()) {
6596 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006597 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6598 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 // take focus if we don't have it already and we should in
6600 // touch mode.
6601 boolean focusTaken = false;
6602 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6603 focusTaken = requestFocus();
6604 }
6605
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006606 if (prepressed) {
6607 // The button is being released before we actually
6608 // showed it as pressed. Make it show the pressed
6609 // state now (before scheduling the click) to ensure
6610 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006611 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006612 }
Joe Malin32736f02011-01-19 16:14:20 -08006613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 if (!mHasPerformedLongPress) {
6615 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006616 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006617
6618 // Only perform take click actions if we were in the pressed state
6619 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006620 // Use a Runnable and post this rather than calling
6621 // performClick directly. This lets other visual state
6622 // of the view update before click actions start.
6623 if (mPerformClick == null) {
6624 mPerformClick = new PerformClick();
6625 }
6626 if (!post(mPerformClick)) {
6627 performClick();
6628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006629 }
6630 }
6631
6632 if (mUnsetPressedState == null) {
6633 mUnsetPressedState = new UnsetPressedState();
6634 }
6635
Adam Powelle14579b2009-12-16 18:39:52 -08006636 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006637 postDelayed(mUnsetPressedState,
6638 ViewConfiguration.getPressedStateDuration());
6639 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006640 // If the post failed, unpress right now
6641 mUnsetPressedState.run();
6642 }
Adam Powelle14579b2009-12-16 18:39:52 -08006643 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 }
6645 break;
6646
6647 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006648 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006649
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006650 if (performButtonActionOnTouchDown(event)) {
6651 break;
6652 }
6653
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006654 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006655 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006656
6657 // For views inside a scrolling container, delay the pressed feedback for
6658 // a short period in case this is a scroll.
6659 if (isInScrollingContainer) {
6660 mPrivateFlags |= PREPRESSED;
6661 if (mPendingCheckForTap == null) {
6662 mPendingCheckForTap = new CheckForTap();
6663 }
6664 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6665 } else {
6666 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006667 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006668 checkForLongClick(0);
6669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 break;
6671
6672 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006673 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006674 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 break;
6676
6677 case MotionEvent.ACTION_MOVE:
6678 final int x = (int) event.getX();
6679 final int y = (int) event.getY();
6680
6681 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006682 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006684 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006686 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006687 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688
Adam Powell4d6f0662012-02-21 15:11:11 -08006689 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006691 }
6692 break;
6693 }
6694 return true;
6695 }
6696
6697 return false;
6698 }
6699
6700 /**
Adam Powell10298662011-08-14 18:26:30 -07006701 * @hide
6702 */
6703 public boolean isInScrollingContainer() {
6704 ViewParent p = getParent();
6705 while (p != null && p instanceof ViewGroup) {
6706 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6707 return true;
6708 }
6709 p = p.getParent();
6710 }
6711 return false;
6712 }
6713
6714 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006715 * Remove the longpress detection timer.
6716 */
6717 private void removeLongPressCallback() {
6718 if (mPendingCheckForLongPress != null) {
6719 removeCallbacks(mPendingCheckForLongPress);
6720 }
6721 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006722
6723 /**
6724 * Remove the pending click action
6725 */
6726 private void removePerformClickCallback() {
6727 if (mPerformClick != null) {
6728 removeCallbacks(mPerformClick);
6729 }
6730 }
6731
Adam Powelle14579b2009-12-16 18:39:52 -08006732 /**
Romain Guya440b002010-02-24 15:57:54 -08006733 * Remove the prepress detection timer.
6734 */
6735 private void removeUnsetPressCallback() {
6736 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6737 setPressed(false);
6738 removeCallbacks(mUnsetPressedState);
6739 }
6740 }
6741
6742 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006743 * Remove the tap detection timer.
6744 */
6745 private void removeTapCallback() {
6746 if (mPendingCheckForTap != null) {
6747 mPrivateFlags &= ~PREPRESSED;
6748 removeCallbacks(mPendingCheckForTap);
6749 }
6750 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006751
6752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 * Cancels a pending long press. Your subclass can use this if you
6754 * want the context menu to come up if the user presses and holds
6755 * at the same place, but you don't want it to come up if they press
6756 * and then move around enough to cause scrolling.
6757 */
6758 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006759 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006760
6761 /*
6762 * The prepressed state handled by the tap callback is a display
6763 * construct, but the tap callback will post a long press callback
6764 * less its own timeout. Remove it here.
6765 */
6766 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006767 }
6768
6769 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006770 * Remove the pending callback for sending a
6771 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6772 */
6773 private void removeSendViewScrolledAccessibilityEventCallback() {
6774 if (mSendViewScrolledAccessibilityEvent != null) {
6775 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6776 }
6777 }
6778
6779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006780 * Sets the TouchDelegate for this View.
6781 */
6782 public void setTouchDelegate(TouchDelegate delegate) {
6783 mTouchDelegate = delegate;
6784 }
6785
6786 /**
6787 * Gets the TouchDelegate for this View.
6788 */
6789 public TouchDelegate getTouchDelegate() {
6790 return mTouchDelegate;
6791 }
6792
6793 /**
6794 * Set flags controlling behavior of this view.
6795 *
6796 * @param flags Constant indicating the value which should be set
6797 * @param mask Constant indicating the bit range that should be changed
6798 */
6799 void setFlags(int flags, int mask) {
6800 int old = mViewFlags;
6801 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6802
6803 int changed = mViewFlags ^ old;
6804 if (changed == 0) {
6805 return;
6806 }
6807 int privateFlags = mPrivateFlags;
6808
6809 /* Check if the FOCUSABLE bit has changed */
6810 if (((changed & FOCUSABLE_MASK) != 0) &&
6811 ((privateFlags & HAS_BOUNDS) !=0)) {
6812 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6813 && ((privateFlags & FOCUSED) != 0)) {
6814 /* Give up focus if we are no longer focusable */
6815 clearFocus();
6816 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6817 && ((privateFlags & FOCUSED) == 0)) {
6818 /*
6819 * Tell the view system that we are now available to take focus
6820 * if no one else already has it.
6821 */
6822 if (mParent != null) mParent.focusableViewAvailable(this);
6823 }
6824 }
6825
6826 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6827 if ((changed & VISIBILITY_MASK) != 0) {
6828 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006829 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006830 * it was not visible. Marking it drawn ensures that the invalidation will
6831 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006833 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006834 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835
6836 needGlobalAttributesUpdate(true);
6837
6838 // a view becoming visible is worth notifying the parent
6839 // about in case nothing has focus. even if this specific view
6840 // isn't focusable, it may contain something that is, so let
6841 // the root view try to give this focus if nothing else does.
6842 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6843 mParent.focusableViewAvailable(this);
6844 }
6845 }
6846 }
6847
6848 /* Check if the GONE bit has changed */
6849 if ((changed & GONE) != 0) {
6850 needGlobalAttributesUpdate(false);
6851 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006852
Romain Guyecd80ee2009-12-03 17:13:02 -08006853 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6854 if (hasFocus()) clearFocus();
6855 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006856 if (mParent instanceof View) {
6857 // GONE views noop invalidation, so invalidate the parent
6858 ((View) mParent).invalidate(true);
6859 }
6860 // Mark the view drawn to ensure that it gets invalidated properly the next
6861 // time it is visible and gets invalidated
6862 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 }
6864 if (mAttachInfo != null) {
6865 mAttachInfo.mViewVisibilityChanged = true;
6866 }
6867 }
6868
6869 /* Check if the VISIBLE bit has changed */
6870 if ((changed & INVISIBLE) != 0) {
6871 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006872 /*
6873 * If this view is becoming invisible, set the DRAWN flag so that
6874 * the next invalidate() will not be skipped.
6875 */
6876 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006877
6878 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6879 // root view becoming invisible shouldn't clear focus
6880 if (getRootView() != this) {
6881 clearFocus();
6882 }
6883 }
6884 if (mAttachInfo != null) {
6885 mAttachInfo.mViewVisibilityChanged = true;
6886 }
6887 }
6888
Adam Powell326d8082009-12-09 15:10:07 -08006889 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006890 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006891 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6892 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006893 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006894 } else if (mParent != null) {
6895 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006896 }
Adam Powell326d8082009-12-09 15:10:07 -08006897 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6898 }
6899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6901 destroyDrawingCache();
6902 }
6903
6904 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6905 destroyDrawingCache();
6906 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006907 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006908 }
6909
6910 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6911 destroyDrawingCache();
6912 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6913 }
6914
6915 if ((changed & DRAW_MASK) != 0) {
6916 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6917 if (mBGDrawable != null) {
6918 mPrivateFlags &= ~SKIP_DRAW;
6919 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6920 } else {
6921 mPrivateFlags |= SKIP_DRAW;
6922 }
6923 } else {
6924 mPrivateFlags &= ~SKIP_DRAW;
6925 }
6926 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006927 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 }
6929
6930 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006931 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 mParent.recomputeViewAttributes(this);
6933 }
6934 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006935
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006936 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006937 requestLayout();
6938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 }
6940
6941 /**
6942 * Change the view's z order in the tree, so it's on top of other sibling
6943 * views
6944 */
6945 public void bringToFront() {
6946 if (mParent != null) {
6947 mParent.bringChildToFront(this);
6948 }
6949 }
6950
6951 /**
6952 * This is called in response to an internal scroll in this view (i.e., the
6953 * view scrolled its own contents). This is typically as a result of
6954 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6955 * called.
6956 *
6957 * @param l Current horizontal scroll origin.
6958 * @param t Current vertical scroll origin.
6959 * @param oldl Previous horizontal scroll origin.
6960 * @param oldt Previous vertical scroll origin.
6961 */
6962 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006963 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6964 postSendViewScrolledAccessibilityEventCallback();
6965 }
6966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 mBackgroundSizeChanged = true;
6968
6969 final AttachInfo ai = mAttachInfo;
6970 if (ai != null) {
6971 ai.mViewScrollChanged = true;
6972 }
6973 }
6974
6975 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006976 * Interface definition for a callback to be invoked when the layout bounds of a view
6977 * changes due to layout processing.
6978 */
6979 public interface OnLayoutChangeListener {
6980 /**
6981 * Called when the focus state of a view has changed.
6982 *
6983 * @param v The view whose state has changed.
6984 * @param left The new value of the view's left property.
6985 * @param top The new value of the view's top property.
6986 * @param right The new value of the view's right property.
6987 * @param bottom The new value of the view's bottom property.
6988 * @param oldLeft The previous value of the view's left property.
6989 * @param oldTop The previous value of the view's top property.
6990 * @param oldRight The previous value of the view's right property.
6991 * @param oldBottom The previous value of the view's bottom property.
6992 */
6993 void onLayoutChange(View v, int left, int top, int right, int bottom,
6994 int oldLeft, int oldTop, int oldRight, int oldBottom);
6995 }
6996
6997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006998 * This is called during layout when the size of this view has changed. If
6999 * you were just added to the view hierarchy, you're called with the old
7000 * values of 0.
7001 *
7002 * @param w Current width of this view.
7003 * @param h Current height of this view.
7004 * @param oldw Old width of this view.
7005 * @param oldh Old height of this view.
7006 */
7007 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7008 }
7009
7010 /**
7011 * Called by draw to draw the child views. This may be overridden
7012 * by derived classes to gain control just before its children are drawn
7013 * (but after its own view has been drawn).
7014 * @param canvas the canvas on which to draw the view
7015 */
7016 protected void dispatchDraw(Canvas canvas) {
7017 }
7018
7019 /**
7020 * Gets the parent of this view. Note that the parent is a
7021 * ViewParent and not necessarily a View.
7022 *
7023 * @return Parent of this view.
7024 */
7025 public final ViewParent getParent() {
7026 return mParent;
7027 }
7028
7029 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007030 * Set the horizontal scrolled position of your view. This will cause a call to
7031 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7032 * invalidated.
7033 * @param value the x position to scroll to
7034 */
7035 public void setScrollX(int value) {
7036 scrollTo(value, mScrollY);
7037 }
7038
7039 /**
7040 * Set the vertical scrolled position of your view. This will cause a call to
7041 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7042 * invalidated.
7043 * @param value the y position to scroll to
7044 */
7045 public void setScrollY(int value) {
7046 scrollTo(mScrollX, value);
7047 }
7048
7049 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007050 * Return the scrolled left position of this view. This is the left edge of
7051 * the displayed part of your view. You do not need to draw any pixels
7052 * farther left, since those are outside of the frame of your view on
7053 * screen.
7054 *
7055 * @return The left edge of the displayed part of your view, in pixels.
7056 */
7057 public final int getScrollX() {
7058 return mScrollX;
7059 }
7060
7061 /**
7062 * Return the scrolled top position of this view. This is the top edge of
7063 * the displayed part of your view. You do not need to draw any pixels above
7064 * it, since those are outside of the frame of your view on screen.
7065 *
7066 * @return The top edge of the displayed part of your view, in pixels.
7067 */
7068 public final int getScrollY() {
7069 return mScrollY;
7070 }
7071
7072 /**
7073 * Return the width of the your view.
7074 *
7075 * @return The width of your view, in pixels.
7076 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007077 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078 public final int getWidth() {
7079 return mRight - mLeft;
7080 }
7081
7082 /**
7083 * Return the height of your view.
7084 *
7085 * @return The height of your view, in pixels.
7086 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007087 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 public final int getHeight() {
7089 return mBottom - mTop;
7090 }
7091
7092 /**
7093 * Return the visible drawing bounds of your view. Fills in the output
7094 * rectangle with the values from getScrollX(), getScrollY(),
7095 * getWidth(), and getHeight().
7096 *
7097 * @param outRect The (scrolled) drawing bounds of the view.
7098 */
7099 public void getDrawingRect(Rect outRect) {
7100 outRect.left = mScrollX;
7101 outRect.top = mScrollY;
7102 outRect.right = mScrollX + (mRight - mLeft);
7103 outRect.bottom = mScrollY + (mBottom - mTop);
7104 }
7105
7106 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007107 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7108 * raw width component (that is the result is masked by
7109 * {@link #MEASURED_SIZE_MASK}).
7110 *
7111 * @return The raw measured width of this view.
7112 */
7113 public final int getMeasuredWidth() {
7114 return mMeasuredWidth & MEASURED_SIZE_MASK;
7115 }
7116
7117 /**
7118 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007119 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007120 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 * This should be used during measurement and layout calculations only. Use
7122 * {@link #getWidth()} to see how wide a view is after layout.
7123 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007124 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007126 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 return mMeasuredWidth;
7128 }
7129
7130 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007131 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7132 * raw width component (that is the result is masked by
7133 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007134 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007135 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 */
7137 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007138 return mMeasuredHeight & MEASURED_SIZE_MASK;
7139 }
7140
7141 /**
7142 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007143 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007144 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7145 * This should be used during measurement and layout calculations only. Use
7146 * {@link #getHeight()} to see how wide a view is after layout.
7147 *
7148 * @return The measured width of this view as a bit mask.
7149 */
7150 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 return mMeasuredHeight;
7152 }
7153
7154 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007155 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7156 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7157 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7158 * and the height component is at the shifted bits
7159 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7160 */
7161 public final int getMeasuredState() {
7162 return (mMeasuredWidth&MEASURED_STATE_MASK)
7163 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7164 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7165 }
7166
7167 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007168 * The transform matrix of this view, which is calculated based on the current
7169 * roation, scale, and pivot properties.
7170 *
7171 * @see #getRotation()
7172 * @see #getScaleX()
7173 * @see #getScaleY()
7174 * @see #getPivotX()
7175 * @see #getPivotY()
7176 * @return The current transform matrix for the view
7177 */
7178 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007179 if (mTransformationInfo != null) {
7180 updateMatrix();
7181 return mTransformationInfo.mMatrix;
7182 }
7183 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007184 }
7185
7186 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007187 * Utility function to determine if the value is far enough away from zero to be
7188 * considered non-zero.
7189 * @param value A floating point value to check for zero-ness
7190 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7191 */
7192 private static boolean nonzero(float value) {
7193 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7194 }
7195
7196 /**
Jeff Brown86671742010-09-30 20:00:15 -07007197 * Returns true if the transform matrix is the identity matrix.
7198 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007199 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007200 * @return True if the transform matrix is the identity matrix, false otherwise.
7201 */
Jeff Brown86671742010-09-30 20:00:15 -07007202 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007203 if (mTransformationInfo != null) {
7204 updateMatrix();
7205 return mTransformationInfo.mMatrixIsIdentity;
7206 }
7207 return true;
7208 }
7209
7210 void ensureTransformationInfo() {
7211 if (mTransformationInfo == null) {
7212 mTransformationInfo = new TransformationInfo();
7213 }
Jeff Brown86671742010-09-30 20:00:15 -07007214 }
7215
7216 /**
7217 * Recomputes the transform matrix if necessary.
7218 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007219 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007220 final TransformationInfo info = mTransformationInfo;
7221 if (info == null) {
7222 return;
7223 }
7224 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007225 // transform-related properties have changed since the last time someone
7226 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007227
7228 // Figure out if we need to update the pivot point
7229 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007230 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7231 info.mPrevWidth = mRight - mLeft;
7232 info.mPrevHeight = mBottom - mTop;
7233 info.mPivotX = info.mPrevWidth / 2f;
7234 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007235 }
7236 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007237 info.mMatrix.reset();
7238 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7239 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7240 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7241 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007242 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007243 if (info.mCamera == null) {
7244 info.mCamera = new Camera();
7245 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007246 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007247 info.mCamera.save();
7248 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7249 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7250 info.mCamera.getMatrix(info.matrix3D);
7251 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7252 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7253 info.mPivotY + info.mTranslationY);
7254 info.mMatrix.postConcat(info.matrix3D);
7255 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007256 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007257 info.mMatrixDirty = false;
7258 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7259 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007260 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007261 }
7262
7263 /**
7264 * Utility method to retrieve the inverse of the current mMatrix property.
7265 * We cache the matrix to avoid recalculating it when transform properties
7266 * have not changed.
7267 *
7268 * @return The inverse of the current matrix of this view.
7269 */
Jeff Brown86671742010-09-30 20:00:15 -07007270 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007271 final TransformationInfo info = mTransformationInfo;
7272 if (info != null) {
7273 updateMatrix();
7274 if (info.mInverseMatrixDirty) {
7275 if (info.mInverseMatrix == null) {
7276 info.mInverseMatrix = new Matrix();
7277 }
7278 info.mMatrix.invert(info.mInverseMatrix);
7279 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007280 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007281 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007282 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007283 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007284 }
7285
7286 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007287 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7288 * views are drawn) from the camera to this view. The camera's distance
7289 * affects 3D transformations, for instance rotations around the X and Y
7290 * axis. If the rotationX or rotationY properties are changed and this view is
7291 * large (more than half the size of the screen), it is recommended to always
7292 * use a camera distance that's greater than the height (X axis rotation) or
7293 * the width (Y axis rotation) of this view.</p>
7294 *
7295 * <p>The distance of the camera from the view plane can have an affect on the
7296 * perspective distortion of the view when it is rotated around the x or y axis.
7297 * For example, a large distance will result in a large viewing angle, and there
7298 * will not be much perspective distortion of the view as it rotates. A short
7299 * distance may cause much more perspective distortion upon rotation, and can
7300 * also result in some drawing artifacts if the rotated view ends up partially
7301 * behind the camera (which is why the recommendation is to use a distance at
7302 * least as far as the size of the view, if the view is to be rotated.)</p>
7303 *
7304 * <p>The distance is expressed in "depth pixels." The default distance depends
7305 * on the screen density. For instance, on a medium density display, the
7306 * default distance is 1280. On a high density display, the default distance
7307 * is 1920.</p>
7308 *
7309 * <p>If you want to specify a distance that leads to visually consistent
7310 * results across various densities, use the following formula:</p>
7311 * <pre>
7312 * float scale = context.getResources().getDisplayMetrics().density;
7313 * view.setCameraDistance(distance * scale);
7314 * </pre>
7315 *
7316 * <p>The density scale factor of a high density display is 1.5,
7317 * and 1920 = 1280 * 1.5.</p>
7318 *
7319 * @param distance The distance in "depth pixels", if negative the opposite
7320 * value is used
7321 *
7322 * @see #setRotationX(float)
7323 * @see #setRotationY(float)
7324 */
7325 public void setCameraDistance(float distance) {
7326 invalidateParentCaches();
7327 invalidate(false);
7328
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007329 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007330 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007331 final TransformationInfo info = mTransformationInfo;
7332 if (info.mCamera == null) {
7333 info.mCamera = new Camera();
7334 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007335 }
7336
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007337 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7338 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007339
7340 invalidate(false);
7341 }
7342
7343 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007344 * The degrees that the view is rotated around the pivot point.
7345 *
Romain Guya5364ee2011-02-24 14:46:04 -08007346 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007347 * @see #getPivotX()
7348 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007349 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007350 * @return The degrees of rotation.
7351 */
Chet Haasea5531132012-02-02 13:41:44 -08007352 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007353 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007354 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007355 }
7356
7357 /**
Chet Haase897247b2010-09-09 14:54:47 -07007358 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7359 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007360 *
7361 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007362 *
7363 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007364 * @see #getPivotX()
7365 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007366 * @see #setRotationX(float)
7367 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007368 *
7369 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007370 */
7371 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007372 ensureTransformationInfo();
7373 final TransformationInfo info = mTransformationInfo;
7374 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007375 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007376 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007377 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007378 info.mRotation = rotation;
7379 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007380 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007381 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007382 }
7383 }
7384
7385 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007386 * The degrees that the view is rotated around the vertical axis through the pivot point.
7387 *
7388 * @see #getPivotX()
7389 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007390 * @see #setRotationY(float)
7391 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007392 * @return The degrees of Y rotation.
7393 */
Chet Haasea5531132012-02-02 13:41:44 -08007394 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007395 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007396 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007397 }
7398
7399 /**
Chet Haase897247b2010-09-09 14:54:47 -07007400 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7401 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7402 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007403 *
7404 * When rotating large views, it is recommended to adjust the camera distance
7405 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007406 *
7407 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007408 *
7409 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007410 * @see #getPivotX()
7411 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007412 * @see #setRotation(float)
7413 * @see #setRotationX(float)
7414 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007415 *
7416 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007417 */
7418 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007419 ensureTransformationInfo();
7420 final TransformationInfo info = mTransformationInfo;
7421 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007422 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007423 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007424 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007425 info.mRotationY = rotationY;
7426 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007427 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007428 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007429 }
7430 }
7431
7432 /**
7433 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7434 *
7435 * @see #getPivotX()
7436 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007437 * @see #setRotationX(float)
7438 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007439 * @return The degrees of X rotation.
7440 */
Chet Haasea5531132012-02-02 13:41:44 -08007441 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007442 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007443 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007444 }
7445
7446 /**
Chet Haase897247b2010-09-09 14:54:47 -07007447 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7448 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7449 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007450 *
7451 * When rotating large views, it is recommended to adjust the camera distance
7452 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007453 *
7454 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007455 *
7456 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007457 * @see #getPivotX()
7458 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007459 * @see #setRotation(float)
7460 * @see #setRotationY(float)
7461 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007462 *
7463 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007464 */
7465 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007466 ensureTransformationInfo();
7467 final TransformationInfo info = mTransformationInfo;
7468 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007469 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007470 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007471 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007472 info.mRotationX = rotationX;
7473 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007474 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007475 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007476 }
7477 }
7478
7479 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007480 * The amount that the view is scaled in x around the pivot point, as a proportion of
7481 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7482 *
Joe Onorato93162322010-09-16 15:42:01 -04007483 * <p>By default, this is 1.0f.
7484 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007485 * @see #getPivotX()
7486 * @see #getPivotY()
7487 * @return The scaling factor.
7488 */
Chet Haasea5531132012-02-02 13:41:44 -08007489 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007490 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007491 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007492 }
7493
7494 /**
7495 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7496 * the view's unscaled width. A value of 1 means that no scaling is applied.
7497 *
7498 * @param scaleX The scaling factor.
7499 * @see #getPivotX()
7500 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007501 *
7502 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007503 */
7504 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007505 ensureTransformationInfo();
7506 final TransformationInfo info = mTransformationInfo;
7507 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007508 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007509 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007510 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007511 info.mScaleX = scaleX;
7512 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007513 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007514 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007515 }
7516 }
7517
7518 /**
7519 * The amount that the view is scaled in y around the pivot point, as a proportion of
7520 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7521 *
Joe Onorato93162322010-09-16 15:42:01 -04007522 * <p>By default, this is 1.0f.
7523 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007524 * @see #getPivotX()
7525 * @see #getPivotY()
7526 * @return The scaling factor.
7527 */
Chet Haasea5531132012-02-02 13:41:44 -08007528 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007529 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007530 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007531 }
7532
7533 /**
7534 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7535 * the view's unscaled width. A value of 1 means that no scaling is applied.
7536 *
7537 * @param scaleY The scaling factor.
7538 * @see #getPivotX()
7539 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007540 *
7541 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007542 */
7543 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007544 ensureTransformationInfo();
7545 final TransformationInfo info = mTransformationInfo;
7546 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007547 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007548 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007549 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007550 info.mScaleY = scaleY;
7551 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007552 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007553 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007554 }
7555 }
7556
7557 /**
7558 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7559 * and {@link #setScaleX(float) scaled}.
7560 *
7561 * @see #getRotation()
7562 * @see #getScaleX()
7563 * @see #getScaleY()
7564 * @see #getPivotY()
7565 * @return The x location of the pivot point.
7566 */
Chet Haasea5531132012-02-02 13:41:44 -08007567 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007568 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007569 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007570 }
7571
7572 /**
7573 * Sets the x location of the point around which the view is
7574 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007575 * By default, the pivot point is centered on the object.
7576 * Setting this property disables this behavior and causes the view to use only the
7577 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007578 *
7579 * @param pivotX The x location of the pivot point.
7580 * @see #getRotation()
7581 * @see #getScaleX()
7582 * @see #getScaleY()
7583 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007584 *
7585 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007586 */
7587 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007588 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007589 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007590 final TransformationInfo info = mTransformationInfo;
7591 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007592 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007593 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007594 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007595 info.mPivotX = pivotX;
7596 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007597 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007598 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007599 }
7600 }
7601
7602 /**
7603 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7604 * and {@link #setScaleY(float) scaled}.
7605 *
7606 * @see #getRotation()
7607 * @see #getScaleX()
7608 * @see #getScaleY()
7609 * @see #getPivotY()
7610 * @return The y location of the pivot point.
7611 */
Chet Haasea5531132012-02-02 13:41:44 -08007612 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007613 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007614 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007615 }
7616
7617 /**
7618 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007619 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7620 * Setting this property disables this behavior and causes the view to use only the
7621 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007622 *
7623 * @param pivotY The y location of the pivot point.
7624 * @see #getRotation()
7625 * @see #getScaleX()
7626 * @see #getScaleY()
7627 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007628 *
7629 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007630 */
7631 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007632 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007633 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007634 final TransformationInfo info = mTransformationInfo;
7635 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007636 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007637 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007638 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007639 info.mPivotY = pivotY;
7640 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007641 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007642 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007643 }
7644 }
7645
7646 /**
7647 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7648 * completely transparent and 1 means the view is completely opaque.
7649 *
Joe Onorato93162322010-09-16 15:42:01 -04007650 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007651 * @return The opacity of the view.
7652 */
Chet Haasea5531132012-02-02 13:41:44 -08007653 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007654 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007655 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007656 }
7657
7658 /**
Romain Guy171c5922011-01-06 10:04:23 -08007659 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7660 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007661 *
Romain Guy171c5922011-01-06 10:04:23 -08007662 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7663 * responsible for applying the opacity itself. Otherwise, calling this method is
7664 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007665 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007666 *
Chet Haasea5531132012-02-02 13:41:44 -08007667 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7668 * performance implications. It is generally best to use the alpha property sparingly and
7669 * transiently, as in the case of fading animations.</p>
7670 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007671 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007672 *
Joe Malin32736f02011-01-19 16:14:20 -08007673 * @see #setLayerType(int, android.graphics.Paint)
7674 *
Chet Haase73066682010-11-29 15:55:32 -08007675 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007676 */
7677 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007678 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007679 if (mTransformationInfo.mAlpha != alpha) {
7680 mTransformationInfo.mAlpha = alpha;
7681 invalidateParentCaches();
7682 if (onSetAlpha((int) (alpha * 255))) {
7683 mPrivateFlags |= ALPHA_SET;
7684 // subclass is handling alpha - don't optimize rendering cache invalidation
7685 invalidate(true);
7686 } else {
7687 mPrivateFlags &= ~ALPHA_SET;
7688 invalidate(false);
7689 }
Chet Haaseed032702010-10-01 14:05:54 -07007690 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007691 }
7692
7693 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007694 * Faster version of setAlpha() which performs the same steps except there are
7695 * no calls to invalidate(). The caller of this function should perform proper invalidation
7696 * on the parent and this object. The return value indicates whether the subclass handles
7697 * alpha (the return value for onSetAlpha()).
7698 *
7699 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007700 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7701 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007702 */
7703 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007704 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007705 if (mTransformationInfo.mAlpha != alpha) {
7706 mTransformationInfo.mAlpha = alpha;
7707 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7708 if (subclassHandlesAlpha) {
7709 mPrivateFlags |= ALPHA_SET;
7710 return true;
7711 } else {
7712 mPrivateFlags &= ~ALPHA_SET;
7713 }
Chet Haasea00f3862011-02-22 06:34:40 -08007714 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007715 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007716 }
7717
7718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 * Top position of this view relative to its parent.
7720 *
7721 * @return The top of this view, in pixels.
7722 */
7723 @ViewDebug.CapturedViewProperty
7724 public final int getTop() {
7725 return mTop;
7726 }
7727
7728 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007729 * Sets the top position of this view relative to its parent. This method is meant to be called
7730 * by the layout system and should not generally be called otherwise, because the property
7731 * may be changed at any time by the layout.
7732 *
7733 * @param top The top of this view, in pixels.
7734 */
7735 public final void setTop(int top) {
7736 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007737 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007738 final boolean matrixIsIdentity = mTransformationInfo == null
7739 || mTransformationInfo.mMatrixIsIdentity;
7740 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007741 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007742 int minTop;
7743 int yLoc;
7744 if (top < mTop) {
7745 minTop = top;
7746 yLoc = top - mTop;
7747 } else {
7748 minTop = mTop;
7749 yLoc = 0;
7750 }
Chet Haasee9140a72011-02-16 16:23:29 -08007751 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007752 }
7753 } else {
7754 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007755 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007756 }
7757
Chet Haaseed032702010-10-01 14:05:54 -07007758 int width = mRight - mLeft;
7759 int oldHeight = mBottom - mTop;
7760
Chet Haase21cd1382010-09-01 17:42:29 -07007761 mTop = top;
7762
Chet Haaseed032702010-10-01 14:05:54 -07007763 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7764
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007765 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007766 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7767 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007768 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007769 }
Chet Haase21cd1382010-09-01 17:42:29 -07007770 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007771 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007772 }
Chet Haase55dbb652010-12-21 20:15:08 -08007773 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007774 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007775 }
7776 }
7777
7778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 * Bottom position of this view relative to its parent.
7780 *
7781 * @return The bottom of this view, in pixels.
7782 */
7783 @ViewDebug.CapturedViewProperty
7784 public final int getBottom() {
7785 return mBottom;
7786 }
7787
7788 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007789 * True if this view has changed since the last time being drawn.
7790 *
7791 * @return The dirty state of this view.
7792 */
7793 public boolean isDirty() {
7794 return (mPrivateFlags & DIRTY_MASK) != 0;
7795 }
7796
7797 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007798 * Sets the bottom position of this view relative to its parent. This method is meant to be
7799 * called by the layout system and should not generally be called otherwise, because the
7800 * property may be changed at any time by the layout.
7801 *
7802 * @param bottom The bottom of this view, in pixels.
7803 */
7804 public final void setBottom(int bottom) {
7805 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007806 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007807 final boolean matrixIsIdentity = mTransformationInfo == null
7808 || mTransformationInfo.mMatrixIsIdentity;
7809 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007810 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007811 int maxBottom;
7812 if (bottom < mBottom) {
7813 maxBottom = mBottom;
7814 } else {
7815 maxBottom = bottom;
7816 }
Chet Haasee9140a72011-02-16 16:23:29 -08007817 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007818 }
7819 } else {
7820 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007821 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007822 }
7823
Chet Haaseed032702010-10-01 14:05:54 -07007824 int width = mRight - mLeft;
7825 int oldHeight = mBottom - mTop;
7826
Chet Haase21cd1382010-09-01 17:42:29 -07007827 mBottom = bottom;
7828
Chet Haaseed032702010-10-01 14:05:54 -07007829 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7830
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007831 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007832 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7833 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007834 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007835 }
Chet Haase21cd1382010-09-01 17:42:29 -07007836 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007837 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007838 }
Chet Haase55dbb652010-12-21 20:15:08 -08007839 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007840 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007841 }
7842 }
7843
7844 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 * Left position of this view relative to its parent.
7846 *
7847 * @return The left edge of this view, in pixels.
7848 */
7849 @ViewDebug.CapturedViewProperty
7850 public final int getLeft() {
7851 return mLeft;
7852 }
7853
7854 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007855 * Sets the left position of this view relative to its parent. This method is meant to be called
7856 * by the layout system and should not generally be called otherwise, because the property
7857 * may be changed at any time by the layout.
7858 *
7859 * @param left The bottom of this view, in pixels.
7860 */
7861 public final void setLeft(int left) {
7862 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007863 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007864 final boolean matrixIsIdentity = mTransformationInfo == null
7865 || mTransformationInfo.mMatrixIsIdentity;
7866 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007867 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007868 int minLeft;
7869 int xLoc;
7870 if (left < mLeft) {
7871 minLeft = left;
7872 xLoc = left - mLeft;
7873 } else {
7874 minLeft = mLeft;
7875 xLoc = 0;
7876 }
Chet Haasee9140a72011-02-16 16:23:29 -08007877 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007878 }
7879 } else {
7880 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007881 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007882 }
7883
Chet Haaseed032702010-10-01 14:05:54 -07007884 int oldWidth = mRight - mLeft;
7885 int height = mBottom - mTop;
7886
Chet Haase21cd1382010-09-01 17:42:29 -07007887 mLeft = left;
7888
Chet Haaseed032702010-10-01 14:05:54 -07007889 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7890
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007891 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007892 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7893 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007894 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007895 }
Chet Haase21cd1382010-09-01 17:42:29 -07007896 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007897 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007898 }
Chet Haase55dbb652010-12-21 20:15:08 -08007899 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007900 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007901 }
7902 }
7903
7904 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 * Right position of this view relative to its parent.
7906 *
7907 * @return The right edge of this view, in pixels.
7908 */
7909 @ViewDebug.CapturedViewProperty
7910 public final int getRight() {
7911 return mRight;
7912 }
7913
7914 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007915 * Sets the right position of this view relative to its parent. This method is meant to be called
7916 * by the layout system and should not generally be called otherwise, because the property
7917 * may be changed at any time by the layout.
7918 *
7919 * @param right The bottom of this view, in pixels.
7920 */
7921 public final void setRight(int right) {
7922 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007923 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007924 final boolean matrixIsIdentity = mTransformationInfo == null
7925 || mTransformationInfo.mMatrixIsIdentity;
7926 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007927 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007928 int maxRight;
7929 if (right < mRight) {
7930 maxRight = mRight;
7931 } else {
7932 maxRight = right;
7933 }
Chet Haasee9140a72011-02-16 16:23:29 -08007934 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007935 }
7936 } else {
7937 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007938 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007939 }
7940
Chet Haaseed032702010-10-01 14:05:54 -07007941 int oldWidth = mRight - mLeft;
7942 int height = mBottom - mTop;
7943
Chet Haase21cd1382010-09-01 17:42:29 -07007944 mRight = right;
7945
Chet Haaseed032702010-10-01 14:05:54 -07007946 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7947
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007948 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007949 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7950 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007951 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007952 }
Chet Haase21cd1382010-09-01 17:42:29 -07007953 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007954 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007955 }
Chet Haase55dbb652010-12-21 20:15:08 -08007956 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007957 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007958 }
7959 }
7960
7961 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007962 * The visual x position of this view, in pixels. This is equivalent to the
7963 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007964 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007965 *
Chet Haasedf030d22010-07-30 17:22:38 -07007966 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007967 */
Chet Haasea5531132012-02-02 13:41:44 -08007968 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007969 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007970 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007971 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007972
Chet Haasedf030d22010-07-30 17:22:38 -07007973 /**
7974 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7975 * {@link #setTranslationX(float) translationX} property to be the difference between
7976 * the x value passed in and the current {@link #getLeft() left} property.
7977 *
7978 * @param x The visual x position of this view, in pixels.
7979 */
7980 public void setX(float x) {
7981 setTranslationX(x - mLeft);
7982 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007983
Chet Haasedf030d22010-07-30 17:22:38 -07007984 /**
7985 * The visual y position of this view, in pixels. This is equivalent to the
7986 * {@link #setTranslationY(float) translationY} property plus the current
7987 * {@link #getTop() top} property.
7988 *
7989 * @return The visual y position of this view, in pixels.
7990 */
Chet Haasea5531132012-02-02 13:41:44 -08007991 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007992 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007993 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007994 }
7995
7996 /**
7997 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7998 * {@link #setTranslationY(float) translationY} property to be the difference between
7999 * the y value passed in and the current {@link #getTop() top} property.
8000 *
8001 * @param y The visual y position of this view, in pixels.
8002 */
8003 public void setY(float y) {
8004 setTranslationY(y - mTop);
8005 }
8006
8007
8008 /**
8009 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8010 * This position is post-layout, in addition to wherever the object's
8011 * layout placed it.
8012 *
8013 * @return The horizontal position of this view relative to its left position, in pixels.
8014 */
Chet Haasea5531132012-02-02 13:41:44 -08008015 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008016 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008017 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008018 }
8019
8020 /**
8021 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8022 * This effectively positions the object post-layout, in addition to wherever the object's
8023 * layout placed it.
8024 *
8025 * @param translationX The horizontal position of this view relative to its left position,
8026 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008027 *
8028 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008029 */
8030 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008031 ensureTransformationInfo();
8032 final TransformationInfo info = mTransformationInfo;
8033 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008034 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008035 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008036 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008037 info.mTranslationX = translationX;
8038 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008039 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008040 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008041 }
8042 }
8043
8044 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008045 * The horizontal location of this view relative to its {@link #getTop() top} position.
8046 * This position is post-layout, in addition to wherever the object's
8047 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008048 *
Chet Haasedf030d22010-07-30 17:22:38 -07008049 * @return The vertical position of this view relative to its top position,
8050 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008051 */
Chet Haasea5531132012-02-02 13:41:44 -08008052 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008053 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008054 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008055 }
8056
8057 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008058 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8059 * This effectively positions the object post-layout, in addition to wherever the object's
8060 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008061 *
Chet Haasedf030d22010-07-30 17:22:38 -07008062 * @param translationY The vertical position of this view relative to its top position,
8063 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008064 *
8065 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008066 */
Chet Haasedf030d22010-07-30 17:22:38 -07008067 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008068 ensureTransformationInfo();
8069 final TransformationInfo info = mTransformationInfo;
8070 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008071 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008072 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008073 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008074 info.mTranslationY = translationY;
8075 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008076 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008077 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07008078 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008079 }
8080
8081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008082 * Hit rectangle in parent's coordinates
8083 *
8084 * @param outRect The hit rectangle of the view.
8085 */
8086 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008087 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008088 final TransformationInfo info = mTransformationInfo;
8089 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008090 outRect.set(mLeft, mTop, mRight, mBottom);
8091 } else {
8092 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008093 tmpRect.set(-info.mPivotX, -info.mPivotY,
8094 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8095 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008096 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8097 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008098 }
8099 }
8100
8101 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008102 * Determines whether the given point, in local coordinates is inside the view.
8103 */
8104 /*package*/ final boolean pointInView(float localX, float localY) {
8105 return localX >= 0 && localX < (mRight - mLeft)
8106 && localY >= 0 && localY < (mBottom - mTop);
8107 }
8108
8109 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008110 * Utility method to determine whether the given point, in local coordinates,
8111 * is inside the view, where the area of the view is expanded by the slop factor.
8112 * This method is called while processing touch-move events to determine if the event
8113 * is still within the view.
8114 */
8115 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008116 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008117 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008118 }
8119
8120 /**
8121 * When a view has focus and the user navigates away from it, the next view is searched for
8122 * starting from the rectangle filled in by this method.
8123 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008124 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8125 * of the view. However, if your view maintains some idea of internal selection,
8126 * such as a cursor, or a selected row or column, you should override this method and
8127 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 *
8129 * @param r The rectangle to fill in, in this view's coordinates.
8130 */
8131 public void getFocusedRect(Rect r) {
8132 getDrawingRect(r);
8133 }
8134
8135 /**
8136 * If some part of this view is not clipped by any of its parents, then
8137 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008138 * coordinates (without taking possible View rotations into account), offset
8139 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8140 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 *
8142 * @param r If true is returned, r holds the global coordinates of the
8143 * visible portion of this view.
8144 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8145 * between this view and its root. globalOffet may be null.
8146 * @return true if r is non-empty (i.e. part of the view is visible at the
8147 * root level.
8148 */
8149 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8150 int width = mRight - mLeft;
8151 int height = mBottom - mTop;
8152 if (width > 0 && height > 0) {
8153 r.set(0, 0, width, height);
8154 if (globalOffset != null) {
8155 globalOffset.set(-mScrollX, -mScrollY);
8156 }
8157 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8158 }
8159 return false;
8160 }
8161
8162 public final boolean getGlobalVisibleRect(Rect r) {
8163 return getGlobalVisibleRect(r, null);
8164 }
8165
8166 public final boolean getLocalVisibleRect(Rect r) {
8167 Point offset = new Point();
8168 if (getGlobalVisibleRect(r, offset)) {
8169 r.offset(-offset.x, -offset.y); // make r local
8170 return true;
8171 }
8172 return false;
8173 }
8174
8175 /**
8176 * Offset this view's vertical location by the specified number of pixels.
8177 *
8178 * @param offset the number of pixels to offset the view by
8179 */
8180 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008181 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008182 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008183 final boolean matrixIsIdentity = mTransformationInfo == null
8184 || mTransformationInfo.mMatrixIsIdentity;
8185 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008186 final ViewParent p = mParent;
8187 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008188 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008189 int minTop;
8190 int maxBottom;
8191 int yLoc;
8192 if (offset < 0) {
8193 minTop = mTop + offset;
8194 maxBottom = mBottom;
8195 yLoc = offset;
8196 } else {
8197 minTop = mTop;
8198 maxBottom = mBottom + offset;
8199 yLoc = 0;
8200 }
8201 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8202 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008203 }
8204 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008205 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008206 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008207
Chet Haasec3aa3612010-06-17 08:50:37 -07008208 mTop += offset;
8209 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008210
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008211 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008212 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008213 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008214 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008215 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 }
8218
8219 /**
8220 * Offset this view's horizontal location by the specified amount of pixels.
8221 *
8222 * @param offset the numer of pixels to offset the view by
8223 */
8224 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008225 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008226 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008227 final boolean matrixIsIdentity = mTransformationInfo == null
8228 || mTransformationInfo.mMatrixIsIdentity;
8229 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008230 final ViewParent p = mParent;
8231 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008232 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008233 int minLeft;
8234 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008235 if (offset < 0) {
8236 minLeft = mLeft + offset;
8237 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008238 } else {
8239 minLeft = mLeft;
8240 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008241 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008242 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008243 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008244 }
8245 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008246 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008247 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008248
Chet Haasec3aa3612010-06-17 08:50:37 -07008249 mLeft += offset;
8250 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008251
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008252 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008253 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008254 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008255 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008256 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 }
8259
8260 /**
8261 * Get the LayoutParams associated with this view. All views should have
8262 * layout parameters. These supply parameters to the <i>parent</i> of this
8263 * view specifying how it should be arranged. There are many subclasses of
8264 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8265 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008266 *
8267 * This method may return null if this View is not attached to a parent
8268 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8269 * was not invoked successfully. When a View is attached to a parent
8270 * ViewGroup, this method must not return null.
8271 *
8272 * @return The LayoutParams associated with this view, or null if no
8273 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008275 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008276 public ViewGroup.LayoutParams getLayoutParams() {
8277 return mLayoutParams;
8278 }
8279
8280 /**
8281 * Set the layout parameters associated with this view. These supply
8282 * parameters to the <i>parent</i> of this view specifying how it should be
8283 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8284 * correspond to the different subclasses of ViewGroup that are responsible
8285 * for arranging their children.
8286 *
Romain Guy01c174b2011-02-22 11:51:06 -08008287 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008288 */
8289 public void setLayoutParams(ViewGroup.LayoutParams params) {
8290 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008291 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 }
8293 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008294 if (mParent instanceof ViewGroup) {
8295 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 requestLayout();
8298 }
8299
8300 /**
8301 * Set the scrolled position of your view. This will cause a call to
8302 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8303 * invalidated.
8304 * @param x the x position to scroll to
8305 * @param y the y position to scroll to
8306 */
8307 public void scrollTo(int x, int y) {
8308 if (mScrollX != x || mScrollY != y) {
8309 int oldX = mScrollX;
8310 int oldY = mScrollY;
8311 mScrollX = x;
8312 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008313 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008315 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008316 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008318 }
8319 }
8320
8321 /**
8322 * Move the scrolled position of your view. This will cause a call to
8323 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8324 * invalidated.
8325 * @param x the amount of pixels to scroll by horizontally
8326 * @param y the amount of pixels to scroll by vertically
8327 */
8328 public void scrollBy(int x, int y) {
8329 scrollTo(mScrollX + x, mScrollY + y);
8330 }
8331
8332 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008333 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8334 * animation to fade the scrollbars out after a default delay. If a subclass
8335 * provides animated scrolling, the start delay should equal the duration
8336 * of the scrolling animation.</p>
8337 *
8338 * <p>The animation starts only if at least one of the scrollbars is
8339 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8340 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8341 * this method returns true, and false otherwise. If the animation is
8342 * started, this method calls {@link #invalidate()}; in that case the
8343 * caller should not call {@link #invalidate()}.</p>
8344 *
8345 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008346 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008347 *
8348 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8349 * and {@link #scrollTo(int, int)}.</p>
8350 *
8351 * @return true if the animation is played, false otherwise
8352 *
8353 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008354 * @see #scrollBy(int, int)
8355 * @see #scrollTo(int, int)
8356 * @see #isHorizontalScrollBarEnabled()
8357 * @see #isVerticalScrollBarEnabled()
8358 * @see #setHorizontalScrollBarEnabled(boolean)
8359 * @see #setVerticalScrollBarEnabled(boolean)
8360 */
8361 protected boolean awakenScrollBars() {
8362 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008363 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008364 }
8365
8366 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008367 * Trigger the scrollbars to draw.
8368 * This method differs from awakenScrollBars() only in its default duration.
8369 * initialAwakenScrollBars() will show the scroll bars for longer than
8370 * usual to give the user more of a chance to notice them.
8371 *
8372 * @return true if the animation is played, false otherwise.
8373 */
8374 private boolean initialAwakenScrollBars() {
8375 return mScrollCache != null &&
8376 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8377 }
8378
8379 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008380 * <p>
8381 * Trigger the scrollbars to draw. When invoked this method starts an
8382 * animation to fade the scrollbars out after a fixed delay. If a subclass
8383 * provides animated scrolling, the start delay should equal the duration of
8384 * the scrolling animation.
8385 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008386 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008387 * <p>
8388 * The animation starts only if at least one of the scrollbars is enabled,
8389 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8390 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8391 * this method returns true, and false otherwise. If the animation is
8392 * started, this method calls {@link #invalidate()}; in that case the caller
8393 * should not call {@link #invalidate()}.
8394 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008395 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008396 * <p>
8397 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008398 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008399 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008400 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008401 * @param startDelay the delay, in milliseconds, after which the animation
8402 * should start; when the delay is 0, the animation starts
8403 * immediately
8404 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008405 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008406 * @see #scrollBy(int, int)
8407 * @see #scrollTo(int, int)
8408 * @see #isHorizontalScrollBarEnabled()
8409 * @see #isVerticalScrollBarEnabled()
8410 * @see #setHorizontalScrollBarEnabled(boolean)
8411 * @see #setVerticalScrollBarEnabled(boolean)
8412 */
8413 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008414 return awakenScrollBars(startDelay, true);
8415 }
Joe Malin32736f02011-01-19 16:14:20 -08008416
Mike Cleron290947b2009-09-29 18:34:32 -07008417 /**
8418 * <p>
8419 * Trigger the scrollbars to draw. When invoked this method starts an
8420 * animation to fade the scrollbars out after a fixed delay. If a subclass
8421 * provides animated scrolling, the start delay should equal the duration of
8422 * the scrolling animation.
8423 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008424 *
Mike Cleron290947b2009-09-29 18:34:32 -07008425 * <p>
8426 * The animation starts only if at least one of the scrollbars is enabled,
8427 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8428 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8429 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008430 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008431 * is set to true; in that case the caller
8432 * should not call {@link #invalidate()}.
8433 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008434 *
Mike Cleron290947b2009-09-29 18:34:32 -07008435 * <p>
8436 * This method should be invoked everytime a subclass directly updates the
8437 * scroll parameters.
8438 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008439 *
Mike Cleron290947b2009-09-29 18:34:32 -07008440 * @param startDelay the delay, in milliseconds, after which the animation
8441 * should start; when the delay is 0, the animation starts
8442 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008443 *
Mike Cleron290947b2009-09-29 18:34:32 -07008444 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008445 *
Mike Cleron290947b2009-09-29 18:34:32 -07008446 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008447 *
Mike Cleron290947b2009-09-29 18:34:32 -07008448 * @see #scrollBy(int, int)
8449 * @see #scrollTo(int, int)
8450 * @see #isHorizontalScrollBarEnabled()
8451 * @see #isVerticalScrollBarEnabled()
8452 * @see #setHorizontalScrollBarEnabled(boolean)
8453 * @see #setVerticalScrollBarEnabled(boolean)
8454 */
8455 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008456 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008457
Mike Cleronf116bf82009-09-27 19:14:12 -07008458 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8459 return false;
8460 }
8461
8462 if (scrollCache.scrollBar == null) {
8463 scrollCache.scrollBar = new ScrollBarDrawable();
8464 }
8465
8466 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8467
Mike Cleron290947b2009-09-29 18:34:32 -07008468 if (invalidate) {
8469 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008470 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008471 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008472
8473 if (scrollCache.state == ScrollabilityCache.OFF) {
8474 // FIXME: this is copied from WindowManagerService.
8475 // We should get this value from the system when it
8476 // is possible to do so.
8477 final int KEY_REPEAT_FIRST_DELAY = 750;
8478 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8479 }
8480
8481 // Tell mScrollCache when we should start fading. This may
8482 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008483 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008484 scrollCache.fadeStartTime = fadeStartTime;
8485 scrollCache.state = ScrollabilityCache.ON;
8486
8487 // Schedule our fader to run, unscheduling any old ones first
8488 if (mAttachInfo != null) {
8489 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8490 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8491 }
8492
8493 return true;
8494 }
8495
8496 return false;
8497 }
8498
8499 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008500 * Do not invalidate views which are not visible and which are not running an animation. They
8501 * will not get drawn and they should not set dirty flags as if they will be drawn
8502 */
8503 private boolean skipInvalidate() {
8504 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8505 (!(mParent instanceof ViewGroup) ||
8506 !((ViewGroup) mParent).isViewTransitioning(this));
8507 }
8508 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008509 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008510 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8511 * in the future. This must be called from a UI thread. To call from a non-UI
8512 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008513 *
8514 * WARNING: This method is destructive to dirty.
8515 * @param dirty the rectangle representing the bounds of the dirty region
8516 */
8517 public void invalidate(Rect dirty) {
8518 if (ViewDebug.TRACE_HIERARCHY) {
8519 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8520 }
8521
Chet Haaseaceafe62011-08-26 15:44:33 -07008522 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008523 return;
8524 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008525 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008526 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8527 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008528 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008529 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008530 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008531 final ViewParent p = mParent;
8532 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008533 //noinspection PointlessBooleanExpression,ConstantConditions
8534 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8535 if (p != null && ai != null && ai.mHardwareAccelerated) {
8536 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008537 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008538 p.invalidateChild(this, null);
8539 return;
8540 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008542 if (p != null && ai != null) {
8543 final int scrollX = mScrollX;
8544 final int scrollY = mScrollY;
8545 final Rect r = ai.mTmpInvalRect;
8546 r.set(dirty.left - scrollX, dirty.top - scrollY,
8547 dirty.right - scrollX, dirty.bottom - scrollY);
8548 mParent.invalidateChild(this, r);
8549 }
8550 }
8551 }
8552
8553 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008554 * 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 -08008555 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008556 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8557 * will be called at some point in the future. This must be called from
8558 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 * @param l the left position of the dirty region
8560 * @param t the top position of the dirty region
8561 * @param r the right position of the dirty region
8562 * @param b the bottom position of the dirty region
8563 */
8564 public void invalidate(int l, int t, int r, int b) {
8565 if (ViewDebug.TRACE_HIERARCHY) {
8566 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8567 }
8568
Chet Haaseaceafe62011-08-26 15:44:33 -07008569 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008570 return;
8571 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008572 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008573 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8574 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008575 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008576 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008577 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008578 final ViewParent p = mParent;
8579 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008580 //noinspection PointlessBooleanExpression,ConstantConditions
8581 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8582 if (p != null && ai != null && ai.mHardwareAccelerated) {
8583 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008584 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008585 p.invalidateChild(this, null);
8586 return;
8587 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 if (p != null && ai != null && l < r && t < b) {
8590 final int scrollX = mScrollX;
8591 final int scrollY = mScrollY;
8592 final Rect tmpr = ai.mTmpInvalRect;
8593 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8594 p.invalidateChild(this, tmpr);
8595 }
8596 }
8597 }
8598
8599 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008600 * Invalidate the whole view. If the view is visible,
8601 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8602 * the future. This must be called from a UI thread. To call from a non-UI thread,
8603 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008604 */
8605 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008606 invalidate(true);
8607 }
Joe Malin32736f02011-01-19 16:14:20 -08008608
Chet Haaseed032702010-10-01 14:05:54 -07008609 /**
8610 * This is where the invalidate() work actually happens. A full invalidate()
8611 * causes the drawing cache to be invalidated, but this function can be called with
8612 * invalidateCache set to false to skip that invalidation step for cases that do not
8613 * need it (for example, a component that remains at the same dimensions with the same
8614 * content).
8615 *
8616 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8617 * well. This is usually true for a full invalidate, but may be set to false if the
8618 * View's contents or dimensions have not changed.
8619 */
Romain Guy849d0a32011-02-01 17:20:48 -08008620 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 if (ViewDebug.TRACE_HIERARCHY) {
8622 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8623 }
8624
Chet Haaseaceafe62011-08-26 15:44:33 -07008625 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008626 return;
8627 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008628 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008629 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008630 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8631 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008632 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008633 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008634 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008635 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008636 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008638 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008639 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008640 //noinspection PointlessBooleanExpression,ConstantConditions
8641 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8642 if (p != null && ai != null && ai.mHardwareAccelerated) {
8643 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008644 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008645 p.invalidateChild(this, null);
8646 return;
8647 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008648 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008650 if (p != null && ai != null) {
8651 final Rect r = ai.mTmpInvalRect;
8652 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8653 // Don't call invalidate -- we don't want to internally scroll
8654 // our own bounds
8655 p.invalidateChild(this, r);
8656 }
8657 }
8658 }
8659
8660 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008661 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008662 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8663 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008664 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8665 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008666 *
8667 * @hide
8668 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008669 protected void invalidateParentCaches() {
8670 if (mParent instanceof View) {
8671 ((View) mParent).mPrivateFlags |= INVALIDATED;
8672 }
8673 }
Joe Malin32736f02011-01-19 16:14:20 -08008674
Romain Guy0fd89bf2011-01-26 15:41:30 -08008675 /**
8676 * Used to indicate that the parent of this view should be invalidated. This functionality
8677 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8678 * which is necessary when various parent-managed properties of the view change, such as
8679 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8680 * an invalidation event to the parent.
8681 *
8682 * @hide
8683 */
8684 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008685 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008686 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008687 }
8688 }
8689
8690 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008691 * Indicates whether this View is opaque. An opaque View guarantees that it will
8692 * draw all the pixels overlapping its bounds using a fully opaque color.
8693 *
8694 * Subclasses of View should override this method whenever possible to indicate
8695 * whether an instance is opaque. Opaque Views are treated in a special way by
8696 * the View hierarchy, possibly allowing it to perform optimizations during
8697 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008698 *
Romain Guy24443ea2009-05-11 11:56:30 -07008699 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008700 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008701 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008702 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008703 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008704 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8705 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008706 }
8707
Adam Powell20232d02010-12-08 21:08:53 -08008708 /**
8709 * @hide
8710 */
8711 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008712 // Opaque if:
8713 // - Has a background
8714 // - Background is opaque
8715 // - Doesn't have scrollbars or scrollbars are inside overlay
8716
8717 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8718 mPrivateFlags |= OPAQUE_BACKGROUND;
8719 } else {
8720 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8721 }
8722
8723 final int flags = mViewFlags;
8724 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8725 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8726 mPrivateFlags |= OPAQUE_SCROLLBARS;
8727 } else {
8728 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8729 }
8730 }
8731
8732 /**
8733 * @hide
8734 */
8735 protected boolean hasOpaqueScrollbars() {
8736 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008737 }
8738
8739 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 * @return A handler associated with the thread running the View. This
8741 * handler can be used to pump events in the UI events queue.
8742 */
8743 public Handler getHandler() {
8744 if (mAttachInfo != null) {
8745 return mAttachInfo.mHandler;
8746 }
8747 return null;
8748 }
8749
8750 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008751 * Gets the view root associated with the View.
8752 * @return The view root, or null if none.
8753 * @hide
8754 */
8755 public ViewRootImpl getViewRootImpl() {
8756 if (mAttachInfo != null) {
8757 return mAttachInfo.mViewRootImpl;
8758 }
8759 return null;
8760 }
8761
8762 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008763 * <p>Causes the Runnable to be added to the message queue.
8764 * The runnable will be run on the user interface thread.</p>
8765 *
8766 * <p>This method can be invoked from outside of the UI thread
8767 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 *
8769 * @param action The Runnable that will be executed.
8770 *
8771 * @return Returns true if the Runnable was successfully placed in to the
8772 * message queue. Returns false on failure, usually because the
8773 * looper processing the message queue is exiting.
8774 */
8775 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008776 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008777 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008778 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008780 // Assume that post will succeed later
8781 ViewRootImpl.getRunQueue().post(action);
8782 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008783 }
8784
8785 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008786 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008787 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008788 * The runnable will be run on the user interface thread.</p>
8789 *
8790 * <p>This method can be invoked from outside of the UI thread
8791 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008792 *
8793 * @param action The Runnable that will be executed.
8794 * @param delayMillis The delay (in milliseconds) until the Runnable
8795 * will be executed.
8796 *
8797 * @return true if the Runnable was successfully placed in to the
8798 * message queue. Returns false on failure, usually because the
8799 * looper processing the message queue is exiting. Note that a
8800 * result of true does not mean the Runnable will be processed --
8801 * if the looper is quit before the delivery time of the message
8802 * occurs then the message will be dropped.
8803 */
8804 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008805 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008806 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008807 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008808 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008809 // Assume that post will succeed later
8810 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8811 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008812 }
8813
8814 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08008815 * <p>Causes the Runnable to execute on the next animation time step.
8816 * The runnable will be run on the user interface thread.</p>
8817 *
8818 * <p>This method can be invoked from outside of the UI thread
8819 * only when this View is attached to a window.</p>
8820 *
8821 * @param action The Runnable that will be executed.
8822 *
8823 * @hide
8824 */
8825 public void postOnAnimation(Runnable action) {
8826 final AttachInfo attachInfo = mAttachInfo;
8827 if (attachInfo != null) {
8828 attachInfo.mViewRootImpl.mChoreographer.postAnimationCallback(action, null);
8829 } else {
8830 // Assume that post will succeed later
8831 ViewRootImpl.getRunQueue().post(action);
8832 }
8833 }
8834
8835 /**
8836 * <p>Causes the Runnable to execute on the next animation time step,
8837 * after the specified amount of time elapses.
8838 * The runnable will be run on the user interface thread.</p>
8839 *
8840 * <p>This method can be invoked from outside of the UI thread
8841 * only when this View is attached to a window.</p>
8842 *
8843 * @param action The Runnable that will be executed.
8844 * @param delayMillis The delay (in milliseconds) until the Runnable
8845 * will be executed.
8846 *
8847 * @hide
8848 */
8849 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
8850 final AttachInfo attachInfo = mAttachInfo;
8851 if (attachInfo != null) {
8852 attachInfo.mViewRootImpl.mChoreographer.postAnimationCallbackDelayed(
8853 action, null, delayMillis);
8854 } else {
8855 // Assume that post will succeed later
8856 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8857 }
8858 }
8859
8860 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008861 * <p>Removes the specified Runnable from the message queue.</p>
8862 *
8863 * <p>This method can be invoked from outside of the UI thread
8864 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 *
8866 * @param action The Runnable to remove from the message handling queue
8867 *
8868 * @return true if this view could ask the Handler to remove the Runnable,
8869 * false otherwise. When the returned value is true, the Runnable
8870 * may or may not have been actually removed from the message queue
8871 * (for instance, if the Runnable was not in the queue already.)
8872 */
8873 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08008874 if (action != null) {
8875 final AttachInfo attachInfo = mAttachInfo;
8876 if (attachInfo != null) {
8877 attachInfo.mHandler.removeCallbacks(action);
8878 attachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(action, null);
8879 } else {
8880 // Assume that post will succeed later
8881 ViewRootImpl.getRunQueue().removeCallbacks(action);
8882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008884 return true;
8885 }
8886
8887 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008888 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8889 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008890 *
Romain Guye63a4f32011-08-11 11:33:31 -07008891 * <p>This method can be invoked from outside of the UI thread
8892 * only when this View is attached to a window.</p>
8893 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008894 * @see #invalidate()
8895 */
8896 public void postInvalidate() {
8897 postInvalidateDelayed(0);
8898 }
8899
8900 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008901 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8902 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8903 *
8904 * <p>This method can be invoked from outside of the UI thread
8905 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 *
8907 * @param left The left coordinate of the rectangle to invalidate.
8908 * @param top The top coordinate of the rectangle to invalidate.
8909 * @param right The right coordinate of the rectangle to invalidate.
8910 * @param bottom The bottom coordinate of the rectangle to invalidate.
8911 *
8912 * @see #invalidate(int, int, int, int)
8913 * @see #invalidate(Rect)
8914 */
8915 public void postInvalidate(int left, int top, int right, int bottom) {
8916 postInvalidateDelayed(0, left, top, right, bottom);
8917 }
8918
8919 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008920 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8921 * loop. Waits for the specified amount of time.</p>
8922 *
8923 * <p>This method can be invoked from outside of the UI thread
8924 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008925 *
8926 * @param delayMilliseconds the duration in milliseconds to delay the
8927 * invalidation by
8928 */
8929 public void postInvalidateDelayed(long delayMilliseconds) {
8930 // We try only with the AttachInfo because there's no point in invalidating
8931 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008932 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008933 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008934 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008935 }
8936 }
8937
8938 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008939 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8940 * through the event loop. Waits for the specified amount of time.</p>
8941 *
8942 * <p>This method can be invoked from outside of the UI thread
8943 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008944 *
8945 * @param delayMilliseconds the duration in milliseconds to delay the
8946 * invalidation by
8947 * @param left The left coordinate of the rectangle to invalidate.
8948 * @param top The top coordinate of the rectangle to invalidate.
8949 * @param right The right coordinate of the rectangle to invalidate.
8950 * @param bottom The bottom coordinate of the rectangle to invalidate.
8951 */
8952 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8953 int right, int bottom) {
8954
8955 // We try only with the AttachInfo because there's no point in invalidating
8956 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008957 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008958 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008959 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8960 info.target = this;
8961 info.left = left;
8962 info.top = top;
8963 info.right = right;
8964 info.bottom = bottom;
8965
Jeff Browna175a5b2012-02-15 19:18:31 -08008966 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008967 }
8968 }
8969
8970 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08008971 * <p>Cause an invalidate to happen on the next animation time step, typically the
8972 * next display frame.</p>
8973 *
8974 * <p>This method can be invoked from outside of the UI thread
8975 * only when this View is attached to a window.</p>
8976 *
8977 * @hide
8978 */
8979 public void postInvalidateOnAnimation() {
8980 // We try only with the AttachInfo because there's no point in invalidating
8981 // if we are not attached to our window
8982 final AttachInfo attachInfo = mAttachInfo;
8983 if (attachInfo != null) {
8984 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
8985 }
8986 }
8987
8988 /**
8989 * <p>Cause an invalidate of the specified area to happen on the next animation
8990 * time step, typically the next display frame.</p>
8991 *
8992 * <p>This method can be invoked from outside of the UI thread
8993 * only when this View is attached to a window.</p>
8994 *
8995 * @param left The left coordinate of the rectangle to invalidate.
8996 * @param top The top coordinate of the rectangle to invalidate.
8997 * @param right The right coordinate of the rectangle to invalidate.
8998 * @param bottom The bottom coordinate of the rectangle to invalidate.
8999 *
9000 * @hide
9001 */
9002 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9003 // We try only with the AttachInfo because there's no point in invalidating
9004 // if we are not attached to our window
9005 final AttachInfo attachInfo = mAttachInfo;
9006 if (attachInfo != null) {
9007 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9008 info.target = this;
9009 info.left = left;
9010 info.top = top;
9011 info.right = right;
9012 info.bottom = bottom;
9013
9014 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9015 }
9016 }
9017
9018 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009019 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9020 * This event is sent at most once every
9021 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9022 */
9023 private void postSendViewScrolledAccessibilityEventCallback() {
9024 if (mSendViewScrolledAccessibilityEvent == null) {
9025 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9026 }
9027 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9028 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9029 postDelayed(mSendViewScrolledAccessibilityEvent,
9030 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9031 }
9032 }
9033
9034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009035 * Called by a parent to request that a child update its values for mScrollX
9036 * and mScrollY if necessary. This will typically be done if the child is
9037 * animating a scroll using a {@link android.widget.Scroller Scroller}
9038 * object.
9039 */
9040 public void computeScroll() {
9041 }
9042
9043 /**
9044 * <p>Indicate whether the horizontal edges are faded when the view is
9045 * scrolled horizontally.</p>
9046 *
9047 * @return true if the horizontal edges should are faded on scroll, false
9048 * otherwise
9049 *
9050 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009051 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009052 */
9053 public boolean isHorizontalFadingEdgeEnabled() {
9054 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9055 }
9056
9057 /**
9058 * <p>Define whether the horizontal edges should be faded when this view
9059 * is scrolled horizontally.</p>
9060 *
9061 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9062 * be faded when the view is scrolled
9063 * horizontally
9064 *
9065 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009066 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009067 */
9068 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9069 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9070 if (horizontalFadingEdgeEnabled) {
9071 initScrollCache();
9072 }
9073
9074 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9075 }
9076 }
9077
9078 /**
9079 * <p>Indicate whether the vertical edges are faded when the view is
9080 * scrolled horizontally.</p>
9081 *
9082 * @return true if the vertical edges should are faded on scroll, false
9083 * otherwise
9084 *
9085 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009086 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009087 */
9088 public boolean isVerticalFadingEdgeEnabled() {
9089 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9090 }
9091
9092 /**
9093 * <p>Define whether the vertical edges should be faded when this view
9094 * is scrolled vertically.</p>
9095 *
9096 * @param verticalFadingEdgeEnabled true if the vertical edges should
9097 * be faded when the view is scrolled
9098 * vertically
9099 *
9100 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009101 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 */
9103 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9104 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9105 if (verticalFadingEdgeEnabled) {
9106 initScrollCache();
9107 }
9108
9109 mViewFlags ^= FADING_EDGE_VERTICAL;
9110 }
9111 }
9112
9113 /**
9114 * Returns the strength, or intensity, of the top faded edge. The strength is
9115 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9116 * returns 0.0 or 1.0 but no value in between.
9117 *
9118 * Subclasses should override this method to provide a smoother fade transition
9119 * when scrolling occurs.
9120 *
9121 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9122 */
9123 protected float getTopFadingEdgeStrength() {
9124 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9125 }
9126
9127 /**
9128 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9129 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9130 * returns 0.0 or 1.0 but no value in between.
9131 *
9132 * Subclasses should override this method to provide a smoother fade transition
9133 * when scrolling occurs.
9134 *
9135 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9136 */
9137 protected float getBottomFadingEdgeStrength() {
9138 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9139 computeVerticalScrollRange() ? 1.0f : 0.0f;
9140 }
9141
9142 /**
9143 * Returns the strength, or intensity, of the left faded edge. The strength is
9144 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9145 * returns 0.0 or 1.0 but no value in between.
9146 *
9147 * Subclasses should override this method to provide a smoother fade transition
9148 * when scrolling occurs.
9149 *
9150 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9151 */
9152 protected float getLeftFadingEdgeStrength() {
9153 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9154 }
9155
9156 /**
9157 * Returns the strength, or intensity, of the right faded edge. The strength is
9158 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9159 * returns 0.0 or 1.0 but no value in between.
9160 *
9161 * Subclasses should override this method to provide a smoother fade transition
9162 * when scrolling occurs.
9163 *
9164 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9165 */
9166 protected float getRightFadingEdgeStrength() {
9167 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9168 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9169 }
9170
9171 /**
9172 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9173 * scrollbar is not drawn by default.</p>
9174 *
9175 * @return true if the horizontal scrollbar should be painted, false
9176 * otherwise
9177 *
9178 * @see #setHorizontalScrollBarEnabled(boolean)
9179 */
9180 public boolean isHorizontalScrollBarEnabled() {
9181 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9182 }
9183
9184 /**
9185 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9186 * scrollbar is not drawn by default.</p>
9187 *
9188 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9189 * be painted
9190 *
9191 * @see #isHorizontalScrollBarEnabled()
9192 */
9193 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9194 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9195 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009196 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009197 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009198 }
9199 }
9200
9201 /**
9202 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9203 * scrollbar is not drawn by default.</p>
9204 *
9205 * @return true if the vertical scrollbar should be painted, false
9206 * otherwise
9207 *
9208 * @see #setVerticalScrollBarEnabled(boolean)
9209 */
9210 public boolean isVerticalScrollBarEnabled() {
9211 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9212 }
9213
9214 /**
9215 * <p>Define whether the vertical scrollbar should be drawn or not. The
9216 * scrollbar is not drawn by default.</p>
9217 *
9218 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9219 * be painted
9220 *
9221 * @see #isVerticalScrollBarEnabled()
9222 */
9223 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9224 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9225 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009226 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009227 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 }
9229 }
9230
Adam Powell20232d02010-12-08 21:08:53 -08009231 /**
9232 * @hide
9233 */
9234 protected void recomputePadding() {
9235 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 }
Joe Malin32736f02011-01-19 16:14:20 -08009237
Mike Cleronfe81d382009-09-28 14:22:16 -07009238 /**
9239 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009240 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009241 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009242 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009243 */
9244 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9245 initScrollCache();
9246 final ScrollabilityCache scrollabilityCache = mScrollCache;
9247 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009248 if (fadeScrollbars) {
9249 scrollabilityCache.state = ScrollabilityCache.OFF;
9250 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009251 scrollabilityCache.state = ScrollabilityCache.ON;
9252 }
9253 }
Joe Malin32736f02011-01-19 16:14:20 -08009254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 /**
Joe Malin32736f02011-01-19 16:14:20 -08009256 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009257 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009258 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009259 * @return true if scrollbar fading is enabled
9260 */
9261 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009262 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009263 }
Joe Malin32736f02011-01-19 16:14:20 -08009264
Mike Cleron52f0a642009-09-28 18:21:37 -07009265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9267 * inset. When inset, they add to the padding of the view. And the scrollbars
9268 * can be drawn inside the padding area or on the edge of the view. For example,
9269 * if a view has a background drawable and you want to draw the scrollbars
9270 * inside the padding specified by the drawable, you can use
9271 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9272 * appear at the edge of the view, ignoring the padding, then you can use
9273 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9274 * @param style the style of the scrollbars. Should be one of
9275 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9276 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9277 * @see #SCROLLBARS_INSIDE_OVERLAY
9278 * @see #SCROLLBARS_INSIDE_INSET
9279 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9280 * @see #SCROLLBARS_OUTSIDE_INSET
9281 */
9282 public void setScrollBarStyle(int style) {
9283 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9284 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009285 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009286 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 }
9288 }
9289
9290 /**
9291 * <p>Returns the current scrollbar style.</p>
9292 * @return the current scrollbar style
9293 * @see #SCROLLBARS_INSIDE_OVERLAY
9294 * @see #SCROLLBARS_INSIDE_INSET
9295 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9296 * @see #SCROLLBARS_OUTSIDE_INSET
9297 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009298 @ViewDebug.ExportedProperty(mapping = {
9299 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9300 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9301 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9302 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9303 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009304 public int getScrollBarStyle() {
9305 return mViewFlags & SCROLLBARS_STYLE_MASK;
9306 }
9307
9308 /**
9309 * <p>Compute the horizontal range that the horizontal scrollbar
9310 * represents.</p>
9311 *
9312 * <p>The range is expressed in arbitrary units that must be the same as the
9313 * units used by {@link #computeHorizontalScrollExtent()} and
9314 * {@link #computeHorizontalScrollOffset()}.</p>
9315 *
9316 * <p>The default range is the drawing width of this view.</p>
9317 *
9318 * @return the total horizontal range represented by the horizontal
9319 * scrollbar
9320 *
9321 * @see #computeHorizontalScrollExtent()
9322 * @see #computeHorizontalScrollOffset()
9323 * @see android.widget.ScrollBarDrawable
9324 */
9325 protected int computeHorizontalScrollRange() {
9326 return getWidth();
9327 }
9328
9329 /**
9330 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9331 * within the horizontal range. This value is used to compute the position
9332 * of the thumb within the scrollbar's track.</p>
9333 *
9334 * <p>The range is expressed in arbitrary units that must be the same as the
9335 * units used by {@link #computeHorizontalScrollRange()} and
9336 * {@link #computeHorizontalScrollExtent()}.</p>
9337 *
9338 * <p>The default offset is the scroll offset of this view.</p>
9339 *
9340 * @return the horizontal offset of the scrollbar's thumb
9341 *
9342 * @see #computeHorizontalScrollRange()
9343 * @see #computeHorizontalScrollExtent()
9344 * @see android.widget.ScrollBarDrawable
9345 */
9346 protected int computeHorizontalScrollOffset() {
9347 return mScrollX;
9348 }
9349
9350 /**
9351 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9352 * within the horizontal range. This value is used to compute the length
9353 * of the thumb within the scrollbar's track.</p>
9354 *
9355 * <p>The range is expressed in arbitrary units that must be the same as the
9356 * units used by {@link #computeHorizontalScrollRange()} and
9357 * {@link #computeHorizontalScrollOffset()}.</p>
9358 *
9359 * <p>The default extent is the drawing width of this view.</p>
9360 *
9361 * @return the horizontal extent of the scrollbar's thumb
9362 *
9363 * @see #computeHorizontalScrollRange()
9364 * @see #computeHorizontalScrollOffset()
9365 * @see android.widget.ScrollBarDrawable
9366 */
9367 protected int computeHorizontalScrollExtent() {
9368 return getWidth();
9369 }
9370
9371 /**
9372 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9373 *
9374 * <p>The range is expressed in arbitrary units that must be the same as the
9375 * units used by {@link #computeVerticalScrollExtent()} and
9376 * {@link #computeVerticalScrollOffset()}.</p>
9377 *
9378 * @return the total vertical range represented by the vertical scrollbar
9379 *
9380 * <p>The default range is the drawing height of this view.</p>
9381 *
9382 * @see #computeVerticalScrollExtent()
9383 * @see #computeVerticalScrollOffset()
9384 * @see android.widget.ScrollBarDrawable
9385 */
9386 protected int computeVerticalScrollRange() {
9387 return getHeight();
9388 }
9389
9390 /**
9391 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9392 * within the horizontal range. This value is used to compute the position
9393 * of the thumb within the scrollbar's track.</p>
9394 *
9395 * <p>The range is expressed in arbitrary units that must be the same as the
9396 * units used by {@link #computeVerticalScrollRange()} and
9397 * {@link #computeVerticalScrollExtent()}.</p>
9398 *
9399 * <p>The default offset is the scroll offset of this view.</p>
9400 *
9401 * @return the vertical offset of the scrollbar's thumb
9402 *
9403 * @see #computeVerticalScrollRange()
9404 * @see #computeVerticalScrollExtent()
9405 * @see android.widget.ScrollBarDrawable
9406 */
9407 protected int computeVerticalScrollOffset() {
9408 return mScrollY;
9409 }
9410
9411 /**
9412 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9413 * within the vertical range. This value is used to compute the length
9414 * of the thumb within the scrollbar's track.</p>
9415 *
9416 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009417 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009418 * {@link #computeVerticalScrollOffset()}.</p>
9419 *
9420 * <p>The default extent is the drawing height of this view.</p>
9421 *
9422 * @return the vertical extent of the scrollbar's thumb
9423 *
9424 * @see #computeVerticalScrollRange()
9425 * @see #computeVerticalScrollOffset()
9426 * @see android.widget.ScrollBarDrawable
9427 */
9428 protected int computeVerticalScrollExtent() {
9429 return getHeight();
9430 }
9431
9432 /**
Adam Powell69159442011-06-13 17:53:06 -07009433 * Check if this view can be scrolled horizontally in a certain direction.
9434 *
9435 * @param direction Negative to check scrolling left, positive to check scrolling right.
9436 * @return true if this view can be scrolled in the specified direction, false otherwise.
9437 */
9438 public boolean canScrollHorizontally(int direction) {
9439 final int offset = computeHorizontalScrollOffset();
9440 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9441 if (range == 0) return false;
9442 if (direction < 0) {
9443 return offset > 0;
9444 } else {
9445 return offset < range - 1;
9446 }
9447 }
9448
9449 /**
9450 * Check if this view can be scrolled vertically in a certain direction.
9451 *
9452 * @param direction Negative to check scrolling up, positive to check scrolling down.
9453 * @return true if this view can be scrolled in the specified direction, false otherwise.
9454 */
9455 public boolean canScrollVertically(int direction) {
9456 final int offset = computeVerticalScrollOffset();
9457 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9458 if (range == 0) return false;
9459 if (direction < 0) {
9460 return offset > 0;
9461 } else {
9462 return offset < range - 1;
9463 }
9464 }
9465
9466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9468 * scrollbars are painted only if they have been awakened first.</p>
9469 *
9470 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009471 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009472 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009474 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009475 // scrollbars are drawn only when the animation is running
9476 final ScrollabilityCache cache = mScrollCache;
9477 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009478
Mike Cleronf116bf82009-09-27 19:14:12 -07009479 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009480
Mike Cleronf116bf82009-09-27 19:14:12 -07009481 if (state == ScrollabilityCache.OFF) {
9482 return;
9483 }
Joe Malin32736f02011-01-19 16:14:20 -08009484
Mike Cleronf116bf82009-09-27 19:14:12 -07009485 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009486
Mike Cleronf116bf82009-09-27 19:14:12 -07009487 if (state == ScrollabilityCache.FADING) {
9488 // We're fading -- get our fade interpolation
9489 if (cache.interpolatorValues == null) {
9490 cache.interpolatorValues = new float[1];
9491 }
Joe Malin32736f02011-01-19 16:14:20 -08009492
Mike Cleronf116bf82009-09-27 19:14:12 -07009493 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009494
Mike Cleronf116bf82009-09-27 19:14:12 -07009495 // Stops the animation if we're done
9496 if (cache.scrollBarInterpolator.timeToValues(values) ==
9497 Interpolator.Result.FREEZE_END) {
9498 cache.state = ScrollabilityCache.OFF;
9499 } else {
9500 cache.scrollBar.setAlpha(Math.round(values[0]));
9501 }
Joe Malin32736f02011-01-19 16:14:20 -08009502
9503 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009504 // drawing. We only want this when we're fading so that
9505 // we prevent excessive redraws
9506 invalidate = true;
9507 } else {
9508 // We're just on -- but we may have been fading before so
9509 // reset alpha
9510 cache.scrollBar.setAlpha(255);
9511 }
9512
Joe Malin32736f02011-01-19 16:14:20 -08009513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009514 final int viewFlags = mViewFlags;
9515
9516 final boolean drawHorizontalScrollBar =
9517 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9518 final boolean drawVerticalScrollBar =
9519 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9520 && !isVerticalScrollBarHidden();
9521
9522 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9523 final int width = mRight - mLeft;
9524 final int height = mBottom - mTop;
9525
9526 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009527
Mike Reede8853fc2009-09-04 14:01:48 -04009528 final int scrollX = mScrollX;
9529 final int scrollY = mScrollY;
9530 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9531
Mike Cleronf116bf82009-09-27 19:14:12 -07009532 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009534 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009535 int size = scrollBar.getSize(false);
9536 if (size <= 0) {
9537 size = cache.scrollBarSize;
9538 }
9539
Mike Cleronf116bf82009-09-27 19:14:12 -07009540 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009541 computeHorizontalScrollOffset(),
9542 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009543 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009544 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009545 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009546 left = scrollX + (mPaddingLeft & inside);
9547 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9548 bottom = top + size;
9549 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9550 if (invalidate) {
9551 invalidate(left, top, right, bottom);
9552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 }
9554
9555 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009556 int size = scrollBar.getSize(true);
9557 if (size <= 0) {
9558 size = cache.scrollBarSize;
9559 }
9560
Mike Reede8853fc2009-09-04 14:01:48 -04009561 scrollBar.setParameters(computeVerticalScrollRange(),
9562 computeVerticalScrollOffset(),
9563 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009564 switch (mVerticalScrollbarPosition) {
9565 default:
9566 case SCROLLBAR_POSITION_DEFAULT:
9567 case SCROLLBAR_POSITION_RIGHT:
9568 left = scrollX + width - size - (mUserPaddingRight & inside);
9569 break;
9570 case SCROLLBAR_POSITION_LEFT:
9571 left = scrollX + (mUserPaddingLeft & inside);
9572 break;
9573 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009574 top = scrollY + (mPaddingTop & inside);
9575 right = left + size;
9576 bottom = scrollY + height - (mUserPaddingBottom & inside);
9577 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9578 if (invalidate) {
9579 invalidate(left, top, right, bottom);
9580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009581 }
9582 }
9583 }
9584 }
Romain Guy8506ab42009-06-11 17:35:47 -07009585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009587 * 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 -08009588 * FastScroller is visible.
9589 * @return whether to temporarily hide the vertical scrollbar
9590 * @hide
9591 */
9592 protected boolean isVerticalScrollBarHidden() {
9593 return false;
9594 }
9595
9596 /**
9597 * <p>Draw the horizontal scrollbar if
9598 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9599 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009600 * @param canvas the canvas on which to draw the scrollbar
9601 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 *
9603 * @see #isHorizontalScrollBarEnabled()
9604 * @see #computeHorizontalScrollRange()
9605 * @see #computeHorizontalScrollExtent()
9606 * @see #computeHorizontalScrollOffset()
9607 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009608 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009609 */
Romain Guy8fb95422010-08-17 18:38:51 -07009610 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9611 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009612 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009613 scrollBar.draw(canvas);
9614 }
Mike Reede8853fc2009-09-04 14:01:48 -04009615
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009617 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9618 * returns true.</p>
9619 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009620 * @param canvas the canvas on which to draw the scrollbar
9621 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009622 *
9623 * @see #isVerticalScrollBarEnabled()
9624 * @see #computeVerticalScrollRange()
9625 * @see #computeVerticalScrollExtent()
9626 * @see #computeVerticalScrollOffset()
9627 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009628 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629 */
Romain Guy8fb95422010-08-17 18:38:51 -07009630 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9631 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009632 scrollBar.setBounds(l, t, r, b);
9633 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009634 }
9635
9636 /**
9637 * Implement this to do your drawing.
9638 *
9639 * @param canvas the canvas on which the background will be drawn
9640 */
9641 protected void onDraw(Canvas canvas) {
9642 }
9643
9644 /*
9645 * Caller is responsible for calling requestLayout if necessary.
9646 * (This allows addViewInLayout to not request a new layout.)
9647 */
9648 void assignParent(ViewParent parent) {
9649 if (mParent == null) {
9650 mParent = parent;
9651 } else if (parent == null) {
9652 mParent = null;
9653 } else {
9654 throw new RuntimeException("view " + this + " being added, but"
9655 + " it already has a parent");
9656 }
9657 }
9658
9659 /**
9660 * This is called when the view is attached to a window. At this point it
9661 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009662 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9663 * however it may be called any time before the first onDraw -- including
9664 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009665 *
9666 * @see #onDetachedFromWindow()
9667 */
9668 protected void onAttachedToWindow() {
9669 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9670 mParent.requestTransparentRegion(this);
9671 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009672 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9673 initialAwakenScrollBars();
9674 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9675 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009676 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009677 // Order is important here: LayoutDirection MUST be resolved before Padding
9678 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009679 resolveLayoutDirectionIfNeeded();
9680 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009681 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009682 if (isFocused()) {
9683 InputMethodManager imm = InputMethodManager.peekInstance();
9684 imm.focusIn(this);
9685 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009686 }
Cibu Johny86666632010-02-22 13:01:02 -08009687
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009688 /**
Romain Guybb9908b2012-03-08 11:14:07 -08009689 * @see #onScreenStateChanged(int)
9690 */
9691 void dispatchScreenStateChanged(int screenState) {
9692 onScreenStateChanged(screenState);
9693 }
9694
9695 /**
9696 * This method is called whenever the state of the screen this view is
9697 * attached to changes. A state change will usually occurs when the screen
9698 * turns on or off (whether it happens automatically or the user does it
9699 * manually.)
9700 *
9701 * @param screenState The new state of the screen. Can be either
9702 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
9703 */
9704 public void onScreenStateChanged(int screenState) {
9705 }
9706
9707 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009708 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9709 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009710 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009711 private void resolveLayoutDirectionIfNeeded() {
9712 // Do not resolve if it is not needed
9713 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9714
9715 // Clear any previous layout direction resolution
9716 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9717
9718 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009719 switch (getLayoutDirection()) {
9720 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009721 // We cannot do the resolution if there is no parent
9722 if (mParent == null) return;
9723
Cibu Johny86666632010-02-22 13:01:02 -08009724 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009725 if (mParent instanceof ViewGroup) {
9726 ViewGroup viewGroup = ((ViewGroup) mParent);
9727
9728 // Check if the parent view group can resolve
9729 if (! viewGroup.canResolveLayoutDirection()) {
9730 return;
9731 }
9732 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9733 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9734 }
Cibu Johny86666632010-02-22 13:01:02 -08009735 }
9736 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009737 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009738 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009739 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009740 case LAYOUT_DIRECTION_LOCALE:
9741 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009742 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009743 }
9744 break;
9745 default:
9746 // Nothing to do, LTR by default
9747 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009748
9749 // Set to resolved
9750 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009751 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009752 // Resolve padding
9753 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009754 }
9755
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009756 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009757 * Called when layout direction has been resolved.
9758 *
9759 * The default implementation does nothing.
9760 */
9761 public void onResolvedLayoutDirectionChanged() {
9762 }
9763
9764 /**
9765 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009766 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009767 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009768 // If the user specified the absolute padding (either with android:padding or
9769 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9770 // use the default padding or the padding from the background drawable
9771 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009772 int resolvedLayoutDirection = getResolvedLayoutDirection();
9773 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009774 case LAYOUT_DIRECTION_RTL:
9775 // Start user padding override Right user padding. Otherwise, if Right user
9776 // padding is not defined, use the default Right padding. If Right user padding
9777 // is defined, just use it.
9778 if (mUserPaddingStart >= 0) {
9779 mUserPaddingRight = mUserPaddingStart;
9780 } else if (mUserPaddingRight < 0) {
9781 mUserPaddingRight = mPaddingRight;
9782 }
9783 if (mUserPaddingEnd >= 0) {
9784 mUserPaddingLeft = mUserPaddingEnd;
9785 } else if (mUserPaddingLeft < 0) {
9786 mUserPaddingLeft = mPaddingLeft;
9787 }
9788 break;
9789 case LAYOUT_DIRECTION_LTR:
9790 default:
9791 // Start user padding override Left user padding. Otherwise, if Left user
9792 // padding is not defined, use the default left padding. If Left user padding
9793 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009794 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009795 mUserPaddingLeft = mUserPaddingStart;
9796 } else if (mUserPaddingLeft < 0) {
9797 mUserPaddingLeft = mPaddingLeft;
9798 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009799 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009800 mUserPaddingRight = mUserPaddingEnd;
9801 } else if (mUserPaddingRight < 0) {
9802 mUserPaddingRight = mPaddingRight;
9803 }
9804 }
9805
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009806 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9807
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009808 if(isPaddingRelative()) {
9809 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
9810 } else {
9811 recomputePadding();
9812 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009813 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009814 }
9815
9816 /**
9817 * Resolve padding depending on the layout direction. Subclasses that care about
9818 * padding resolution should override this method. The default implementation does
9819 * nothing.
9820 *
9821 * @param layoutDirection the direction of the layout
9822 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -08009823 * @see {@link #LAYOUT_DIRECTION_LTR}
9824 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009825 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009826 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009827 }
9828
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009829 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009830 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009831 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009832 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009833 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009834 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009835 switch (getLayoutDirection()) {
9836 case LAYOUT_DIRECTION_INHERIT:
9837 return (mParent != null);
9838 default:
9839 return true;
9840 }
9841 }
9842
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009843 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009844 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
9845 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009846 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009847 public void resetResolvedLayoutDirection() {
9848 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009849 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009850 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009851 // Reset also the text direction
9852 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009853 }
9854
9855 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009856 * Called during reset of resolved layout direction.
9857 *
9858 * Subclasses need to override this method to clear cached information that depends on the
9859 * resolved layout direction, or to inform child views that inherit their layout direction.
9860 *
9861 * The default implementation does nothing.
9862 */
9863 public void onResolvedLayoutDirectionReset() {
9864 }
9865
9866 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009867 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009868 *
9869 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009870 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009871 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009872 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -08009873 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009874 }
9875
9876 /**
9877 * This is called when the view is detached from a window. At this point it
9878 * no longer has a surface for drawing.
9879 *
9880 * @see #onAttachedToWindow()
9881 */
9882 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009883 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009884
Romain Guya440b002010-02-24 15:57:54 -08009885 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009886 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009887 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009888 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009890 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009891
Romain Guy6d7475d2011-07-27 16:28:21 -07009892 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009893
Chet Haasedaf98e92011-01-10 14:10:36 -08009894 if (mDisplayList != null) {
9895 mDisplayList.invalidate();
9896 }
9897
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009898 if (mAttachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009899 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009900 }
9901
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009902 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009903
9904 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009905 }
9906
9907 /**
9908 * @return The number of times this view has been attached to a window
9909 */
9910 protected int getWindowAttachCount() {
9911 return mWindowAttachCount;
9912 }
9913
9914 /**
9915 * Retrieve a unique token identifying the window this view is attached to.
9916 * @return Return the window's token for use in
9917 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9918 */
9919 public IBinder getWindowToken() {
9920 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9921 }
9922
9923 /**
9924 * Retrieve a unique token identifying the top-level "real" window of
9925 * the window that this view is attached to. That is, this is like
9926 * {@link #getWindowToken}, except if the window this view in is a panel
9927 * window (attached to another containing window), then the token of
9928 * the containing window is returned instead.
9929 *
9930 * @return Returns the associated window token, either
9931 * {@link #getWindowToken()} or the containing window's token.
9932 */
9933 public IBinder getApplicationWindowToken() {
9934 AttachInfo ai = mAttachInfo;
9935 if (ai != null) {
9936 IBinder appWindowToken = ai.mPanelParentWindowToken;
9937 if (appWindowToken == null) {
9938 appWindowToken = ai.mWindowToken;
9939 }
9940 return appWindowToken;
9941 }
9942 return null;
9943 }
9944
9945 /**
9946 * Retrieve private session object this view hierarchy is using to
9947 * communicate with the window manager.
9948 * @return the session object to communicate with the window manager
9949 */
9950 /*package*/ IWindowSession getWindowSession() {
9951 return mAttachInfo != null ? mAttachInfo.mSession : null;
9952 }
9953
9954 /**
9955 * @param info the {@link android.view.View.AttachInfo} to associated with
9956 * this view
9957 */
9958 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9959 //System.out.println("Attached! " + this);
9960 mAttachInfo = info;
9961 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009962 // We will need to evaluate the drawable state at least once.
9963 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009964 if (mFloatingTreeObserver != null) {
9965 info.mTreeObserver.merge(mFloatingTreeObserver);
9966 mFloatingTreeObserver = null;
9967 }
9968 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9969 mAttachInfo.mScrollContainers.add(this);
9970 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9971 }
9972 performCollectViewAttributes(visibility);
9973 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009974
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009975 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009976 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009977 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009978 if (listeners != null && listeners.size() > 0) {
9979 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9980 // perform the dispatching. The iterator is a safe guard against listeners that
9981 // could mutate the list by calling the various add/remove methods. This prevents
9982 // the array from being modified while we iterate it.
9983 for (OnAttachStateChangeListener listener : listeners) {
9984 listener.onViewAttachedToWindow(this);
9985 }
9986 }
9987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 int vis = info.mWindowVisibility;
9989 if (vis != GONE) {
9990 onWindowVisibilityChanged(vis);
9991 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009992 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9993 // If nobody has evaluated the drawable state yet, then do it now.
9994 refreshDrawableState();
9995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009996 }
9997
9998 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 AttachInfo info = mAttachInfo;
10000 if (info != null) {
10001 int vis = info.mWindowVisibility;
10002 if (vis != GONE) {
10003 onWindowVisibilityChanged(GONE);
10004 }
10005 }
10006
10007 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010008
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010009 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010010 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010011 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010012 if (listeners != null && listeners.size() > 0) {
10013 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10014 // perform the dispatching. The iterator is a safe guard against listeners that
10015 // could mutate the list by calling the various add/remove methods. This prevents
10016 // the array from being modified while we iterate it.
10017 for (OnAttachStateChangeListener listener : listeners) {
10018 listener.onViewDetachedFromWindow(this);
10019 }
10020 }
10021
Romain Guy01d5edc2011-01-28 11:28:53 -080010022 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010023 mAttachInfo.mScrollContainers.remove(this);
10024 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10025 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010027 mAttachInfo = null;
10028 }
10029
10030 /**
10031 * Store this view hierarchy's frozen state into the given container.
10032 *
10033 * @param container The SparseArray in which to save the view's state.
10034 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010035 * @see #restoreHierarchyState(android.util.SparseArray)
10036 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10037 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 */
10039 public void saveHierarchyState(SparseArray<Parcelable> container) {
10040 dispatchSaveInstanceState(container);
10041 }
10042
10043 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010044 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10045 * this view and its children. May be overridden to modify how freezing happens to a
10046 * 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 -080010047 *
10048 * @param container The SparseArray in which to save the view's state.
10049 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010050 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10051 * @see #saveHierarchyState(android.util.SparseArray)
10052 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010053 */
10054 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10055 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10056 mPrivateFlags &= ~SAVE_STATE_CALLED;
10057 Parcelable state = onSaveInstanceState();
10058 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10059 throw new IllegalStateException(
10060 "Derived class did not call super.onSaveInstanceState()");
10061 }
10062 if (state != null) {
10063 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10064 // + ": " + state);
10065 container.put(mID, state);
10066 }
10067 }
10068 }
10069
10070 /**
10071 * Hook allowing a view to generate a representation of its internal state
10072 * that can later be used to create a new instance with that same state.
10073 * This state should only contain information that is not persistent or can
10074 * not be reconstructed later. For example, you will never store your
10075 * current position on screen because that will be computed again when a
10076 * new instance of the view is placed in its view hierarchy.
10077 * <p>
10078 * Some examples of things you may store here: the current cursor position
10079 * in a text view (but usually not the text itself since that is stored in a
10080 * content provider or other persistent storage), the currently selected
10081 * item in a list view.
10082 *
10083 * @return Returns a Parcelable object containing the view's current dynamic
10084 * state, or null if there is nothing interesting to save. The
10085 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010086 * @see #onRestoreInstanceState(android.os.Parcelable)
10087 * @see #saveHierarchyState(android.util.SparseArray)
10088 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010089 * @see #setSaveEnabled(boolean)
10090 */
10091 protected Parcelable onSaveInstanceState() {
10092 mPrivateFlags |= SAVE_STATE_CALLED;
10093 return BaseSavedState.EMPTY_STATE;
10094 }
10095
10096 /**
10097 * Restore this view hierarchy's frozen state from the given container.
10098 *
10099 * @param container The SparseArray which holds previously frozen states.
10100 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010101 * @see #saveHierarchyState(android.util.SparseArray)
10102 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10103 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 */
10105 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10106 dispatchRestoreInstanceState(container);
10107 }
10108
10109 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010110 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10111 * state for this view and its children. May be overridden to modify how restoring
10112 * happens to a view's children; for example, some views may want to not store state
10113 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114 *
10115 * @param container The SparseArray which holds previously saved state.
10116 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010117 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10118 * @see #restoreHierarchyState(android.util.SparseArray)
10119 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010120 */
10121 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10122 if (mID != NO_ID) {
10123 Parcelable state = container.get(mID);
10124 if (state != null) {
10125 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10126 // + ": " + state);
10127 mPrivateFlags &= ~SAVE_STATE_CALLED;
10128 onRestoreInstanceState(state);
10129 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10130 throw new IllegalStateException(
10131 "Derived class did not call super.onRestoreInstanceState()");
10132 }
10133 }
10134 }
10135 }
10136
10137 /**
10138 * Hook allowing a view to re-apply a representation of its internal state that had previously
10139 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10140 * null state.
10141 *
10142 * @param state The frozen state that had previously been returned by
10143 * {@link #onSaveInstanceState}.
10144 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010145 * @see #onSaveInstanceState()
10146 * @see #restoreHierarchyState(android.util.SparseArray)
10147 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 */
10149 protected void onRestoreInstanceState(Parcelable state) {
10150 mPrivateFlags |= SAVE_STATE_CALLED;
10151 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010152 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10153 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010154 + "when two views of different type have the same id in the same hierarchy. "
10155 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010156 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010157 }
10158 }
10159
10160 /**
10161 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10162 *
10163 * @return the drawing start time in milliseconds
10164 */
10165 public long getDrawingTime() {
10166 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10167 }
10168
10169 /**
10170 * <p>Enables or disables the duplication of the parent's state into this view. When
10171 * duplication is enabled, this view gets its drawable state from its parent rather
10172 * than from its own internal properties.</p>
10173 *
10174 * <p>Note: in the current implementation, setting this property to true after the
10175 * view was added to a ViewGroup might have no effect at all. This property should
10176 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10177 *
10178 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10179 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010180 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010181 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10182 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010183 *
10184 * @param enabled True to enable duplication of the parent's drawable state, false
10185 * to disable it.
10186 *
10187 * @see #getDrawableState()
10188 * @see #isDuplicateParentStateEnabled()
10189 */
10190 public void setDuplicateParentStateEnabled(boolean enabled) {
10191 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10192 }
10193
10194 /**
10195 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10196 *
10197 * @return True if this view's drawable state is duplicated from the parent,
10198 * false otherwise
10199 *
10200 * @see #getDrawableState()
10201 * @see #setDuplicateParentStateEnabled(boolean)
10202 */
10203 public boolean isDuplicateParentStateEnabled() {
10204 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10205 }
10206
10207 /**
Romain Guy171c5922011-01-06 10:04:23 -080010208 * <p>Specifies the type of layer backing this view. The layer can be
10209 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10210 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010211 *
Romain Guy171c5922011-01-06 10:04:23 -080010212 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10213 * instance that controls how the layer is composed on screen. The following
10214 * properties of the paint are taken into account when composing the layer:</p>
10215 * <ul>
10216 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10217 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10218 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10219 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010220 *
Romain Guy171c5922011-01-06 10:04:23 -080010221 * <p>If this view has an alpha value set to < 1.0 by calling
10222 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10223 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10224 * equivalent to setting a hardware layer on this view and providing a paint with
10225 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010226 *
Romain Guy171c5922011-01-06 10:04:23 -080010227 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10228 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10229 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010230 *
Romain Guy171c5922011-01-06 10:04:23 -080010231 * @param layerType The ype of layer to use with this view, must be one of
10232 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10233 * {@link #LAYER_TYPE_HARDWARE}
10234 * @param paint The paint used to compose the layer. This argument is optional
10235 * and can be null. It is ignored when the layer type is
10236 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010237 *
10238 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010239 * @see #LAYER_TYPE_NONE
10240 * @see #LAYER_TYPE_SOFTWARE
10241 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010242 * @see #setAlpha(float)
10243 *
Romain Guy171c5922011-01-06 10:04:23 -080010244 * @attr ref android.R.styleable#View_layerType
10245 */
10246 public void setLayerType(int layerType, Paint paint) {
10247 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010248 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010249 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10250 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010251
Romain Guyd6cd5722011-01-17 14:42:41 -080010252 if (layerType == mLayerType) {
10253 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10254 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010255 invalidateParentCaches();
10256 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010257 }
10258 return;
10259 }
Romain Guy171c5922011-01-06 10:04:23 -080010260
10261 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010262 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010263 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010264 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010265 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010266 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010267 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010268 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010269 default:
10270 break;
Romain Guy171c5922011-01-06 10:04:23 -080010271 }
10272
10273 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010274 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10275 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10276 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010277
Romain Guy0fd89bf2011-01-26 15:41:30 -080010278 invalidateParentCaches();
10279 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010280 }
10281
10282 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010283 * Indicates whether this view has a static layer. A view with layer type
10284 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10285 * dynamic.
10286 */
10287 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010288 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010289 }
10290
10291 /**
Romain Guy171c5922011-01-06 10:04:23 -080010292 * Indicates what type of layer is currently associated with this view. By default
10293 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10294 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10295 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010296 *
Romain Guy171c5922011-01-06 10:04:23 -080010297 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10298 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010299 *
10300 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010301 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010302 * @see #LAYER_TYPE_NONE
10303 * @see #LAYER_TYPE_SOFTWARE
10304 * @see #LAYER_TYPE_HARDWARE
10305 */
10306 public int getLayerType() {
10307 return mLayerType;
10308 }
Joe Malin32736f02011-01-19 16:14:20 -080010309
Romain Guy6c319ca2011-01-11 14:29:25 -080010310 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010311 * Forces this view's layer to be created and this view to be rendered
10312 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10313 * invoking this method will have no effect.
10314 *
10315 * This method can for instance be used to render a view into its layer before
10316 * starting an animation. If this view is complex, rendering into the layer
10317 * before starting the animation will avoid skipping frames.
10318 *
10319 * @throws IllegalStateException If this view is not attached to a window
10320 *
10321 * @see #setLayerType(int, android.graphics.Paint)
10322 */
10323 public void buildLayer() {
10324 if (mLayerType == LAYER_TYPE_NONE) return;
10325
10326 if (mAttachInfo == null) {
10327 throw new IllegalStateException("This view must be attached to a window first");
10328 }
10329
10330 switch (mLayerType) {
10331 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010332 if (mAttachInfo.mHardwareRenderer != null &&
10333 mAttachInfo.mHardwareRenderer.isEnabled() &&
10334 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010335 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010336 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010337 break;
10338 case LAYER_TYPE_SOFTWARE:
10339 buildDrawingCache(true);
10340 break;
10341 }
10342 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010343
10344 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10345 void flushLayer() {
10346 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10347 mHardwareLayer.flush();
10348 }
10349 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010350
10351 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010352 * <p>Returns a hardware layer that can be used to draw this view again
10353 * without executing its draw method.</p>
10354 *
10355 * @return A HardwareLayer ready to render, or null if an error occurred.
10356 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010357 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010358 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10359 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010360 return null;
10361 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010362
10363 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010364
10365 final int width = mRight - mLeft;
10366 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010367
Romain Guy6c319ca2011-01-11 14:29:25 -080010368 if (width == 0 || height == 0) {
10369 return null;
10370 }
10371
10372 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10373 if (mHardwareLayer == null) {
10374 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10375 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010376 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010377 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10378 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010379 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010380 }
10381
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010382 // The layer is not valid if the underlying GPU resources cannot be allocated
10383 if (!mHardwareLayer.isValid()) {
10384 return null;
10385 }
10386
Michael Jurka7e52caf2012-03-06 15:57:06 -080010387 mHardwareLayer.redraw(getDisplayList(), mLocalDirtyRect);
10388 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010389 }
10390
10391 return mHardwareLayer;
10392 }
Romain Guy171c5922011-01-06 10:04:23 -080010393
Romain Guy589b0bb2011-10-10 13:57:47 -070010394 /**
10395 * Destroys this View's hardware layer if possible.
10396 *
10397 * @return True if the layer was destroyed, false otherwise.
10398 *
10399 * @see #setLayerType(int, android.graphics.Paint)
10400 * @see #LAYER_TYPE_HARDWARE
10401 */
Romain Guy65b345f2011-07-27 18:51:50 -070010402 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010403 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010404 AttachInfo info = mAttachInfo;
10405 if (info != null && info.mHardwareRenderer != null &&
10406 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10407 mHardwareLayer.destroy();
10408 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010409
Romain Guy9c4b79a2011-11-10 19:23:58 -080010410 invalidate(true);
10411 invalidateParentCaches();
10412 }
Romain Guy65b345f2011-07-27 18:51:50 -070010413 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010414 }
Romain Guy65b345f2011-07-27 18:51:50 -070010415 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010416 }
10417
Romain Guy171c5922011-01-06 10:04:23 -080010418 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010419 * Destroys all hardware rendering resources. This method is invoked
10420 * when the system needs to reclaim resources. Upon execution of this
10421 * method, you should free any OpenGL resources created by the view.
10422 *
10423 * Note: you <strong>must</strong> call
10424 * <code>super.destroyHardwareResources()</code> when overriding
10425 * this method.
10426 *
10427 * @hide
10428 */
10429 protected void destroyHardwareResources() {
10430 destroyLayer();
10431 }
10432
10433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10435 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10436 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10437 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10438 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10439 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010440 *
Romain Guy171c5922011-01-06 10:04:23 -080010441 * <p>Enabling the drawing cache is similar to
10442 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010443 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10444 * drawing cache has no effect on rendering because the system uses a different mechanism
10445 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10446 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10447 * for information on how to enable software and hardware layers.</p>
10448 *
10449 * <p>This API can be used to manually generate
10450 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10451 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010452 *
10453 * @param enabled true to enable the drawing cache, false otherwise
10454 *
10455 * @see #isDrawingCacheEnabled()
10456 * @see #getDrawingCache()
10457 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010458 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 */
10460 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010461 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10463 }
10464
10465 /**
10466 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10467 *
10468 * @return true if the drawing cache is enabled
10469 *
10470 * @see #setDrawingCacheEnabled(boolean)
10471 * @see #getDrawingCache()
10472 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010473 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010474 public boolean isDrawingCacheEnabled() {
10475 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10476 }
10477
10478 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010479 * Debugging utility which recursively outputs the dirty state of a view and its
10480 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010481 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010482 * @hide
10483 */
Romain Guy676b1732011-02-14 14:45:33 -080010484 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010485 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10486 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10487 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10488 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10489 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10490 if (clear) {
10491 mPrivateFlags &= clearMask;
10492 }
10493 if (this instanceof ViewGroup) {
10494 ViewGroup parent = (ViewGroup) this;
10495 final int count = parent.getChildCount();
10496 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010497 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010498 child.outputDirtyFlags(indent + " ", clear, clearMask);
10499 }
10500 }
10501 }
10502
10503 /**
10504 * This method is used by ViewGroup to cause its children to restore or recreate their
10505 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10506 * to recreate its own display list, which would happen if it went through the normal
10507 * draw/dispatchDraw mechanisms.
10508 *
10509 * @hide
10510 */
10511 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010512
10513 /**
10514 * A view that is not attached or hardware accelerated cannot create a display list.
10515 * This method checks these conditions and returns the appropriate result.
10516 *
10517 * @return true if view has the ability to create a display list, false otherwise.
10518 *
10519 * @hide
10520 */
10521 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010522 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010523 }
Joe Malin32736f02011-01-19 16:14:20 -080010524
Chet Haasedaf98e92011-01-10 14:10:36 -080010525 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010526 * @return The HardwareRenderer associated with that view or null if hardware rendering
10527 * is not supported or this this has not been attached to a window.
10528 *
10529 * @hide
10530 */
10531 public HardwareRenderer getHardwareRenderer() {
10532 if (mAttachInfo != null) {
10533 return mAttachInfo.mHardwareRenderer;
10534 }
10535 return null;
10536 }
10537
10538 /**
Romain Guyb051e892010-09-28 19:09:36 -070010539 * <p>Returns a display list that can be used to draw this view again
10540 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010541 *
Romain Guyb051e892010-09-28 19:09:36 -070010542 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010543 *
10544 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010545 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010546 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010547 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010548 return null;
10549 }
10550
Chet Haasedaf98e92011-01-10 14:10:36 -080010551 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10552 mDisplayList == null || !mDisplayList.isValid() ||
10553 mRecreateDisplayList)) {
10554 // Don't need to recreate the display list, just need to tell our
10555 // children to restore/recreate theirs
10556 if (mDisplayList != null && mDisplayList.isValid() &&
10557 !mRecreateDisplayList) {
10558 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10559 mPrivateFlags &= ~DIRTY_MASK;
10560 dispatchGetDisplayList();
10561
10562 return mDisplayList;
10563 }
10564
10565 // If we got here, we're recreating it. Mark it as such to ensure that
10566 // we copy in child display lists into ours in drawChild()
10567 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010568 if (mDisplayList == null) {
Romain Guy13631f32012-01-30 17:41:55 -080010569 final String name = getClass().getSimpleName();
10570 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
Chet Haasedaf98e92011-01-10 14:10:36 -080010571 // If we're creating a new display list, make sure our parent gets invalidated
10572 // since they will need to recreate their display list to account for this
10573 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010574 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010575 }
Romain Guyb051e892010-09-28 19:09:36 -070010576
10577 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010578 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010579 try {
10580 int width = mRight - mLeft;
10581 int height = mBottom - mTop;
10582
10583 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010584 // The dirty rect should always be null for a display list
10585 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010586
Chet Haasedaf98e92011-01-10 14:10:36 -080010587 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010588
10589 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010590 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010591 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010592 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010593
Romain Guyb051e892010-09-28 19:09:36 -070010594 // Fast path for layouts with no backgrounds
10595 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010596 dispatchDraw(canvas);
10597 } else {
10598 draw(canvas);
10599 }
Romain Guyb051e892010-09-28 19:09:36 -070010600 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010601 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010602 canvas.onPostDraw();
10603
10604 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010605 }
Chet Haase77785f92011-01-25 23:22:09 -080010606 } else {
10607 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10608 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010609 }
10610
10611 return mDisplayList;
10612 }
10613
10614 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010615 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010616 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010617 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010618 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010619 * @see #getDrawingCache(boolean)
10620 */
10621 public Bitmap getDrawingCache() {
10622 return getDrawingCache(false);
10623 }
10624
10625 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10627 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10628 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10629 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10630 * request the drawing cache by calling this method and draw it on screen if the
10631 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010632 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010633 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10634 * this method will create a bitmap of the same size as this view. Because this bitmap
10635 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10636 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10637 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10638 * size than the view. This implies that your application must be able to handle this
10639 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010640 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010641 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10642 * the current density of the screen when the application is in compatibility
10643 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010645 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010646 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010647 * @see #setDrawingCacheEnabled(boolean)
10648 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010649 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010650 * @see #destroyDrawingCache()
10651 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010652 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10654 return null;
10655 }
10656 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010657 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010658 }
Romain Guy02890fd2010-08-06 17:58:44 -070010659 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660 }
10661
10662 /**
10663 * <p>Frees the resources used by the drawing cache. If you call
10664 * {@link #buildDrawingCache()} manually without calling
10665 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10666 * should cleanup the cache with this method afterwards.</p>
10667 *
10668 * @see #setDrawingCacheEnabled(boolean)
10669 * @see #buildDrawingCache()
10670 * @see #getDrawingCache()
10671 */
10672 public void destroyDrawingCache() {
10673 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010674 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 mDrawingCache = null;
10676 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010677 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010678 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010679 mUnscaledDrawingCache = null;
10680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010681 }
10682
10683 /**
10684 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010685 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 * view will always be drawn on top of a solid color.
10687 *
10688 * @param color The background color to use for the drawing cache's bitmap
10689 *
10690 * @see #setDrawingCacheEnabled(boolean)
10691 * @see #buildDrawingCache()
10692 * @see #getDrawingCache()
10693 */
10694 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010695 if (color != mDrawingCacheBackgroundColor) {
10696 mDrawingCacheBackgroundColor = color;
10697 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 }
10700
10701 /**
10702 * @see #setDrawingCacheBackgroundColor(int)
10703 *
10704 * @return The background color to used for the drawing cache's bitmap
10705 */
10706 public int getDrawingCacheBackgroundColor() {
10707 return mDrawingCacheBackgroundColor;
10708 }
10709
10710 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010711 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010712 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010713 * @see #buildDrawingCache(boolean)
10714 */
10715 public void buildDrawingCache() {
10716 buildDrawingCache(false);
10717 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010718
Romain Guyfbd8f692009-06-26 14:51:58 -070010719 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010720 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10721 *
10722 * <p>If you call {@link #buildDrawingCache()} manually without calling
10723 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10724 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010725 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010726 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10727 * this method will create a bitmap of the same size as this view. Because this bitmap
10728 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10729 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10730 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10731 * size than the view. This implies that your application must be able to handle this
10732 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010733 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010734 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10735 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010736 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010737 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010738 *
10739 * @see #getDrawingCache()
10740 * @see #destroyDrawingCache()
10741 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010742 public void buildDrawingCache(boolean autoScale) {
10743 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010744 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010745 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746
10747 if (ViewDebug.TRACE_HIERARCHY) {
10748 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750
Romain Guy8506ab42009-06-11 17:35:47 -070010751 int width = mRight - mLeft;
10752 int height = mBottom - mTop;
10753
10754 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010755 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010756
Romain Guye1123222009-06-29 14:24:56 -070010757 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010758 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10759 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761
10762 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010763 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010764 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010765
10766 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010767 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010768 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10770 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010771 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010772 return;
10773 }
10774
10775 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010776 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777
10778 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010779 Bitmap.Config quality;
10780 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010781 // Never pick ARGB_4444 because it looks awful
10782 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010783 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10784 case DRAWING_CACHE_QUALITY_AUTO:
10785 quality = Bitmap.Config.ARGB_8888;
10786 break;
10787 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010788 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010789 break;
10790 case DRAWING_CACHE_QUALITY_HIGH:
10791 quality = Bitmap.Config.ARGB_8888;
10792 break;
10793 default:
10794 quality = Bitmap.Config.ARGB_8888;
10795 break;
10796 }
10797 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010798 // Optimization for translucent windows
10799 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010800 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 }
10802
10803 // Try to cleanup memory
10804 if (bitmap != null) bitmap.recycle();
10805
10806 try {
10807 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010808 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010809 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010810 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010811 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010812 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010813 }
Adam Powell26153a32010-11-08 15:22:27 -080010814 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010815 } catch (OutOfMemoryError e) {
10816 // If there is not enough memory to create the bitmap cache, just
10817 // ignore the issue as bitmap caches are not required to draw the
10818 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010819 if (autoScale) {
10820 mDrawingCache = null;
10821 } else {
10822 mUnscaledDrawingCache = null;
10823 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010824 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010825 return;
10826 }
10827
10828 clear = drawingCacheBackgroundColor != 0;
10829 }
10830
10831 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010832 if (attachInfo != null) {
10833 canvas = attachInfo.mCanvas;
10834 if (canvas == null) {
10835 canvas = new Canvas();
10836 }
10837 canvas.setBitmap(bitmap);
10838 // Temporarily clobber the cached Canvas in case one of our children
10839 // is also using a drawing cache. Without this, the children would
10840 // steal the canvas by attaching their own bitmap to it and bad, bad
10841 // thing would happen (invisible views, corrupted drawings, etc.)
10842 attachInfo.mCanvas = null;
10843 } else {
10844 // This case should hopefully never or seldom happen
10845 canvas = new Canvas(bitmap);
10846 }
10847
10848 if (clear) {
10849 bitmap.eraseColor(drawingCacheBackgroundColor);
10850 }
10851
10852 computeScroll();
10853 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010854
Romain Guye1123222009-06-29 14:24:56 -070010855 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010856 final float scale = attachInfo.mApplicationScale;
10857 canvas.scale(scale, scale);
10858 }
Joe Malin32736f02011-01-19 16:14:20 -080010859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860 canvas.translate(-mScrollX, -mScrollY);
10861
Romain Guy5bcdff42009-05-14 21:27:18 -070010862 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010863 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10864 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010865 mPrivateFlags |= DRAWING_CACHE_VALID;
10866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867
10868 // Fast path for layouts with no backgrounds
10869 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10870 if (ViewDebug.TRACE_HIERARCHY) {
10871 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10872 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010873 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010874 dispatchDraw(canvas);
10875 } else {
10876 draw(canvas);
10877 }
10878
10879 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010880 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010881
10882 if (attachInfo != null) {
10883 // Restore the cached Canvas for our siblings
10884 attachInfo.mCanvas = canvas;
10885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010886 }
10887 }
10888
10889 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010890 * Create a snapshot of the view into a bitmap. We should probably make
10891 * some form of this public, but should think about the API.
10892 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010893 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010894 int width = mRight - mLeft;
10895 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010896
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010897 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010898 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010899 width = (int) ((width * scale) + 0.5f);
10900 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010901
Romain Guy8c11e312009-09-14 15:15:30 -070010902 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010903 if (bitmap == null) {
10904 throw new OutOfMemoryError();
10905 }
10906
Romain Guyc529d8d2011-09-06 15:01:39 -070010907 Resources resources = getResources();
10908 if (resources != null) {
10909 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10910 }
Joe Malin32736f02011-01-19 16:14:20 -080010911
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010912 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010913 if (attachInfo != null) {
10914 canvas = attachInfo.mCanvas;
10915 if (canvas == null) {
10916 canvas = new Canvas();
10917 }
10918 canvas.setBitmap(bitmap);
10919 // Temporarily clobber the cached Canvas in case one of our children
10920 // is also using a drawing cache. Without this, the children would
10921 // steal the canvas by attaching their own bitmap to it and bad, bad
10922 // things would happen (invisible views, corrupted drawings, etc.)
10923 attachInfo.mCanvas = null;
10924 } else {
10925 // This case should hopefully never or seldom happen
10926 canvas = new Canvas(bitmap);
10927 }
10928
Romain Guy5bcdff42009-05-14 21:27:18 -070010929 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010930 bitmap.eraseColor(backgroundColor);
10931 }
10932
10933 computeScroll();
10934 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010935 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010936 canvas.translate(-mScrollX, -mScrollY);
10937
Romain Guy5bcdff42009-05-14 21:27:18 -070010938 // Temporarily remove the dirty mask
10939 int flags = mPrivateFlags;
10940 mPrivateFlags &= ~DIRTY_MASK;
10941
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010942 // Fast path for layouts with no backgrounds
10943 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10944 dispatchDraw(canvas);
10945 } else {
10946 draw(canvas);
10947 }
10948
Romain Guy5bcdff42009-05-14 21:27:18 -070010949 mPrivateFlags = flags;
10950
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010951 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010952 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010953
10954 if (attachInfo != null) {
10955 // Restore the cached Canvas for our siblings
10956 attachInfo.mCanvas = canvas;
10957 }
Romain Guy8506ab42009-06-11 17:35:47 -070010958
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010959 return bitmap;
10960 }
10961
10962 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010963 * Indicates whether this View is currently in edit mode. A View is usually
10964 * in edit mode when displayed within a developer tool. For instance, if
10965 * this View is being drawn by a visual user interface builder, this method
10966 * should return true.
10967 *
10968 * Subclasses should check the return value of this method to provide
10969 * different behaviors if their normal behavior might interfere with the
10970 * host environment. For instance: the class spawns a thread in its
10971 * constructor, the drawing code relies on device-specific features, etc.
10972 *
10973 * This method is usually checked in the drawing code of custom widgets.
10974 *
10975 * @return True if this View is in edit mode, false otherwise.
10976 */
10977 public boolean isInEditMode() {
10978 return false;
10979 }
10980
10981 /**
10982 * If the View draws content inside its padding and enables fading edges,
10983 * it needs to support padding offsets. Padding offsets are added to the
10984 * fading edges to extend the length of the fade so that it covers pixels
10985 * drawn inside the padding.
10986 *
10987 * Subclasses of this class should override this method if they need
10988 * to draw content inside the padding.
10989 *
10990 * @return True if padding offset must be applied, false otherwise.
10991 *
10992 * @see #getLeftPaddingOffset()
10993 * @see #getRightPaddingOffset()
10994 * @see #getTopPaddingOffset()
10995 * @see #getBottomPaddingOffset()
10996 *
10997 * @since CURRENT
10998 */
10999 protected boolean isPaddingOffsetRequired() {
11000 return false;
11001 }
11002
11003 /**
11004 * Amount by which to extend the left fading region. Called only when
11005 * {@link #isPaddingOffsetRequired()} returns true.
11006 *
11007 * @return The left padding offset in pixels.
11008 *
11009 * @see #isPaddingOffsetRequired()
11010 *
11011 * @since CURRENT
11012 */
11013 protected int getLeftPaddingOffset() {
11014 return 0;
11015 }
11016
11017 /**
11018 * Amount by which to extend the right fading region. Called only when
11019 * {@link #isPaddingOffsetRequired()} returns true.
11020 *
11021 * @return The right padding offset in pixels.
11022 *
11023 * @see #isPaddingOffsetRequired()
11024 *
11025 * @since CURRENT
11026 */
11027 protected int getRightPaddingOffset() {
11028 return 0;
11029 }
11030
11031 /**
11032 * Amount by which to extend the top fading region. Called only when
11033 * {@link #isPaddingOffsetRequired()} returns true.
11034 *
11035 * @return The top padding offset in pixels.
11036 *
11037 * @see #isPaddingOffsetRequired()
11038 *
11039 * @since CURRENT
11040 */
11041 protected int getTopPaddingOffset() {
11042 return 0;
11043 }
11044
11045 /**
11046 * Amount by which to extend the bottom fading region. Called only when
11047 * {@link #isPaddingOffsetRequired()} returns true.
11048 *
11049 * @return The bottom padding offset in pixels.
11050 *
11051 * @see #isPaddingOffsetRequired()
11052 *
11053 * @since CURRENT
11054 */
11055 protected int getBottomPaddingOffset() {
11056 return 0;
11057 }
11058
11059 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011060 * @hide
11061 * @param offsetRequired
11062 */
11063 protected int getFadeTop(boolean offsetRequired) {
11064 int top = mPaddingTop;
11065 if (offsetRequired) top += getTopPaddingOffset();
11066 return top;
11067 }
11068
11069 /**
11070 * @hide
11071 * @param offsetRequired
11072 */
11073 protected int getFadeHeight(boolean offsetRequired) {
11074 int padding = mPaddingTop;
11075 if (offsetRequired) padding += getTopPaddingOffset();
11076 return mBottom - mTop - mPaddingBottom - padding;
11077 }
11078
11079 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011080 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011081 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011082 *
Romain Guy2bffd262010-09-12 17:40:02 -070011083 * <p>Even if this method returns true, it does not mean that every call
11084 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11085 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011086 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011087 * window is hardware accelerated,
11088 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11089 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011090 *
Romain Guy2bffd262010-09-12 17:40:02 -070011091 * @return True if the view is attached to a window and the window is
11092 * hardware accelerated; false in any other case.
11093 */
11094 public boolean isHardwareAccelerated() {
11095 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11096 }
Joe Malin32736f02011-01-19 16:14:20 -080011097
Romain Guy2bffd262010-09-12 17:40:02 -070011098 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011099 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11100 * case of an active Animation being run on the view.
11101 */
11102 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11103 Animation a, boolean scalingRequired) {
11104 Transformation invalidationTransform;
11105 final int flags = parent.mGroupFlags;
11106 final boolean initialized = a.isInitialized();
11107 if (!initialized) {
11108 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11109 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11110 onAnimationStart();
11111 }
11112
11113 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11114 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11115 if (parent.mInvalidationTransformation == null) {
11116 parent.mInvalidationTransformation = new Transformation();
11117 }
11118 invalidationTransform = parent.mInvalidationTransformation;
11119 a.getTransformation(drawingTime, invalidationTransform, 1f);
11120 } else {
11121 invalidationTransform = parent.mChildTransformation;
11122 }
11123 if (more) {
11124 if (!a.willChangeBounds()) {
11125 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11126 parent.FLAG_OPTIMIZE_INVALIDATE) {
11127 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11128 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11129 // The child need to draw an animation, potentially offscreen, so
11130 // make sure we do not cancel invalidate requests
11131 parent.mPrivateFlags |= DRAW_ANIMATION;
11132 parent.invalidate(mLeft, mTop, mRight, mBottom);
11133 }
11134 } else {
11135 if (parent.mInvalidateRegion == null) {
11136 parent.mInvalidateRegion = new RectF();
11137 }
11138 final RectF region = parent.mInvalidateRegion;
11139 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11140 invalidationTransform);
11141
11142 // The child need to draw an animation, potentially offscreen, so
11143 // make sure we do not cancel invalidate requests
11144 parent.mPrivateFlags |= DRAW_ANIMATION;
11145
11146 final int left = mLeft + (int) region.left;
11147 final int top = mTop + (int) region.top;
11148 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11149 top + (int) (region.height() + .5f));
11150 }
11151 }
11152 return more;
11153 }
11154
11155 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011156 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11157 * This draw() method is an implementation detail and is not intended to be overridden or
11158 * to be called from anywhere else other than ViewGroup.drawChild().
11159 */
11160 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
11161 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011162 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011163 final int flags = parent.mGroupFlags;
11164
11165 if ((flags & parent.FLAG_CLEAR_TRANSFORMATION) == parent.FLAG_CLEAR_TRANSFORMATION) {
11166 parent.mChildTransformation.clear();
11167 parent.mGroupFlags &= ~parent.FLAG_CLEAR_TRANSFORMATION;
11168 }
11169
11170 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011171 boolean concatMatrix = false;
11172
11173 boolean scalingRequired = false;
11174 boolean caching;
11175 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11176
11177 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
11178 if ((flags & parent.FLAG_CHILDREN_DRAWN_WITH_CACHE) == parent.FLAG_CHILDREN_DRAWN_WITH_CACHE ||
11179 (flags & parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) == parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) {
11180 caching = true;
11181 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11182 } else {
11183 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11184 }
11185
Chet Haasebcca79a2012-02-14 08:45:14 -080011186 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011187 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011188 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011189 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011190 transformToApply = parent.mChildTransformation;
Chet Haase64a48c12012-02-13 16:33:29 -080011191 } else if ((flags & parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
11192 parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011193 final boolean hasTransform =
11194 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011195 if (hasTransform) {
11196 final int transformType = parent.mChildTransformation.getTransformationType();
11197 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11198 parent.mChildTransformation : null;
11199 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11200 }
11201 }
11202
11203 concatMatrix |= !childHasIdentityMatrix;
11204
11205 // Sets the flag as early as possible to allow draw() implementations
11206 // to call invalidate() successfully when doing animations
11207 mPrivateFlags |= DRAWN;
11208
Chet Haasebcca79a2012-02-14 08:45:14 -080011209 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011210 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11211 return more;
11212 }
11213
11214 if (hardwareAccelerated) {
11215 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11216 // retain the flag's value temporarily in the mRecreateDisplayList flag
11217 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11218 mPrivateFlags &= ~INVALIDATED;
11219 }
11220
11221 computeScroll();
11222
11223 final int sx = mScrollX;
11224 final int sy = mScrollY;
11225
11226 DisplayList displayList = null;
11227 Bitmap cache = null;
11228 boolean hasDisplayList = false;
11229 if (caching) {
11230 if (!hardwareAccelerated) {
11231 if (layerType != LAYER_TYPE_NONE) {
11232 layerType = LAYER_TYPE_SOFTWARE;
11233 buildDrawingCache(true);
11234 }
11235 cache = getDrawingCache(true);
11236 } else {
11237 switch (layerType) {
11238 case LAYER_TYPE_SOFTWARE:
11239 buildDrawingCache(true);
11240 cache = getDrawingCache(true);
11241 break;
11242 case LAYER_TYPE_NONE:
11243 // Delay getting the display list until animation-driven alpha values are
11244 // set up and possibly passed on to the view
11245 hasDisplayList = canHaveDisplayList();
11246 break;
11247 }
11248 }
11249 }
11250
11251 final boolean hasNoCache = cache == null || hasDisplayList;
11252 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11253 layerType != LAYER_TYPE_HARDWARE;
11254
11255 final int restoreTo = canvas.save();
11256 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011257 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011258 } else {
Chet Haasebcca79a2012-02-14 08:45:14 -080011259 canvas.translate(mLeft, mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011260 if (scalingRequired) {
11261 // mAttachInfo cannot be null, otherwise scalingRequired == false
11262 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11263 canvas.scale(scale, scale);
11264 }
11265 }
11266
11267 float alpha = getAlpha();
11268 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11269 if (transformToApply != null || !childHasIdentityMatrix) {
11270 int transX = 0;
11271 int transY = 0;
11272
11273 if (offsetForScroll) {
11274 transX = -sx;
11275 transY = -sy;
11276 }
11277
11278 if (transformToApply != null) {
11279 if (concatMatrix) {
11280 // Undo the scroll translation, apply the transformation matrix,
11281 // then redo the scroll translate to get the correct result.
11282 canvas.translate(-transX, -transY);
11283 canvas.concat(transformToApply.getMatrix());
11284 canvas.translate(transX, transY);
11285 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11286 }
11287
11288 float transformAlpha = transformToApply.getAlpha();
11289 if (transformAlpha < 1.0f) {
11290 alpha *= transformToApply.getAlpha();
11291 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11292 }
11293 }
11294
11295 if (!childHasIdentityMatrix) {
11296 canvas.translate(-transX, -transY);
11297 canvas.concat(getMatrix());
11298 canvas.translate(transX, transY);
11299 }
11300 }
11301
11302 if (alpha < 1.0f) {
11303 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11304 if (hasNoCache) {
11305 final int multipliedAlpha = (int) (255 * alpha);
11306 if (!onSetAlpha(multipliedAlpha)) {
11307 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11308 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN ||
11309 layerType != LAYER_TYPE_NONE) {
11310 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11311 }
11312 if (layerType == LAYER_TYPE_NONE) {
11313 final int scrollX = hasDisplayList ? 0 : sx;
11314 final int scrollY = hasDisplayList ? 0 : sy;
Chet Haasebcca79a2012-02-14 08:45:14 -080011315 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11316 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011317 }
11318 } else {
11319 // Alpha is handled by the child directly, clobber the layer's alpha
11320 mPrivateFlags |= ALPHA_SET;
11321 }
11322 }
11323 }
11324 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11325 onSetAlpha(255);
11326 mPrivateFlags &= ~ALPHA_SET;
11327 }
11328
11329 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN) {
11330 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011331 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011332 } else {
11333 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011334 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011335 } else {
11336 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11337 }
11338 }
11339 }
11340
11341 if (hasDisplayList) {
11342 displayList = getDisplayList();
11343 if (!displayList.isValid()) {
11344 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11345 // to getDisplayList(), the display list will be marked invalid and we should not
11346 // try to use it again.
11347 displayList = null;
11348 hasDisplayList = false;
11349 }
11350 }
11351
11352 if (hasNoCache) {
11353 boolean layerRendered = false;
11354 if (layerType == LAYER_TYPE_HARDWARE) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011355 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080011356 if (layer != null && layer.isValid()) {
11357 mLayerPaint.setAlpha((int) (alpha * 255));
11358 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11359 layerRendered = true;
11360 } else {
11361 final int scrollX = hasDisplayList ? 0 : sx;
11362 final int scrollY = hasDisplayList ? 0 : sy;
11363 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011364 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011365 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11366 }
11367 }
11368
11369 if (!layerRendered) {
11370 if (!hasDisplayList) {
11371 // Fast path for layouts with no backgrounds
11372 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11373 if (ViewDebug.TRACE_HIERARCHY) {
11374 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11375 }
11376 mPrivateFlags &= ~DIRTY_MASK;
11377 dispatchDraw(canvas);
11378 } else {
11379 draw(canvas);
11380 }
11381 } else {
11382 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011383 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011384 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011385 }
11386 }
11387 } else if (cache != null) {
11388 mPrivateFlags &= ~DIRTY_MASK;
11389 Paint cachePaint;
11390
11391 if (layerType == LAYER_TYPE_NONE) {
11392 cachePaint = parent.mCachePaint;
11393 if (cachePaint == null) {
11394 cachePaint = new Paint();
11395 cachePaint.setDither(false);
11396 parent.mCachePaint = cachePaint;
11397 }
11398 if (alpha < 1.0f) {
11399 cachePaint.setAlpha((int) (alpha * 255));
11400 parent.mGroupFlags |= parent.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haasebcca79a2012-02-14 08:45:14 -080011401 } else if ((flags & parent.FLAG_ALPHA_LOWER_THAN_ONE) ==
11402 parent.FLAG_ALPHA_LOWER_THAN_ONE) {
Chet Haase64a48c12012-02-13 16:33:29 -080011403 cachePaint.setAlpha(255);
11404 parent.mGroupFlags &= ~parent.FLAG_ALPHA_LOWER_THAN_ONE;
11405 }
11406 } else {
11407 cachePaint = mLayerPaint;
11408 cachePaint.setAlpha((int) (alpha * 255));
11409 }
11410 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11411 }
11412
11413 canvas.restoreToCount(restoreTo);
11414
11415 if (a != null && !more) {
11416 if (!hardwareAccelerated && !a.getFillAfter()) {
11417 onSetAlpha(255);
11418 }
11419 parent.finishAnimatingView(this, a);
11420 }
11421
11422 if (more && hardwareAccelerated) {
11423 // invalidation is the trigger to recreate display lists, so if we're using
11424 // display lists to render, force an invalidate to allow the animation to
11425 // continue drawing another frame
11426 parent.invalidate(true);
11427 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11428 // alpha animations should cause the child to recreate its display list
11429 invalidate(true);
11430 }
11431 }
11432
11433 mRecreateDisplayList = false;
11434
11435 return more;
11436 }
11437
11438 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439 * Manually render this view (and all of its children) to the given Canvas.
11440 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011441 * called. When implementing a view, implement
11442 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11443 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011444 *
11445 * @param canvas The Canvas to which the View is rendered.
11446 */
11447 public void draw(Canvas canvas) {
11448 if (ViewDebug.TRACE_HIERARCHY) {
11449 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11450 }
11451
Romain Guy5bcdff42009-05-14 21:27:18 -070011452 final int privateFlags = mPrivateFlags;
11453 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11454 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11455 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011457 /*
11458 * Draw traversal performs several drawing steps which must be executed
11459 * in the appropriate order:
11460 *
11461 * 1. Draw the background
11462 * 2. If necessary, save the canvas' layers to prepare for fading
11463 * 3. Draw view's content
11464 * 4. Draw children
11465 * 5. If necessary, draw the fading edges and restore layers
11466 * 6. Draw decorations (scrollbars for instance)
11467 */
11468
11469 // Step 1, draw the background, if needed
11470 int saveCount;
11471
Romain Guy24443ea2009-05-11 11:56:30 -070011472 if (!dirtyOpaque) {
11473 final Drawable background = mBGDrawable;
11474 if (background != null) {
11475 final int scrollX = mScrollX;
11476 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011477
Romain Guy24443ea2009-05-11 11:56:30 -070011478 if (mBackgroundSizeChanged) {
11479 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11480 mBackgroundSizeChanged = false;
11481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011482
Romain Guy24443ea2009-05-11 11:56:30 -070011483 if ((scrollX | scrollY) == 0) {
11484 background.draw(canvas);
11485 } else {
11486 canvas.translate(scrollX, scrollY);
11487 background.draw(canvas);
11488 canvas.translate(-scrollX, -scrollY);
11489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011490 }
11491 }
11492
11493 // skip step 2 & 5 if possible (common case)
11494 final int viewFlags = mViewFlags;
11495 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11496 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11497 if (!verticalEdges && !horizontalEdges) {
11498 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011499 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011500
11501 // Step 4, draw the children
11502 dispatchDraw(canvas);
11503
11504 // Step 6, draw decorations (scrollbars)
11505 onDrawScrollBars(canvas);
11506
11507 // we're done...
11508 return;
11509 }
11510
11511 /*
11512 * Here we do the full fledged routine...
11513 * (this is an uncommon case where speed matters less,
11514 * this is why we repeat some of the tests that have been
11515 * done above)
11516 */
11517
11518 boolean drawTop = false;
11519 boolean drawBottom = false;
11520 boolean drawLeft = false;
11521 boolean drawRight = false;
11522
11523 float topFadeStrength = 0.0f;
11524 float bottomFadeStrength = 0.0f;
11525 float leftFadeStrength = 0.0f;
11526 float rightFadeStrength = 0.0f;
11527
11528 // Step 2, save the canvas' layers
11529 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011530
11531 final boolean offsetRequired = isPaddingOffsetRequired();
11532 if (offsetRequired) {
11533 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011534 }
11535
11536 int left = mScrollX + paddingLeft;
11537 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011538 int top = mScrollY + getFadeTop(offsetRequired);
11539 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011540
11541 if (offsetRequired) {
11542 right += getRightPaddingOffset();
11543 bottom += getBottomPaddingOffset();
11544 }
11545
11546 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011547 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11548 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011549
11550 // clip the fade length if top and bottom fades overlap
11551 // overlapping fades produce odd-looking artifacts
11552 if (verticalEdges && (top + length > bottom - length)) {
11553 length = (bottom - top) / 2;
11554 }
11555
11556 // also clip horizontal fades if necessary
11557 if (horizontalEdges && (left + length > right - length)) {
11558 length = (right - left) / 2;
11559 }
11560
11561 if (verticalEdges) {
11562 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011563 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011564 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011565 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011566 }
11567
11568 if (horizontalEdges) {
11569 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011570 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011571 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011572 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011573 }
11574
11575 saveCount = canvas.getSaveCount();
11576
11577 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011578 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011579 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11580
11581 if (drawTop) {
11582 canvas.saveLayer(left, top, right, top + length, null, flags);
11583 }
11584
11585 if (drawBottom) {
11586 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11587 }
11588
11589 if (drawLeft) {
11590 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11591 }
11592
11593 if (drawRight) {
11594 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11595 }
11596 } else {
11597 scrollabilityCache.setFadeColor(solidColor);
11598 }
11599
11600 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011601 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011602
11603 // Step 4, draw the children
11604 dispatchDraw(canvas);
11605
11606 // Step 5, draw the fade effect and restore layers
11607 final Paint p = scrollabilityCache.paint;
11608 final Matrix matrix = scrollabilityCache.matrix;
11609 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011610
11611 if (drawTop) {
11612 matrix.setScale(1, fadeHeight * topFadeStrength);
11613 matrix.postTranslate(left, top);
11614 fade.setLocalMatrix(matrix);
11615 canvas.drawRect(left, top, right, top + length, p);
11616 }
11617
11618 if (drawBottom) {
11619 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11620 matrix.postRotate(180);
11621 matrix.postTranslate(left, bottom);
11622 fade.setLocalMatrix(matrix);
11623 canvas.drawRect(left, bottom - length, right, bottom, p);
11624 }
11625
11626 if (drawLeft) {
11627 matrix.setScale(1, fadeHeight * leftFadeStrength);
11628 matrix.postRotate(-90);
11629 matrix.postTranslate(left, top);
11630 fade.setLocalMatrix(matrix);
11631 canvas.drawRect(left, top, left + length, bottom, p);
11632 }
11633
11634 if (drawRight) {
11635 matrix.setScale(1, fadeHeight * rightFadeStrength);
11636 matrix.postRotate(90);
11637 matrix.postTranslate(right, top);
11638 fade.setLocalMatrix(matrix);
11639 canvas.drawRect(right - length, top, right, bottom, p);
11640 }
11641
11642 canvas.restoreToCount(saveCount);
11643
11644 // Step 6, draw decorations (scrollbars)
11645 onDrawScrollBars(canvas);
11646 }
11647
11648 /**
11649 * Override this if your view is known to always be drawn on top of a solid color background,
11650 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11651 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11652 * should be set to 0xFF.
11653 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011654 * @see #setVerticalFadingEdgeEnabled(boolean)
11655 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011656 *
11657 * @return The known solid color background for this view, or 0 if the color may vary
11658 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011659 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011660 public int getSolidColor() {
11661 return 0;
11662 }
11663
11664 /**
11665 * Build a human readable string representation of the specified view flags.
11666 *
11667 * @param flags the view flags to convert to a string
11668 * @return a String representing the supplied flags
11669 */
11670 private static String printFlags(int flags) {
11671 String output = "";
11672 int numFlags = 0;
11673 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11674 output += "TAKES_FOCUS";
11675 numFlags++;
11676 }
11677
11678 switch (flags & VISIBILITY_MASK) {
11679 case INVISIBLE:
11680 if (numFlags > 0) {
11681 output += " ";
11682 }
11683 output += "INVISIBLE";
11684 // USELESS HERE numFlags++;
11685 break;
11686 case GONE:
11687 if (numFlags > 0) {
11688 output += " ";
11689 }
11690 output += "GONE";
11691 // USELESS HERE numFlags++;
11692 break;
11693 default:
11694 break;
11695 }
11696 return output;
11697 }
11698
11699 /**
11700 * Build a human readable string representation of the specified private
11701 * view flags.
11702 *
11703 * @param privateFlags the private view flags to convert to a string
11704 * @return a String representing the supplied flags
11705 */
11706 private static String printPrivateFlags(int privateFlags) {
11707 String output = "";
11708 int numFlags = 0;
11709
11710 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11711 output += "WANTS_FOCUS";
11712 numFlags++;
11713 }
11714
11715 if ((privateFlags & FOCUSED) == FOCUSED) {
11716 if (numFlags > 0) {
11717 output += " ";
11718 }
11719 output += "FOCUSED";
11720 numFlags++;
11721 }
11722
11723 if ((privateFlags & SELECTED) == SELECTED) {
11724 if (numFlags > 0) {
11725 output += " ";
11726 }
11727 output += "SELECTED";
11728 numFlags++;
11729 }
11730
11731 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11732 if (numFlags > 0) {
11733 output += " ";
11734 }
11735 output += "IS_ROOT_NAMESPACE";
11736 numFlags++;
11737 }
11738
11739 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11740 if (numFlags > 0) {
11741 output += " ";
11742 }
11743 output += "HAS_BOUNDS";
11744 numFlags++;
11745 }
11746
11747 if ((privateFlags & DRAWN) == DRAWN) {
11748 if (numFlags > 0) {
11749 output += " ";
11750 }
11751 output += "DRAWN";
11752 // USELESS HERE numFlags++;
11753 }
11754 return output;
11755 }
11756
11757 /**
11758 * <p>Indicates whether or not this view's layout will be requested during
11759 * the next hierarchy layout pass.</p>
11760 *
11761 * @return true if the layout will be forced during next layout pass
11762 */
11763 public boolean isLayoutRequested() {
11764 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11765 }
11766
11767 /**
11768 * Assign a size and position to a view and all of its
11769 * descendants
11770 *
11771 * <p>This is the second phase of the layout mechanism.
11772 * (The first is measuring). In this phase, each parent calls
11773 * layout on all of its children to position them.
11774 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011775 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011776 *
Chet Haase9c087442011-01-12 16:20:16 -080011777 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 * Derived classes with children should override
11779 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011780 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011781 *
11782 * @param l Left position, relative to parent
11783 * @param t Top position, relative to parent
11784 * @param r Right position, relative to parent
11785 * @param b Bottom position, relative to parent
11786 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011787 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011788 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011789 int oldL = mLeft;
11790 int oldT = mTop;
11791 int oldB = mBottom;
11792 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011793 boolean changed = setFrame(l, t, r, b);
11794 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11795 if (ViewDebug.TRACE_HIERARCHY) {
11796 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11797 }
11798
11799 onLayout(changed, l, t, r, b);
11800 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011801
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011802 ListenerInfo li = mListenerInfo;
11803 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011804 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011805 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011806 int numListeners = listenersCopy.size();
11807 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011808 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011809 }
11810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011811 }
11812 mPrivateFlags &= ~FORCE_LAYOUT;
11813 }
11814
11815 /**
11816 * Called from layout when this view should
11817 * assign a size and position to each of its children.
11818 *
11819 * Derived classes with children should override
11820 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011821 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011822 * @param changed This is a new size or position for this view
11823 * @param left Left position, relative to parent
11824 * @param top Top position, relative to parent
11825 * @param right Right position, relative to parent
11826 * @param bottom Bottom position, relative to parent
11827 */
11828 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11829 }
11830
11831 /**
11832 * Assign a size and position to this view.
11833 *
11834 * This is called from layout.
11835 *
11836 * @param left Left position, relative to parent
11837 * @param top Top position, relative to parent
11838 * @param right Right position, relative to parent
11839 * @param bottom Bottom position, relative to parent
11840 * @return true if the new size and position are different than the
11841 * previous ones
11842 * {@hide}
11843 */
11844 protected boolean setFrame(int left, int top, int right, int bottom) {
11845 boolean changed = false;
11846
11847 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011848 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011849 + right + "," + bottom + ")");
11850 }
11851
11852 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11853 changed = true;
11854
11855 // Remember our drawn bit
11856 int drawn = mPrivateFlags & DRAWN;
11857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011858 int oldWidth = mRight - mLeft;
11859 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011860 int newWidth = right - left;
11861 int newHeight = bottom - top;
11862 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11863
11864 // Invalidate our old position
11865 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011866
11867 mLeft = left;
11868 mTop = top;
11869 mRight = right;
11870 mBottom = bottom;
11871
11872 mPrivateFlags |= HAS_BOUNDS;
11873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011874
Chet Haase75755e22011-07-18 17:48:25 -070011875 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011876 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11877 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011878 if (mTransformationInfo != null) {
11879 mTransformationInfo.mMatrixDirty = true;
11880 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011882 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11883 }
11884
11885 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11886 // If we are visible, force the DRAWN bit to on so that
11887 // this invalidate will go through (at least to our parent).
11888 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011889 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011890 // the DRAWN bit.
11891 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011892 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011893 // parent display list may need to be recreated based on a change in the bounds
11894 // of any child
11895 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011896 }
11897
11898 // Reset drawn bit to original value (invalidate turns it off)
11899 mPrivateFlags |= drawn;
11900
11901 mBackgroundSizeChanged = true;
11902 }
11903 return changed;
11904 }
11905
11906 /**
11907 * Finalize inflating a view from XML. This is called as the last phase
11908 * of inflation, after all child views have been added.
11909 *
11910 * <p>Even if the subclass overrides onFinishInflate, they should always be
11911 * sure to call the super method, so that we get called.
11912 */
11913 protected void onFinishInflate() {
11914 }
11915
11916 /**
11917 * Returns the resources associated with this view.
11918 *
11919 * @return Resources object.
11920 */
11921 public Resources getResources() {
11922 return mResources;
11923 }
11924
11925 /**
11926 * Invalidates the specified Drawable.
11927 *
11928 * @param drawable the drawable to invalidate
11929 */
11930 public void invalidateDrawable(Drawable drawable) {
11931 if (verifyDrawable(drawable)) {
11932 final Rect dirty = drawable.getBounds();
11933 final int scrollX = mScrollX;
11934 final int scrollY = mScrollY;
11935
11936 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11937 dirty.right + scrollX, dirty.bottom + scrollY);
11938 }
11939 }
11940
11941 /**
11942 * Schedules an action on a drawable to occur at a specified time.
11943 *
11944 * @param who the recipient of the action
11945 * @param what the action to run on the drawable
11946 * @param when the time at which the action must occur. Uses the
11947 * {@link SystemClock#uptimeMillis} timebase.
11948 */
11949 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011950 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011951 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080011952 if (mAttachInfo != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011953 mAttachInfo.mViewRootImpl.mChoreographer.postAnimationCallbackDelayed(
11954 what, who, Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080011955 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011956 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080011957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011958 }
11959 }
11960
11961 /**
11962 * Cancels a scheduled action on a drawable.
11963 *
11964 * @param who the recipient of the action
11965 * @param what the action to cancel
11966 */
11967 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011968 if (verifyDrawable(who) && what != null) {
11969 if (mAttachInfo != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011970 mAttachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(what, who);
Adam Powell37419d72011-11-10 11:32:09 -080011971 } else {
11972 ViewRootImpl.getRunQueue().removeCallbacks(what);
11973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011974 }
11975 }
11976
11977 /**
11978 * Unschedule any events associated with the given Drawable. This can be
11979 * used when selecting a new Drawable into a view, so that the previous
11980 * one is completely unscheduled.
11981 *
11982 * @param who The Drawable to unschedule.
11983 *
11984 * @see #drawableStateChanged
11985 */
11986 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080011987 if (mAttachInfo != null && who != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011988 mAttachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 }
11990 }
11991
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011992 /**
11993 * Return the layout direction of a given Drawable.
11994 *
11995 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011996 */
11997 public int getResolvedLayoutDirection(Drawable who) {
11998 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011999 }
12000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012001 /**
12002 * If your view subclass is displaying its own Drawable objects, it should
12003 * override this function and return true for any Drawable it is
12004 * displaying. This allows animations for those drawables to be
12005 * scheduled.
12006 *
12007 * <p>Be sure to call through to the super class when overriding this
12008 * function.
12009 *
12010 * @param who The Drawable to verify. Return true if it is one you are
12011 * displaying, else return the result of calling through to the
12012 * super class.
12013 *
12014 * @return boolean If true than the Drawable is being displayed in the
12015 * view; else false and it is not allowed to animate.
12016 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012017 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12018 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012019 */
12020 protected boolean verifyDrawable(Drawable who) {
12021 return who == mBGDrawable;
12022 }
12023
12024 /**
12025 * This function is called whenever the state of the view changes in such
12026 * a way that it impacts the state of drawables being shown.
12027 *
12028 * <p>Be sure to call through to the superclass when overriding this
12029 * function.
12030 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012031 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012032 */
12033 protected void drawableStateChanged() {
12034 Drawable d = mBGDrawable;
12035 if (d != null && d.isStateful()) {
12036 d.setState(getDrawableState());
12037 }
12038 }
12039
12040 /**
12041 * Call this to force a view to update its drawable state. This will cause
12042 * drawableStateChanged to be called on this view. Views that are interested
12043 * in the new state should call getDrawableState.
12044 *
12045 * @see #drawableStateChanged
12046 * @see #getDrawableState
12047 */
12048 public void refreshDrawableState() {
12049 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12050 drawableStateChanged();
12051
12052 ViewParent parent = mParent;
12053 if (parent != null) {
12054 parent.childDrawableStateChanged(this);
12055 }
12056 }
12057
12058 /**
12059 * Return an array of resource IDs of the drawable states representing the
12060 * current state of the view.
12061 *
12062 * @return The current drawable state
12063 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012064 * @see Drawable#setState(int[])
12065 * @see #drawableStateChanged()
12066 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012067 */
12068 public final int[] getDrawableState() {
12069 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12070 return mDrawableState;
12071 } else {
12072 mDrawableState = onCreateDrawableState(0);
12073 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12074 return mDrawableState;
12075 }
12076 }
12077
12078 /**
12079 * Generate the new {@link android.graphics.drawable.Drawable} state for
12080 * this view. This is called by the view
12081 * system when the cached Drawable state is determined to be invalid. To
12082 * retrieve the current state, you should use {@link #getDrawableState}.
12083 *
12084 * @param extraSpace if non-zero, this is the number of extra entries you
12085 * would like in the returned array in which you can place your own
12086 * states.
12087 *
12088 * @return Returns an array holding the current {@link Drawable} state of
12089 * the view.
12090 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012091 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012092 */
12093 protected int[] onCreateDrawableState(int extraSpace) {
12094 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12095 mParent instanceof View) {
12096 return ((View) mParent).onCreateDrawableState(extraSpace);
12097 }
12098
12099 int[] drawableState;
12100
12101 int privateFlags = mPrivateFlags;
12102
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012103 int viewStateIndex = 0;
12104 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12105 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12106 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012107 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012108 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12109 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012110 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12111 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012112 // This is set if HW acceleration is requested, even if the current
12113 // process doesn't allow it. This is just to allow app preview
12114 // windows to better match their app.
12115 viewStateIndex |= VIEW_STATE_ACCELERATED;
12116 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012117 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012118
Christopher Tate3d4bf172011-03-28 16:16:46 -070012119 final int privateFlags2 = mPrivateFlags2;
12120 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12121 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012123 drawableState = VIEW_STATE_SETS[viewStateIndex];
12124
12125 //noinspection ConstantIfStatement
12126 if (false) {
12127 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12128 Log.i("View", toString()
12129 + " pressed=" + ((privateFlags & PRESSED) != 0)
12130 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12131 + " fo=" + hasFocus()
12132 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012133 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012134 + ": " + Arrays.toString(drawableState));
12135 }
12136
12137 if (extraSpace == 0) {
12138 return drawableState;
12139 }
12140
12141 final int[] fullState;
12142 if (drawableState != null) {
12143 fullState = new int[drawableState.length + extraSpace];
12144 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12145 } else {
12146 fullState = new int[extraSpace];
12147 }
12148
12149 return fullState;
12150 }
12151
12152 /**
12153 * Merge your own state values in <var>additionalState</var> into the base
12154 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012155 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012156 *
12157 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012158 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012159 * own additional state values.
12160 *
12161 * @param additionalState The additional state values you would like
12162 * added to <var>baseState</var>; this array is not modified.
12163 *
12164 * @return As a convenience, the <var>baseState</var> array you originally
12165 * passed into the function is returned.
12166 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012167 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012168 */
12169 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12170 final int N = baseState.length;
12171 int i = N - 1;
12172 while (i >= 0 && baseState[i] == 0) {
12173 i--;
12174 }
12175 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12176 return baseState;
12177 }
12178
12179 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012180 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12181 * on all Drawable objects associated with this view.
12182 */
12183 public void jumpDrawablesToCurrentState() {
12184 if (mBGDrawable != null) {
12185 mBGDrawable.jumpToCurrentState();
12186 }
12187 }
12188
12189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012190 * Sets the background color for this view.
12191 * @param color the color of the background
12192 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012193 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012194 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012195 if (mBGDrawable instanceof ColorDrawable) {
12196 ((ColorDrawable) mBGDrawable).setColor(color);
12197 } else {
12198 setBackgroundDrawable(new ColorDrawable(color));
12199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012200 }
12201
12202 /**
12203 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012204 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012205 * @param resid The identifier of the resource.
12206 * @attr ref android.R.styleable#View_background
12207 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012208 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012209 public void setBackgroundResource(int resid) {
12210 if (resid != 0 && resid == mBackgroundResource) {
12211 return;
12212 }
12213
12214 Drawable d= null;
12215 if (resid != 0) {
12216 d = mResources.getDrawable(resid);
12217 }
12218 setBackgroundDrawable(d);
12219
12220 mBackgroundResource = resid;
12221 }
12222
12223 /**
12224 * Set the background to a given Drawable, or remove the background. If the
12225 * background has padding, this View's padding is set to the background's
12226 * padding. However, when a background is removed, this View's padding isn't
12227 * touched. If setting the padding is desired, please use
12228 * {@link #setPadding(int, int, int, int)}.
12229 *
12230 * @param d The Drawable to use as the background, or null to remove the
12231 * background
12232 */
12233 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012234 if (d == mBGDrawable) {
12235 return;
12236 }
12237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012238 boolean requestLayout = false;
12239
12240 mBackgroundResource = 0;
12241
12242 /*
12243 * Regardless of whether we're setting a new background or not, we want
12244 * to clear the previous drawable.
12245 */
12246 if (mBGDrawable != null) {
12247 mBGDrawable.setCallback(null);
12248 unscheduleDrawable(mBGDrawable);
12249 }
12250
12251 if (d != null) {
12252 Rect padding = sThreadLocal.get();
12253 if (padding == null) {
12254 padding = new Rect();
12255 sThreadLocal.set(padding);
12256 }
12257 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012258 switch (d.getResolvedLayoutDirectionSelf()) {
12259 case LAYOUT_DIRECTION_RTL:
12260 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12261 break;
12262 case LAYOUT_DIRECTION_LTR:
12263 default:
12264 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012266 }
12267
12268 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12269 // if it has a different minimum size, we should layout again
12270 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12271 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12272 requestLayout = true;
12273 }
12274
12275 d.setCallback(this);
12276 if (d.isStateful()) {
12277 d.setState(getDrawableState());
12278 }
12279 d.setVisible(getVisibility() == VISIBLE, false);
12280 mBGDrawable = d;
12281
12282 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12283 mPrivateFlags &= ~SKIP_DRAW;
12284 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12285 requestLayout = true;
12286 }
12287 } else {
12288 /* Remove the background */
12289 mBGDrawable = null;
12290
12291 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12292 /*
12293 * This view ONLY drew the background before and we're removing
12294 * the background, so now it won't draw anything
12295 * (hence we SKIP_DRAW)
12296 */
12297 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12298 mPrivateFlags |= SKIP_DRAW;
12299 }
12300
12301 /*
12302 * When the background is set, we try to apply its padding to this
12303 * View. When the background is removed, we don't touch this View's
12304 * padding. This is noted in the Javadocs. Hence, we don't need to
12305 * requestLayout(), the invalidate() below is sufficient.
12306 */
12307
12308 // The old background's minimum size could have affected this
12309 // View's layout, so let's requestLayout
12310 requestLayout = true;
12311 }
12312
Romain Guy8f1344f52009-05-15 16:03:59 -070012313 computeOpaqueFlags();
12314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012315 if (requestLayout) {
12316 requestLayout();
12317 }
12318
12319 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012320 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012321 }
12322
12323 /**
12324 * Gets the background drawable
12325 * @return The drawable used as the background for this view, if any.
12326 */
12327 public Drawable getBackground() {
12328 return mBGDrawable;
12329 }
12330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012331 /**
12332 * Sets the padding. The view may add on the space required to display
12333 * the scrollbars, depending on the style and visibility of the scrollbars.
12334 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12335 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12336 * from the values set in this call.
12337 *
12338 * @attr ref android.R.styleable#View_padding
12339 * @attr ref android.R.styleable#View_paddingBottom
12340 * @attr ref android.R.styleable#View_paddingLeft
12341 * @attr ref android.R.styleable#View_paddingRight
12342 * @attr ref android.R.styleable#View_paddingTop
12343 * @param left the left padding in pixels
12344 * @param top the top padding in pixels
12345 * @param right the right padding in pixels
12346 * @param bottom the bottom padding in pixels
12347 */
12348 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012349 mUserPaddingStart = -1;
12350 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012351 mUserPaddingRelative = false;
12352
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012353 internalSetPadding(left, top, right, bottom);
12354 }
12355
12356 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080012357 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012358 mUserPaddingRight = right;
12359 mUserPaddingBottom = bottom;
12360
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012361 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012362 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070012363
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012364 // Common case is there are no scroll bars.
12365 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012366 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012367 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012368 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012369 switch (mVerticalScrollbarPosition) {
12370 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012371 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12372 left += offset;
12373 } else {
12374 right += offset;
12375 }
12376 break;
Adam Powell20232d02010-12-08 21:08:53 -080012377 case SCROLLBAR_POSITION_RIGHT:
12378 right += offset;
12379 break;
12380 case SCROLLBAR_POSITION_LEFT:
12381 left += offset;
12382 break;
12383 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012384 }
Adam Powell20232d02010-12-08 21:08:53 -080012385 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012386 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12387 ? 0 : getHorizontalScrollbarHeight();
12388 }
12389 }
Romain Guy8506ab42009-06-11 17:35:47 -070012390
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012391 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012392 changed = true;
12393 mPaddingLeft = left;
12394 }
12395 if (mPaddingTop != top) {
12396 changed = true;
12397 mPaddingTop = top;
12398 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012399 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012400 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012401 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012402 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012403 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012404 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012405 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012406 }
12407
12408 if (changed) {
12409 requestLayout();
12410 }
12411 }
12412
12413 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012414 * Sets the relative padding. The view may add on the space required to display
12415 * the scrollbars, depending on the style and visibility of the scrollbars.
12416 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12417 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12418 * from the values set in this call.
12419 *
12420 * @attr ref android.R.styleable#View_padding
12421 * @attr ref android.R.styleable#View_paddingBottom
12422 * @attr ref android.R.styleable#View_paddingStart
12423 * @attr ref android.R.styleable#View_paddingEnd
12424 * @attr ref android.R.styleable#View_paddingTop
12425 * @param start the start padding in pixels
12426 * @param top the top padding in pixels
12427 * @param end the end padding in pixels
12428 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012429 */
12430 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012431 mUserPaddingStart = start;
12432 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012433 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012434
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012435 switch(getResolvedLayoutDirection()) {
12436 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012437 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012438 break;
12439 case LAYOUT_DIRECTION_LTR:
12440 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012441 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012442 }
12443 }
12444
12445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012446 * Returns the top padding of this view.
12447 *
12448 * @return the top padding in pixels
12449 */
12450 public int getPaddingTop() {
12451 return mPaddingTop;
12452 }
12453
12454 /**
12455 * Returns the bottom padding of this view. If there are inset and enabled
12456 * scrollbars, this value may include the space required to display the
12457 * scrollbars as well.
12458 *
12459 * @return the bottom padding in pixels
12460 */
12461 public int getPaddingBottom() {
12462 return mPaddingBottom;
12463 }
12464
12465 /**
12466 * Returns the left padding of this view. If there are inset and enabled
12467 * scrollbars, this value may include the space required to display the
12468 * scrollbars as well.
12469 *
12470 * @return the left padding in pixels
12471 */
12472 public int getPaddingLeft() {
12473 return mPaddingLeft;
12474 }
12475
12476 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070012477 * Returns the start padding of this view depending on its resolved layout direction.
12478 * If there are inset and enabled scrollbars, this value may include the space
12479 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012480 *
12481 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012482 */
12483 public int getPaddingStart() {
12484 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12485 mPaddingRight : mPaddingLeft;
12486 }
12487
12488 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012489 * Returns the right padding of this view. If there are inset and enabled
12490 * scrollbars, this value may include the space required to display the
12491 * scrollbars as well.
12492 *
12493 * @return the right padding in pixels
12494 */
12495 public int getPaddingRight() {
12496 return mPaddingRight;
12497 }
12498
12499 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070012500 * Returns the end padding of this view depending on its resolved layout direction.
12501 * If there are inset and enabled scrollbars, this value may include the space
12502 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012503 *
12504 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012505 */
12506 public int getPaddingEnd() {
12507 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12508 mPaddingLeft : mPaddingRight;
12509 }
12510
12511 /**
12512 * Return if the padding as been set thru relative values
12513 * {@link #setPaddingRelative(int, int, int, int)} or thru
12514 * @attr ref android.R.styleable#View_paddingStart or
12515 * @attr ref android.R.styleable#View_paddingEnd
12516 *
12517 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012518 */
12519 public boolean isPaddingRelative() {
12520 return mUserPaddingRelative;
12521 }
12522
12523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012524 * Changes the selection state of this view. A view can be selected or not.
12525 * Note that selection is not the same as focus. Views are typically
12526 * selected in the context of an AdapterView like ListView or GridView;
12527 * the selected view is the view that is highlighted.
12528 *
12529 * @param selected true if the view must be selected, false otherwise
12530 */
12531 public void setSelected(boolean selected) {
12532 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12533 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012534 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012535 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536 refreshDrawableState();
12537 dispatchSetSelected(selected);
12538 }
12539 }
12540
12541 /**
12542 * Dispatch setSelected to all of this View's children.
12543 *
12544 * @see #setSelected(boolean)
12545 *
12546 * @param selected The new selected state
12547 */
12548 protected void dispatchSetSelected(boolean selected) {
12549 }
12550
12551 /**
12552 * Indicates the selection state of this view.
12553 *
12554 * @return true if the view is selected, false otherwise
12555 */
12556 @ViewDebug.ExportedProperty
12557 public boolean isSelected() {
12558 return (mPrivateFlags & SELECTED) != 0;
12559 }
12560
12561 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012562 * Changes the activated state of this view. A view can be activated or not.
12563 * Note that activation is not the same as selection. Selection is
12564 * a transient property, representing the view (hierarchy) the user is
12565 * currently interacting with. Activation is a longer-term state that the
12566 * user can move views in and out of. For example, in a list view with
12567 * single or multiple selection enabled, the views in the current selection
12568 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12569 * here.) The activated state is propagated down to children of the view it
12570 * is set on.
12571 *
12572 * @param activated true if the view must be activated, false otherwise
12573 */
12574 public void setActivated(boolean activated) {
12575 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12576 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012577 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012578 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012579 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012580 }
12581 }
12582
12583 /**
12584 * Dispatch setActivated to all of this View's children.
12585 *
12586 * @see #setActivated(boolean)
12587 *
12588 * @param activated The new activated state
12589 */
12590 protected void dispatchSetActivated(boolean activated) {
12591 }
12592
12593 /**
12594 * Indicates the activation state of this view.
12595 *
12596 * @return true if the view is activated, false otherwise
12597 */
12598 @ViewDebug.ExportedProperty
12599 public boolean isActivated() {
12600 return (mPrivateFlags & ACTIVATED) != 0;
12601 }
12602
12603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012604 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12605 * observer can be used to get notifications when global events, like
12606 * layout, happen.
12607 *
12608 * The returned ViewTreeObserver observer is not guaranteed to remain
12609 * valid for the lifetime of this View. If the caller of this method keeps
12610 * a long-lived reference to ViewTreeObserver, it should always check for
12611 * the return value of {@link ViewTreeObserver#isAlive()}.
12612 *
12613 * @return The ViewTreeObserver for this view's hierarchy.
12614 */
12615 public ViewTreeObserver getViewTreeObserver() {
12616 if (mAttachInfo != null) {
12617 return mAttachInfo.mTreeObserver;
12618 }
12619 if (mFloatingTreeObserver == null) {
12620 mFloatingTreeObserver = new ViewTreeObserver();
12621 }
12622 return mFloatingTreeObserver;
12623 }
12624
12625 /**
12626 * <p>Finds the topmost view in the current view hierarchy.</p>
12627 *
12628 * @return the topmost view containing this view
12629 */
12630 public View getRootView() {
12631 if (mAttachInfo != null) {
12632 final View v = mAttachInfo.mRootView;
12633 if (v != null) {
12634 return v;
12635 }
12636 }
Romain Guy8506ab42009-06-11 17:35:47 -070012637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012638 View parent = this;
12639
12640 while (parent.mParent != null && parent.mParent instanceof View) {
12641 parent = (View) parent.mParent;
12642 }
12643
12644 return parent;
12645 }
12646
12647 /**
12648 * <p>Computes the coordinates of this view on the screen. The argument
12649 * must be an array of two integers. After the method returns, the array
12650 * contains the x and y location in that order.</p>
12651 *
12652 * @param location an array of two integers in which to hold the coordinates
12653 */
12654 public void getLocationOnScreen(int[] location) {
12655 getLocationInWindow(location);
12656
12657 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012658 if (info != null) {
12659 location[0] += info.mWindowLeft;
12660 location[1] += info.mWindowTop;
12661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012662 }
12663
12664 /**
12665 * <p>Computes the coordinates of this view in its window. The argument
12666 * must be an array of two integers. After the method returns, the array
12667 * contains the x and y location in that order.</p>
12668 *
12669 * @param location an array of two integers in which to hold the coordinates
12670 */
12671 public void getLocationInWindow(int[] location) {
12672 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012673 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012674 }
12675
Gilles Debunne6583ce52011-12-06 18:09:02 -080012676 if (mAttachInfo == null) {
12677 // When the view is not attached to a window, this method does not make sense
12678 location[0] = location[1] = 0;
12679 return;
12680 }
12681
Gilles Debunnecea45132011-11-24 02:19:27 +010012682 float[] position = mAttachInfo.mTmpTransformLocation;
12683 position[0] = position[1] = 0.0f;
12684
12685 if (!hasIdentityMatrix()) {
12686 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012688
Gilles Debunnecea45132011-11-24 02:19:27 +010012689 position[0] += mLeft;
12690 position[1] += mTop;
12691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012692 ViewParent viewParent = mParent;
12693 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012694 final View view = (View) viewParent;
12695
12696 position[0] -= view.mScrollX;
12697 position[1] -= view.mScrollY;
12698
12699 if (!view.hasIdentityMatrix()) {
12700 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012701 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012702
12703 position[0] += view.mLeft;
12704 position[1] += view.mTop;
12705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012706 viewParent = view.mParent;
12707 }
Romain Guy8506ab42009-06-11 17:35:47 -070012708
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012709 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012710 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012711 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12712 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012713 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012714
12715 location[0] = (int) (position[0] + 0.5f);
12716 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012717 }
12718
12719 /**
12720 * {@hide}
12721 * @param id the id of the view to be found
12722 * @return the view of the specified id, null if cannot be found
12723 */
12724 protected View findViewTraversal(int id) {
12725 if (id == mID) {
12726 return this;
12727 }
12728 return null;
12729 }
12730
12731 /**
12732 * {@hide}
12733 * @param tag the tag of the view to be found
12734 * @return the view of specified tag, null if cannot be found
12735 */
12736 protected View findViewWithTagTraversal(Object tag) {
12737 if (tag != null && tag.equals(mTag)) {
12738 return this;
12739 }
12740 return null;
12741 }
12742
12743 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012744 * {@hide}
12745 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012746 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012747 * @return The first view that matches the predicate or null.
12748 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012749 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012750 if (predicate.apply(this)) {
12751 return this;
12752 }
12753 return null;
12754 }
12755
12756 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012757 * Look for a child view with the given id. If this view has the given
12758 * id, return this view.
12759 *
12760 * @param id The id to search for.
12761 * @return The view that has the given id in the hierarchy or null
12762 */
12763 public final View findViewById(int id) {
12764 if (id < 0) {
12765 return null;
12766 }
12767 return findViewTraversal(id);
12768 }
12769
12770 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012771 * Finds a view by its unuque and stable accessibility id.
12772 *
12773 * @param accessibilityId The searched accessibility id.
12774 * @return The found view.
12775 */
12776 final View findViewByAccessibilityId(int accessibilityId) {
12777 if (accessibilityId < 0) {
12778 return null;
12779 }
12780 return findViewByAccessibilityIdTraversal(accessibilityId);
12781 }
12782
12783 /**
12784 * Performs the traversal to find a view by its unuque and stable accessibility id.
12785 *
12786 * <strong>Note:</strong>This method does not stop at the root namespace
12787 * boundary since the user can touch the screen at an arbitrary location
12788 * potentially crossing the root namespace bounday which will send an
12789 * accessibility event to accessibility services and they should be able
12790 * to obtain the event source. Also accessibility ids are guaranteed to be
12791 * unique in the window.
12792 *
12793 * @param accessibilityId The accessibility id.
12794 * @return The found view.
12795 */
12796 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12797 if (getAccessibilityViewId() == accessibilityId) {
12798 return this;
12799 }
12800 return null;
12801 }
12802
12803 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012804 * Look for a child view with the given tag. If this view has the given
12805 * tag, return this view.
12806 *
12807 * @param tag The tag to search for, using "tag.equals(getTag())".
12808 * @return The View that has the given tag in the hierarchy or null
12809 */
12810 public final View findViewWithTag(Object tag) {
12811 if (tag == null) {
12812 return null;
12813 }
12814 return findViewWithTagTraversal(tag);
12815 }
12816
12817 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012818 * {@hide}
12819 * Look for a child view that matches the specified predicate.
12820 * If this view matches the predicate, return this view.
12821 *
12822 * @param predicate The predicate to evaluate.
12823 * @return The first view that matches the predicate or null.
12824 */
12825 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012826 return findViewByPredicateTraversal(predicate, null);
12827 }
12828
12829 /**
12830 * {@hide}
12831 * Look for a child view that matches the specified predicate,
12832 * starting with the specified view and its descendents and then
12833 * recusively searching the ancestors and siblings of that view
12834 * until this view is reached.
12835 *
12836 * This method is useful in cases where the predicate does not match
12837 * a single unique view (perhaps multiple views use the same id)
12838 * and we are trying to find the view that is "closest" in scope to the
12839 * starting view.
12840 *
12841 * @param start The view to start from.
12842 * @param predicate The predicate to evaluate.
12843 * @return The first view that matches the predicate or null.
12844 */
12845 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12846 View childToSkip = null;
12847 for (;;) {
12848 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12849 if (view != null || start == this) {
12850 return view;
12851 }
12852
12853 ViewParent parent = start.getParent();
12854 if (parent == null || !(parent instanceof View)) {
12855 return null;
12856 }
12857
12858 childToSkip = start;
12859 start = (View) parent;
12860 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012861 }
12862
12863 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012864 * Sets the identifier for this view. The identifier does not have to be
12865 * unique in this view's hierarchy. The identifier should be a positive
12866 * number.
12867 *
12868 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012869 * @see #getId()
12870 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012871 *
12872 * @param id a number used to identify the view
12873 *
12874 * @attr ref android.R.styleable#View_id
12875 */
12876 public void setId(int id) {
12877 mID = id;
12878 }
12879
12880 /**
12881 * {@hide}
12882 *
12883 * @param isRoot true if the view belongs to the root namespace, false
12884 * otherwise
12885 */
12886 public void setIsRootNamespace(boolean isRoot) {
12887 if (isRoot) {
12888 mPrivateFlags |= IS_ROOT_NAMESPACE;
12889 } else {
12890 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12891 }
12892 }
12893
12894 /**
12895 * {@hide}
12896 *
12897 * @return true if the view belongs to the root namespace, false otherwise
12898 */
12899 public boolean isRootNamespace() {
12900 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12901 }
12902
12903 /**
12904 * Returns this view's identifier.
12905 *
12906 * @return a positive integer used to identify the view or {@link #NO_ID}
12907 * if the view has no ID
12908 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012909 * @see #setId(int)
12910 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012911 * @attr ref android.R.styleable#View_id
12912 */
12913 @ViewDebug.CapturedViewProperty
12914 public int getId() {
12915 return mID;
12916 }
12917
12918 /**
12919 * Returns this view's tag.
12920 *
12921 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012922 *
12923 * @see #setTag(Object)
12924 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012925 */
12926 @ViewDebug.ExportedProperty
12927 public Object getTag() {
12928 return mTag;
12929 }
12930
12931 /**
12932 * Sets the tag associated with this view. A tag can be used to mark
12933 * a view in its hierarchy and does not have to be unique within the
12934 * hierarchy. Tags can also be used to store data within a view without
12935 * resorting to another data structure.
12936 *
12937 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012938 *
12939 * @see #getTag()
12940 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012941 */
12942 public void setTag(final Object tag) {
12943 mTag = tag;
12944 }
12945
12946 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012947 * Returns the tag associated with this view and the specified key.
12948 *
12949 * @param key The key identifying the tag
12950 *
12951 * @return the Object stored in this view as a tag
12952 *
12953 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012954 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012955 */
12956 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012957 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012958 return null;
12959 }
12960
12961 /**
12962 * Sets a tag associated with this view and a key. A tag can be used
12963 * to mark a view in its hierarchy and does not have to be unique within
12964 * the hierarchy. Tags can also be used to store data within a view
12965 * without resorting to another data structure.
12966 *
12967 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012968 * application to ensure it is unique (see the <a
12969 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12970 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012971 * the Android framework or not associated with any package will cause
12972 * an {@link IllegalArgumentException} to be thrown.
12973 *
12974 * @param key The key identifying the tag
12975 * @param tag An Object to tag the view with
12976 *
12977 * @throws IllegalArgumentException If they specified key is not valid
12978 *
12979 * @see #setTag(Object)
12980 * @see #getTag(int)
12981 */
12982 public void setTag(int key, final Object tag) {
12983 // If the package id is 0x00 or 0x01, it's either an undefined package
12984 // or a framework id
12985 if ((key >>> 24) < 2) {
12986 throw new IllegalArgumentException("The key must be an application-specific "
12987 + "resource id.");
12988 }
12989
Adam Powell2b2f6d62011-09-23 11:15:39 -070012990 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012991 }
12992
12993 /**
12994 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12995 * framework id.
12996 *
12997 * @hide
12998 */
12999 public void setTagInternal(int key, Object tag) {
13000 if ((key >>> 24) != 0x1) {
13001 throw new IllegalArgumentException("The key must be a framework-specific "
13002 + "resource id.");
13003 }
13004
Adam Powell2b2f6d62011-09-23 11:15:39 -070013005 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013006 }
13007
Adam Powell2b2f6d62011-09-23 11:15:39 -070013008 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013009 if (mKeyedTags == null) {
13010 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013011 }
13012
Adam Powell7db82ac2011-09-22 19:44:04 -070013013 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013014 }
13015
13016 /**
Romain Guy13922e02009-05-12 17:56:14 -070013017 * @param consistency The type of consistency. See ViewDebug for more information.
13018 *
13019 * @hide
13020 */
13021 protected boolean dispatchConsistencyCheck(int consistency) {
13022 return onConsistencyCheck(consistency);
13023 }
13024
13025 /**
13026 * Method that subclasses should implement to check their consistency. The type of
13027 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013028 *
Romain Guy13922e02009-05-12 17:56:14 -070013029 * @param consistency The type of consistency. See ViewDebug for more information.
13030 *
13031 * @throws IllegalStateException if the view is in an inconsistent state.
13032 *
13033 * @hide
13034 */
13035 protected boolean onConsistencyCheck(int consistency) {
13036 boolean result = true;
13037
13038 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13039 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13040
13041 if (checkLayout) {
13042 if (getParent() == null) {
13043 result = false;
13044 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13045 "View " + this + " does not have a parent.");
13046 }
13047
13048 if (mAttachInfo == null) {
13049 result = false;
13050 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13051 "View " + this + " is not attached to a window.");
13052 }
13053 }
13054
13055 if (checkDrawing) {
13056 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13057 // from their draw() method
13058
13059 if ((mPrivateFlags & DRAWN) != DRAWN &&
13060 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13061 result = false;
13062 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13063 "View " + this + " was invalidated but its drawing cache is valid.");
13064 }
13065 }
13066
13067 return result;
13068 }
13069
13070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013071 * Prints information about this view in the log output, with the tag
13072 * {@link #VIEW_LOG_TAG}.
13073 *
13074 * @hide
13075 */
13076 public void debug() {
13077 debug(0);
13078 }
13079
13080 /**
13081 * Prints information about this view in the log output, with the tag
13082 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13083 * indentation defined by the <code>depth</code>.
13084 *
13085 * @param depth the indentation level
13086 *
13087 * @hide
13088 */
13089 protected void debug(int depth) {
13090 String output = debugIndent(depth - 1);
13091
13092 output += "+ " + this;
13093 int id = getId();
13094 if (id != -1) {
13095 output += " (id=" + id + ")";
13096 }
13097 Object tag = getTag();
13098 if (tag != null) {
13099 output += " (tag=" + tag + ")";
13100 }
13101 Log.d(VIEW_LOG_TAG, output);
13102
13103 if ((mPrivateFlags & FOCUSED) != 0) {
13104 output = debugIndent(depth) + " FOCUSED";
13105 Log.d(VIEW_LOG_TAG, output);
13106 }
13107
13108 output = debugIndent(depth);
13109 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13110 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13111 + "} ";
13112 Log.d(VIEW_LOG_TAG, output);
13113
13114 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13115 || mPaddingBottom != 0) {
13116 output = debugIndent(depth);
13117 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13118 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13119 Log.d(VIEW_LOG_TAG, output);
13120 }
13121
13122 output = debugIndent(depth);
13123 output += "mMeasureWidth=" + mMeasuredWidth +
13124 " mMeasureHeight=" + mMeasuredHeight;
13125 Log.d(VIEW_LOG_TAG, output);
13126
13127 output = debugIndent(depth);
13128 if (mLayoutParams == null) {
13129 output += "BAD! no layout params";
13130 } else {
13131 output = mLayoutParams.debug(output);
13132 }
13133 Log.d(VIEW_LOG_TAG, output);
13134
13135 output = debugIndent(depth);
13136 output += "flags={";
13137 output += View.printFlags(mViewFlags);
13138 output += "}";
13139 Log.d(VIEW_LOG_TAG, output);
13140
13141 output = debugIndent(depth);
13142 output += "privateFlags={";
13143 output += View.printPrivateFlags(mPrivateFlags);
13144 output += "}";
13145 Log.d(VIEW_LOG_TAG, output);
13146 }
13147
13148 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013149 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013150 *
13151 * @param depth the indentation level
13152 * @return a String containing (depth * 2 + 3) * 2 white spaces
13153 *
13154 * @hide
13155 */
13156 protected static String debugIndent(int depth) {
13157 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13158 for (int i = 0; i < (depth * 2) + 3; i++) {
13159 spaces.append(' ').append(' ');
13160 }
13161 return spaces.toString();
13162 }
13163
13164 /**
13165 * <p>Return the offset of the widget's text baseline from the widget's top
13166 * boundary. If this widget does not support baseline alignment, this
13167 * method returns -1. </p>
13168 *
13169 * @return the offset of the baseline within the widget's bounds or -1
13170 * if baseline alignment is not supported
13171 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013172 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013173 public int getBaseline() {
13174 return -1;
13175 }
13176
13177 /**
13178 * Call this when something has changed which has invalidated the
13179 * layout of this view. This will schedule a layout pass of the view
13180 * tree.
13181 */
13182 public void requestLayout() {
13183 if (ViewDebug.TRACE_HIERARCHY) {
13184 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13185 }
13186
13187 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013188 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013189
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013190 if (mParent != null) {
13191 if (mLayoutParams != null) {
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080013192 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013193 }
13194 if (!mParent.isLayoutRequested()) {
13195 mParent.requestLayout();
13196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013197 }
13198 }
13199
13200 /**
13201 * Forces this view to be laid out during the next layout pass.
13202 * This method does not call requestLayout() or forceLayout()
13203 * on the parent.
13204 */
13205 public void forceLayout() {
13206 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013207 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013208 }
13209
13210 /**
13211 * <p>
13212 * This is called to find out how big a view should be. The parent
13213 * supplies constraint information in the width and height parameters.
13214 * </p>
13215 *
13216 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013217 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013218 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013219 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013220 * </p>
13221 *
13222 *
13223 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13224 * parent
13225 * @param heightMeasureSpec Vertical space requirements as imposed by the
13226 * parent
13227 *
13228 * @see #onMeasure(int, int)
13229 */
13230 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13231 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13232 widthMeasureSpec != mOldWidthMeasureSpec ||
13233 heightMeasureSpec != mOldHeightMeasureSpec) {
13234
13235 // first clears the measured dimension flag
13236 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13237
13238 if (ViewDebug.TRACE_HIERARCHY) {
13239 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13240 }
13241
13242 // measure ourselves, this should set the measured dimension flag back
13243 onMeasure(widthMeasureSpec, heightMeasureSpec);
13244
13245 // flag not set, setMeasuredDimension() was not invoked, we raise
13246 // an exception to warn the developer
13247 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13248 throw new IllegalStateException("onMeasure() did not set the"
13249 + " measured dimension by calling"
13250 + " setMeasuredDimension()");
13251 }
13252
13253 mPrivateFlags |= LAYOUT_REQUIRED;
13254 }
13255
13256 mOldWidthMeasureSpec = widthMeasureSpec;
13257 mOldHeightMeasureSpec = heightMeasureSpec;
13258 }
13259
13260 /**
13261 * <p>
13262 * Measure the view and its content to determine the measured width and the
13263 * measured height. This method is invoked by {@link #measure(int, int)} and
13264 * should be overriden by subclasses to provide accurate and efficient
13265 * measurement of their contents.
13266 * </p>
13267 *
13268 * <p>
13269 * <strong>CONTRACT:</strong> When overriding this method, you
13270 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13271 * measured width and height of this view. Failure to do so will trigger an
13272 * <code>IllegalStateException</code>, thrown by
13273 * {@link #measure(int, int)}. Calling the superclass'
13274 * {@link #onMeasure(int, int)} is a valid use.
13275 * </p>
13276 *
13277 * <p>
13278 * The base class implementation of measure defaults to the background size,
13279 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13280 * override {@link #onMeasure(int, int)} to provide better measurements of
13281 * their content.
13282 * </p>
13283 *
13284 * <p>
13285 * If this method is overridden, it is the subclass's responsibility to make
13286 * sure the measured height and width are at least the view's minimum height
13287 * and width ({@link #getSuggestedMinimumHeight()} and
13288 * {@link #getSuggestedMinimumWidth()}).
13289 * </p>
13290 *
13291 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13292 * The requirements are encoded with
13293 * {@link android.view.View.MeasureSpec}.
13294 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13295 * The requirements are encoded with
13296 * {@link android.view.View.MeasureSpec}.
13297 *
13298 * @see #getMeasuredWidth()
13299 * @see #getMeasuredHeight()
13300 * @see #setMeasuredDimension(int, int)
13301 * @see #getSuggestedMinimumHeight()
13302 * @see #getSuggestedMinimumWidth()
13303 * @see android.view.View.MeasureSpec#getMode(int)
13304 * @see android.view.View.MeasureSpec#getSize(int)
13305 */
13306 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13307 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13308 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13309 }
13310
13311 /**
13312 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13313 * measured width and measured height. Failing to do so will trigger an
13314 * exception at measurement time.</p>
13315 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013316 * @param measuredWidth The measured width of this view. May be a complex
13317 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13318 * {@link #MEASURED_STATE_TOO_SMALL}.
13319 * @param measuredHeight The measured height of this view. May be a complex
13320 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13321 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013322 */
13323 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13324 mMeasuredWidth = measuredWidth;
13325 mMeasuredHeight = measuredHeight;
13326
13327 mPrivateFlags |= MEASURED_DIMENSION_SET;
13328 }
13329
13330 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013331 * Merge two states as returned by {@link #getMeasuredState()}.
13332 * @param curState The current state as returned from a view or the result
13333 * of combining multiple views.
13334 * @param newState The new view state to combine.
13335 * @return Returns a new integer reflecting the combination of the two
13336 * states.
13337 */
13338 public static int combineMeasuredStates(int curState, int newState) {
13339 return curState | newState;
13340 }
13341
13342 /**
13343 * Version of {@link #resolveSizeAndState(int, int, int)}
13344 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13345 */
13346 public static int resolveSize(int size, int measureSpec) {
13347 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13348 }
13349
13350 /**
13351 * Utility to reconcile a desired size and state, with constraints imposed
13352 * by a MeasureSpec. Will take the desired size, unless a different size
13353 * is imposed by the constraints. The returned value is a compound integer,
13354 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13355 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13356 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013357 *
13358 * @param size How big the view wants to be
13359 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013360 * @return Size information bit mask as defined by
13361 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013362 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013363 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013364 int result = size;
13365 int specMode = MeasureSpec.getMode(measureSpec);
13366 int specSize = MeasureSpec.getSize(measureSpec);
13367 switch (specMode) {
13368 case MeasureSpec.UNSPECIFIED:
13369 result = size;
13370 break;
13371 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013372 if (specSize < size) {
13373 result = specSize | MEASURED_STATE_TOO_SMALL;
13374 } else {
13375 result = size;
13376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013377 break;
13378 case MeasureSpec.EXACTLY:
13379 result = specSize;
13380 break;
13381 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013382 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013383 }
13384
13385 /**
13386 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013387 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013388 * by the MeasureSpec.
13389 *
13390 * @param size Default size for this view
13391 * @param measureSpec Constraints imposed by the parent
13392 * @return The size this view should be.
13393 */
13394 public static int getDefaultSize(int size, int measureSpec) {
13395 int result = size;
13396 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013397 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013398
13399 switch (specMode) {
13400 case MeasureSpec.UNSPECIFIED:
13401 result = size;
13402 break;
13403 case MeasureSpec.AT_MOST:
13404 case MeasureSpec.EXACTLY:
13405 result = specSize;
13406 break;
13407 }
13408 return result;
13409 }
13410
13411 /**
13412 * Returns the suggested minimum height that the view should use. This
13413 * returns the maximum of the view's minimum height
13414 * and the background's minimum height
13415 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13416 * <p>
13417 * When being used in {@link #onMeasure(int, int)}, the caller should still
13418 * ensure the returned height is within the requirements of the parent.
13419 *
13420 * @return The suggested minimum height of the view.
13421 */
13422 protected int getSuggestedMinimumHeight() {
13423 int suggestedMinHeight = mMinHeight;
13424
13425 if (mBGDrawable != null) {
13426 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13427 if (suggestedMinHeight < bgMinHeight) {
13428 suggestedMinHeight = bgMinHeight;
13429 }
13430 }
13431
13432 return suggestedMinHeight;
13433 }
13434
13435 /**
13436 * Returns the suggested minimum width that the view should use. This
13437 * returns the maximum of the view's minimum width)
13438 * and the background's minimum width
13439 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13440 * <p>
13441 * When being used in {@link #onMeasure(int, int)}, the caller should still
13442 * ensure the returned width is within the requirements of the parent.
13443 *
13444 * @return The suggested minimum width of the view.
13445 */
13446 protected int getSuggestedMinimumWidth() {
13447 int suggestedMinWidth = mMinWidth;
13448
13449 if (mBGDrawable != null) {
13450 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13451 if (suggestedMinWidth < bgMinWidth) {
13452 suggestedMinWidth = bgMinWidth;
13453 }
13454 }
13455
13456 return suggestedMinWidth;
13457 }
13458
13459 /**
13460 * Sets the minimum height of the view. It is not guaranteed the view will
13461 * be able to achieve this minimum height (for example, if its parent layout
13462 * constrains it with less available height).
13463 *
13464 * @param minHeight The minimum height the view will try to be.
13465 */
13466 public void setMinimumHeight(int minHeight) {
13467 mMinHeight = minHeight;
13468 }
13469
13470 /**
13471 * Sets the minimum width of the view. It is not guaranteed the view will
13472 * be able to achieve this minimum width (for example, if its parent layout
13473 * constrains it with less available width).
13474 *
13475 * @param minWidth The minimum width the view will try to be.
13476 */
13477 public void setMinimumWidth(int minWidth) {
13478 mMinWidth = minWidth;
13479 }
13480
13481 /**
13482 * Get the animation currently associated with this view.
13483 *
13484 * @return The animation that is currently playing or
13485 * scheduled to play for this view.
13486 */
13487 public Animation getAnimation() {
13488 return mCurrentAnimation;
13489 }
13490
13491 /**
13492 * Start the specified animation now.
13493 *
13494 * @param animation the animation to start now
13495 */
13496 public void startAnimation(Animation animation) {
13497 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13498 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013499 invalidateParentCaches();
13500 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013501 }
13502
13503 /**
13504 * Cancels any animations for this view.
13505 */
13506 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013507 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013508 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013511 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013512 }
13513
13514 /**
13515 * Sets the next animation to play for this view.
13516 * If you want the animation to play immediately, use
13517 * startAnimation. This method provides allows fine-grained
13518 * control over the start time and invalidation, but you
13519 * must make sure that 1) the animation has a start time set, and
13520 * 2) the view will be invalidated when the animation is supposed to
13521 * start.
13522 *
13523 * @param animation The next animation, or null.
13524 */
13525 public void setAnimation(Animation animation) {
13526 mCurrentAnimation = animation;
13527 if (animation != null) {
13528 animation.reset();
13529 }
13530 }
13531
13532 /**
13533 * Invoked by a parent ViewGroup to notify the start of the animation
13534 * currently associated with this view. If you override this method,
13535 * always call super.onAnimationStart();
13536 *
13537 * @see #setAnimation(android.view.animation.Animation)
13538 * @see #getAnimation()
13539 */
13540 protected void onAnimationStart() {
13541 mPrivateFlags |= ANIMATION_STARTED;
13542 }
13543
13544 /**
13545 * Invoked by a parent ViewGroup to notify the end of the animation
13546 * currently associated with this view. If you override this method,
13547 * always call super.onAnimationEnd();
13548 *
13549 * @see #setAnimation(android.view.animation.Animation)
13550 * @see #getAnimation()
13551 */
13552 protected void onAnimationEnd() {
13553 mPrivateFlags &= ~ANIMATION_STARTED;
13554 }
13555
13556 /**
13557 * Invoked if there is a Transform that involves alpha. Subclass that can
13558 * draw themselves with the specified alpha should return true, and then
13559 * respect that alpha when their onDraw() is called. If this returns false
13560 * then the view may be redirected to draw into an offscreen buffer to
13561 * fulfill the request, which will look fine, but may be slower than if the
13562 * subclass handles it internally. The default implementation returns false.
13563 *
13564 * @param alpha The alpha (0..255) to apply to the view's drawing
13565 * @return true if the view can draw with the specified alpha.
13566 */
13567 protected boolean onSetAlpha(int alpha) {
13568 return false;
13569 }
13570
13571 /**
13572 * This is used by the RootView to perform an optimization when
13573 * the view hierarchy contains one or several SurfaceView.
13574 * SurfaceView is always considered transparent, but its children are not,
13575 * therefore all View objects remove themselves from the global transparent
13576 * region (passed as a parameter to this function).
13577 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013578 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013579 *
13580 * @return Returns true if the effective visibility of the view at this
13581 * point is opaque, regardless of the transparent region; returns false
13582 * if it is possible for underlying windows to be seen behind the view.
13583 *
13584 * {@hide}
13585 */
13586 public boolean gatherTransparentRegion(Region region) {
13587 final AttachInfo attachInfo = mAttachInfo;
13588 if (region != null && attachInfo != null) {
13589 final int pflags = mPrivateFlags;
13590 if ((pflags & SKIP_DRAW) == 0) {
13591 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13592 // remove it from the transparent region.
13593 final int[] location = attachInfo.mTransparentLocation;
13594 getLocationInWindow(location);
13595 region.op(location[0], location[1], location[0] + mRight - mLeft,
13596 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13597 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13598 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13599 // exists, so we remove the background drawable's non-transparent
13600 // parts from this transparent region.
13601 applyDrawableToTransparentRegion(mBGDrawable, region);
13602 }
13603 }
13604 return true;
13605 }
13606
13607 /**
13608 * Play a sound effect for this view.
13609 *
13610 * <p>The framework will play sound effects for some built in actions, such as
13611 * clicking, but you may wish to play these effects in your widget,
13612 * for instance, for internal navigation.
13613 *
13614 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13615 * {@link #isSoundEffectsEnabled()} is true.
13616 *
13617 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13618 */
13619 public void playSoundEffect(int soundConstant) {
13620 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13621 return;
13622 }
13623 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13624 }
13625
13626 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013627 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013628 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013629 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013630 *
13631 * <p>The framework will provide haptic feedback for some built in actions,
13632 * such as long presses, but you may wish to provide feedback for your
13633 * own widget.
13634 *
13635 * <p>The feedback will only be performed if
13636 * {@link #isHapticFeedbackEnabled()} is true.
13637 *
13638 * @param feedbackConstant One of the constants defined in
13639 * {@link HapticFeedbackConstants}
13640 */
13641 public boolean performHapticFeedback(int feedbackConstant) {
13642 return performHapticFeedback(feedbackConstant, 0);
13643 }
13644
13645 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013646 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013647 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013648 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013649 *
13650 * @param feedbackConstant One of the constants defined in
13651 * {@link HapticFeedbackConstants}
13652 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13653 */
13654 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13655 if (mAttachInfo == null) {
13656 return false;
13657 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013658 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013659 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013660 && !isHapticFeedbackEnabled()) {
13661 return false;
13662 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013663 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13664 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013665 }
13666
13667 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013668 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013669 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13670 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013671 */
13672 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013673 if (visibility != mSystemUiVisibility) {
13674 mSystemUiVisibility = visibility;
13675 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13676 mParent.recomputeViewAttributes(this);
13677 }
Joe Onorato664644d2011-01-23 17:53:23 -080013678 }
13679 }
13680
13681 /**
13682 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013683 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13684 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013685 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013686 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013687 return mSystemUiVisibility;
13688 }
13689
Scott Mainec6331b2011-05-24 16:55:56 -070013690 /**
13691 * Set a listener to receive callbacks when the visibility of the system bar changes.
13692 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13693 */
Joe Onorato664644d2011-01-23 17:53:23 -080013694 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013695 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013696 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13697 mParent.recomputeViewAttributes(this);
13698 }
13699 }
13700
13701 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013702 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13703 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013704 */
13705 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013706 ListenerInfo li = mListenerInfo;
13707 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13708 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013709 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013710 }
13711 }
13712
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013713 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13714 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13715 if (val != mSystemUiVisibility) {
13716 setSystemUiVisibility(val);
13717 }
13718 }
13719
Joe Onorato664644d2011-01-23 17:53:23 -080013720 /**
Joe Malin32736f02011-01-19 16:14:20 -080013721 * Creates an image that the system displays during the drag and drop
13722 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13723 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13724 * appearance as the given View. The default also positions the center of the drag shadow
13725 * directly under the touch point. If no View is provided (the constructor with no parameters
13726 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13727 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13728 * default is an invisible drag shadow.
13729 * <p>
13730 * You are not required to use the View you provide to the constructor as the basis of the
13731 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13732 * anything you want as the drag shadow.
13733 * </p>
13734 * <p>
13735 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13736 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13737 * size and position of the drag shadow. It uses this data to construct a
13738 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13739 * so that your application can draw the shadow image in the Canvas.
13740 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013741 *
13742 * <div class="special reference">
13743 * <h3>Developer Guides</h3>
13744 * <p>For a guide to implementing drag and drop features, read the
13745 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13746 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013747 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013748 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013749 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013750
13751 /**
Joe Malin32736f02011-01-19 16:14:20 -080013752 * Constructs a shadow image builder based on a View. By default, the resulting drag
13753 * shadow will have the same appearance and dimensions as the View, with the touch point
13754 * over the center of the View.
13755 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013756 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013757 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013758 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013759 }
13760
Christopher Tate17ed60c2011-01-18 12:50:26 -080013761 /**
13762 * Construct a shadow builder object with no associated View. This
13763 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13764 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13765 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013766 * reference to any View object. If they are not overridden, then the result is an
13767 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013768 */
13769 public DragShadowBuilder() {
13770 mView = new WeakReference<View>(null);
13771 }
13772
13773 /**
13774 * Returns the View object that had been passed to the
13775 * {@link #View.DragShadowBuilder(View)}
13776 * constructor. If that View parameter was {@code null} or if the
13777 * {@link #View.DragShadowBuilder()}
13778 * constructor was used to instantiate the builder object, this method will return
13779 * null.
13780 *
13781 * @return The View object associate with this builder object.
13782 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013783 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013784 final public View getView() {
13785 return mView.get();
13786 }
13787
Christopher Tate2c095f32010-10-04 14:13:40 -070013788 /**
Joe Malin32736f02011-01-19 16:14:20 -080013789 * Provides the metrics for the shadow image. These include the dimensions of
13790 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013791 * be centered under the touch location while dragging.
13792 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013793 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013794 * same as the dimensions of the View itself and centers the shadow under
13795 * the touch point.
13796 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013797 *
Joe Malin32736f02011-01-19 16:14:20 -080013798 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13799 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13800 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13801 * image.
13802 *
13803 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13804 * shadow image that should be underneath the touch point during the drag and drop
13805 * operation. Your application must set {@link android.graphics.Point#x} to the
13806 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013807 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013808 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013809 final View view = mView.get();
13810 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013811 shadowSize.set(view.getWidth(), view.getHeight());
13812 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013813 } else {
13814 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13815 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013816 }
13817
13818 /**
Joe Malin32736f02011-01-19 16:14:20 -080013819 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13820 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013821 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013822 *
Joe Malin32736f02011-01-19 16:14:20 -080013823 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013824 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013825 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013826 final View view = mView.get();
13827 if (view != null) {
13828 view.draw(canvas);
13829 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013830 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013831 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013832 }
13833 }
13834
13835 /**
Joe Malin32736f02011-01-19 16:14:20 -080013836 * Starts a drag and drop operation. When your application calls this method, it passes a
13837 * {@link android.view.View.DragShadowBuilder} object to the system. The
13838 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13839 * to get metrics for the drag shadow, and then calls the object's
13840 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13841 * <p>
13842 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13843 * drag events to all the View objects in your application that are currently visible. It does
13844 * this either by calling the View object's drag listener (an implementation of
13845 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13846 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13847 * Both are passed a {@link android.view.DragEvent} object that has a
13848 * {@link android.view.DragEvent#getAction()} value of
13849 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13850 * </p>
13851 * <p>
13852 * Your application can invoke startDrag() on any attached View object. The View object does not
13853 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13854 * be related to the View the user selected for dragging.
13855 * </p>
13856 * @param data A {@link android.content.ClipData} object pointing to the data to be
13857 * transferred by the drag and drop operation.
13858 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13859 * drag shadow.
13860 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13861 * drop operation. This Object is put into every DragEvent object sent by the system during the
13862 * current drag.
13863 * <p>
13864 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13865 * to the target Views. For example, it can contain flags that differentiate between a
13866 * a copy operation and a move operation.
13867 * </p>
13868 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13869 * so the parameter should be set to 0.
13870 * @return {@code true} if the method completes successfully, or
13871 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13872 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013873 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013874 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013875 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013876 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013877 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013878 }
13879 boolean okay = false;
13880
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013881 Point shadowSize = new Point();
13882 Point shadowTouchPoint = new Point();
13883 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013884
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013885 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13886 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13887 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013888 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013889
Chris Tatea32dcf72010-10-14 12:13:50 -070013890 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013891 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13892 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013893 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013894 Surface surface = new Surface();
13895 try {
13896 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013897 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013898 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013899 + " surface=" + surface);
13900 if (token != null) {
13901 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013902 try {
Chris Tate6b391282010-10-14 15:48:59 -070013903 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013904 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013905 } finally {
13906 surface.unlockCanvasAndPost(canvas);
13907 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013908
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013909 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013910
13911 // Cache the local state object for delivery with DragEvents
13912 root.setLocalDragState(myLocalState);
13913
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013914 // repurpose 'shadowSize' for the last touch point
13915 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013916
Christopher Tatea53146c2010-09-07 11:57:52 -070013917 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013918 shadowSize.x, shadowSize.y,
13919 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013920 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013921
13922 // Off and running! Release our local surface instance; the drag
13923 // shadow surface is now managed by the system process.
13924 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013925 }
13926 } catch (Exception e) {
13927 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13928 surface.destroy();
13929 }
13930
13931 return okay;
13932 }
13933
Christopher Tatea53146c2010-09-07 11:57:52 -070013934 /**
Joe Malin32736f02011-01-19 16:14:20 -080013935 * Handles drag events sent by the system following a call to
13936 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13937 *<p>
13938 * When the system calls this method, it passes a
13939 * {@link android.view.DragEvent} object. A call to
13940 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13941 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13942 * operation.
13943 * @param event The {@link android.view.DragEvent} sent by the system.
13944 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13945 * in DragEvent, indicating the type of drag event represented by this object.
13946 * @return {@code true} if the method was successful, otherwise {@code false}.
13947 * <p>
13948 * The method should return {@code true} in response to an action type of
13949 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13950 * operation.
13951 * </p>
13952 * <p>
13953 * The method should also return {@code true} in response to an action type of
13954 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13955 * {@code false} if it didn't.
13956 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013957 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013958 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013959 return false;
13960 }
13961
13962 /**
Joe Malin32736f02011-01-19 16:14:20 -080013963 * Detects if this View is enabled and has a drag event listener.
13964 * If both are true, then it calls the drag event listener with the
13965 * {@link android.view.DragEvent} it received. If the drag event listener returns
13966 * {@code true}, then dispatchDragEvent() returns {@code true}.
13967 * <p>
13968 * For all other cases, the method calls the
13969 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13970 * method and returns its result.
13971 * </p>
13972 * <p>
13973 * This ensures that a drag event is always consumed, even if the View does not have a drag
13974 * event listener. However, if the View has a listener and the listener returns true, then
13975 * onDragEvent() is not called.
13976 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013977 */
13978 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013979 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013980 ListenerInfo li = mListenerInfo;
13981 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13982 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013983 return true;
13984 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013985 return onDragEvent(event);
13986 }
13987
Christopher Tate3d4bf172011-03-28 16:16:46 -070013988 boolean canAcceptDrag() {
13989 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13990 }
13991
Christopher Tatea53146c2010-09-07 11:57:52 -070013992 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013993 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13994 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013995 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013996 */
13997 public void onCloseSystemDialogs(String reason) {
13998 }
Joe Malin32736f02011-01-19 16:14:20 -080013999
Dianne Hackbornffa42482009-09-23 22:20:11 -070014000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014001 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014002 * update a Region being computed for
14003 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014004 * that any non-transparent parts of the Drawable are removed from the
14005 * given transparent region.
14006 *
14007 * @param dr The Drawable whose transparency is to be applied to the region.
14008 * @param region A Region holding the current transparency information,
14009 * where any parts of the region that are set are considered to be
14010 * transparent. On return, this region will be modified to have the
14011 * transparency information reduced by the corresponding parts of the
14012 * Drawable that are not transparent.
14013 * {@hide}
14014 */
14015 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14016 if (DBG) {
14017 Log.i("View", "Getting transparent region for: " + this);
14018 }
14019 final Region r = dr.getTransparentRegion();
14020 final Rect db = dr.getBounds();
14021 final AttachInfo attachInfo = mAttachInfo;
14022 if (r != null && attachInfo != null) {
14023 final int w = getRight()-getLeft();
14024 final int h = getBottom()-getTop();
14025 if (db.left > 0) {
14026 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14027 r.op(0, 0, db.left, h, Region.Op.UNION);
14028 }
14029 if (db.right < w) {
14030 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14031 r.op(db.right, 0, w, h, Region.Op.UNION);
14032 }
14033 if (db.top > 0) {
14034 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14035 r.op(0, 0, w, db.top, Region.Op.UNION);
14036 }
14037 if (db.bottom < h) {
14038 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14039 r.op(0, db.bottom, w, h, Region.Op.UNION);
14040 }
14041 final int[] location = attachInfo.mTransparentLocation;
14042 getLocationInWindow(location);
14043 r.translate(location[0], location[1]);
14044 region.op(r, Region.Op.INTERSECT);
14045 } else {
14046 region.op(db, Region.Op.DIFFERENCE);
14047 }
14048 }
14049
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014050 private void checkForLongClick(int delayOffset) {
14051 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14052 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014053
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014054 if (mPendingCheckForLongPress == null) {
14055 mPendingCheckForLongPress = new CheckForLongPress();
14056 }
14057 mPendingCheckForLongPress.rememberWindowAttachCount();
14058 postDelayed(mPendingCheckForLongPress,
14059 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061 }
14062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014063 /**
14064 * Inflate a view from an XML resource. This convenience method wraps the {@link
14065 * LayoutInflater} class, which provides a full range of options for view inflation.
14066 *
14067 * @param context The Context object for your activity or application.
14068 * @param resource The resource ID to inflate
14069 * @param root A view group that will be the parent. Used to properly inflate the
14070 * layout_* parameters.
14071 * @see LayoutInflater
14072 */
14073 public static View inflate(Context context, int resource, ViewGroup root) {
14074 LayoutInflater factory = LayoutInflater.from(context);
14075 return factory.inflate(resource, root);
14076 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014078 /**
Adam Powell637d3372010-08-25 14:37:03 -070014079 * Scroll the view with standard behavior for scrolling beyond the normal
14080 * content boundaries. Views that call this method should override
14081 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14082 * results of an over-scroll operation.
14083 *
14084 * Views can use this method to handle any touch or fling-based scrolling.
14085 *
14086 * @param deltaX Change in X in pixels
14087 * @param deltaY Change in Y in pixels
14088 * @param scrollX Current X scroll value in pixels before applying deltaX
14089 * @param scrollY Current Y scroll value in pixels before applying deltaY
14090 * @param scrollRangeX Maximum content scroll range along the X axis
14091 * @param scrollRangeY Maximum content scroll range along the Y axis
14092 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14093 * along the X axis.
14094 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14095 * along the Y axis.
14096 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14097 * @return true if scrolling was clamped to an over-scroll boundary along either
14098 * axis, false otherwise.
14099 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014100 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014101 protected boolean overScrollBy(int deltaX, int deltaY,
14102 int scrollX, int scrollY,
14103 int scrollRangeX, int scrollRangeY,
14104 int maxOverScrollX, int maxOverScrollY,
14105 boolean isTouchEvent) {
14106 final int overScrollMode = mOverScrollMode;
14107 final boolean canScrollHorizontal =
14108 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14109 final boolean canScrollVertical =
14110 computeVerticalScrollRange() > computeVerticalScrollExtent();
14111 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14112 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14113 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14114 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14115
14116 int newScrollX = scrollX + deltaX;
14117 if (!overScrollHorizontal) {
14118 maxOverScrollX = 0;
14119 }
14120
14121 int newScrollY = scrollY + deltaY;
14122 if (!overScrollVertical) {
14123 maxOverScrollY = 0;
14124 }
14125
14126 // Clamp values if at the limits and record
14127 final int left = -maxOverScrollX;
14128 final int right = maxOverScrollX + scrollRangeX;
14129 final int top = -maxOverScrollY;
14130 final int bottom = maxOverScrollY + scrollRangeY;
14131
14132 boolean clampedX = false;
14133 if (newScrollX > right) {
14134 newScrollX = right;
14135 clampedX = true;
14136 } else if (newScrollX < left) {
14137 newScrollX = left;
14138 clampedX = true;
14139 }
14140
14141 boolean clampedY = false;
14142 if (newScrollY > bottom) {
14143 newScrollY = bottom;
14144 clampedY = true;
14145 } else if (newScrollY < top) {
14146 newScrollY = top;
14147 clampedY = true;
14148 }
14149
14150 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14151
14152 return clampedX || clampedY;
14153 }
14154
14155 /**
14156 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14157 * respond to the results of an over-scroll operation.
14158 *
14159 * @param scrollX New X scroll value in pixels
14160 * @param scrollY New Y scroll value in pixels
14161 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14162 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14163 */
14164 protected void onOverScrolled(int scrollX, int scrollY,
14165 boolean clampedX, boolean clampedY) {
14166 // Intentionally empty.
14167 }
14168
14169 /**
14170 * Returns the over-scroll mode for this view. The result will be
14171 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14172 * (allow over-scrolling only if the view content is larger than the container),
14173 * or {@link #OVER_SCROLL_NEVER}.
14174 *
14175 * @return This view's over-scroll mode.
14176 */
14177 public int getOverScrollMode() {
14178 return mOverScrollMode;
14179 }
14180
14181 /**
14182 * Set the over-scroll mode for this view. Valid over-scroll modes are
14183 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14184 * (allow over-scrolling only if the view content is larger than the container),
14185 * or {@link #OVER_SCROLL_NEVER}.
14186 *
14187 * Setting the over-scroll mode of a view will have an effect only if the
14188 * view is capable of scrolling.
14189 *
14190 * @param overScrollMode The new over-scroll mode for this view.
14191 */
14192 public void setOverScrollMode(int overScrollMode) {
14193 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14194 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14195 overScrollMode != OVER_SCROLL_NEVER) {
14196 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14197 }
14198 mOverScrollMode = overScrollMode;
14199 }
14200
14201 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014202 * Gets a scale factor that determines the distance the view should scroll
14203 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14204 * @return The vertical scroll scale factor.
14205 * @hide
14206 */
14207 protected float getVerticalScrollFactor() {
14208 if (mVerticalScrollFactor == 0) {
14209 TypedValue outValue = new TypedValue();
14210 if (!mContext.getTheme().resolveAttribute(
14211 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14212 throw new IllegalStateException(
14213 "Expected theme to define listPreferredItemHeight.");
14214 }
14215 mVerticalScrollFactor = outValue.getDimension(
14216 mContext.getResources().getDisplayMetrics());
14217 }
14218 return mVerticalScrollFactor;
14219 }
14220
14221 /**
14222 * Gets a scale factor that determines the distance the view should scroll
14223 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14224 * @return The horizontal scroll scale factor.
14225 * @hide
14226 */
14227 protected float getHorizontalScrollFactor() {
14228 // TODO: Should use something else.
14229 return getVerticalScrollFactor();
14230 }
14231
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014232 /**
14233 * Return the value specifying the text direction or policy that was set with
14234 * {@link #setTextDirection(int)}.
14235 *
14236 * @return the defined text direction. It can be one of:
14237 *
14238 * {@link #TEXT_DIRECTION_INHERIT},
14239 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14240 * {@link #TEXT_DIRECTION_ANY_RTL},
14241 * {@link #TEXT_DIRECTION_LTR},
14242 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014243 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014244 */
14245 public int getTextDirection() {
14246 return mTextDirection;
14247 }
14248
14249 /**
14250 * Set the text direction.
14251 *
14252 * @param textDirection the direction to set. Should be one of:
14253 *
14254 * {@link #TEXT_DIRECTION_INHERIT},
14255 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14256 * {@link #TEXT_DIRECTION_ANY_RTL},
14257 * {@link #TEXT_DIRECTION_LTR},
14258 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014259 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014260 */
14261 public void setTextDirection(int textDirection) {
14262 if (textDirection != mTextDirection) {
14263 mTextDirection = textDirection;
14264 resetResolvedTextDirection();
14265 requestLayout();
14266 }
14267 }
14268
14269 /**
14270 * Return the resolved text direction.
14271 *
14272 * @return the resolved text direction. Return one of:
14273 *
Doug Feltcb3791202011-07-07 11:57:48 -070014274 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14275 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014276 * {@link #TEXT_DIRECTION_LTR},
14277 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014278 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014279 */
14280 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014281 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014282 resolveTextDirection();
14283 }
14284 return mResolvedTextDirection;
14285 }
14286
14287 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014288 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14289 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014290 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014291 public void resolveTextDirection() {
14292 if (mResolvedTextDirection != TEXT_DIRECTION_INHERIT) {
14293 // Resolution has already been done.
14294 return;
14295 }
Doug Feltcb3791202011-07-07 11:57:48 -070014296 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
14297 mResolvedTextDirection = mTextDirection;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014298 } else if (mParent != null && mParent instanceof ViewGroup) {
Doug Feltcb3791202011-07-07 11:57:48 -070014299 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014300 } else {
14301 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Doug Feltcb3791202011-07-07 11:57:48 -070014302 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014303 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014304 }
14305
14306 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014307 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014308 * resolution should override this method.
14309 *
14310 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014311 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014312 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014313 }
14314
14315 /**
14316 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014317 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014318 * reset is done.
14319 */
14320 public void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014321 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014322 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014323 }
14324
14325 /**
14326 * Called when text direction is reset. Subclasses that care about text direction reset should
14327 * override this method and do a reset of the text direction of their children. The default
14328 * implementation does nothing.
14329 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014330 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014331 }
14332
Chet Haaseb39f0512011-05-24 14:36:40 -070014333 //
14334 // Properties
14335 //
14336 /**
14337 * A Property wrapper around the <code>alpha</code> functionality handled by the
14338 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14339 */
Chet Haased47f1532011-12-16 11:18:52 -080014340 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014341 @Override
14342 public void setValue(View object, float value) {
14343 object.setAlpha(value);
14344 }
14345
14346 @Override
14347 public Float get(View object) {
14348 return object.getAlpha();
14349 }
14350 };
14351
14352 /**
14353 * A Property wrapper around the <code>translationX</code> functionality handled by the
14354 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14355 */
Chet Haased47f1532011-12-16 11:18:52 -080014356 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014357 @Override
14358 public void setValue(View object, float value) {
14359 object.setTranslationX(value);
14360 }
14361
14362 @Override
14363 public Float get(View object) {
14364 return object.getTranslationX();
14365 }
14366 };
14367
14368 /**
14369 * A Property wrapper around the <code>translationY</code> functionality handled by the
14370 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14371 */
Chet Haased47f1532011-12-16 11:18:52 -080014372 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014373 @Override
14374 public void setValue(View object, float value) {
14375 object.setTranslationY(value);
14376 }
14377
14378 @Override
14379 public Float get(View object) {
14380 return object.getTranslationY();
14381 }
14382 };
14383
14384 /**
14385 * A Property wrapper around the <code>x</code> functionality handled by the
14386 * {@link View#setX(float)} and {@link View#getX()} methods.
14387 */
Chet Haased47f1532011-12-16 11:18:52 -080014388 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014389 @Override
14390 public void setValue(View object, float value) {
14391 object.setX(value);
14392 }
14393
14394 @Override
14395 public Float get(View object) {
14396 return object.getX();
14397 }
14398 };
14399
14400 /**
14401 * A Property wrapper around the <code>y</code> functionality handled by the
14402 * {@link View#setY(float)} and {@link View#getY()} methods.
14403 */
Chet Haased47f1532011-12-16 11:18:52 -080014404 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014405 @Override
14406 public void setValue(View object, float value) {
14407 object.setY(value);
14408 }
14409
14410 @Override
14411 public Float get(View object) {
14412 return object.getY();
14413 }
14414 };
14415
14416 /**
14417 * A Property wrapper around the <code>rotation</code> functionality handled by the
14418 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14419 */
Chet Haased47f1532011-12-16 11:18:52 -080014420 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014421 @Override
14422 public void setValue(View object, float value) {
14423 object.setRotation(value);
14424 }
14425
14426 @Override
14427 public Float get(View object) {
14428 return object.getRotation();
14429 }
14430 };
14431
14432 /**
14433 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14434 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14435 */
Chet Haased47f1532011-12-16 11:18:52 -080014436 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014437 @Override
14438 public void setValue(View object, float value) {
14439 object.setRotationX(value);
14440 }
14441
14442 @Override
14443 public Float get(View object) {
14444 return object.getRotationX();
14445 }
14446 };
14447
14448 /**
14449 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14450 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14451 */
Chet Haased47f1532011-12-16 11:18:52 -080014452 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014453 @Override
14454 public void setValue(View object, float value) {
14455 object.setRotationY(value);
14456 }
14457
14458 @Override
14459 public Float get(View object) {
14460 return object.getRotationY();
14461 }
14462 };
14463
14464 /**
14465 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14466 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14467 */
Chet Haased47f1532011-12-16 11:18:52 -080014468 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014469 @Override
14470 public void setValue(View object, float value) {
14471 object.setScaleX(value);
14472 }
14473
14474 @Override
14475 public Float get(View object) {
14476 return object.getScaleX();
14477 }
14478 };
14479
14480 /**
14481 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14482 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14483 */
Chet Haased47f1532011-12-16 11:18:52 -080014484 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014485 @Override
14486 public void setValue(View object, float value) {
14487 object.setScaleY(value);
14488 }
14489
14490 @Override
14491 public Float get(View object) {
14492 return object.getScaleY();
14493 }
14494 };
14495
Jeff Brown33bbfd22011-02-24 20:55:35 -080014496 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014497 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14498 * Each MeasureSpec represents a requirement for either the width or the height.
14499 * A MeasureSpec is comprised of a size and a mode. There are three possible
14500 * modes:
14501 * <dl>
14502 * <dt>UNSPECIFIED</dt>
14503 * <dd>
14504 * The parent has not imposed any constraint on the child. It can be whatever size
14505 * it wants.
14506 * </dd>
14507 *
14508 * <dt>EXACTLY</dt>
14509 * <dd>
14510 * The parent has determined an exact size for the child. The child is going to be
14511 * given those bounds regardless of how big it wants to be.
14512 * </dd>
14513 *
14514 * <dt>AT_MOST</dt>
14515 * <dd>
14516 * The child can be as large as it wants up to the specified size.
14517 * </dd>
14518 * </dl>
14519 *
14520 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14521 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14522 */
14523 public static class MeasureSpec {
14524 private static final int MODE_SHIFT = 30;
14525 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14526
14527 /**
14528 * Measure specification mode: The parent has not imposed any constraint
14529 * on the child. It can be whatever size it wants.
14530 */
14531 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14532
14533 /**
14534 * Measure specification mode: The parent has determined an exact size
14535 * for the child. The child is going to be given those bounds regardless
14536 * of how big it wants to be.
14537 */
14538 public static final int EXACTLY = 1 << MODE_SHIFT;
14539
14540 /**
14541 * Measure specification mode: The child can be as large as it wants up
14542 * to the specified size.
14543 */
14544 public static final int AT_MOST = 2 << MODE_SHIFT;
14545
14546 /**
14547 * Creates a measure specification based on the supplied size and mode.
14548 *
14549 * The mode must always be one of the following:
14550 * <ul>
14551 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14552 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14553 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14554 * </ul>
14555 *
14556 * @param size the size of the measure specification
14557 * @param mode the mode of the measure specification
14558 * @return the measure specification based on size and mode
14559 */
14560 public static int makeMeasureSpec(int size, int mode) {
14561 return size + mode;
14562 }
14563
14564 /**
14565 * Extracts the mode from the supplied measure specification.
14566 *
14567 * @param measureSpec the measure specification to extract the mode from
14568 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14569 * {@link android.view.View.MeasureSpec#AT_MOST} or
14570 * {@link android.view.View.MeasureSpec#EXACTLY}
14571 */
14572 public static int getMode(int measureSpec) {
14573 return (measureSpec & MODE_MASK);
14574 }
14575
14576 /**
14577 * Extracts the size from the supplied measure specification.
14578 *
14579 * @param measureSpec the measure specification to extract the size from
14580 * @return the size in pixels defined in the supplied measure specification
14581 */
14582 public static int getSize(int measureSpec) {
14583 return (measureSpec & ~MODE_MASK);
14584 }
14585
14586 /**
14587 * Returns a String representation of the specified measure
14588 * specification.
14589 *
14590 * @param measureSpec the measure specification to convert to a String
14591 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14592 */
14593 public static String toString(int measureSpec) {
14594 int mode = getMode(measureSpec);
14595 int size = getSize(measureSpec);
14596
14597 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14598
14599 if (mode == UNSPECIFIED)
14600 sb.append("UNSPECIFIED ");
14601 else if (mode == EXACTLY)
14602 sb.append("EXACTLY ");
14603 else if (mode == AT_MOST)
14604 sb.append("AT_MOST ");
14605 else
14606 sb.append(mode).append(" ");
14607
14608 sb.append(size);
14609 return sb.toString();
14610 }
14611 }
14612
14613 class CheckForLongPress implements Runnable {
14614
14615 private int mOriginalWindowAttachCount;
14616
14617 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014618 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014619 && mOriginalWindowAttachCount == mWindowAttachCount) {
14620 if (performLongClick()) {
14621 mHasPerformedLongPress = true;
14622 }
14623 }
14624 }
14625
14626 public void rememberWindowAttachCount() {
14627 mOriginalWindowAttachCount = mWindowAttachCount;
14628 }
14629 }
Joe Malin32736f02011-01-19 16:14:20 -080014630
Adam Powelle14579b2009-12-16 18:39:52 -080014631 private final class CheckForTap implements Runnable {
14632 public void run() {
14633 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014634 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014635 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014636 }
14637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014638
Adam Powella35d7682010-03-12 14:48:13 -080014639 private final class PerformClick implements Runnable {
14640 public void run() {
14641 performClick();
14642 }
14643 }
14644
Dianne Hackborn63042d62011-01-26 18:56:29 -080014645 /** @hide */
14646 public void hackTurnOffWindowResizeAnim(boolean off) {
14647 mAttachInfo.mTurnOffWindowResizeAnim = off;
14648 }
Joe Malin32736f02011-01-19 16:14:20 -080014649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014650 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014651 * This method returns a ViewPropertyAnimator object, which can be used to animate
14652 * specific properties on this View.
14653 *
14654 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14655 */
14656 public ViewPropertyAnimator animate() {
14657 if (mAnimator == null) {
14658 mAnimator = new ViewPropertyAnimator(this);
14659 }
14660 return mAnimator;
14661 }
14662
14663 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014664 * Interface definition for a callback to be invoked when a key event is
14665 * dispatched to this view. The callback will be invoked before the key
14666 * event is given to the view.
14667 */
14668 public interface OnKeyListener {
14669 /**
14670 * Called when a key is dispatched to a view. This allows listeners to
14671 * get a chance to respond before the target view.
14672 *
14673 * @param v The view the key has been dispatched to.
14674 * @param keyCode The code for the physical key that was pressed
14675 * @param event The KeyEvent object containing full information about
14676 * the event.
14677 * @return True if the listener has consumed the event, false otherwise.
14678 */
14679 boolean onKey(View v, int keyCode, KeyEvent event);
14680 }
14681
14682 /**
14683 * Interface definition for a callback to be invoked when a touch event is
14684 * dispatched to this view. The callback will be invoked before the touch
14685 * event is given to the view.
14686 */
14687 public interface OnTouchListener {
14688 /**
14689 * Called when a touch event is dispatched to a view. This allows listeners to
14690 * get a chance to respond before the target view.
14691 *
14692 * @param v The view the touch event has been dispatched to.
14693 * @param event The MotionEvent object containing full information about
14694 * the event.
14695 * @return True if the listener has consumed the event, false otherwise.
14696 */
14697 boolean onTouch(View v, MotionEvent event);
14698 }
14699
14700 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014701 * Interface definition for a callback to be invoked when a hover event is
14702 * dispatched to this view. The callback will be invoked before the hover
14703 * event is given to the view.
14704 */
14705 public interface OnHoverListener {
14706 /**
14707 * Called when a hover event is dispatched to a view. This allows listeners to
14708 * get a chance to respond before the target view.
14709 *
14710 * @param v The view the hover event has been dispatched to.
14711 * @param event The MotionEvent object containing full information about
14712 * the event.
14713 * @return True if the listener has consumed the event, false otherwise.
14714 */
14715 boolean onHover(View v, MotionEvent event);
14716 }
14717
14718 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014719 * Interface definition for a callback to be invoked when a generic motion event is
14720 * dispatched to this view. The callback will be invoked before the generic motion
14721 * event is given to the view.
14722 */
14723 public interface OnGenericMotionListener {
14724 /**
14725 * Called when a generic motion event is dispatched to a view. This allows listeners to
14726 * get a chance to respond before the target view.
14727 *
14728 * @param v The view the generic motion event has been dispatched to.
14729 * @param event The MotionEvent object containing full information about
14730 * the event.
14731 * @return True if the listener has consumed the event, false otherwise.
14732 */
14733 boolean onGenericMotion(View v, MotionEvent event);
14734 }
14735
14736 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014737 * Interface definition for a callback to be invoked when a view has been clicked and held.
14738 */
14739 public interface OnLongClickListener {
14740 /**
14741 * Called when a view has been clicked and held.
14742 *
14743 * @param v The view that was clicked and held.
14744 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014745 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014746 */
14747 boolean onLongClick(View v);
14748 }
14749
14750 /**
Chris Tate32affef2010-10-18 15:29:21 -070014751 * Interface definition for a callback to be invoked when a drag is being dispatched
14752 * to this view. The callback will be invoked before the hosting view's own
14753 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14754 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014755 *
14756 * <div class="special reference">
14757 * <h3>Developer Guides</h3>
14758 * <p>For a guide to implementing drag and drop features, read the
14759 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14760 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014761 */
14762 public interface OnDragListener {
14763 /**
14764 * Called when a drag event is dispatched to a view. This allows listeners
14765 * to get a chance to override base View behavior.
14766 *
Joe Malin32736f02011-01-19 16:14:20 -080014767 * @param v The View that received the drag event.
14768 * @param event The {@link android.view.DragEvent} object for the drag event.
14769 * @return {@code true} if the drag event was handled successfully, or {@code false}
14770 * if the drag event was not handled. Note that {@code false} will trigger the View
14771 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014772 */
14773 boolean onDrag(View v, DragEvent event);
14774 }
14775
14776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014777 * Interface definition for a callback to be invoked when the focus state of
14778 * a view changed.
14779 */
14780 public interface OnFocusChangeListener {
14781 /**
14782 * Called when the focus state of a view has changed.
14783 *
14784 * @param v The view whose state has changed.
14785 * @param hasFocus The new focus state of v.
14786 */
14787 void onFocusChange(View v, boolean hasFocus);
14788 }
14789
14790 /**
14791 * Interface definition for a callback to be invoked when a view is clicked.
14792 */
14793 public interface OnClickListener {
14794 /**
14795 * Called when a view has been clicked.
14796 *
14797 * @param v The view that was clicked.
14798 */
14799 void onClick(View v);
14800 }
14801
14802 /**
14803 * Interface definition for a callback to be invoked when the context menu
14804 * for this view is being built.
14805 */
14806 public interface OnCreateContextMenuListener {
14807 /**
14808 * Called when the context menu for this view is being built. It is not
14809 * safe to hold onto the menu after this method returns.
14810 *
14811 * @param menu The context menu that is being built
14812 * @param v The view for which the context menu is being built
14813 * @param menuInfo Extra information about the item for which the
14814 * context menu should be shown. This information will vary
14815 * depending on the class of v.
14816 */
14817 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14818 }
14819
Joe Onorato664644d2011-01-23 17:53:23 -080014820 /**
14821 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014822 * visibility. This reports <strong>global</strong> changes to the system UI
14823 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014824 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014825 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014826 */
14827 public interface OnSystemUiVisibilityChangeListener {
14828 /**
14829 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014830 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014831 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014832 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014833 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14834 * <strong>global</strong> state of the UI visibility flags, not what your
14835 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014836 */
14837 public void onSystemUiVisibilityChange(int visibility);
14838 }
14839
Adam Powell4afd62b2011-02-18 15:02:18 -080014840 /**
14841 * Interface definition for a callback to be invoked when this view is attached
14842 * or detached from its window.
14843 */
14844 public interface OnAttachStateChangeListener {
14845 /**
14846 * Called when the view is attached to a window.
14847 * @param v The view that was attached
14848 */
14849 public void onViewAttachedToWindow(View v);
14850 /**
14851 * Called when the view is detached from a window.
14852 * @param v The view that was detached
14853 */
14854 public void onViewDetachedFromWindow(View v);
14855 }
14856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014857 private final class UnsetPressedState implements Runnable {
14858 public void run() {
14859 setPressed(false);
14860 }
14861 }
14862
14863 /**
14864 * Base class for derived classes that want to save and restore their own
14865 * state in {@link android.view.View#onSaveInstanceState()}.
14866 */
14867 public static class BaseSavedState extends AbsSavedState {
14868 /**
14869 * Constructor used when reading from a parcel. Reads the state of the superclass.
14870 *
14871 * @param source
14872 */
14873 public BaseSavedState(Parcel source) {
14874 super(source);
14875 }
14876
14877 /**
14878 * Constructor called by derived classes when creating their SavedState objects
14879 *
14880 * @param superState The state of the superclass of this view
14881 */
14882 public BaseSavedState(Parcelable superState) {
14883 super(superState);
14884 }
14885
14886 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14887 new Parcelable.Creator<BaseSavedState>() {
14888 public BaseSavedState createFromParcel(Parcel in) {
14889 return new BaseSavedState(in);
14890 }
14891
14892 public BaseSavedState[] newArray(int size) {
14893 return new BaseSavedState[size];
14894 }
14895 };
14896 }
14897
14898 /**
14899 * A set of information given to a view when it is attached to its parent
14900 * window.
14901 */
14902 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014903 interface Callbacks {
14904 void playSoundEffect(int effectId);
14905 boolean performHapticFeedback(int effectId, boolean always);
14906 }
14907
14908 /**
14909 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14910 * to a Handler. This class contains the target (View) to invalidate and
14911 * the coordinates of the dirty rectangle.
14912 *
14913 * For performance purposes, this class also implements a pool of up to
14914 * POOL_LIMIT objects that get reused. This reduces memory allocations
14915 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014916 */
Romain Guyd928d682009-03-31 17:52:16 -070014917 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014918 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014919 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14920 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014921 public InvalidateInfo newInstance() {
14922 return new InvalidateInfo();
14923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014924
Romain Guyd928d682009-03-31 17:52:16 -070014925 public void onAcquired(InvalidateInfo element) {
14926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014927
Romain Guyd928d682009-03-31 17:52:16 -070014928 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014929 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014930 }
14931 }, POOL_LIMIT)
14932 );
14933
14934 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014935 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014936
14937 View target;
14938
14939 int left;
14940 int top;
14941 int right;
14942 int bottom;
14943
Romain Guyd928d682009-03-31 17:52:16 -070014944 public void setNextPoolable(InvalidateInfo element) {
14945 mNext = element;
14946 }
14947
14948 public InvalidateInfo getNextPoolable() {
14949 return mNext;
14950 }
14951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014952 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014953 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014954 }
14955
14956 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014957 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014958 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014959
14960 public boolean isPooled() {
14961 return mIsPooled;
14962 }
14963
14964 public void setPooled(boolean isPooled) {
14965 mIsPooled = isPooled;
14966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014967 }
14968
14969 final IWindowSession mSession;
14970
14971 final IWindow mWindow;
14972
14973 final IBinder mWindowToken;
14974
14975 final Callbacks mRootCallbacks;
14976
Romain Guy59a12ca2011-06-09 17:48:21 -070014977 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014979 /**
14980 * The top view of the hierarchy.
14981 */
14982 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014984 IBinder mPanelParentWindowToken;
14985 Surface mSurface;
14986
Romain Guyb051e892010-09-28 19:09:36 -070014987 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014988 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014989 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014990
Romain Guy7e4e5612012-03-05 14:37:29 -080014991 boolean mScreenOn;
14992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014993 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014994 * Scale factor used by the compatibility mode
14995 */
14996 float mApplicationScale;
14997
14998 /**
14999 * Indicates whether the application is in compatibility mode
15000 */
15001 boolean mScalingRequired;
15002
15003 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015004 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080015005 */
15006 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080015007
Dianne Hackborn63042d62011-01-26 18:56:29 -080015008 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015009 * Left position of this view's window
15010 */
15011 int mWindowLeft;
15012
15013 /**
15014 * Top position of this view's window
15015 */
15016 int mWindowTop;
15017
15018 /**
Adam Powell26153a32010-11-08 15:22:27 -080015019 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070015020 */
Adam Powell26153a32010-11-08 15:22:27 -080015021 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070015022
15023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015024 * For windows that are full-screen but using insets to layout inside
15025 * of the screen decorations, these are the current insets for the
15026 * content of the window.
15027 */
15028 final Rect mContentInsets = new Rect();
15029
15030 /**
15031 * For windows that are full-screen but using insets to layout inside
15032 * of the screen decorations, these are the current insets for the
15033 * actual visible parts of the window.
15034 */
15035 final Rect mVisibleInsets = new Rect();
15036
15037 /**
15038 * The internal insets given by this window. This value is
15039 * supplied by the client (through
15040 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
15041 * be given to the window manager when changed to be used in laying
15042 * out windows behind it.
15043 */
15044 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
15045 = new ViewTreeObserver.InternalInsetsInfo();
15046
15047 /**
15048 * All views in the window's hierarchy that serve as scroll containers,
15049 * used to determine if the window can be resized or must be panned
15050 * to adjust for a soft input area.
15051 */
15052 final ArrayList<View> mScrollContainers = new ArrayList<View>();
15053
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070015054 final KeyEvent.DispatcherState mKeyDispatchState
15055 = new KeyEvent.DispatcherState();
15056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015057 /**
15058 * Indicates whether the view's window currently has the focus.
15059 */
15060 boolean mHasWindowFocus;
15061
15062 /**
15063 * The current visibility of the window.
15064 */
15065 int mWindowVisibility;
15066
15067 /**
15068 * Indicates the time at which drawing started to occur.
15069 */
15070 long mDrawingTime;
15071
15072 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070015073 * Indicates whether or not ignoring the DIRTY_MASK flags.
15074 */
15075 boolean mIgnoreDirtyState;
15076
15077 /**
Romain Guy02ccac62011-06-24 13:20:23 -070015078 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
15079 * to avoid clearing that flag prematurely.
15080 */
15081 boolean mSetIgnoreDirtyState = false;
15082
15083 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015084 * Indicates whether the view's window is currently in touch mode.
15085 */
15086 boolean mInTouchMode;
15087
15088 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015089 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015090 * the next time it performs a traversal
15091 */
15092 boolean mRecomputeGlobalAttributes;
15093
15094 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015095 * Always report new attributes at next traversal.
15096 */
15097 boolean mForceReportNewAttributes;
15098
15099 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015100 * Set during a traveral if any views want to keep the screen on.
15101 */
15102 boolean mKeepScreenOn;
15103
15104 /**
Joe Onorato664644d2011-01-23 17:53:23 -080015105 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
15106 */
15107 int mSystemUiVisibility;
15108
15109 /**
15110 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
15111 * attached.
15112 */
15113 boolean mHasSystemUiListeners;
15114
15115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015116 * Set if the visibility of any views has changed.
15117 */
15118 boolean mViewVisibilityChanged;
15119
15120 /**
15121 * Set to true if a view has been scrolled.
15122 */
15123 boolean mViewScrollChanged;
15124
15125 /**
15126 * Global to the view hierarchy used as a temporary for dealing with
15127 * x/y points in the transparent region computations.
15128 */
15129 final int[] mTransparentLocation = new int[2];
15130
15131 /**
15132 * Global to the view hierarchy used as a temporary for dealing with
15133 * x/y points in the ViewGroup.invalidateChild implementation.
15134 */
15135 final int[] mInvalidateChildLocation = new int[2];
15136
Chet Haasec3aa3612010-06-17 08:50:37 -070015137
15138 /**
15139 * Global to the view hierarchy used as a temporary for dealing with
15140 * x/y location when view is transformed.
15141 */
15142 final float[] mTmpTransformLocation = new float[2];
15143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015144 /**
15145 * The view tree observer used to dispatch global events like
15146 * layout, pre-draw, touch mode change, etc.
15147 */
15148 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15149
15150 /**
15151 * A Canvas used by the view hierarchy to perform bitmap caching.
15152 */
15153 Canvas mCanvas;
15154
15155 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015156 * The view root impl.
15157 */
15158 final ViewRootImpl mViewRootImpl;
15159
15160 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015161 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015162 * handler can be used to pump events in the UI events queue.
15163 */
15164 final Handler mHandler;
15165
15166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015167 * Temporary for use in computing invalidate rectangles while
15168 * calling up the hierarchy.
15169 */
15170 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015171
15172 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015173 * Temporary for use in computing hit areas with transformed views
15174 */
15175 final RectF mTmpTransformRect = new RectF();
15176
15177 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015178 * Temporary list for use in collecting focusable descendents of a view.
15179 */
15180 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015182 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015183 * The id of the window for accessibility purposes.
15184 */
15185 int mAccessibilityWindowId = View.NO_ID;
15186
15187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015188 * Creates a new set of attachment information with the specified
15189 * events handler and thread.
15190 *
15191 * @param handler the events handler the view must use
15192 */
15193 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015194 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015195 mSession = session;
15196 mWindow = window;
15197 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015198 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015199 mHandler = handler;
15200 mRootCallbacks = effectPlayer;
15201 }
15202 }
15203
15204 /**
15205 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15206 * is supported. This avoids keeping too many unused fields in most
15207 * instances of View.</p>
15208 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015209 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015210
Mike Cleronf116bf82009-09-27 19:14:12 -070015211 /**
15212 * Scrollbars are not visible
15213 */
15214 public static final int OFF = 0;
15215
15216 /**
15217 * Scrollbars are visible
15218 */
15219 public static final int ON = 1;
15220
15221 /**
15222 * Scrollbars are fading away
15223 */
15224 public static final int FADING = 2;
15225
15226 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015228 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015229 public int scrollBarDefaultDelayBeforeFade;
15230 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015231
15232 public int scrollBarSize;
15233 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015234 public float[] interpolatorValues;
15235 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015236
15237 public final Paint paint;
15238 public final Matrix matrix;
15239 public Shader shader;
15240
Mike Cleronf116bf82009-09-27 19:14:12 -070015241 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15242
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015243 private static final float[] OPAQUE = { 255 };
15244 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015245
Mike Cleronf116bf82009-09-27 19:14:12 -070015246 /**
15247 * When fading should start. This time moves into the future every time
15248 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15249 */
15250 public long fadeStartTime;
15251
15252
15253 /**
15254 * The current state of the scrollbars: ON, OFF, or FADING
15255 */
15256 public int state = OFF;
15257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015258 private int mLastColor;
15259
Mike Cleronf116bf82009-09-27 19:14:12 -070015260 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015261 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15262 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015263 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15264 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015265
15266 paint = new Paint();
15267 matrix = new Matrix();
15268 // use use a height of 1, and then wack the matrix each time we
15269 // actually use it.
15270 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015272 paint.setShader(shader);
15273 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015274 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015275 }
Romain Guy8506ab42009-06-11 17:35:47 -070015276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015277 public void setFadeColor(int color) {
15278 if (color != 0 && color != mLastColor) {
15279 mLastColor = color;
15280 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015281
Romain Guye55e1a72009-08-27 10:42:26 -070015282 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15283 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015285 paint.setShader(shader);
15286 // Restore the default transfer mode (src_over)
15287 paint.setXfermode(null);
15288 }
15289 }
Joe Malin32736f02011-01-19 16:14:20 -080015290
Mike Cleronf116bf82009-09-27 19:14:12 -070015291 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015292 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015293 if (now >= fadeStartTime) {
15294
15295 // the animation fades the scrollbars out by changing
15296 // the opacity (alpha) from fully opaque to fully
15297 // transparent
15298 int nextFrame = (int) now;
15299 int framesCount = 0;
15300
15301 Interpolator interpolator = scrollBarInterpolator;
15302
15303 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015304 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015305
15306 // End transparent
15307 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015308 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015309
15310 state = FADING;
15311
15312 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015313 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015314 }
15315 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015316 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015317
Svetoslav Ganova0156172011-06-26 17:55:44 -070015318 /**
15319 * Resuable callback for sending
15320 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15321 */
15322 private class SendViewScrolledAccessibilityEvent implements Runnable {
15323 public volatile boolean mIsPending;
15324
15325 public void run() {
15326 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15327 mIsPending = false;
15328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015329 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015330
15331 /**
15332 * <p>
15333 * This class represents a delegate that can be registered in a {@link View}
15334 * to enhance accessibility support via composition rather via inheritance.
15335 * It is specifically targeted to widget developers that extend basic View
15336 * classes i.e. classes in package android.view, that would like their
15337 * applications to be backwards compatible.
15338 * </p>
15339 * <p>
15340 * A scenario in which a developer would like to use an accessibility delegate
15341 * is overriding a method introduced in a later API version then the minimal API
15342 * version supported by the application. For example, the method
15343 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15344 * in API version 4 when the accessibility APIs were first introduced. If a
15345 * developer would like his application to run on API version 4 devices (assuming
15346 * all other APIs used by the application are version 4 or lower) and take advantage
15347 * of this method, instead of overriding the method which would break the application's
15348 * backwards compatibility, he can override the corresponding method in this
15349 * delegate and register the delegate in the target View if the API version of
15350 * the system is high enough i.e. the API version is same or higher to the API
15351 * version that introduced
15352 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15353 * </p>
15354 * <p>
15355 * Here is an example implementation:
15356 * </p>
15357 * <code><pre><p>
15358 * if (Build.VERSION.SDK_INT >= 14) {
15359 * // If the API version is equal of higher than the version in
15360 * // which onInitializeAccessibilityNodeInfo was introduced we
15361 * // register a delegate with a customized implementation.
15362 * View view = findViewById(R.id.view_id);
15363 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15364 * public void onInitializeAccessibilityNodeInfo(View host,
15365 * AccessibilityNodeInfo info) {
15366 * // Let the default implementation populate the info.
15367 * super.onInitializeAccessibilityNodeInfo(host, info);
15368 * // Set some other information.
15369 * info.setEnabled(host.isEnabled());
15370 * }
15371 * });
15372 * }
15373 * </code></pre></p>
15374 * <p>
15375 * This delegate contains methods that correspond to the accessibility methods
15376 * in View. If a delegate has been specified the implementation in View hands
15377 * off handling to the corresponding method in this delegate. The default
15378 * implementation the delegate methods behaves exactly as the corresponding
15379 * method in View for the case of no accessibility delegate been set. Hence,
15380 * to customize the behavior of a View method, clients can override only the
15381 * corresponding delegate method without altering the behavior of the rest
15382 * accessibility related methods of the host view.
15383 * </p>
15384 */
15385 public static class AccessibilityDelegate {
15386
15387 /**
15388 * Sends an accessibility event of the given type. If accessibility is not
15389 * enabled this method has no effect.
15390 * <p>
15391 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15392 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15393 * been set.
15394 * </p>
15395 *
15396 * @param host The View hosting the delegate.
15397 * @param eventType The type of the event to send.
15398 *
15399 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15400 */
15401 public void sendAccessibilityEvent(View host, int eventType) {
15402 host.sendAccessibilityEventInternal(eventType);
15403 }
15404
15405 /**
15406 * Sends an accessibility event. This method behaves exactly as
15407 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15408 * empty {@link AccessibilityEvent} and does not perform a check whether
15409 * accessibility is enabled.
15410 * <p>
15411 * The default implementation behaves as
15412 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15413 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15414 * the case of no accessibility delegate been set.
15415 * </p>
15416 *
15417 * @param host The View hosting the delegate.
15418 * @param event The event to send.
15419 *
15420 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15421 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15422 */
15423 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15424 host.sendAccessibilityEventUncheckedInternal(event);
15425 }
15426
15427 /**
15428 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15429 * to its children for adding their text content to the event.
15430 * <p>
15431 * The default implementation behaves as
15432 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15433 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15434 * the case of no accessibility delegate been set.
15435 * </p>
15436 *
15437 * @param host The View hosting the delegate.
15438 * @param event The event.
15439 * @return True if the event population was completed.
15440 *
15441 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15442 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15443 */
15444 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15445 return host.dispatchPopulateAccessibilityEventInternal(event);
15446 }
15447
15448 /**
15449 * Gives a chance to the host View to populate the accessibility event with its
15450 * text content.
15451 * <p>
15452 * The default implementation behaves as
15453 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15454 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15455 * the case of no accessibility delegate been set.
15456 * </p>
15457 *
15458 * @param host The View hosting the delegate.
15459 * @param event The accessibility event which to populate.
15460 *
15461 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15462 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15463 */
15464 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15465 host.onPopulateAccessibilityEventInternal(event);
15466 }
15467
15468 /**
15469 * Initializes an {@link AccessibilityEvent} with information about the
15470 * the host View which is the event source.
15471 * <p>
15472 * The default implementation behaves as
15473 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15474 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15475 * the case of no accessibility delegate been set.
15476 * </p>
15477 *
15478 * @param host The View hosting the delegate.
15479 * @param event The event to initialize.
15480 *
15481 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15482 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15483 */
15484 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15485 host.onInitializeAccessibilityEventInternal(event);
15486 }
15487
15488 /**
15489 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15490 * <p>
15491 * The default implementation behaves as
15492 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15493 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15494 * the case of no accessibility delegate been set.
15495 * </p>
15496 *
15497 * @param host The View hosting the delegate.
15498 * @param info The instance to initialize.
15499 *
15500 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15501 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15502 */
15503 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15504 host.onInitializeAccessibilityNodeInfoInternal(info);
15505 }
15506
15507 /**
15508 * Called when a child of the host View has requested sending an
15509 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15510 * to augment the event.
15511 * <p>
15512 * The default implementation behaves as
15513 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15514 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15515 * the case of no accessibility delegate been set.
15516 * </p>
15517 *
15518 * @param host The View hosting the delegate.
15519 * @param child The child which requests sending the event.
15520 * @param event The event to be sent.
15521 * @return True if the event should be sent
15522 *
15523 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15524 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15525 */
15526 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15527 AccessibilityEvent event) {
15528 return host.onRequestSendAccessibilityEventInternal(child, event);
15529 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015530
15531 /**
15532 * Gets the provider for managing a virtual view hierarchy rooted at this View
15533 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15534 * that explore the window content.
15535 * <p>
15536 * The default implementation behaves as
15537 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15538 * the case of no accessibility delegate been set.
15539 * </p>
15540 *
15541 * @return The provider.
15542 *
15543 * @see AccessibilityNodeProvider
15544 */
15545 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15546 return null;
15547 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015549}