blob: 6ac27a70b46e1dd02ba810614875b477939f8047 [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
339 * {@link #setPadding(int, int, int, int)} method and queried by calling
340 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700341 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 * </p>
343 *
344 * <p>
345 * Even though a view can define a padding, it does not provide any support for
346 * margins. However, view groups provide such a support. Refer to
347 * {@link android.view.ViewGroup} and
348 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
349 * </p>
350 *
351 * <a name="Layout"></a>
352 * <h3>Layout</h3>
353 * <p>
354 * Layout is a two pass process: a measure pass and a layout pass. The measuring
355 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
356 * of the view tree. Each view pushes dimension specifications down the tree
357 * during the recursion. At the end of the measure pass, every view has stored
358 * its measurements. The second pass happens in
359 * {@link #layout(int,int,int,int)} and is also top-down. During
360 * this pass each parent is responsible for positioning all of its children
361 * using the sizes computed in the measure pass.
362 * </p>
363 *
364 * <p>
365 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
366 * {@link #getMeasuredHeight()} values must be set, along with those for all of
367 * that view's descendants. A view's measured width and measured height values
368 * must respect the constraints imposed by the view's parents. This guarantees
369 * that at the end of the measure pass, all parents accept all of their
370 * children's measurements. A parent view may call measure() more than once on
371 * its children. For example, the parent may measure each child once with
372 * unspecified dimensions to find out how big they want to be, then call
373 * measure() on them again with actual numbers if the sum of all the children's
374 * unconstrained sizes is too big or too small.
375 * </p>
376 *
377 * <p>
378 * The measure pass uses two classes to communicate dimensions. The
379 * {@link MeasureSpec} class is used by views to tell their parents how they
380 * want to be measured and positioned. The base LayoutParams class just
381 * describes how big the view wants to be for both width and height. For each
382 * dimension, it can specify one of:
383 * <ul>
384 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800385 * <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 -0800386 * (minus padding)
387 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
388 * enclose its content (plus padding).
389 * </ul>
390 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
391 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
392 * an X and Y value.
393 * </p>
394 *
395 * <p>
396 * MeasureSpecs are used to push requirements down the tree from parent to
397 * child. A MeasureSpec can be in one of three modes:
398 * <ul>
399 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
400 * of a child view. For example, a LinearLayout may call measure() on its child
401 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
402 * tall the child view wants to be given a width of 240 pixels.
403 * <li>EXACTLY: This is used by the parent to impose an exact size on the
404 * child. The child must use this size, and guarantee that all of its
405 * descendants will fit within this size.
406 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
407 * child. The child must gurantee that it and all of its descendants will fit
408 * within this size.
409 * </ul>
410 * </p>
411 *
412 * <p>
413 * To intiate a layout, call {@link #requestLayout}. This method is typically
414 * called by a view on itself when it believes that is can no longer fit within
415 * its current bounds.
416 * </p>
417 *
418 * <a name="Drawing"></a>
419 * <h3>Drawing</h3>
420 * <p>
421 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700422 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * this means that parents will draw before (i.e., behind) their children, with
424 * siblings drawn in the order they appear in the tree.
425 * If you set a background drawable for a View, then the View will draw it for you
426 * before calling back to its <code>onDraw()</code> method.
427 * </p>
428 *
429 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700430 * 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 -0800431 * </p>
432 *
433 * <p>
434 * To force a view to draw, call {@link #invalidate()}.
435 * </p>
436 *
437 * <a name="EventHandlingThreading"></a>
438 * <h3>Event Handling and Threading</h3>
439 * <p>
440 * The basic cycle of a view is as follows:
441 * <ol>
442 * <li>An event comes in and is dispatched to the appropriate view. The view
443 * handles the event and notifies any listeners.</li>
444 * <li>If in the course of processing the event, the view's bounds may need
445 * to be changed, the view will call {@link #requestLayout()}.</li>
446 * <li>Similarly, if in the course of processing the event the view's appearance
447 * may need to be changed, the view will call {@link #invalidate()}.</li>
448 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
449 * the framework will take care of measuring, laying out, and drawing the tree
450 * as appropriate.</li>
451 * </ol>
452 * </p>
453 *
454 * <p><em>Note: The entire view tree is single threaded. You must always be on
455 * the UI thread when calling any method on any view.</em>
456 * If you are doing work on other threads and want to update the state of a view
457 * from that thread, you should use a {@link Handler}.
458 * </p>
459 *
460 * <a name="FocusHandling"></a>
461 * <h3>Focus Handling</h3>
462 * <p>
463 * The framework will handle routine focus movement in response to user input.
464 * This includes changing the focus as views are removed or hidden, or as new
465 * views become available. Views indicate their willingness to take focus
466 * through the {@link #isFocusable} method. To change whether a view can take
467 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
468 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
469 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
470 * </p>
471 * <p>
472 * Focus movement is based on an algorithm which finds the nearest neighbor in a
473 * given direction. In rare cases, the default algorithm may not match the
474 * intended behavior of the developer. In these situations, you can provide
475 * explicit overrides by using these XML attributes in the layout file:
476 * <pre>
477 * nextFocusDown
478 * nextFocusLeft
479 * nextFocusRight
480 * nextFocusUp
481 * </pre>
482 * </p>
483 *
484 *
485 * <p>
486 * To get a particular view to take focus, call {@link #requestFocus()}.
487 * </p>
488 *
489 * <a name="TouchMode"></a>
490 * <h3>Touch Mode</h3>
491 * <p>
492 * When a user is navigating a user interface via directional keys such as a D-pad, it is
493 * necessary to give focus to actionable items such as buttons so the user can see
494 * what will take input. If the device has touch capabilities, however, and the user
495 * begins interacting with the interface by touching it, it is no longer necessary to
496 * always highlight, or give focus to, a particular view. This motivates a mode
497 * for interaction named 'touch mode'.
498 * </p>
499 * <p>
500 * For a touch capable device, once the user touches the screen, the device
501 * will enter touch mode. From this point onward, only views for which
502 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
503 * Other views that are touchable, like buttons, will not take focus when touched; they will
504 * only fire the on click listeners.
505 * </p>
506 * <p>
507 * Any time a user hits a directional key, such as a D-pad direction, the view device will
508 * exit touch mode, and find a view to take focus, so that the user may resume interacting
509 * with the user interface without touching the screen again.
510 * </p>
511 * <p>
512 * The touch mode state is maintained across {@link android.app.Activity}s. Call
513 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
514 * </p>
515 *
516 * <a name="Scrolling"></a>
517 * <h3>Scrolling</h3>
518 * <p>
519 * The framework provides basic support for views that wish to internally
520 * scroll their content. This includes keeping track of the X and Y scroll
521 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800522 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700523 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * </p>
525 *
526 * <a name="Tags"></a>
527 * <h3>Tags</h3>
528 * <p>
529 * Unlike IDs, tags are not used to identify views. Tags are essentially an
530 * extra piece of information that can be associated with a view. They are most
531 * often used as a convenience to store data related to views in the views
532 * themselves rather than by putting them in a separate structure.
533 * </p>
534 *
535 * <a name="Animation"></a>
536 * <h3>Animation</h3>
537 * <p>
538 * You can attach an {@link Animation} object to a view using
539 * {@link #setAnimation(Animation)} or
540 * {@link #startAnimation(Animation)}. The animation can alter the scale,
541 * rotation, translation and alpha of a view over time. If the animation is
542 * attached to a view that has children, the animation will affect the entire
543 * subtree rooted by that node. When an animation is started, the framework will
544 * take care of redrawing the appropriate views until the animation completes.
545 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800546 * <p>
547 * Starting with Android 3.0, the preferred way of animating views is to use the
548 * {@link android.animation} package APIs.
549 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 *
Jeff Brown85a31762010-09-01 17:01:00 -0700551 * <a name="Security"></a>
552 * <h3>Security</h3>
553 * <p>
554 * Sometimes it is essential that an application be able to verify that an action
555 * is being performed with the full knowledge and consent of the user, such as
556 * granting a permission request, making a purchase or clicking on an advertisement.
557 * Unfortunately, a malicious application could try to spoof the user into
558 * performing these actions, unaware, by concealing the intended purpose of the view.
559 * As a remedy, the framework offers a touch filtering mechanism that can be used to
560 * improve the security of views that provide access to sensitive functionality.
561 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700562 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800563 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700564 * will discard touches that are received whenever the view's window is obscured by
565 * another visible window. As a result, the view will not receive touches whenever a
566 * toast, dialog or other window appears above the view's window.
567 * </p><p>
568 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700569 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
570 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700571 * </p>
572 *
Romain Guy171c5922011-01-06 10:04:23 -0800573 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700574 * @attr ref android.R.styleable#View_background
575 * @attr ref android.R.styleable#View_clickable
576 * @attr ref android.R.styleable#View_contentDescription
577 * @attr ref android.R.styleable#View_drawingCacheQuality
578 * @attr ref android.R.styleable#View_duplicateParentState
579 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700580 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700581 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700582 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700584 * @attr ref android.R.styleable#View_isScrollContainer
585 * @attr ref android.R.styleable#View_focusable
586 * @attr ref android.R.styleable#View_focusableInTouchMode
587 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
588 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800589 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700590 * @attr ref android.R.styleable#View_longClickable
591 * @attr ref android.R.styleable#View_minHeight
592 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * @attr ref android.R.styleable#View_nextFocusDown
594 * @attr ref android.R.styleable#View_nextFocusLeft
595 * @attr ref android.R.styleable#View_nextFocusRight
596 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700597 * @attr ref android.R.styleable#View_onClick
598 * @attr ref android.R.styleable#View_padding
599 * @attr ref android.R.styleable#View_paddingBottom
600 * @attr ref android.R.styleable#View_paddingLeft
601 * @attr ref android.R.styleable#View_paddingRight
602 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800603 * @attr ref android.R.styleable#View_paddingStart
604 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700605 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800606 * @attr ref android.R.styleable#View_rotation
607 * @attr ref android.R.styleable#View_rotationX
608 * @attr ref android.R.styleable#View_rotationY
609 * @attr ref android.R.styleable#View_scaleX
610 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * @attr ref android.R.styleable#View_scrollX
612 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700613 * @attr ref android.R.styleable#View_scrollbarSize
614 * @attr ref android.R.styleable#View_scrollbarStyle
615 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700616 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
617 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
619 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_scrollbarThumbVertical
621 * @attr ref android.R.styleable#View_scrollbarTrackVertical
622 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_soundEffectsEnabled
625 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800626 * @attr ref android.R.styleable#View_transformPivotX
627 * @attr ref android.R.styleable#View_transformPivotY
628 * @attr ref android.R.styleable#View_translationX
629 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700630 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 *
632 * @see android.view.ViewGroup
633 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700634public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
635 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
Scott Main812634c22011-07-27 13:22:35 -0700671 * This view is visible.
672 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
673 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 */
675 public static final int VISIBLE = 0x00000000;
676
677 /**
678 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700679 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
680 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 */
682 public static final int INVISIBLE = 0x00000004;
683
684 /**
685 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700686 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
687 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static final int GONE = 0x00000008;
690
691 /**
692 * Mask for use with setFlags indicating bits used for visibility.
693 * {@hide}
694 */
695 static final int VISIBILITY_MASK = 0x0000000C;
696
697 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
698
699 /**
700 * This view is enabled. Intrepretation varies by subclass.
701 * Use with ENABLED_MASK when calling setFlags.
702 * {@hide}
703 */
704 static final int ENABLED = 0x00000000;
705
706 /**
707 * This view is disabled. Intrepretation varies by subclass.
708 * Use with ENABLED_MASK when calling setFlags.
709 * {@hide}
710 */
711 static final int DISABLED = 0x00000020;
712
713 /**
714 * Mask for use with setFlags indicating bits used for indicating whether
715 * this view is enabled
716 * {@hide}
717 */
718 static final int ENABLED_MASK = 0x00000020;
719
720 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700721 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
722 * called and further optimizations will be performed. It is okay to have
723 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 * {@hide}
725 */
726 static final int WILL_NOT_DRAW = 0x00000080;
727
728 /**
729 * Mask for use with setFlags indicating bits used for indicating whether
730 * this view is will draw
731 * {@hide}
732 */
733 static final int DRAW_MASK = 0x00000080;
734
735 /**
736 * <p>This view doesn't show scrollbars.</p>
737 * {@hide}
738 */
739 static final int SCROLLBARS_NONE = 0x00000000;
740
741 /**
742 * <p>This view shows horizontal scrollbars.</p>
743 * {@hide}
744 */
745 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
746
747 /**
748 * <p>This view shows vertical scrollbars.</p>
749 * {@hide}
750 */
751 static final int SCROLLBARS_VERTICAL = 0x00000200;
752
753 /**
754 * <p>Mask for use with setFlags indicating bits used for indicating which
755 * scrollbars are enabled.</p>
756 * {@hide}
757 */
758 static final int SCROLLBARS_MASK = 0x00000300;
759
Jeff Brown85a31762010-09-01 17:01:00 -0700760 /**
761 * Indicates that the view should filter touches when its window is obscured.
762 * Refer to the class comments for more information about this security feature.
763 * {@hide}
764 */
765 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
766
767 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
769 /**
770 * <p>This view doesn't show fading edges.</p>
771 * {@hide}
772 */
773 static final int FADING_EDGE_NONE = 0x00000000;
774
775 /**
776 * <p>This view shows horizontal fading edges.</p>
777 * {@hide}
778 */
779 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
780
781 /**
782 * <p>This view shows vertical fading edges.</p>
783 * {@hide}
784 */
785 static final int FADING_EDGE_VERTICAL = 0x00002000;
786
787 /**
788 * <p>Mask for use with setFlags indicating bits used for indicating which
789 * fading edges are enabled.</p>
790 * {@hide}
791 */
792 static final int FADING_EDGE_MASK = 0x00003000;
793
794 /**
795 * <p>Indicates this view can be clicked. When clickable, a View reacts
796 * to clicks by notifying the OnClickListener.<p>
797 * {@hide}
798 */
799 static final int CLICKABLE = 0x00004000;
800
801 /**
802 * <p>Indicates this view is caching its drawing into a bitmap.</p>
803 * {@hide}
804 */
805 static final int DRAWING_CACHE_ENABLED = 0x00008000;
806
807 /**
808 * <p>Indicates that no icicle should be saved for this view.<p>
809 * {@hide}
810 */
811 static final int SAVE_DISABLED = 0x000010000;
812
813 /**
814 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
815 * property.</p>
816 * {@hide}
817 */
818 static final int SAVE_DISABLED_MASK = 0x000010000;
819
820 /**
821 * <p>Indicates that no drawing cache should ever be created for this view.<p>
822 * {@hide}
823 */
824 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
825
826 /**
827 * <p>Indicates this view can take / keep focus when int touch mode.</p>
828 * {@hide}
829 */
830 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
831
832 /**
833 * <p>Enables low quality mode for the drawing cache.</p>
834 */
835 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
836
837 /**
838 * <p>Enables high quality mode for the drawing cache.</p>
839 */
840 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
841
842 /**
843 * <p>Enables automatic quality mode for the drawing cache.</p>
844 */
845 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
846
847 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
848 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
849 };
850
851 /**
852 * <p>Mask for use with setFlags indicating bits used for the cache
853 * quality property.</p>
854 * {@hide}
855 */
856 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
857
858 /**
859 * <p>
860 * Indicates this view can be long clicked. When long clickable, a View
861 * reacts to long clicks by notifying the OnLongClickListener or showing a
862 * context menu.
863 * </p>
864 * {@hide}
865 */
866 static final int LONG_CLICKABLE = 0x00200000;
867
868 /**
869 * <p>Indicates that this view gets its drawable states from its direct parent
870 * and ignores its original internal states.</p>
871 *
872 * @hide
873 */
874 static final int DUPLICATE_PARENT_STATE = 0x00400000;
875
876 /**
877 * The scrollbar style to display the scrollbars inside the content area,
878 * without increasing the padding. The scrollbars will be overlaid with
879 * translucency on the view's content.
880 */
881 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
882
883 /**
884 * The scrollbar style to display the scrollbars inside the padded area,
885 * increasing the padding of the view. The scrollbars will not overlap the
886 * content area of the view.
887 */
888 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
889
890 /**
891 * The scrollbar style to display the scrollbars at the edge of the view,
892 * without increasing the padding. The scrollbars will be overlaid with
893 * translucency.
894 */
895 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
896
897 /**
898 * The scrollbar style to display the scrollbars at the edge of the view,
899 * increasing the padding of the view. The scrollbars will only overlap the
900 * background, if any.
901 */
902 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
903
904 /**
905 * Mask to check if the scrollbar style is overlay or inset.
906 * {@hide}
907 */
908 static final int SCROLLBARS_INSET_MASK = 0x01000000;
909
910 /**
911 * Mask to check if the scrollbar style is inside or outside.
912 * {@hide}
913 */
914 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
915
916 /**
917 * Mask for scrollbar style.
918 * {@hide}
919 */
920 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
921
922 /**
923 * View flag indicating that the screen should remain on while the
924 * window containing this view is visible to the user. This effectively
925 * takes care of automatically setting the WindowManager's
926 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
927 */
928 public static final int KEEP_SCREEN_ON = 0x04000000;
929
930 /**
931 * View flag indicating whether this view should have sound effects enabled
932 * for events such as clicking and touching.
933 */
934 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
935
936 /**
937 * View flag indicating whether this view should have haptic feedback
938 * enabled for events such as long presses.
939 */
940 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
941
942 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700943 * <p>Indicates that the view hierarchy should stop saving state when
944 * it reaches this view. If state saving is initiated immediately at
945 * the view, it will be allowed.
946 * {@hide}
947 */
948 static final int PARENT_SAVE_DISABLED = 0x20000000;
949
950 /**
951 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
952 * {@hide}
953 */
954 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
955
956 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800957 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700958 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800959 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700960 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800961
962 /**
963 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700964 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800965 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700966 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800967
968 /**
969 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700970 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800971 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800973
974 /**
975 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700976 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800977 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700978 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800979
980 /**
981 * Mask for use with setFlags indicating bits used for horizontalDirection.
982 * {@hide}
983 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700984 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800985
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700986 /*
987 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
988 * flag value.
989 * {@hide}
990 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700991 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
992 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800993
994 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700995 * Default horizontalDirection.
996 * {@hide}
997 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700998 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700999
1000 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001001 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1002 * should add all focusable Views regardless if they are focusable in touch mode.
1003 */
1004 public static final int FOCUSABLES_ALL = 0x00000000;
1005
1006 /**
1007 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1008 * should add only Views focusable in touch mode.
1009 */
1010 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1011
1012 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001013 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 * item.
1015 */
1016 public static final int FOCUS_BACKWARD = 0x00000001;
1017
1018 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001019 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * item.
1021 */
1022 public static final int FOCUS_FORWARD = 0x00000002;
1023
1024 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001025 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 */
1027 public static final int FOCUS_LEFT = 0x00000011;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final int FOCUS_UP = 0x00000021;
1033
1034 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001035 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public static final int FOCUS_RIGHT = 0x00000042;
1038
1039 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001040 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public static final int FOCUS_DOWN = 0x00000082;
1043
1044 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001045 * Bits of {@link #getMeasuredWidthAndState()} and
1046 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1047 */
1048 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1049
1050 /**
1051 * Bits of {@link #getMeasuredWidthAndState()} and
1052 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1053 */
1054 public static final int MEASURED_STATE_MASK = 0xff000000;
1055
1056 /**
1057 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1058 * for functions that combine both width and height into a single int,
1059 * such as {@link #getMeasuredState()} and the childState argument of
1060 * {@link #resolveSizeAndState(int, int, int)}.
1061 */
1062 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1063
1064 /**
1065 * Bit of {@link #getMeasuredWidthAndState()} and
1066 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1067 * is smaller that the space the view would like to have.
1068 */
1069 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1070
1071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 * Base View state sets
1073 */
1074 // Singles
1075 /**
1076 * Indicates the view has no states set. States are used with
1077 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1078 * view depending on its state.
1079 *
1080 * @see android.graphics.drawable.Drawable
1081 * @see #getDrawableState()
1082 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001083 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 /**
1085 * Indicates the view is enabled. States are used with
1086 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1087 * view depending on its state.
1088 *
1089 * @see android.graphics.drawable.Drawable
1090 * @see #getDrawableState()
1091 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001092 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 /**
1094 * Indicates the view is focused. States are used with
1095 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1096 * view depending on its state.
1097 *
1098 * @see android.graphics.drawable.Drawable
1099 * @see #getDrawableState()
1100 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001101 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 /**
1103 * Indicates the view is selected. States are used with
1104 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1105 * view depending on its state.
1106 *
1107 * @see android.graphics.drawable.Drawable
1108 * @see #getDrawableState()
1109 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001110 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 /**
1112 * Indicates the view is pressed. States are used with
1113 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1114 * view depending on its state.
1115 *
1116 * @see android.graphics.drawable.Drawable
1117 * @see #getDrawableState()
1118 * @hide
1119 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001120 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 /**
1122 * Indicates the view's window has focus. States are used with
1123 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1124 * view depending on its state.
1125 *
1126 * @see android.graphics.drawable.Drawable
1127 * @see #getDrawableState()
1128 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001129 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 // Doubles
1131 /**
1132 * Indicates the view is enabled and has the focus.
1133 *
1134 * @see #ENABLED_STATE_SET
1135 * @see #FOCUSED_STATE_SET
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 /**
1139 * Indicates the view is enabled and selected.
1140 *
1141 * @see #ENABLED_STATE_SET
1142 * @see #SELECTED_STATE_SET
1143 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001144 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 /**
1146 * Indicates the view is enabled and that its window has focus.
1147 *
1148 * @see #ENABLED_STATE_SET
1149 * @see #WINDOW_FOCUSED_STATE_SET
1150 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001151 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 /**
1153 * Indicates the view is focused and selected.
1154 *
1155 * @see #FOCUSED_STATE_SET
1156 * @see #SELECTED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view has the focus and that its window has the focus.
1161 *
1162 * @see #FOCUSED_STATE_SET
1163 * @see #WINDOW_FOCUSED_STATE_SET
1164 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001165 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 /**
1167 * Indicates the view is selected and that its window has the focus.
1168 *
1169 * @see #SELECTED_STATE_SET
1170 * @see #WINDOW_FOCUSED_STATE_SET
1171 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001172 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 // Triples
1174 /**
1175 * Indicates the view is enabled, focused and selected.
1176 *
1177 * @see #ENABLED_STATE_SET
1178 * @see #FOCUSED_STATE_SET
1179 * @see #SELECTED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 /**
1183 * Indicates the view is enabled, focused and its window has the focus.
1184 *
1185 * @see #ENABLED_STATE_SET
1186 * @see #FOCUSED_STATE_SET
1187 * @see #WINDOW_FOCUSED_STATE_SET
1188 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001189 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 /**
1191 * Indicates the view is enabled, selected and its window has the focus.
1192 *
1193 * @see #ENABLED_STATE_SET
1194 * @see #SELECTED_STATE_SET
1195 * @see #WINDOW_FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is focused, selected and its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #SELECTED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is enabled, focused, selected and its window
1208 * has the focus.
1209 *
1210 * @see #ENABLED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 * @see #SELECTED_STATE_SET
1213 * @see #WINDOW_FOCUSED_STATE_SET
1214 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001215 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 /**
1217 * Indicates the view is pressed and its window has the focus.
1218 *
1219 * @see #PRESSED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and selected.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #SELECTED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, selected and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #SELECTED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed and focused.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is pressed, focused and its window has the focus.
1247 *
1248 * @see #PRESSED_STATE_SET
1249 * @see #FOCUSED_STATE_SET
1250 * @see #WINDOW_FOCUSED_STATE_SET
1251 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001252 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * Indicates the view is pressed, focused and selected.
1255 *
1256 * @see #PRESSED_STATE_SET
1257 * @see #SELECTED_STATE_SET
1258 * @see #FOCUSED_STATE_SET
1259 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001260 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 /**
1262 * Indicates the view is pressed, focused, selected and its window has the focus.
1263 *
1264 * @see #PRESSED_STATE_SET
1265 * @see #FOCUSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 * @see #WINDOW_FOCUSED_STATE_SET
1268 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001269 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 /**
1271 * Indicates the view is pressed and enabled.
1272 *
1273 * @see #PRESSED_STATE_SET
1274 * @see #ENABLED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed, enabled and its window has the focus.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #ENABLED_STATE_SET
1282 * @see #WINDOW_FOCUSED_STATE_SET
1283 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001284 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 /**
1286 * Indicates the view is pressed, enabled and selected.
1287 *
1288 * @see #PRESSED_STATE_SET
1289 * @see #ENABLED_STATE_SET
1290 * @see #SELECTED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed, enabled, selected and its window has the
1295 * focus.
1296 *
1297 * @see #PRESSED_STATE_SET
1298 * @see #ENABLED_STATE_SET
1299 * @see #SELECTED_STATE_SET
1300 * @see #WINDOW_FOCUSED_STATE_SET
1301 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001302 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 /**
1304 * Indicates the view is pressed, enabled and focused.
1305 *
1306 * @see #PRESSED_STATE_SET
1307 * @see #ENABLED_STATE_SET
1308 * @see #FOCUSED_STATE_SET
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
1312 * Indicates the view is pressed, enabled, focused and its window has the
1313 * focus.
1314 *
1315 * @see #PRESSED_STATE_SET
1316 * @see #ENABLED_STATE_SET
1317 * @see #FOCUSED_STATE_SET
1318 * @see #WINDOW_FOCUSED_STATE_SET
1319 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001320 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 /**
1322 * Indicates the view is pressed, enabled, focused and selected.
1323 *
1324 * @see #PRESSED_STATE_SET
1325 * @see #ENABLED_STATE_SET
1326 * @see #SELECTED_STATE_SET
1327 * @see #FOCUSED_STATE_SET
1328 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 /**
1331 * Indicates the view is pressed, enabled, focused, selected and its window
1332 * has the focus.
1333 *
1334 * @see #PRESSED_STATE_SET
1335 * @see #ENABLED_STATE_SET
1336 * @see #SELECTED_STATE_SET
1337 * @see #FOCUSED_STATE_SET
1338 * @see #WINDOW_FOCUSED_STATE_SET
1339 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341
1342 /**
1343 * The order here is very important to {@link #getDrawableState()}
1344 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 private static final int[][] VIEW_STATE_SETS;
1346
Romain Guyb051e892010-09-28 19:09:36 -07001347 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1348 static final int VIEW_STATE_SELECTED = 1 << 1;
1349 static final int VIEW_STATE_FOCUSED = 1 << 2;
1350 static final int VIEW_STATE_ENABLED = 1 << 3;
1351 static final int VIEW_STATE_PRESSED = 1 << 4;
1352 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001353 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001354 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001355 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1356 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001357
1358 static final int[] VIEW_STATE_IDS = new int[] {
1359 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1360 R.attr.state_selected, VIEW_STATE_SELECTED,
1361 R.attr.state_focused, VIEW_STATE_FOCUSED,
1362 R.attr.state_enabled, VIEW_STATE_ENABLED,
1363 R.attr.state_pressed, VIEW_STATE_PRESSED,
1364 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001365 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001366 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001367 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1368 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 };
1370
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001371 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001372 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1373 throw new IllegalStateException(
1374 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1375 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001377 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001379 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001381 orderedIds[i * 2] = viewState;
1382 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 }
1384 }
1385 }
Romain Guyb051e892010-09-28 19:09:36 -07001386 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1387 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1388 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 int numBits = Integer.bitCount(i);
1390 int[] set = new int[numBits];
1391 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001392 for (int j = 0; j < orderedIds.length; j += 2) {
1393 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 set[pos++] = orderedIds[j];
1395 }
1396 }
1397 VIEW_STATE_SETS[i] = set;
1398 }
1399
1400 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1401 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1402 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1403 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1405 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1406 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1408 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1410 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1412 | VIEW_STATE_FOCUSED];
1413 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1414 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1416 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1418 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1420 | VIEW_STATE_ENABLED];
1421 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1423 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1428 | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1431 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1432
1433 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1434 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1436 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1438 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1440 | VIEW_STATE_PRESSED];
1441 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1443 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1448 | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1452 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1454 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1459 | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1462 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1465 | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1468 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1471 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1472 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1474 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1475 | VIEW_STATE_PRESSED];
1476 }
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001479 * Accessibility event types that are dispatched for text population.
1480 */
1481 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1482 AccessibilityEvent.TYPE_VIEW_CLICKED
1483 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1484 | AccessibilityEvent.TYPE_VIEW_SELECTED
1485 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1486 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1487 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001488 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001489 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1490 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001491
1492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * Temporary Rect currently for use in setBackground(). This will probably
1494 * be extended in the future to hold our own class with more than just
1495 * a Rect. :)
1496 */
1497 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001498
1499 /**
1500 * Map used to store views' tags.
1501 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001502 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001505 * The next available accessiiblity id.
1506 */
1507 private static int sNextAccessibilityViewId;
1508
1509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * The animation currently associated with this view.
1511 * @hide
1512 */
1513 protected Animation mCurrentAnimation = null;
1514
1515 /**
1516 * Width as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
1523 * Height as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001530 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1531 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1532 * its display list. This flag, used only when hw accelerated, allows us to clear the
1533 * flag while retaining this information until it's needed (at getDisplayList() time and
1534 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1535 *
1536 * {@hide}
1537 */
1538 boolean mRecreateDisplayList = false;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The view's identifier.
1542 * {@hide}
1543 *
1544 * @see #setId(int)
1545 * @see #getId()
1546 */
1547 @ViewDebug.ExportedProperty(resolveId = true)
1548 int mID = NO_ID;
1549
1550 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001551 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001552 */
1553 int mAccessibilityViewId = NO_ID;
1554
1555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 * The view's tag.
1557 * {@hide}
1558 *
1559 * @see #setTag(Object)
1560 * @see #getTag()
1561 */
1562 protected Object mTag;
1563
1564 // for mPrivateFlags:
1565 /** {@hide} */
1566 static final int WANTS_FOCUS = 0x00000001;
1567 /** {@hide} */
1568 static final int FOCUSED = 0x00000002;
1569 /** {@hide} */
1570 static final int SELECTED = 0x00000004;
1571 /** {@hide} */
1572 static final int IS_ROOT_NAMESPACE = 0x00000008;
1573 /** {@hide} */
1574 static final int HAS_BOUNDS = 0x00000010;
1575 /** {@hide} */
1576 static final int DRAWN = 0x00000020;
1577 /**
1578 * When this flag is set, this view is running an animation on behalf of its
1579 * children and should therefore not cancel invalidate requests, even if they
1580 * lie outside of this view's bounds.
1581 *
1582 * {@hide}
1583 */
1584 static final int DRAW_ANIMATION = 0x00000040;
1585 /** {@hide} */
1586 static final int SKIP_DRAW = 0x00000080;
1587 /** {@hide} */
1588 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1589 /** {@hide} */
1590 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1591 /** {@hide} */
1592 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1593 /** {@hide} */
1594 static final int MEASURED_DIMENSION_SET = 0x00000800;
1595 /** {@hide} */
1596 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001597 /** {@hide} */
1598 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599
1600 private static final int PRESSED = 0x00004000;
1601
1602 /** {@hide} */
1603 static final int DRAWING_CACHE_VALID = 0x00008000;
1604 /**
1605 * Flag used to indicate that this view should be drawn once more (and only once
1606 * more) after its animation has completed.
1607 * {@hide}
1608 */
1609 static final int ANIMATION_STARTED = 0x00010000;
1610
1611 private static final int SAVE_STATE_CALLED = 0x00020000;
1612
1613 /**
1614 * Indicates that the View returned true when onSetAlpha() was called and that
1615 * the alpha must be restored.
1616 * {@hide}
1617 */
1618 static final int ALPHA_SET = 0x00040000;
1619
1620 /**
1621 * Set by {@link #setScrollContainer(boolean)}.
1622 */
1623 static final int SCROLL_CONTAINER = 0x00080000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1629
1630 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001631 * View flag indicating whether this view was invalidated (fully or partially.)
1632 *
1633 * @hide
1634 */
1635 static final int DIRTY = 0x00200000;
1636
1637 /**
1638 * View flag indicating whether this view was invalidated by an opaque
1639 * invalidate request.
1640 *
1641 * @hide
1642 */
1643 static final int DIRTY_OPAQUE = 0x00400000;
1644
1645 /**
1646 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1647 *
1648 * @hide
1649 */
1650 static final int DIRTY_MASK = 0x00600000;
1651
1652 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001653 * Indicates whether the background is opaque.
1654 *
1655 * @hide
1656 */
1657 static final int OPAQUE_BACKGROUND = 0x00800000;
1658
1659 /**
1660 * Indicates whether the scrollbars are opaque.
1661 *
1662 * @hide
1663 */
1664 static final int OPAQUE_SCROLLBARS = 0x01000000;
1665
1666 /**
1667 * Indicates whether the view is opaque.
1668 *
1669 * @hide
1670 */
1671 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001672
Adam Powelle14579b2009-12-16 18:39:52 -08001673 /**
1674 * Indicates a prepressed state;
1675 * the short time between ACTION_DOWN and recognizing
1676 * a 'real' press. Prepressed is used to recognize quick taps
1677 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001678 *
Adam Powelle14579b2009-12-16 18:39:52 -08001679 * @hide
1680 */
1681 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001682
Adam Powellc9fbaab2010-02-16 17:16:19 -08001683 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001684 * Indicates whether the view is temporarily detached.
1685 *
1686 * @hide
1687 */
1688 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001689
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 /**
1691 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001692 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001693 * @hide
1694 */
1695 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001696
1697 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001698 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1699 * @hide
1700 */
1701 private static final int HOVERED = 0x10000000;
1702
1703 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001704 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1705 * for transform operations
1706 *
1707 * @hide
1708 */
Adam Powellf37df072010-09-17 16:22:49 -07001709 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001710
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001711 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001712 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001713
Chet Haasefd2b0022010-08-06 13:08:56 -07001714 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001715 * Indicates that this view was specifically invalidated, not just dirtied because some
1716 * child view was invalidated. The flag is used to determine when we need to recreate
1717 * a view's display list (as opposed to just returning a reference to its existing
1718 * display list).
1719 *
1720 * @hide
1721 */
1722 static final int INVALIDATED = 0x80000000;
1723
Christopher Tate3d4bf172011-03-28 16:16:46 -07001724 /* Masks for mPrivateFlags2 */
1725
1726 /**
1727 * Indicates that this view has reported that it can accept the current drag's content.
1728 * Cleared when the drag operation concludes.
1729 * @hide
1730 */
1731 static final int DRAG_CAN_ACCEPT = 0x00000001;
1732
1733 /**
1734 * Indicates that this view is currently directly under the drag location in a
1735 * drag-and-drop operation involving content that it can accept. Cleared when
1736 * the drag exits the view, or when the drag operation concludes.
1737 * @hide
1738 */
1739 static final int DRAG_HOVERED = 0x00000002;
1740
Cibu Johny86666632010-02-22 13:01:02 -08001741 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001742 * Indicates whether the view layout direction has been resolved and drawn to the
1743 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001744 *
1745 * @hide
1746 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001747 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1748
1749 /**
1750 * Indicates whether the view layout direction has been resolved.
1751 *
1752 * @hide
1753 */
1754 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1755
Cibu Johny86666632010-02-22 13:01:02 -08001756
Adam Powell539ee872012-02-03 19:00:49 -08001757 /**
1758 * Indicates that the view is tracking some sort of transient state
1759 * that the app should not need to be aware of, but that the framework
1760 * should take special care to preserve.
1761 *
1762 * @hide
1763 */
1764 static final int HAS_TRANSIENT_STATE = 0x00000010;
1765
1766
Christopher Tate3d4bf172011-03-28 16:16:46 -07001767 /* End of masks for mPrivateFlags2 */
1768
1769 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1770
Chet Haasedaf98e92011-01-10 14:10:36 -08001771 /**
Adam Powell637d3372010-08-25 14:37:03 -07001772 * Always allow a user to over-scroll this view, provided it is a
1773 * view that can scroll.
1774 *
1775 * @see #getOverScrollMode()
1776 * @see #setOverScrollMode(int)
1777 */
1778 public static final int OVER_SCROLL_ALWAYS = 0;
1779
1780 /**
1781 * Allow a user to over-scroll this view only if the content is large
1782 * enough to meaningfully scroll, provided it is a view that can scroll.
1783 *
1784 * @see #getOverScrollMode()
1785 * @see #setOverScrollMode(int)
1786 */
1787 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1788
1789 /**
1790 * Never allow a user to over-scroll this view.
1791 *
1792 * @see #getOverScrollMode()
1793 * @see #setOverScrollMode(int)
1794 */
1795 public static final int OVER_SCROLL_NEVER = 2;
1796
1797 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001798 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001799 *
Joe Malin32736f02011-01-19 16:14:20 -08001800 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001801 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001802 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001803
1804 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001805 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1806 *
1807 * This is for use in games, book readers, video players, or any other "immersive" application
1808 * where the usual system chrome is deemed too distracting.
1809 *
1810 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001811 *
Joe Malin32736f02011-01-19 16:14:20 -08001812 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001813 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001814 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1815
1816 /**
1817 * View has requested that the system navigation be temporarily hidden.
1818 *
1819 * This is an even less obtrusive state than that called for by
1820 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1821 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1822 * those to disappear. This is useful (in conjunction with the
1823 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1824 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1825 * window flags) for displaying content using every last pixel on the display.
1826 *
1827 * There is a limitation: because navigation controls are so important, the least user
1828 * interaction will cause them to reappear immediately.
1829 *
1830 * @see #setSystemUiVisibility(int)
1831 */
1832 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1833
1834 /**
1835 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1836 */
1837 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1838
1839 /**
1840 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1841 */
1842 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001843
1844 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001845 * @hide
1846 *
1847 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1848 * out of the public fields to keep the undefined bits out of the developer's way.
1849 *
1850 * Flag to make the status bar not expandable. Unless you also
1851 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1852 */
1853 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1854
1855 /**
1856 * @hide
1857 *
1858 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1859 * out of the public fields to keep the undefined bits out of the developer's way.
1860 *
1861 * Flag to hide notification icons and scrolling ticker text.
1862 */
1863 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
1871 * Flag to disable incoming notification alerts. This will not block
1872 * icons, but it will block sound, vibrating and other visual or aural notifications.
1873 */
1874 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1875
1876 /**
1877 * @hide
1878 *
1879 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1880 * out of the public fields to keep the undefined bits out of the developer's way.
1881 *
1882 * Flag to hide only the scrolling ticker. Note that
1883 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1884 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1885 */
1886 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1887
1888 /**
1889 * @hide
1890 *
1891 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1892 * out of the public fields to keep the undefined bits out of the developer's way.
1893 *
1894 * Flag to hide the center system info area.
1895 */
1896 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1897
1898 /**
1899 * @hide
1900 *
1901 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1902 * out of the public fields to keep the undefined bits out of the developer's way.
1903 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001904 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001905 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1906 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001907 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001908
1909 /**
1910 * @hide
1911 *
1912 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1913 * out of the public fields to keep the undefined bits out of the developer's way.
1914 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001915 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001916 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1917 */
1918 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1919
1920 /**
1921 * @hide
1922 *
1923 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1924 * out of the public fields to keep the undefined bits out of the developer's way.
1925 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001926 * Flag to hide only the clock. You might use this if your activity has
1927 * its own clock making the status bar's clock redundant.
1928 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001929 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1930
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001931 /**
1932 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001933 *
1934 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1935 * out of the public fields to keep the undefined bits out of the developer's way.
1936 *
1937 * Flag to hide only the recent apps button. Don't use this
1938 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1939 */
1940 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1941
1942 /**
1943 * @hide
1944 *
1945 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1946 *
1947 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1948 */
1949 @Deprecated
1950 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1951 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1952
1953 /**
1954 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001955 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001956 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001957
1958 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001959 * These are the system UI flags that can be cleared by events outside
1960 * of an application. Currently this is just the ability to tap on the
1961 * screen while hiding the navigation bar to have it return.
1962 * @hide
1963 */
1964 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1965 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1966
1967 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001968 * Find views that render the specified text.
1969 *
1970 * @see #findViewsWithText(ArrayList, CharSequence, int)
1971 */
1972 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1973
1974 /**
1975 * Find find views that contain the specified content description.
1976 *
1977 * @see #findViewsWithText(ArrayList, CharSequence, int)
1978 */
1979 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1980
1981 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07001982 * Find views that contain {@link AccessibilityNodeProvider}. Such
1983 * a View is a root of virtual view hierarchy and may contain the searched
1984 * text. If this flag is set Views with providers are automatically
1985 * added and it is a responsibility of the client to call the APIs of
1986 * the provider to determine whether the virtual tree rooted at this View
1987 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
1988 * represeting the virtual views with this text.
1989 *
1990 * @see #findViewsWithText(ArrayList, CharSequence, int)
1991 *
1992 * @hide
1993 */
1994 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
1995
1996 /**
Adam Powell637d3372010-08-25 14:37:03 -07001997 * Controls the over-scroll mode for this view.
1998 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1999 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2000 * and {@link #OVER_SCROLL_NEVER}.
2001 */
2002 private int mOverScrollMode;
2003
2004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 * The parent this view is attached to.
2006 * {@hide}
2007 *
2008 * @see #getParent()
2009 */
2010 protected ViewParent mParent;
2011
2012 /**
2013 * {@hide}
2014 */
2015 AttachInfo mAttachInfo;
2016
2017 /**
2018 * {@hide}
2019 */
Romain Guy809a7f62009-05-14 15:44:42 -07002020 @ViewDebug.ExportedProperty(flagMapping = {
2021 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2022 name = "FORCE_LAYOUT"),
2023 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2024 name = "LAYOUT_REQUIRED"),
2025 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002026 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002027 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2028 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2029 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2030 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2031 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002033 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034
2035 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002036 * This view's request for the visibility of the status bar.
2037 * @hide
2038 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002039 @ViewDebug.ExportedProperty(flagMapping = {
2040 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2041 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2042 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2043 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2044 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2045 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2046 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2047 equals = SYSTEM_UI_FLAG_VISIBLE,
2048 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2049 })
Joe Onorato664644d2011-01-23 17:53:23 -08002050 int mSystemUiVisibility;
2051
2052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 * Count of how many windows this view has been attached to.
2054 */
2055 int mWindowAttachCount;
2056
2057 /**
2058 * The layout parameters associated with this view and used by the parent
2059 * {@link android.view.ViewGroup} to determine how this view should be
2060 * laid out.
2061 * {@hide}
2062 */
2063 protected ViewGroup.LayoutParams mLayoutParams;
2064
2065 /**
2066 * The view flags hold various views states.
2067 * {@hide}
2068 */
2069 @ViewDebug.ExportedProperty
2070 int mViewFlags;
2071
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002072 static class TransformationInfo {
2073 /**
2074 * The transform matrix for the View. This transform is calculated internally
2075 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2076 * is used by default. Do *not* use this variable directly; instead call
2077 * getMatrix(), which will automatically recalculate the matrix if necessary
2078 * to get the correct matrix based on the latest rotation and scale properties.
2079 */
2080 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002081
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002082 /**
2083 * The transform matrix for the View. This transform is calculated internally
2084 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2085 * is used by default. Do *not* use this variable directly; instead call
2086 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2087 * to get the correct matrix based on the latest rotation and scale properties.
2088 */
2089 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002090
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002091 /**
2092 * An internal variable that tracks whether we need to recalculate the
2093 * transform matrix, based on whether the rotation or scaleX/Y properties
2094 * have changed since the matrix was last calculated.
2095 */
2096 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002097
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002098 /**
2099 * An internal variable that tracks whether we need to recalculate the
2100 * transform matrix, based on whether the rotation or scaleX/Y properties
2101 * have changed since the matrix was last calculated.
2102 */
2103 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002104
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002105 /**
2106 * A variable that tracks whether we need to recalculate the
2107 * transform matrix, based on whether the rotation or scaleX/Y properties
2108 * have changed since the matrix was last calculated. This variable
2109 * is only valid after a call to updateMatrix() or to a function that
2110 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2111 */
2112 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002113
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002114 /**
2115 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2116 */
2117 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002118
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002119 /**
2120 * This matrix is used when computing the matrix for 3D rotations.
2121 */
2122 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002123
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002124 /**
2125 * These prev values are used to recalculate a centered pivot point when necessary. The
2126 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2127 * set), so thes values are only used then as well.
2128 */
2129 private int mPrevWidth = -1;
2130 private int mPrevHeight = -1;
2131
2132 /**
2133 * The degrees rotation around the vertical axis through the pivot point.
2134 */
2135 @ViewDebug.ExportedProperty
2136 float mRotationY = 0f;
2137
2138 /**
2139 * The degrees rotation around the horizontal axis through the pivot point.
2140 */
2141 @ViewDebug.ExportedProperty
2142 float mRotationX = 0f;
2143
2144 /**
2145 * The degrees rotation around the pivot point.
2146 */
2147 @ViewDebug.ExportedProperty
2148 float mRotation = 0f;
2149
2150 /**
2151 * The amount of translation of the object away from its left property (post-layout).
2152 */
2153 @ViewDebug.ExportedProperty
2154 float mTranslationX = 0f;
2155
2156 /**
2157 * The amount of translation of the object away from its top property (post-layout).
2158 */
2159 @ViewDebug.ExportedProperty
2160 float mTranslationY = 0f;
2161
2162 /**
2163 * The amount of scale in the x direction around the pivot point. A
2164 * value of 1 means no scaling is applied.
2165 */
2166 @ViewDebug.ExportedProperty
2167 float mScaleX = 1f;
2168
2169 /**
2170 * The amount of scale in the y direction around the pivot point. A
2171 * value of 1 means no scaling is applied.
2172 */
2173 @ViewDebug.ExportedProperty
2174 float mScaleY = 1f;
2175
2176 /**
Chet Haasea33de552012-02-03 16:28:24 -08002177 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002178 */
2179 @ViewDebug.ExportedProperty
2180 float mPivotX = 0f;
2181
2182 /**
Chet Haasea33de552012-02-03 16:28:24 -08002183 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002184 */
2185 @ViewDebug.ExportedProperty
2186 float mPivotY = 0f;
2187
2188 /**
2189 * The opacity of the View. This is a value from 0 to 1, where 0 means
2190 * completely transparent and 1 means completely opaque.
2191 */
2192 @ViewDebug.ExportedProperty
2193 float mAlpha = 1f;
2194 }
2195
2196 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002197
Joe Malin32736f02011-01-19 16:14:20 -08002198 private boolean mLastIsOpaque;
2199
Chet Haasefd2b0022010-08-06 13:08:56 -07002200 /**
2201 * Convenience value to check for float values that are close enough to zero to be considered
2202 * zero.
2203 */
Romain Guy2542d192010-08-18 11:47:12 -07002204 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002205
2206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 * The distance in pixels from the left edge of this view's parent
2208 * to the left edge of this view.
2209 * {@hide}
2210 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002211 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 protected int mLeft;
2213 /**
2214 * The distance in pixels from the left edge of this view's parent
2215 * to the right edge of this view.
2216 * {@hide}
2217 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002218 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 protected int mRight;
2220 /**
2221 * The distance in pixels from the top edge of this view's parent
2222 * to the top edge of this view.
2223 * {@hide}
2224 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002225 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 protected int mTop;
2227 /**
2228 * The distance in pixels from the top edge of this view's parent
2229 * to the bottom edge of this view.
2230 * {@hide}
2231 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002232 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 protected int mBottom;
2234
2235 /**
2236 * The offset, in pixels, by which the content of this view is scrolled
2237 * horizontally.
2238 * {@hide}
2239 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002240 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 protected int mScrollX;
2242 /**
2243 * The offset, in pixels, by which the content of this view is scrolled
2244 * vertically.
2245 * {@hide}
2246 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002247 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 protected int mScrollY;
2249
2250 /**
2251 * The left padding in pixels, that is the distance in pixels between the
2252 * left edge of this view and the left edge of its content.
2253 * {@hide}
2254 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002255 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 protected int mPaddingLeft;
2257 /**
2258 * The right padding in pixels, that is the distance in pixels between the
2259 * right edge of this view and the right edge of its content.
2260 * {@hide}
2261 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002262 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 protected int mPaddingRight;
2264 /**
2265 * The top padding in pixels, that is the distance in pixels between the
2266 * top edge of this view and the top edge of its content.
2267 * {@hide}
2268 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002269 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 protected int mPaddingTop;
2271 /**
2272 * The bottom padding in pixels, that is the distance in pixels between the
2273 * bottom edge of this view and the bottom edge of its content.
2274 * {@hide}
2275 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002276 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 protected int mPaddingBottom;
2278
2279 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002280 * Briefly describes the view and is primarily used for accessibility support.
2281 */
2282 private CharSequence mContentDescription;
2283
2284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002286 *
2287 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002289 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002290 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291
2292 /**
2293 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002294 *
2295 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002297 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002298 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002300 /**
Adam Powell20232d02010-12-08 21:08:53 -08002301 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002302 *
2303 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002304 */
2305 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002306 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002307
2308 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002309 * Cache if the user padding is relative.
2310 *
2311 */
2312 @ViewDebug.ExportedProperty(category = "padding")
2313 boolean mUserPaddingRelative;
2314
2315 /**
2316 * Cache the paddingStart set by the user to append to the scrollbar's size.
2317 *
2318 */
2319 @ViewDebug.ExportedProperty(category = "padding")
2320 int mUserPaddingStart;
2321
2322 /**
2323 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2324 *
2325 */
2326 @ViewDebug.ExportedProperty(category = "padding")
2327 int mUserPaddingEnd;
2328
2329 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002330 * @hide
2331 */
2332 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2333 /**
2334 * @hide
2335 */
2336 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 private Drawable mBGDrawable;
2339
2340 private int mBackgroundResource;
2341 private boolean mBackgroundSizeChanged;
2342
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002343 static class ListenerInfo {
2344 /**
2345 * Listener used to dispatch focus change events.
2346 * This field should be made private, so it is hidden from the SDK.
2347 * {@hide}
2348 */
2349 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002351 /**
2352 * Listeners for layout change events.
2353 */
2354 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002355
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002356 /**
2357 * Listeners for attach events.
2358 */
2359 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002360
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002361 /**
2362 * Listener used to dispatch click events.
2363 * This field should be made private, so it is hidden from the SDK.
2364 * {@hide}
2365 */
2366 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002368 /**
2369 * Listener used to dispatch long click events.
2370 * This field should be made private, so it is hidden from the SDK.
2371 * {@hide}
2372 */
2373 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002375 /**
2376 * Listener used to build the context menu.
2377 * This field should be made private, so it is hidden from the SDK.
2378 * {@hide}
2379 */
2380 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002382 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002384 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002386 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002387
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002388 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002389
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002390 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002391
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002392 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2393 }
2394
2395 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 /**
2398 * The application environment this view lives in.
2399 * This field should be made private, so it is hidden from the SDK.
2400 * {@hide}
2401 */
2402 protected Context mContext;
2403
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002404 private final Resources mResources;
2405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 private ScrollabilityCache mScrollCache;
2407
2408 private int[] mDrawableState = null;
2409
Romain Guy0211a0a2011-02-14 16:34:59 -08002410 /**
2411 * Set to true when drawing cache is enabled and cannot be created.
2412 *
2413 * @hide
2414 */
2415 public boolean mCachingFailed;
2416
Romain Guy02890fd2010-08-06 17:58:44 -07002417 private Bitmap mDrawingCache;
2418 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002419 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002420 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421
2422 /**
2423 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2424 * the user may specify which view to go to next.
2425 */
2426 private int mNextFocusLeftId = View.NO_ID;
2427
2428 /**
2429 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2430 * the user may specify which view to go to next.
2431 */
2432 private int mNextFocusRightId = View.NO_ID;
2433
2434 /**
2435 * When this view has focus and the next focus is {@link #FOCUS_UP},
2436 * the user may specify which view to go to next.
2437 */
2438 private int mNextFocusUpId = View.NO_ID;
2439
2440 /**
2441 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2442 * the user may specify which view to go to next.
2443 */
2444 private int mNextFocusDownId = View.NO_ID;
2445
Jeff Brown4e6319b2010-12-13 10:36:51 -08002446 /**
2447 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2448 * the user may specify which view to go to next.
2449 */
2450 int mNextFocusForwardId = View.NO_ID;
2451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002453 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002454 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002455 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 private UnsetPressedState mUnsetPressedState;
2458
2459 /**
2460 * Whether the long press's action has been invoked. The tap's action is invoked on the
2461 * up event while a long press is invoked as soon as the long press duration is reached, so
2462 * a long press could be performed before the tap is checked, in which case the tap's action
2463 * should not be invoked.
2464 */
2465 private boolean mHasPerformedLongPress;
2466
2467 /**
2468 * The minimum height of the view. We'll try our best to have the height
2469 * of this view to at least this amount.
2470 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002471 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 private int mMinHeight;
2473
2474 /**
2475 * The minimum width of the view. We'll try our best to have the width
2476 * of this view to at least this amount.
2477 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002478 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 private int mMinWidth;
2480
2481 /**
2482 * The delegate to handle touch events that are physically in this view
2483 * but should be handled by another view.
2484 */
2485 private TouchDelegate mTouchDelegate = null;
2486
2487 /**
2488 * Solid color to use as a background when creating the drawing cache. Enables
2489 * the cache to use 16 bit bitmaps instead of 32 bit.
2490 */
2491 private int mDrawingCacheBackgroundColor = 0;
2492
2493 /**
2494 * Special tree observer used when mAttachInfo is null.
2495 */
2496 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002497
Adam Powelle14579b2009-12-16 18:39:52 -08002498 /**
2499 * Cache the touch slop from the context that created the view.
2500 */
2501 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002504 * Object that handles automatic animation of view properties.
2505 */
2506 private ViewPropertyAnimator mAnimator = null;
2507
2508 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002509 * Flag indicating that a drag can cross window boundaries. When
2510 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2511 * with this flag set, all visible applications will be able to participate
2512 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002513 *
2514 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002515 */
2516 public static final int DRAG_FLAG_GLOBAL = 1;
2517
2518 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002519 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2520 */
2521 private float mVerticalScrollFactor;
2522
2523 /**
Adam Powell20232d02010-12-08 21:08:53 -08002524 * Position of the vertical scroll bar.
2525 */
2526 private int mVerticalScrollbarPosition;
2527
2528 /**
2529 * Position the scroll bar at the default position as determined by the system.
2530 */
2531 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2532
2533 /**
2534 * Position the scroll bar along the left edge.
2535 */
2536 public static final int SCROLLBAR_POSITION_LEFT = 1;
2537
2538 /**
2539 * Position the scroll bar along the right edge.
2540 */
2541 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2542
2543 /**
Romain Guy171c5922011-01-06 10:04:23 -08002544 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002545 *
2546 * @see #getLayerType()
2547 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002548 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002549 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002550 */
2551 public static final int LAYER_TYPE_NONE = 0;
2552
2553 /**
2554 * <p>Indicates that the view has a software layer. A software layer is backed
2555 * by a bitmap and causes the view to be rendered using Android's software
2556 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002557 *
Romain Guy171c5922011-01-06 10:04:23 -08002558 * <p>Software layers have various usages:</p>
2559 * <p>When the application is not using hardware acceleration, a software layer
2560 * is useful to apply a specific color filter and/or blending mode and/or
2561 * translucency to a view and all its children.</p>
2562 * <p>When the application is using hardware acceleration, a software layer
2563 * is useful to render drawing primitives not supported by the hardware
2564 * accelerated pipeline. It can also be used to cache a complex view tree
2565 * into a texture and reduce the complexity of drawing operations. For instance,
2566 * when animating a complex view tree with a translation, a software layer can
2567 * be used to render the view tree only once.</p>
2568 * <p>Software layers should be avoided when the affected view tree updates
2569 * often. Every update will require to re-render the software layer, which can
2570 * potentially be slow (particularly when hardware acceleration is turned on
2571 * since the layer will have to be uploaded into a hardware texture after every
2572 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002573 *
2574 * @see #getLayerType()
2575 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002576 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002577 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002578 */
2579 public static final int LAYER_TYPE_SOFTWARE = 1;
2580
2581 /**
2582 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2583 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2584 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2585 * rendering pipeline, but only if hardware acceleration is turned on for the
2586 * view hierarchy. When hardware acceleration is turned off, hardware layers
2587 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002588 *
Romain Guy171c5922011-01-06 10:04:23 -08002589 * <p>A hardware layer is useful to apply a specific color filter and/or
2590 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002591 * <p>A hardware layer can be used to cache a complex view tree into a
2592 * texture and reduce the complexity of drawing operations. For instance,
2593 * when animating a complex view tree with a translation, a hardware layer can
2594 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002595 * <p>A hardware layer can also be used to increase the rendering quality when
2596 * rotation transformations are applied on a view. It can also be used to
2597 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002598 *
2599 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002600 * @see #setLayerType(int, android.graphics.Paint)
2601 * @see #LAYER_TYPE_NONE
2602 * @see #LAYER_TYPE_SOFTWARE
2603 */
2604 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002605
Romain Guy3aaff3a2011-01-12 14:18:47 -08002606 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2607 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2608 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2609 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2610 })
Romain Guy171c5922011-01-06 10:04:23 -08002611 int mLayerType = LAYER_TYPE_NONE;
2612 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002613 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002614
2615 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002616 * Set to true when the view is sending hover accessibility events because it
2617 * is the innermost hovered view.
2618 */
2619 private boolean mSendingHoverAccessibilityEvents;
2620
2621 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002622 * Delegate for injecting accessiblity functionality.
2623 */
2624 AccessibilityDelegate mAccessibilityDelegate;
2625
2626 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002627 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002628 */
2629 public static final int TEXT_DIRECTION_INHERIT = 0;
2630
2631 /**
2632 * Text direction is using "first strong algorithm". The first strong directional character
2633 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002634 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002635 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002636 */
2637 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2638
2639 /**
2640 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2641 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002642 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002643 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002644 */
2645 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2646
2647 /**
2648 * Text direction is forced to LTR.
2649 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002650 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002651 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002652
2653 /**
2654 * Text direction is forced to RTL.
2655 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002656 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002657 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002658
2659 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002660 * Text direction is coming from the system Locale.
2661 *
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002662 */
2663 public static final int TEXT_DIRECTION_LOCALE = 5;
2664
2665 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002666 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002667 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002668 */
2669 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2670
2671 /**
2672 * The text direction that has been defined by {@link #setTextDirection(int)}.
2673 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002674 */
2675 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002676 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2677 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2678 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2679 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002680 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2681 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002682 })
Doug Feltcb3791202011-07-07 11:57:48 -07002683 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002684
2685 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002686 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002687 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002688 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2689 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002690 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002691 */
2692 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002693 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2694 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2695 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002696 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002697 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2698 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002699 })
Doug Feltcb3791202011-07-07 11:57:48 -07002700 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002701
2702 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002703 * Consistency verifier for debugging purposes.
2704 * @hide
2705 */
2706 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2707 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2708 new InputEventConsistencyVerifier(this, 0) : null;
2709
2710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 * Simple constructor to use when creating a view from code.
2712 *
2713 * @param context The Context the view is running in, through which it can
2714 * access the current theme, resources, etc.
2715 */
2716 public View(Context context) {
2717 mContext = context;
2718 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002719 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002720 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002721 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002722 mUserPaddingStart = -1;
2723 mUserPaddingEnd = -1;
2724 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 }
2726
2727 /**
2728 * Constructor that is called when inflating a view from XML. This is called
2729 * when a view is being constructed from an XML file, supplying attributes
2730 * that were specified in the XML file. This version uses a default style of
2731 * 0, so the only attribute values applied are those in the Context's Theme
2732 * and the given AttributeSet.
2733 *
2734 * <p>
2735 * The method onFinishInflate() will be called after all children have been
2736 * added.
2737 *
2738 * @param context The Context the view is running in, through which it can
2739 * access the current theme, resources, etc.
2740 * @param attrs The attributes of the XML tag that is inflating the view.
2741 * @see #View(Context, AttributeSet, int)
2742 */
2743 public View(Context context, AttributeSet attrs) {
2744 this(context, attrs, 0);
2745 }
2746
2747 /**
2748 * Perform inflation from XML and apply a class-specific base style. This
2749 * constructor of View allows subclasses to use their own base style when
2750 * they are inflating. For example, a Button class's constructor would call
2751 * this version of the super class constructor and supply
2752 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2753 * the theme's button style to modify all of the base view attributes (in
2754 * particular its background) as well as the Button class's attributes.
2755 *
2756 * @param context The Context the view is running in, through which it can
2757 * access the current theme, resources, etc.
2758 * @param attrs The attributes of the XML tag that is inflating the view.
2759 * @param defStyle The default style to apply to this view. If 0, no style
2760 * will be applied (beyond what is included in the theme). This may
2761 * either be an attribute resource, whose value will be retrieved
2762 * from the current theme, or an explicit style resource.
2763 * @see #View(Context, AttributeSet)
2764 */
2765 public View(Context context, AttributeSet attrs, int defStyle) {
2766 this(context);
2767
2768 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2769 defStyle, 0);
2770
2771 Drawable background = null;
2772
2773 int leftPadding = -1;
2774 int topPadding = -1;
2775 int rightPadding = -1;
2776 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002777 int startPadding = -1;
2778 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779
2780 int padding = -1;
2781
2782 int viewFlagValues = 0;
2783 int viewFlagMasks = 0;
2784
2785 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 int x = 0;
2788 int y = 0;
2789
Chet Haase73066682010-11-29 15:55:32 -08002790 float tx = 0;
2791 float ty = 0;
2792 float rotation = 0;
2793 float rotationX = 0;
2794 float rotationY = 0;
2795 float sx = 1f;
2796 float sy = 1f;
2797 boolean transformSet = false;
2798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2800
Adam Powell637d3372010-08-25 14:37:03 -07002801 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 final int N = a.getIndexCount();
2803 for (int i = 0; i < N; i++) {
2804 int attr = a.getIndex(i);
2805 switch (attr) {
2806 case com.android.internal.R.styleable.View_background:
2807 background = a.getDrawable(attr);
2808 break;
2809 case com.android.internal.R.styleable.View_padding:
2810 padding = a.getDimensionPixelSize(attr, -1);
2811 break;
2812 case com.android.internal.R.styleable.View_paddingLeft:
2813 leftPadding = a.getDimensionPixelSize(attr, -1);
2814 break;
2815 case com.android.internal.R.styleable.View_paddingTop:
2816 topPadding = a.getDimensionPixelSize(attr, -1);
2817 break;
2818 case com.android.internal.R.styleable.View_paddingRight:
2819 rightPadding = a.getDimensionPixelSize(attr, -1);
2820 break;
2821 case com.android.internal.R.styleable.View_paddingBottom:
2822 bottomPadding = a.getDimensionPixelSize(attr, -1);
2823 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002824 case com.android.internal.R.styleable.View_paddingStart:
2825 startPadding = a.getDimensionPixelSize(attr, -1);
2826 break;
2827 case com.android.internal.R.styleable.View_paddingEnd:
2828 endPadding = a.getDimensionPixelSize(attr, -1);
2829 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 case com.android.internal.R.styleable.View_scrollX:
2831 x = a.getDimensionPixelOffset(attr, 0);
2832 break;
2833 case com.android.internal.R.styleable.View_scrollY:
2834 y = a.getDimensionPixelOffset(attr, 0);
2835 break;
Chet Haase73066682010-11-29 15:55:32 -08002836 case com.android.internal.R.styleable.View_alpha:
2837 setAlpha(a.getFloat(attr, 1f));
2838 break;
2839 case com.android.internal.R.styleable.View_transformPivotX:
2840 setPivotX(a.getDimensionPixelOffset(attr, 0));
2841 break;
2842 case com.android.internal.R.styleable.View_transformPivotY:
2843 setPivotY(a.getDimensionPixelOffset(attr, 0));
2844 break;
2845 case com.android.internal.R.styleable.View_translationX:
2846 tx = a.getDimensionPixelOffset(attr, 0);
2847 transformSet = true;
2848 break;
2849 case com.android.internal.R.styleable.View_translationY:
2850 ty = a.getDimensionPixelOffset(attr, 0);
2851 transformSet = true;
2852 break;
2853 case com.android.internal.R.styleable.View_rotation:
2854 rotation = a.getFloat(attr, 0);
2855 transformSet = true;
2856 break;
2857 case com.android.internal.R.styleable.View_rotationX:
2858 rotationX = a.getFloat(attr, 0);
2859 transformSet = true;
2860 break;
2861 case com.android.internal.R.styleable.View_rotationY:
2862 rotationY = a.getFloat(attr, 0);
2863 transformSet = true;
2864 break;
2865 case com.android.internal.R.styleable.View_scaleX:
2866 sx = a.getFloat(attr, 1f);
2867 transformSet = true;
2868 break;
2869 case com.android.internal.R.styleable.View_scaleY:
2870 sy = a.getFloat(attr, 1f);
2871 transformSet = true;
2872 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 case com.android.internal.R.styleable.View_id:
2874 mID = a.getResourceId(attr, NO_ID);
2875 break;
2876 case com.android.internal.R.styleable.View_tag:
2877 mTag = a.getText(attr);
2878 break;
2879 case com.android.internal.R.styleable.View_fitsSystemWindows:
2880 if (a.getBoolean(attr, false)) {
2881 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2882 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2883 }
2884 break;
2885 case com.android.internal.R.styleable.View_focusable:
2886 if (a.getBoolean(attr, false)) {
2887 viewFlagValues |= FOCUSABLE;
2888 viewFlagMasks |= FOCUSABLE_MASK;
2889 }
2890 break;
2891 case com.android.internal.R.styleable.View_focusableInTouchMode:
2892 if (a.getBoolean(attr, false)) {
2893 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2894 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2895 }
2896 break;
2897 case com.android.internal.R.styleable.View_clickable:
2898 if (a.getBoolean(attr, false)) {
2899 viewFlagValues |= CLICKABLE;
2900 viewFlagMasks |= CLICKABLE;
2901 }
2902 break;
2903 case com.android.internal.R.styleable.View_longClickable:
2904 if (a.getBoolean(attr, false)) {
2905 viewFlagValues |= LONG_CLICKABLE;
2906 viewFlagMasks |= LONG_CLICKABLE;
2907 }
2908 break;
2909 case com.android.internal.R.styleable.View_saveEnabled:
2910 if (!a.getBoolean(attr, true)) {
2911 viewFlagValues |= SAVE_DISABLED;
2912 viewFlagMasks |= SAVE_DISABLED_MASK;
2913 }
2914 break;
2915 case com.android.internal.R.styleable.View_duplicateParentState:
2916 if (a.getBoolean(attr, false)) {
2917 viewFlagValues |= DUPLICATE_PARENT_STATE;
2918 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2919 }
2920 break;
2921 case com.android.internal.R.styleable.View_visibility:
2922 final int visibility = a.getInt(attr, 0);
2923 if (visibility != 0) {
2924 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2925 viewFlagMasks |= VISIBILITY_MASK;
2926 }
2927 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002928 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002929 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002930 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002931 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002932 final int layoutDirection = a.getInt(attr, -1);
2933 if (layoutDirection != -1) {
2934 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002935 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002936 // Set to default (LAYOUT_DIRECTION_INHERIT)
2937 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002938 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002939 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002940 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 case com.android.internal.R.styleable.View_drawingCacheQuality:
2942 final int cacheQuality = a.getInt(attr, 0);
2943 if (cacheQuality != 0) {
2944 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2945 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2946 }
2947 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002948 case com.android.internal.R.styleable.View_contentDescription:
2949 mContentDescription = a.getString(attr);
2950 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2952 if (!a.getBoolean(attr, true)) {
2953 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2954 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2955 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002956 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2958 if (!a.getBoolean(attr, true)) {
2959 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2960 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2961 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002962 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 case R.styleable.View_scrollbars:
2964 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2965 if (scrollbars != SCROLLBARS_NONE) {
2966 viewFlagValues |= scrollbars;
2967 viewFlagMasks |= SCROLLBARS_MASK;
2968 initializeScrollbars(a);
2969 }
2970 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002971 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002973 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2974 // Ignore the attribute starting with ICS
2975 break;
2976 }
2977 // With builds < ICS, fall through and apply fading edges
2978 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2980 if (fadingEdge != FADING_EDGE_NONE) {
2981 viewFlagValues |= fadingEdge;
2982 viewFlagMasks |= FADING_EDGE_MASK;
2983 initializeFadingEdge(a);
2984 }
2985 break;
2986 case R.styleable.View_scrollbarStyle:
2987 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2988 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2989 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2990 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2991 }
2992 break;
2993 case R.styleable.View_isScrollContainer:
2994 setScrollContainer = true;
2995 if (a.getBoolean(attr, false)) {
2996 setScrollContainer(true);
2997 }
2998 break;
2999 case com.android.internal.R.styleable.View_keepScreenOn:
3000 if (a.getBoolean(attr, false)) {
3001 viewFlagValues |= KEEP_SCREEN_ON;
3002 viewFlagMasks |= KEEP_SCREEN_ON;
3003 }
3004 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003005 case R.styleable.View_filterTouchesWhenObscured:
3006 if (a.getBoolean(attr, false)) {
3007 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3008 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3009 }
3010 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 case R.styleable.View_nextFocusLeft:
3012 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3013 break;
3014 case R.styleable.View_nextFocusRight:
3015 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3016 break;
3017 case R.styleable.View_nextFocusUp:
3018 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3019 break;
3020 case R.styleable.View_nextFocusDown:
3021 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3022 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003023 case R.styleable.View_nextFocusForward:
3024 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3025 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 case R.styleable.View_minWidth:
3027 mMinWidth = a.getDimensionPixelSize(attr, 0);
3028 break;
3029 case R.styleable.View_minHeight:
3030 mMinHeight = a.getDimensionPixelSize(attr, 0);
3031 break;
Romain Guy9a817362009-05-01 10:57:14 -07003032 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003033 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003034 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003035 + "be used within a restricted context");
3036 }
3037
Romain Guy9a817362009-05-01 10:57:14 -07003038 final String handlerName = a.getString(attr);
3039 if (handlerName != null) {
3040 setOnClickListener(new OnClickListener() {
3041 private Method mHandler;
3042
3043 public void onClick(View v) {
3044 if (mHandler == null) {
3045 try {
3046 mHandler = getContext().getClass().getMethod(handlerName,
3047 View.class);
3048 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003049 int id = getId();
3050 String idText = id == NO_ID ? "" : " with id '"
3051 + getContext().getResources().getResourceEntryName(
3052 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003053 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003054 handlerName + "(View) in the activity "
3055 + getContext().getClass() + " for onClick handler"
3056 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003057 }
3058 }
3059
3060 try {
3061 mHandler.invoke(getContext(), View.this);
3062 } catch (IllegalAccessException e) {
3063 throw new IllegalStateException("Could not execute non "
3064 + "public method of the activity", e);
3065 } catch (InvocationTargetException e) {
3066 throw new IllegalStateException("Could not execute "
3067 + "method of the activity", e);
3068 }
3069 }
3070 });
3071 }
3072 break;
Adam Powell637d3372010-08-25 14:37:03 -07003073 case R.styleable.View_overScrollMode:
3074 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3075 break;
Adam Powell20232d02010-12-08 21:08:53 -08003076 case R.styleable.View_verticalScrollbarPosition:
3077 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3078 break;
Romain Guy171c5922011-01-06 10:04:23 -08003079 case R.styleable.View_layerType:
3080 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3081 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003082 case R.styleable.View_textDirection:
3083 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3084 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 }
3086 }
3087
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003088 a.recycle();
3089
Adam Powell637d3372010-08-25 14:37:03 -07003090 setOverScrollMode(overScrollMode);
3091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 if (background != null) {
3093 setBackgroundDrawable(background);
3094 }
3095
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003096 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3097
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003098 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3099 // layout direction). Those cached values will be used later during padding resolution.
3100 mUserPaddingStart = startPadding;
3101 mUserPaddingEnd = endPadding;
3102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 if (padding >= 0) {
3104 leftPadding = padding;
3105 topPadding = padding;
3106 rightPadding = padding;
3107 bottomPadding = padding;
3108 }
3109
3110 // If the user specified the padding (either with android:padding or
3111 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3112 // use the default padding or the padding from the background drawable
3113 // (stored at this point in mPadding*)
3114 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3115 topPadding >= 0 ? topPadding : mPaddingTop,
3116 rightPadding >= 0 ? rightPadding : mPaddingRight,
3117 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3118
3119 if (viewFlagMasks != 0) {
3120 setFlags(viewFlagValues, viewFlagMasks);
3121 }
3122
3123 // Needs to be called after mViewFlags is set
3124 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3125 recomputePadding();
3126 }
3127
3128 if (x != 0 || y != 0) {
3129 scrollTo(x, y);
3130 }
3131
Chet Haase73066682010-11-29 15:55:32 -08003132 if (transformSet) {
3133 setTranslationX(tx);
3134 setTranslationY(ty);
3135 setRotation(rotation);
3136 setRotationX(rotationX);
3137 setRotationY(rotationY);
3138 setScaleX(sx);
3139 setScaleY(sy);
3140 }
3141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3143 setScrollContainer(true);
3144 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003145
3146 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148
3149 /**
3150 * Non-public constructor for use in testing
3151 */
3152 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003153 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 }
3155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 /**
3157 * <p>
3158 * Initializes the fading edges from a given set of styled attributes. This
3159 * method should be called by subclasses that need fading edges and when an
3160 * instance of these subclasses is created programmatically rather than
3161 * being inflated from XML. This method is automatically called when the XML
3162 * is inflated.
3163 * </p>
3164 *
3165 * @param a the styled attributes set to initialize the fading edges from
3166 */
3167 protected void initializeFadingEdge(TypedArray a) {
3168 initScrollCache();
3169
3170 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3171 R.styleable.View_fadingEdgeLength,
3172 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3173 }
3174
3175 /**
3176 * Returns the size of the vertical faded edges used to indicate that more
3177 * content in this view is visible.
3178 *
3179 * @return The size in pixels of the vertical faded edge or 0 if vertical
3180 * faded edges are not enabled for this view.
3181 * @attr ref android.R.styleable#View_fadingEdgeLength
3182 */
3183 public int getVerticalFadingEdgeLength() {
3184 if (isVerticalFadingEdgeEnabled()) {
3185 ScrollabilityCache cache = mScrollCache;
3186 if (cache != null) {
3187 return cache.fadingEdgeLength;
3188 }
3189 }
3190 return 0;
3191 }
3192
3193 /**
3194 * Set the size of the faded edge used to indicate that more content in this
3195 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003196 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3197 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3198 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 *
3200 * @param length The size in pixels of the faded edge used to indicate that more
3201 * content in this view is visible.
3202 */
3203 public void setFadingEdgeLength(int length) {
3204 initScrollCache();
3205 mScrollCache.fadingEdgeLength = length;
3206 }
3207
3208 /**
3209 * Returns the size of the horizontal faded edges used to indicate that more
3210 * content in this view is visible.
3211 *
3212 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3213 * faded edges are not enabled for this view.
3214 * @attr ref android.R.styleable#View_fadingEdgeLength
3215 */
3216 public int getHorizontalFadingEdgeLength() {
3217 if (isHorizontalFadingEdgeEnabled()) {
3218 ScrollabilityCache cache = mScrollCache;
3219 if (cache != null) {
3220 return cache.fadingEdgeLength;
3221 }
3222 }
3223 return 0;
3224 }
3225
3226 /**
3227 * Returns the width of the vertical scrollbar.
3228 *
3229 * @return The width in pixels of the vertical scrollbar or 0 if there
3230 * is no vertical scrollbar.
3231 */
3232 public int getVerticalScrollbarWidth() {
3233 ScrollabilityCache cache = mScrollCache;
3234 if (cache != null) {
3235 ScrollBarDrawable scrollBar = cache.scrollBar;
3236 if (scrollBar != null) {
3237 int size = scrollBar.getSize(true);
3238 if (size <= 0) {
3239 size = cache.scrollBarSize;
3240 }
3241 return size;
3242 }
3243 return 0;
3244 }
3245 return 0;
3246 }
3247
3248 /**
3249 * Returns the height of the horizontal scrollbar.
3250 *
3251 * @return The height in pixels of the horizontal scrollbar or 0 if
3252 * there is no horizontal scrollbar.
3253 */
3254 protected int getHorizontalScrollbarHeight() {
3255 ScrollabilityCache cache = mScrollCache;
3256 if (cache != null) {
3257 ScrollBarDrawable scrollBar = cache.scrollBar;
3258 if (scrollBar != null) {
3259 int size = scrollBar.getSize(false);
3260 if (size <= 0) {
3261 size = cache.scrollBarSize;
3262 }
3263 return size;
3264 }
3265 return 0;
3266 }
3267 return 0;
3268 }
3269
3270 /**
3271 * <p>
3272 * Initializes the scrollbars from a given set of styled attributes. This
3273 * method should be called by subclasses that need scrollbars and when an
3274 * instance of these subclasses is created programmatically rather than
3275 * being inflated from XML. This method is automatically called when the XML
3276 * is inflated.
3277 * </p>
3278 *
3279 * @param a the styled attributes set to initialize the scrollbars from
3280 */
3281 protected void initializeScrollbars(TypedArray a) {
3282 initScrollCache();
3283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003285
Mike Cleronf116bf82009-09-27 19:14:12 -07003286 if (scrollabilityCache.scrollBar == null) {
3287 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3288 }
Joe Malin32736f02011-01-19 16:14:20 -08003289
Romain Guy8bda2482010-03-02 11:42:11 -08003290 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291
Mike Cleronf116bf82009-09-27 19:14:12 -07003292 if (!fadeScrollbars) {
3293 scrollabilityCache.state = ScrollabilityCache.ON;
3294 }
3295 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003296
3297
Mike Cleronf116bf82009-09-27 19:14:12 -07003298 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3299 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3300 .getScrollBarFadeDuration());
3301 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3302 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3303 ViewConfiguration.getScrollDefaultDelay());
3304
Joe Malin32736f02011-01-19 16:14:20 -08003305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3307 com.android.internal.R.styleable.View_scrollbarSize,
3308 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3309
3310 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3311 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3312
3313 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3314 if (thumb != null) {
3315 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3316 }
3317
3318 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3319 false);
3320 if (alwaysDraw) {
3321 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3322 }
3323
3324 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3325 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3326
3327 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3328 if (thumb != null) {
3329 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3330 }
3331
3332 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3333 false);
3334 if (alwaysDraw) {
3335 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3336 }
3337
3338 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003339 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 }
3341
3342 /**
3343 * <p>
3344 * Initalizes the scrollability cache if necessary.
3345 * </p>
3346 */
3347 private void initScrollCache() {
3348 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003349 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 }
3351 }
3352
3353 /**
Adam Powell20232d02010-12-08 21:08:53 -08003354 * Set the position of the vertical scroll bar. Should be one of
3355 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3356 * {@link #SCROLLBAR_POSITION_RIGHT}.
3357 *
3358 * @param position Where the vertical scroll bar should be positioned.
3359 */
3360 public void setVerticalScrollbarPosition(int position) {
3361 if (mVerticalScrollbarPosition != position) {
3362 mVerticalScrollbarPosition = position;
3363 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003364 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003365 }
3366 }
3367
3368 /**
3369 * @return The position where the vertical scroll bar will show, if applicable.
3370 * @see #setVerticalScrollbarPosition(int)
3371 */
3372 public int getVerticalScrollbarPosition() {
3373 return mVerticalScrollbarPosition;
3374 }
3375
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003376 ListenerInfo getListenerInfo() {
3377 if (mListenerInfo != null) {
3378 return mListenerInfo;
3379 }
3380 mListenerInfo = new ListenerInfo();
3381 return mListenerInfo;
3382 }
3383
Adam Powell20232d02010-12-08 21:08:53 -08003384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 * Register a callback to be invoked when focus of this view changed.
3386 *
3387 * @param l The callback that will run.
3388 */
3389 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003390 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
3392
3393 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003394 * Add a listener that will be called when the bounds of the view change due to
3395 * layout processing.
3396 *
3397 * @param listener The listener that will be called when layout bounds change.
3398 */
3399 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003400 ListenerInfo li = getListenerInfo();
3401 if (li.mOnLayoutChangeListeners == null) {
3402 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003403 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003404 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3405 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003406 }
Chet Haase21cd1382010-09-01 17:42:29 -07003407 }
3408
3409 /**
3410 * Remove a listener for layout changes.
3411 *
3412 * @param listener The listener for layout bounds change.
3413 */
3414 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003415 ListenerInfo li = mListenerInfo;
3416 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003417 return;
3418 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003419 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003420 }
3421
3422 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003423 * Add a listener for attach state changes.
3424 *
3425 * This listener will be called whenever this view is attached or detached
3426 * from a window. Remove the listener using
3427 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3428 *
3429 * @param listener Listener to attach
3430 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3431 */
3432 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003433 ListenerInfo li = getListenerInfo();
3434 if (li.mOnAttachStateChangeListeners == null) {
3435 li.mOnAttachStateChangeListeners
3436 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003437 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003438 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003439 }
3440
3441 /**
3442 * Remove a listener for attach state changes. The listener will receive no further
3443 * notification of window attach/detach events.
3444 *
3445 * @param listener Listener to remove
3446 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3447 */
3448 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003449 ListenerInfo li = mListenerInfo;
3450 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003451 return;
3452 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003453 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003454 }
3455
3456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 * Returns the focus-change callback registered for this view.
3458 *
3459 * @return The callback, or null if one is not registered.
3460 */
3461 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003462 ListenerInfo li = mListenerInfo;
3463 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 }
3465
3466 /**
3467 * Register a callback to be invoked when this view is clicked. If this view is not
3468 * clickable, it becomes clickable.
3469 *
3470 * @param l The callback that will run
3471 *
3472 * @see #setClickable(boolean)
3473 */
3474 public void setOnClickListener(OnClickListener l) {
3475 if (!isClickable()) {
3476 setClickable(true);
3477 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003478 getListenerInfo().mOnClickListener = l;
3479 }
3480
3481 /**
3482 * Return whether this view has an attached OnClickListener. Returns
3483 * true if there is a listener, false if there is none.
3484 */
3485 public boolean hasOnClickListeners() {
3486 ListenerInfo li = mListenerInfo;
3487 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 }
3489
3490 /**
3491 * Register a callback to be invoked when this view is clicked and held. If this view is not
3492 * long clickable, it becomes long clickable.
3493 *
3494 * @param l The callback that will run
3495 *
3496 * @see #setLongClickable(boolean)
3497 */
3498 public void setOnLongClickListener(OnLongClickListener l) {
3499 if (!isLongClickable()) {
3500 setLongClickable(true);
3501 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003502 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
3504
3505 /**
3506 * Register a callback to be invoked when the context menu for this view is
3507 * being built. If this view is not long clickable, it becomes long clickable.
3508 *
3509 * @param l The callback that will run
3510 *
3511 */
3512 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3513 if (!isLongClickable()) {
3514 setLongClickable(true);
3515 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003516 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 }
3518
3519 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003520 * Call this view's OnClickListener, if it is defined. Performs all normal
3521 * actions associated with clicking: reporting accessibility event, playing
3522 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 *
3524 * @return True there was an assigned OnClickListener that was called, false
3525 * otherwise is returned.
3526 */
3527 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003528 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3529
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003530 ListenerInfo li = mListenerInfo;
3531 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003533 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 return true;
3535 }
3536
3537 return false;
3538 }
3539
3540 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003541 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3542 * this only calls the listener, and does not do any associated clicking
3543 * actions like reporting an accessibility event.
3544 *
3545 * @return True there was an assigned OnClickListener that was called, false
3546 * otherwise is returned.
3547 */
3548 public boolean callOnClick() {
3549 ListenerInfo li = mListenerInfo;
3550 if (li != null && li.mOnClickListener != null) {
3551 li.mOnClickListener.onClick(this);
3552 return true;
3553 }
3554 return false;
3555 }
3556
3557 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003558 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3559 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003561 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 */
3563 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003564 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003567 ListenerInfo li = mListenerInfo;
3568 if (li != null && li.mOnLongClickListener != null) {
3569 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 }
3571 if (!handled) {
3572 handled = showContextMenu();
3573 }
3574 if (handled) {
3575 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3576 }
3577 return handled;
3578 }
3579
3580 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003581 * Performs button-related actions during a touch down event.
3582 *
3583 * @param event The event.
3584 * @return True if the down was consumed.
3585 *
3586 * @hide
3587 */
3588 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3589 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3590 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3591 return true;
3592 }
3593 }
3594 return false;
3595 }
3596
3597 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 * Bring up the context menu for this view.
3599 *
3600 * @return Whether a context menu was displayed.
3601 */
3602 public boolean showContextMenu() {
3603 return getParent().showContextMenuForChild(this);
3604 }
3605
3606 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003607 * Bring up the context menu for this view, referring to the item under the specified point.
3608 *
3609 * @param x The referenced x coordinate.
3610 * @param y The referenced y coordinate.
3611 * @param metaState The keyboard modifiers that were pressed.
3612 * @return Whether a context menu was displayed.
3613 *
3614 * @hide
3615 */
3616 public boolean showContextMenu(float x, float y, int metaState) {
3617 return showContextMenu();
3618 }
3619
3620 /**
Adam Powell6e346362010-07-23 10:18:23 -07003621 * Start an action mode.
3622 *
3623 * @param callback Callback that will control the lifecycle of the action mode
3624 * @return The new action mode if it is started, null otherwise
3625 *
3626 * @see ActionMode
3627 */
3628 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003629 ViewParent parent = getParent();
3630 if (parent == null) return null;
3631 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003632 }
3633
3634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 * Register a callback to be invoked when a key is pressed in this view.
3636 * @param l the key listener to attach to this view
3637 */
3638 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003639 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 }
3641
3642 /**
3643 * Register a callback to be invoked when a touch event is sent to this view.
3644 * @param l the touch listener to attach to this view
3645 */
3646 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003647 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 }
3649
3650 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003651 * Register a callback to be invoked when a generic motion event is sent to this view.
3652 * @param l the generic motion listener to attach to this view
3653 */
3654 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003655 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003656 }
3657
3658 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003659 * Register a callback to be invoked when a hover event is sent to this view.
3660 * @param l the hover listener to attach to this view
3661 */
3662 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003663 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003664 }
3665
3666 /**
Joe Malin32736f02011-01-19 16:14:20 -08003667 * Register a drag event listener callback object for this View. The parameter is
3668 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3669 * View, the system calls the
3670 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3671 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003672 */
3673 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003674 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003675 }
3676
3677 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003678 * Give this view focus. This will cause
3679 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 *
3681 * Note: this does not check whether this {@link View} should get focus, it just
3682 * gives it focus no matter what. It should only be called internally by framework
3683 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3684 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003685 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3686 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 * focus moved when requestFocus() is called. It may not always
3688 * apply, in which case use the default View.FOCUS_DOWN.
3689 * @param previouslyFocusedRect The rectangle of the view that had focus
3690 * prior in this View's coordinate system.
3691 */
3692 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3693 if (DBG) {
3694 System.out.println(this + " requestFocus()");
3695 }
3696
3697 if ((mPrivateFlags & FOCUSED) == 0) {
3698 mPrivateFlags |= FOCUSED;
3699
3700 if (mParent != null) {
3701 mParent.requestChildFocus(this, this);
3702 }
3703
3704 onFocusChanged(true, direction, previouslyFocusedRect);
3705 refreshDrawableState();
3706 }
3707 }
3708
3709 /**
3710 * Request that a rectangle of this view be visible on the screen,
3711 * scrolling if necessary just enough.
3712 *
3713 * <p>A View should call this if it maintains some notion of which part
3714 * of its content is interesting. For example, a text editing view
3715 * should call this when its cursor moves.
3716 *
3717 * @param rectangle The rectangle.
3718 * @return Whether any parent scrolled.
3719 */
3720 public boolean requestRectangleOnScreen(Rect rectangle) {
3721 return requestRectangleOnScreen(rectangle, false);
3722 }
3723
3724 /**
3725 * Request that a rectangle of this view be visible on the screen,
3726 * scrolling if necessary just enough.
3727 *
3728 * <p>A View should call this if it maintains some notion of which part
3729 * of its content is interesting. For example, a text editing view
3730 * should call this when its cursor moves.
3731 *
3732 * <p>When <code>immediate</code> is set to true, scrolling will not be
3733 * animated.
3734 *
3735 * @param rectangle The rectangle.
3736 * @param immediate True to forbid animated scrolling, false otherwise
3737 * @return Whether any parent scrolled.
3738 */
3739 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3740 View child = this;
3741 ViewParent parent = mParent;
3742 boolean scrolled = false;
3743 while (parent != null) {
3744 scrolled |= parent.requestChildRectangleOnScreen(child,
3745 rectangle, immediate);
3746
3747 // offset rect so next call has the rectangle in the
3748 // coordinate system of its direct child.
3749 rectangle.offset(child.getLeft(), child.getTop());
3750 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3751
3752 if (!(parent instanceof View)) {
3753 break;
3754 }
Romain Guy8506ab42009-06-11 17:35:47 -07003755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 child = (View) parent;
3757 parent = child.getParent();
3758 }
3759 return scrolled;
3760 }
3761
3762 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003763 * Called when this view wants to give up focus. If focus is cleared
3764 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3765 * <p>
3766 * <strong>Note:</strong> When a View clears focus the framework is trying
3767 * to give focus to the first focusable View from the top. Hence, if this
3768 * View is the first from the top that can take focus, then its focus will
3769 * not be cleared nor will the focus change callback be invoked.
3770 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 */
3772 public void clearFocus() {
3773 if (DBG) {
3774 System.out.println(this + " clearFocus()");
3775 }
3776
3777 if ((mPrivateFlags & FOCUSED) != 0) {
3778 mPrivateFlags &= ~FOCUSED;
3779
3780 if (mParent != null) {
3781 mParent.clearChildFocus(this);
3782 }
3783
3784 onFocusChanged(false, 0, null);
3785 refreshDrawableState();
3786 }
3787 }
3788
3789 /**
3790 * Called to clear the focus of a view that is about to be removed.
3791 * Doesn't call clearChildFocus, which prevents this view from taking
3792 * focus again before it has been removed from the parent
3793 */
3794 void clearFocusForRemoval() {
3795 if ((mPrivateFlags & FOCUSED) != 0) {
3796 mPrivateFlags &= ~FOCUSED;
3797
3798 onFocusChanged(false, 0, null);
3799 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003800
3801 // The view cleared focus and invoked the callbacks, so now is the
3802 // time to give focus to the the first focusable from the top to
3803 // ensure that the gain focus is announced after clear focus.
3804 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 }
3806 }
3807
3808 /**
3809 * Called internally by the view system when a new view is getting focus.
3810 * This is what clears the old focus.
3811 */
3812 void unFocus() {
3813 if (DBG) {
3814 System.out.println(this + " unFocus()");
3815 }
3816
3817 if ((mPrivateFlags & FOCUSED) != 0) {
3818 mPrivateFlags &= ~FOCUSED;
3819
3820 onFocusChanged(false, 0, null);
3821 refreshDrawableState();
3822 }
3823 }
3824
3825 /**
3826 * Returns true if this view has focus iteself, or is the ancestor of the
3827 * view that has focus.
3828 *
3829 * @return True if this view has or contains focus, false otherwise.
3830 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003831 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 public boolean hasFocus() {
3833 return (mPrivateFlags & FOCUSED) != 0;
3834 }
3835
3836 /**
3837 * Returns true if this view is focusable or if it contains a reachable View
3838 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3839 * is a View whose parents do not block descendants focus.
3840 *
3841 * Only {@link #VISIBLE} views are considered focusable.
3842 *
3843 * @return True if the view is focusable or if the view contains a focusable
3844 * View, false otherwise.
3845 *
3846 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3847 */
3848 public boolean hasFocusable() {
3849 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3850 }
3851
3852 /**
3853 * Called by the view system when the focus state of this view changes.
3854 * When the focus change event is caused by directional navigation, direction
3855 * and previouslyFocusedRect provide insight into where the focus is coming from.
3856 * When overriding, be sure to call up through to the super class so that
3857 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003858 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 * @param gainFocus True if the View has focus; false otherwise.
3860 * @param direction The direction focus has moved when requestFocus()
3861 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003862 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3863 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3864 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3866 * system, of the previously focused view. If applicable, this will be
3867 * passed in as finer grained information about where the focus is coming
3868 * from (in addition to direction). Will be <code>null</code> otherwise.
3869 */
3870 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003871 if (gainFocus) {
3872 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3873 }
3874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 InputMethodManager imm = InputMethodManager.peekInstance();
3876 if (!gainFocus) {
3877 if (isPressed()) {
3878 setPressed(false);
3879 }
3880 if (imm != null && mAttachInfo != null
3881 && mAttachInfo.mHasWindowFocus) {
3882 imm.focusOut(this);
3883 }
Romain Guya2431d02009-04-30 16:30:00 -07003884 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 } else if (imm != null && mAttachInfo != null
3886 && mAttachInfo.mHasWindowFocus) {
3887 imm.focusIn(this);
3888 }
Romain Guy8506ab42009-06-11 17:35:47 -07003889
Romain Guy0fd89bf2011-01-26 15:41:30 -08003890 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003891 ListenerInfo li = mListenerInfo;
3892 if (li != null && li.mOnFocusChangeListener != null) {
3893 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
Joe Malin32736f02011-01-19 16:14:20 -08003895
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003896 if (mAttachInfo != null) {
3897 mAttachInfo.mKeyDispatchState.reset(this);
3898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 }
3900
3901 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003902 * Sends an accessibility event of the given type. If accessiiblity is
3903 * not enabled this method has no effect. The default implementation calls
3904 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3905 * to populate information about the event source (this View), then calls
3906 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3907 * populate the text content of the event source including its descendants,
3908 * and last calls
3909 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3910 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003911 * <p>
3912 * If an {@link AccessibilityDelegate} has been specified via calling
3913 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3914 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3915 * responsible for handling this call.
3916 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003917 *
Scott Mainb303d832011-10-12 16:45:18 -07003918 * @param eventType The type of the event to send, as defined by several types from
3919 * {@link android.view.accessibility.AccessibilityEvent}, such as
3920 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3921 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003922 *
3923 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3924 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3925 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003926 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003927 */
3928 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003929 if (mAccessibilityDelegate != null) {
3930 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3931 } else {
3932 sendAccessibilityEventInternal(eventType);
3933 }
3934 }
3935
3936 /**
3937 * @see #sendAccessibilityEvent(int)
3938 *
3939 * Note: Called from the default {@link AccessibilityDelegate}.
3940 */
3941 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003942 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3943 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3944 }
3945 }
3946
3947 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003948 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3949 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003950 * perform a check whether accessibility is enabled.
3951 * <p>
3952 * If an {@link AccessibilityDelegate} has been specified via calling
3953 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3954 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3955 * is responsible for handling this call.
3956 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003957 *
3958 * @param event The event to send.
3959 *
3960 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003961 */
3962 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003963 if (mAccessibilityDelegate != null) {
3964 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3965 } else {
3966 sendAccessibilityEventUncheckedInternal(event);
3967 }
3968 }
3969
3970 /**
3971 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3972 *
3973 * Note: Called from the default {@link AccessibilityDelegate}.
3974 */
3975 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003976 if (!isShown()) {
3977 return;
3978 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003979 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003980 // Only a subset of accessibility events populates text content.
3981 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3982 dispatchPopulateAccessibilityEvent(event);
3983 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003984 // In the beginning we called #isShown(), so we know that getParent() is not null.
3985 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003986 }
3987
3988 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003989 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3990 * to its children for adding their text content to the event. Note that the
3991 * event text is populated in a separate dispatch path since we add to the
3992 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003993 * A typical implementation will call
3994 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3995 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3996 * on each child. Override this method if custom population of the event text
3997 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003998 * <p>
3999 * If an {@link AccessibilityDelegate} has been specified via calling
4000 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4001 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4002 * is responsible for handling this call.
4003 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004004 * <p>
4005 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4006 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4007 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004008 *
4009 * @param event The event.
4010 *
4011 * @return True if the event population was completed.
4012 */
4013 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004014 if (mAccessibilityDelegate != null) {
4015 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4016 } else {
4017 return dispatchPopulateAccessibilityEventInternal(event);
4018 }
4019 }
4020
4021 /**
4022 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4023 *
4024 * Note: Called from the default {@link AccessibilityDelegate}.
4025 */
4026 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004027 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004028 return false;
4029 }
4030
4031 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004032 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004033 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004034 * text content. While this method is free to modify event
4035 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004036 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4037 * <p>
4038 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004039 * to the text added by the super implementation:
4040 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004041 * super.onPopulateAccessibilityEvent(event);
4042 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4043 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4044 * mCurrentDate.getTimeInMillis(), flags);
4045 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004046 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004047 * <p>
4048 * If an {@link AccessibilityDelegate} has been specified via calling
4049 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4050 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4051 * is responsible for handling this call.
4052 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004053 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4054 * information to the event, in case the default implementation has basic information to add.
4055 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004056 *
4057 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004058 *
4059 * @see #sendAccessibilityEvent(int)
4060 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004061 */
4062 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004063 if (mAccessibilityDelegate != null) {
4064 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4065 } else {
4066 onPopulateAccessibilityEventInternal(event);
4067 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004068 }
4069
4070 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004071 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4072 *
4073 * Note: Called from the default {@link AccessibilityDelegate}.
4074 */
4075 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4076
4077 }
4078
4079 /**
4080 * Initializes an {@link AccessibilityEvent} with information about
4081 * this View which is the event source. In other words, the source of
4082 * an accessibility event is the view whose state change triggered firing
4083 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004084 * <p>
4085 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004086 * to properties set by the super implementation:
4087 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4088 * super.onInitializeAccessibilityEvent(event);
4089 * event.setPassword(true);
4090 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004091 * <p>
4092 * If an {@link AccessibilityDelegate} has been specified via calling
4093 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4094 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4095 * is responsible for handling this call.
4096 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004097 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4098 * information to the event, in case the default implementation has basic information to add.
4099 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004100 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004101 *
4102 * @see #sendAccessibilityEvent(int)
4103 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4104 */
4105 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004106 if (mAccessibilityDelegate != null) {
4107 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4108 } else {
4109 onInitializeAccessibilityEventInternal(event);
4110 }
4111 }
4112
4113 /**
4114 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4115 *
4116 * Note: Called from the default {@link AccessibilityDelegate}.
4117 */
4118 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004119 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004120 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004121 event.setPackageName(getContext().getPackageName());
4122 event.setEnabled(isEnabled());
4123 event.setContentDescription(mContentDescription);
4124
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004125 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4126 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4127 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4128 FOCUSABLES_ALL);
4129 event.setItemCount(focusablesTempList.size());
4130 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4131 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004132 }
4133 }
4134
4135 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004136 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4137 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4138 * This method is responsible for obtaining an accessibility node info from a
4139 * pool of reusable instances and calling
4140 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4141 * initialize the former.
4142 * <p>
4143 * Note: The client is responsible for recycling the obtained instance by calling
4144 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4145 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004146 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004147 * @return A populated {@link AccessibilityNodeInfo}.
4148 *
4149 * @see AccessibilityNodeInfo
4150 */
4151 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004152 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4153 if (provider != null) {
4154 return provider.createAccessibilityNodeInfo(View.NO_ID);
4155 } else {
4156 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4157 onInitializeAccessibilityNodeInfo(info);
4158 return info;
4159 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004160 }
4161
4162 /**
4163 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4164 * The base implementation sets:
4165 * <ul>
4166 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004167 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4168 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004169 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4170 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4171 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4172 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4173 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4174 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4175 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4176 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4177 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4178 * </ul>
4179 * <p>
4180 * Subclasses should override this method, call the super implementation,
4181 * and set additional attributes.
4182 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004183 * <p>
4184 * If an {@link AccessibilityDelegate} has been specified via calling
4185 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4186 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4187 * is responsible for handling this call.
4188 * </p>
4189 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004190 * @param info The instance to initialize.
4191 */
4192 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004193 if (mAccessibilityDelegate != null) {
4194 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4195 } else {
4196 onInitializeAccessibilityNodeInfoInternal(info);
4197 }
4198 }
4199
4200 /**
4201 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4202 *
4203 * Note: Called from the default {@link AccessibilityDelegate}.
4204 */
4205 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004206 Rect bounds = mAttachInfo.mTmpInvalRect;
4207 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004208 info.setBoundsInParent(bounds);
4209
4210 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4211 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004212 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004213 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4214 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004215
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004216 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4217 ViewParent parent = getParent();
4218 if (parent instanceof View) {
4219 View parentView = (View) parent;
4220 info.setParent(parentView);
4221 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004222 }
4223
4224 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004225 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004226 info.setContentDescription(getContentDescription());
4227
4228 info.setEnabled(isEnabled());
4229 info.setClickable(isClickable());
4230 info.setFocusable(isFocusable());
4231 info.setFocused(isFocused());
4232 info.setSelected(isSelected());
4233 info.setLongClickable(isLongClickable());
4234
4235 // TODO: These make sense only if we are in an AdapterView but all
4236 // views can be selected. Maybe from accessiiblity perspective
4237 // we should report as selectable view in an AdapterView.
4238 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4239 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4240
4241 if (isFocusable()) {
4242 if (isFocused()) {
4243 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4244 } else {
4245 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4246 }
4247 }
4248 }
4249
4250 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004251 * Sets a delegate for implementing accessibility support via compositon as
4252 * opposed to inheritance. The delegate's primary use is for implementing
4253 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4254 *
4255 * @param delegate The delegate instance.
4256 *
4257 * @see AccessibilityDelegate
4258 */
4259 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4260 mAccessibilityDelegate = delegate;
4261 }
4262
4263 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004264 * Gets the provider for managing a virtual view hierarchy rooted at this View
4265 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4266 * that explore the window content.
4267 * <p>
4268 * If this method returns an instance, this instance is responsible for managing
4269 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4270 * View including the one representing the View itself. Similarly the returned
4271 * instance is responsible for performing accessibility actions on any virtual
4272 * view or the root view itself.
4273 * </p>
4274 * <p>
4275 * If an {@link AccessibilityDelegate} has been specified via calling
4276 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4277 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4278 * is responsible for handling this call.
4279 * </p>
4280 *
4281 * @return The provider.
4282 *
4283 * @see AccessibilityNodeProvider
4284 */
4285 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4286 if (mAccessibilityDelegate != null) {
4287 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4288 } else {
4289 return null;
4290 }
4291 }
4292
4293 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004294 * Gets the unique identifier of this view on the screen for accessibility purposes.
4295 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4296 *
4297 * @return The view accessibility id.
4298 *
4299 * @hide
4300 */
4301 public int getAccessibilityViewId() {
4302 if (mAccessibilityViewId == NO_ID) {
4303 mAccessibilityViewId = sNextAccessibilityViewId++;
4304 }
4305 return mAccessibilityViewId;
4306 }
4307
4308 /**
4309 * Gets the unique identifier of the window in which this View reseides.
4310 *
4311 * @return The window accessibility id.
4312 *
4313 * @hide
4314 */
4315 public int getAccessibilityWindowId() {
4316 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4317 }
4318
4319 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004320 * Gets the {@link View} description. It briefly describes the view and is
4321 * primarily used for accessibility support. Set this property to enable
4322 * better accessibility support for your application. This is especially
4323 * true for views that do not have textual representation (For example,
4324 * ImageButton).
4325 *
4326 * @return The content descriptiopn.
4327 *
4328 * @attr ref android.R.styleable#View_contentDescription
4329 */
4330 public CharSequence getContentDescription() {
4331 return mContentDescription;
4332 }
4333
4334 /**
4335 * Sets the {@link View} description. It briefly describes the view and is
4336 * primarily used for accessibility support. Set this property to enable
4337 * better accessibility support for your application. This is especially
4338 * true for views that do not have textual representation (For example,
4339 * ImageButton).
4340 *
4341 * @param contentDescription The content description.
4342 *
4343 * @attr ref android.R.styleable#View_contentDescription
4344 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004345 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004346 public void setContentDescription(CharSequence contentDescription) {
4347 mContentDescription = contentDescription;
4348 }
4349
4350 /**
Romain Guya2431d02009-04-30 16:30:00 -07004351 * Invoked whenever this view loses focus, either by losing window focus or by losing
4352 * focus within its window. This method can be used to clear any state tied to the
4353 * focus. For instance, if a button is held pressed with the trackball and the window
4354 * loses focus, this method can be used to cancel the press.
4355 *
4356 * Subclasses of View overriding this method should always call super.onFocusLost().
4357 *
4358 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004359 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004360 *
4361 * @hide pending API council approval
4362 */
4363 protected void onFocusLost() {
4364 resetPressedState();
4365 }
4366
4367 private void resetPressedState() {
4368 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4369 return;
4370 }
4371
4372 if (isPressed()) {
4373 setPressed(false);
4374
4375 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004376 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004377 }
4378 }
4379 }
4380
4381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 * Returns true if this view has focus
4383 *
4384 * @return True if this view has focus, false otherwise.
4385 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004386 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 public boolean isFocused() {
4388 return (mPrivateFlags & FOCUSED) != 0;
4389 }
4390
4391 /**
4392 * Find the view in the hierarchy rooted at this view that currently has
4393 * focus.
4394 *
4395 * @return The view that currently has focus, or null if no focused view can
4396 * be found.
4397 */
4398 public View findFocus() {
4399 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4400 }
4401
4402 /**
4403 * Change whether this view is one of the set of scrollable containers in
4404 * its window. This will be used to determine whether the window can
4405 * resize or must pan when a soft input area is open -- scrollable
4406 * containers allow the window to use resize mode since the container
4407 * will appropriately shrink.
4408 */
4409 public void setScrollContainer(boolean isScrollContainer) {
4410 if (isScrollContainer) {
4411 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4412 mAttachInfo.mScrollContainers.add(this);
4413 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4414 }
4415 mPrivateFlags |= SCROLL_CONTAINER;
4416 } else {
4417 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4418 mAttachInfo.mScrollContainers.remove(this);
4419 }
4420 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4421 }
4422 }
4423
4424 /**
4425 * Returns the quality of the drawing cache.
4426 *
4427 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4428 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4429 *
4430 * @see #setDrawingCacheQuality(int)
4431 * @see #setDrawingCacheEnabled(boolean)
4432 * @see #isDrawingCacheEnabled()
4433 *
4434 * @attr ref android.R.styleable#View_drawingCacheQuality
4435 */
4436 public int getDrawingCacheQuality() {
4437 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4438 }
4439
4440 /**
4441 * Set the drawing cache quality of this view. This value is used only when the
4442 * drawing cache is enabled
4443 *
4444 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4445 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4446 *
4447 * @see #getDrawingCacheQuality()
4448 * @see #setDrawingCacheEnabled(boolean)
4449 * @see #isDrawingCacheEnabled()
4450 *
4451 * @attr ref android.R.styleable#View_drawingCacheQuality
4452 */
4453 public void setDrawingCacheQuality(int quality) {
4454 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4455 }
4456
4457 /**
4458 * Returns whether the screen should remain on, corresponding to the current
4459 * value of {@link #KEEP_SCREEN_ON}.
4460 *
4461 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4462 *
4463 * @see #setKeepScreenOn(boolean)
4464 *
4465 * @attr ref android.R.styleable#View_keepScreenOn
4466 */
4467 public boolean getKeepScreenOn() {
4468 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4469 }
4470
4471 /**
4472 * Controls whether the screen should remain on, modifying the
4473 * value of {@link #KEEP_SCREEN_ON}.
4474 *
4475 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4476 *
4477 * @see #getKeepScreenOn()
4478 *
4479 * @attr ref android.R.styleable#View_keepScreenOn
4480 */
4481 public void setKeepScreenOn(boolean keepScreenOn) {
4482 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4483 }
4484
4485 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004486 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4487 * @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 -08004488 *
4489 * @attr ref android.R.styleable#View_nextFocusLeft
4490 */
4491 public int getNextFocusLeftId() {
4492 return mNextFocusLeftId;
4493 }
4494
4495 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004496 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4497 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4498 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 *
4500 * @attr ref android.R.styleable#View_nextFocusLeft
4501 */
4502 public void setNextFocusLeftId(int nextFocusLeftId) {
4503 mNextFocusLeftId = nextFocusLeftId;
4504 }
4505
4506 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004507 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4508 * @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 -08004509 *
4510 * @attr ref android.R.styleable#View_nextFocusRight
4511 */
4512 public int getNextFocusRightId() {
4513 return mNextFocusRightId;
4514 }
4515
4516 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004517 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4518 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4519 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 *
4521 * @attr ref android.R.styleable#View_nextFocusRight
4522 */
4523 public void setNextFocusRightId(int nextFocusRightId) {
4524 mNextFocusRightId = nextFocusRightId;
4525 }
4526
4527 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004528 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4529 * @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 -08004530 *
4531 * @attr ref android.R.styleable#View_nextFocusUp
4532 */
4533 public int getNextFocusUpId() {
4534 return mNextFocusUpId;
4535 }
4536
4537 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004538 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4539 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4540 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 *
4542 * @attr ref android.R.styleable#View_nextFocusUp
4543 */
4544 public void setNextFocusUpId(int nextFocusUpId) {
4545 mNextFocusUpId = nextFocusUpId;
4546 }
4547
4548 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004549 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4550 * @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 -08004551 *
4552 * @attr ref android.R.styleable#View_nextFocusDown
4553 */
4554 public int getNextFocusDownId() {
4555 return mNextFocusDownId;
4556 }
4557
4558 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004559 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4560 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4561 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 *
4563 * @attr ref android.R.styleable#View_nextFocusDown
4564 */
4565 public void setNextFocusDownId(int nextFocusDownId) {
4566 mNextFocusDownId = nextFocusDownId;
4567 }
4568
4569 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004570 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4571 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4572 *
4573 * @attr ref android.R.styleable#View_nextFocusForward
4574 */
4575 public int getNextFocusForwardId() {
4576 return mNextFocusForwardId;
4577 }
4578
4579 /**
4580 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4581 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4582 * decide automatically.
4583 *
4584 * @attr ref android.R.styleable#View_nextFocusForward
4585 */
4586 public void setNextFocusForwardId(int nextFocusForwardId) {
4587 mNextFocusForwardId = nextFocusForwardId;
4588 }
4589
4590 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 * Returns the visibility of this view and all of its ancestors
4592 *
4593 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4594 */
4595 public boolean isShown() {
4596 View current = this;
4597 //noinspection ConstantConditions
4598 do {
4599 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4600 return false;
4601 }
4602 ViewParent parent = current.mParent;
4603 if (parent == null) {
4604 return false; // We are not attached to the view root
4605 }
4606 if (!(parent instanceof View)) {
4607 return true;
4608 }
4609 current = (View) parent;
4610 } while (current != null);
4611
4612 return false;
4613 }
4614
4615 /**
4616 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4617 * is set
4618 *
4619 * @param insets Insets for system windows
4620 *
4621 * @return True if this view applied the insets, false otherwise
4622 */
4623 protected boolean fitSystemWindows(Rect insets) {
4624 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4625 mPaddingLeft = insets.left;
4626 mPaddingTop = insets.top;
4627 mPaddingRight = insets.right;
4628 mPaddingBottom = insets.bottom;
4629 requestLayout();
4630 return true;
4631 }
4632 return false;
4633 }
4634
4635 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004636 * Set whether or not this view should account for system screen decorations
4637 * such as the status bar and inset its content. This allows this view to be
4638 * positioned in absolute screen coordinates and remain visible to the user.
4639 *
4640 * <p>This should only be used by top-level window decor views.
4641 *
4642 * @param fitSystemWindows true to inset content for system screen decorations, false for
4643 * default behavior.
4644 *
4645 * @attr ref android.R.styleable#View_fitsSystemWindows
4646 */
4647 public void setFitsSystemWindows(boolean fitSystemWindows) {
4648 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4649 }
4650
4651 /**
4652 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4653 * will account for system screen decorations such as the status bar and inset its
4654 * content. This allows the view to be positioned in absolute screen coordinates
4655 * and remain visible to the user.
4656 *
4657 * @return true if this view will adjust its content bounds for system screen decorations.
4658 *
4659 * @attr ref android.R.styleable#View_fitsSystemWindows
4660 */
4661 public boolean fitsSystemWindows() {
4662 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4663 }
4664
4665 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 * Returns the visibility status for this view.
4667 *
4668 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4669 * @attr ref android.R.styleable#View_visibility
4670 */
4671 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004672 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4673 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4674 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 })
4676 public int getVisibility() {
4677 return mViewFlags & VISIBILITY_MASK;
4678 }
4679
4680 /**
4681 * Set the enabled state of this view.
4682 *
4683 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4684 * @attr ref android.R.styleable#View_visibility
4685 */
4686 @RemotableViewMethod
4687 public void setVisibility(int visibility) {
4688 setFlags(visibility, VISIBILITY_MASK);
4689 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4690 }
4691
4692 /**
4693 * Returns the enabled status for this view. The interpretation of the
4694 * enabled state varies by subclass.
4695 *
4696 * @return True if this view is enabled, false otherwise.
4697 */
4698 @ViewDebug.ExportedProperty
4699 public boolean isEnabled() {
4700 return (mViewFlags & ENABLED_MASK) == ENABLED;
4701 }
4702
4703 /**
4704 * Set the enabled state of this view. The interpretation of the enabled
4705 * state varies by subclass.
4706 *
4707 * @param enabled True if this view is enabled, false otherwise.
4708 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004709 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004711 if (enabled == isEnabled()) return;
4712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4714
4715 /*
4716 * The View most likely has to change its appearance, so refresh
4717 * the drawable state.
4718 */
4719 refreshDrawableState();
4720
4721 // Invalidate too, since the default behavior for views is to be
4722 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004723 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 }
4725
4726 /**
4727 * Set whether this view can receive the focus.
4728 *
4729 * Setting this to false will also ensure that this view is not focusable
4730 * in touch mode.
4731 *
4732 * @param focusable If true, this view can receive the focus.
4733 *
4734 * @see #setFocusableInTouchMode(boolean)
4735 * @attr ref android.R.styleable#View_focusable
4736 */
4737 public void setFocusable(boolean focusable) {
4738 if (!focusable) {
4739 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4740 }
4741 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4742 }
4743
4744 /**
4745 * Set whether this view can receive focus while in touch mode.
4746 *
4747 * Setting this to true will also ensure that this view is focusable.
4748 *
4749 * @param focusableInTouchMode If true, this view can receive the focus while
4750 * in touch mode.
4751 *
4752 * @see #setFocusable(boolean)
4753 * @attr ref android.R.styleable#View_focusableInTouchMode
4754 */
4755 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4756 // Focusable in touch mode should always be set before the focusable flag
4757 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4758 // which, in touch mode, will not successfully request focus on this view
4759 // because the focusable in touch mode flag is not set
4760 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4761 if (focusableInTouchMode) {
4762 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4763 }
4764 }
4765
4766 /**
4767 * Set whether this view should have sound effects enabled for events such as
4768 * clicking and touching.
4769 *
4770 * <p>You may wish to disable sound effects for a view if you already play sounds,
4771 * for instance, a dial key that plays dtmf tones.
4772 *
4773 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4774 * @see #isSoundEffectsEnabled()
4775 * @see #playSoundEffect(int)
4776 * @attr ref android.R.styleable#View_soundEffectsEnabled
4777 */
4778 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4779 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4780 }
4781
4782 /**
4783 * @return whether this view should have sound effects enabled for events such as
4784 * clicking and touching.
4785 *
4786 * @see #setSoundEffectsEnabled(boolean)
4787 * @see #playSoundEffect(int)
4788 * @attr ref android.R.styleable#View_soundEffectsEnabled
4789 */
4790 @ViewDebug.ExportedProperty
4791 public boolean isSoundEffectsEnabled() {
4792 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4793 }
4794
4795 /**
4796 * Set whether this view should have haptic feedback for events such as
4797 * long presses.
4798 *
4799 * <p>You may wish to disable haptic feedback if your view already controls
4800 * its own haptic feedback.
4801 *
4802 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4803 * @see #isHapticFeedbackEnabled()
4804 * @see #performHapticFeedback(int)
4805 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4806 */
4807 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4808 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4809 }
4810
4811 /**
4812 * @return whether this view should have haptic feedback enabled for events
4813 * long presses.
4814 *
4815 * @see #setHapticFeedbackEnabled(boolean)
4816 * @see #performHapticFeedback(int)
4817 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4818 */
4819 @ViewDebug.ExportedProperty
4820 public boolean isHapticFeedbackEnabled() {
4821 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4822 }
4823
4824 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004825 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004826 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004827 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4828 * {@link #LAYOUT_DIRECTION_RTL},
4829 * {@link #LAYOUT_DIRECTION_INHERIT} or
4830 * {@link #LAYOUT_DIRECTION_LOCALE}.
4831 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004832 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004833 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004834 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4835 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4836 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4837 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004838 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004839 public int getLayoutDirection() {
4840 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004841 }
4842
4843 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004844 * Set the layout direction for this view. This will propagate a reset of layout direction
4845 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004846 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004847 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4848 * {@link #LAYOUT_DIRECTION_RTL},
4849 * {@link #LAYOUT_DIRECTION_INHERIT} or
4850 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004851 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004852 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004853 */
4854 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004855 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004856 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004857 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004858 // Setting the flag will also request a layout.
4859 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4860 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004861 }
4862
4863 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004864 * Returns the resolved layout direction for this view.
4865 *
4866 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4867 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004868 */
4869 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4870 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4871 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4872 })
4873 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004874 resolveLayoutDirectionIfNeeded();
4875 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004876 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4877 }
4878
4879 /**
4880 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4881 * layout attribute and/or the inherited value from the parent.</p>
4882 *
4883 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004884 */
4885 @ViewDebug.ExportedProperty(category = "layout")
4886 public boolean isLayoutRtl() {
4887 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4888 }
4889
4890 /**
Adam Powell539ee872012-02-03 19:00:49 -08004891 * Indicates whether the view is currently tracking transient state that the
4892 * app should not need to concern itself with saving and restoring, but that
4893 * the framework should take special note to preserve when possible.
4894 *
4895 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004896 */
4897 @ViewDebug.ExportedProperty(category = "layout")
4898 public boolean hasTransientState() {
4899 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4900 }
4901
4902 /**
4903 * Set whether this view is currently tracking transient state that the
4904 * framework should attempt to preserve when possible.
4905 *
4906 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004907 */
4908 public void setHasTransientState(boolean hasTransientState) {
4909 if (hasTransientState() == hasTransientState) return;
4910
4911 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4912 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4913 if (mParent != null) {
4914 try {
4915 mParent.childHasTransientStateChanged(this, hasTransientState);
4916 } catch (AbstractMethodError e) {
4917 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
4918 " does not fully implement ViewParent", e);
4919 }
4920 }
4921 }
4922
4923 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 * If this view doesn't do any drawing on its own, set this flag to
4925 * allow further optimizations. By default, this flag is not set on
4926 * View, but could be set on some View subclasses such as ViewGroup.
4927 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004928 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4929 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 *
4931 * @param willNotDraw whether or not this View draw on its own
4932 */
4933 public void setWillNotDraw(boolean willNotDraw) {
4934 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4935 }
4936
4937 /**
4938 * Returns whether or not this View draws on its own.
4939 *
4940 * @return true if this view has nothing to draw, false otherwise
4941 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004942 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 public boolean willNotDraw() {
4944 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4945 }
4946
4947 /**
4948 * When a View's drawing cache is enabled, drawing is redirected to an
4949 * offscreen bitmap. Some views, like an ImageView, must be able to
4950 * bypass this mechanism if they already draw a single bitmap, to avoid
4951 * unnecessary usage of the memory.
4952 *
4953 * @param willNotCacheDrawing true if this view does not cache its
4954 * drawing, false otherwise
4955 */
4956 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4957 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4958 }
4959
4960 /**
4961 * Returns whether or not this View can cache its drawing or not.
4962 *
4963 * @return true if this view does not cache its drawing, false otherwise
4964 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004965 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 public boolean willNotCacheDrawing() {
4967 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4968 }
4969
4970 /**
4971 * Indicates whether this view reacts to click events or not.
4972 *
4973 * @return true if the view is clickable, false otherwise
4974 *
4975 * @see #setClickable(boolean)
4976 * @attr ref android.R.styleable#View_clickable
4977 */
4978 @ViewDebug.ExportedProperty
4979 public boolean isClickable() {
4980 return (mViewFlags & CLICKABLE) == CLICKABLE;
4981 }
4982
4983 /**
4984 * Enables or disables click events for this view. When a view
4985 * is clickable it will change its state to "pressed" on every click.
4986 * Subclasses should set the view clickable to visually react to
4987 * user's clicks.
4988 *
4989 * @param clickable true to make the view clickable, false otherwise
4990 *
4991 * @see #isClickable()
4992 * @attr ref android.R.styleable#View_clickable
4993 */
4994 public void setClickable(boolean clickable) {
4995 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4996 }
4997
4998 /**
4999 * Indicates whether this view reacts to long click events or not.
5000 *
5001 * @return true if the view is long clickable, false otherwise
5002 *
5003 * @see #setLongClickable(boolean)
5004 * @attr ref android.R.styleable#View_longClickable
5005 */
5006 public boolean isLongClickable() {
5007 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5008 }
5009
5010 /**
5011 * Enables or disables long click events for this view. When a view is long
5012 * clickable it reacts to the user holding down the button for a longer
5013 * duration than a tap. This event can either launch the listener or a
5014 * context menu.
5015 *
5016 * @param longClickable true to make the view long clickable, false otherwise
5017 * @see #isLongClickable()
5018 * @attr ref android.R.styleable#View_longClickable
5019 */
5020 public void setLongClickable(boolean longClickable) {
5021 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5022 }
5023
5024 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005025 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 *
5027 * @see #isClickable()
5028 * @see #setClickable(boolean)
5029 *
5030 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5031 * the View's internal state from a previously set "pressed" state.
5032 */
5033 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005034 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 if (pressed) {
5037 mPrivateFlags |= PRESSED;
5038 } else {
5039 mPrivateFlags &= ~PRESSED;
5040 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005041
5042 if (needsRefresh) {
5043 refreshDrawableState();
5044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 dispatchSetPressed(pressed);
5046 }
5047
5048 /**
5049 * Dispatch setPressed to all of this View's children.
5050 *
5051 * @see #setPressed(boolean)
5052 *
5053 * @param pressed The new pressed state
5054 */
5055 protected void dispatchSetPressed(boolean pressed) {
5056 }
5057
5058 /**
5059 * Indicates whether the view is currently in pressed state. Unless
5060 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5061 * the pressed state.
5062 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005063 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 * @see #isClickable()
5065 * @see #setClickable(boolean)
5066 *
5067 * @return true if the view is currently pressed, false otherwise
5068 */
5069 public boolean isPressed() {
5070 return (mPrivateFlags & PRESSED) == PRESSED;
5071 }
5072
5073 /**
5074 * Indicates whether this view will save its state (that is,
5075 * whether its {@link #onSaveInstanceState} method will be called).
5076 *
5077 * @return Returns true if the view state saving is enabled, else false.
5078 *
5079 * @see #setSaveEnabled(boolean)
5080 * @attr ref android.R.styleable#View_saveEnabled
5081 */
5082 public boolean isSaveEnabled() {
5083 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5084 }
5085
5086 /**
5087 * Controls whether the saving of this view's state is
5088 * enabled (that is, whether its {@link #onSaveInstanceState} method
5089 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005090 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005091 * for its state to be saved. This flag can only disable the
5092 * saving of this view; any child views may still have their state saved.
5093 *
5094 * @param enabled Set to false to <em>disable</em> state saving, or true
5095 * (the default) to allow it.
5096 *
5097 * @see #isSaveEnabled()
5098 * @see #setId(int)
5099 * @see #onSaveInstanceState()
5100 * @attr ref android.R.styleable#View_saveEnabled
5101 */
5102 public void setSaveEnabled(boolean enabled) {
5103 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5104 }
5105
Jeff Brown85a31762010-09-01 17:01:00 -07005106 /**
5107 * Gets whether the framework should discard touches when the view's
5108 * window is obscured by another visible window.
5109 * Refer to the {@link View} security documentation for more details.
5110 *
5111 * @return True if touch filtering is enabled.
5112 *
5113 * @see #setFilterTouchesWhenObscured(boolean)
5114 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5115 */
5116 @ViewDebug.ExportedProperty
5117 public boolean getFilterTouchesWhenObscured() {
5118 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5119 }
5120
5121 /**
5122 * Sets whether the framework should discard touches when the view's
5123 * window is obscured by another visible window.
5124 * Refer to the {@link View} security documentation for more details.
5125 *
5126 * @param enabled True if touch filtering should be enabled.
5127 *
5128 * @see #getFilterTouchesWhenObscured
5129 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5130 */
5131 public void setFilterTouchesWhenObscured(boolean enabled) {
5132 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5133 FILTER_TOUCHES_WHEN_OBSCURED);
5134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135
5136 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005137 * Indicates whether the entire hierarchy under this view will save its
5138 * state when a state saving traversal occurs from its parent. The default
5139 * is true; if false, these views will not be saved unless
5140 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5141 *
5142 * @return Returns true if the view state saving from parent is enabled, else false.
5143 *
5144 * @see #setSaveFromParentEnabled(boolean)
5145 */
5146 public boolean isSaveFromParentEnabled() {
5147 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5148 }
5149
5150 /**
5151 * Controls whether the entire hierarchy under this view will save its
5152 * state when a state saving traversal occurs from its parent. The default
5153 * is true; if false, these views will not be saved unless
5154 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5155 *
5156 * @param enabled Set to false to <em>disable</em> state saving, or true
5157 * (the default) to allow it.
5158 *
5159 * @see #isSaveFromParentEnabled()
5160 * @see #setId(int)
5161 * @see #onSaveInstanceState()
5162 */
5163 public void setSaveFromParentEnabled(boolean enabled) {
5164 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5165 }
5166
5167
5168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 * Returns whether this View is able to take focus.
5170 *
5171 * @return True if this view can take focus, or false otherwise.
5172 * @attr ref android.R.styleable#View_focusable
5173 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005174 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 public final boolean isFocusable() {
5176 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5177 }
5178
5179 /**
5180 * When a view is focusable, it may not want to take focus when in touch mode.
5181 * For example, a button would like focus when the user is navigating via a D-pad
5182 * so that the user can click on it, but once the user starts touching the screen,
5183 * the button shouldn't take focus
5184 * @return Whether the view is focusable in touch mode.
5185 * @attr ref android.R.styleable#View_focusableInTouchMode
5186 */
5187 @ViewDebug.ExportedProperty
5188 public final boolean isFocusableInTouchMode() {
5189 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5190 }
5191
5192 /**
5193 * Find the nearest view in the specified direction that can take focus.
5194 * This does not actually give focus to that view.
5195 *
5196 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5197 *
5198 * @return The nearest focusable in the specified direction, or null if none
5199 * can be found.
5200 */
5201 public View focusSearch(int direction) {
5202 if (mParent != null) {
5203 return mParent.focusSearch(this, direction);
5204 } else {
5205 return null;
5206 }
5207 }
5208
5209 /**
5210 * This method is the last chance for the focused view and its ancestors to
5211 * respond to an arrow key. This is called when the focused view did not
5212 * consume the key internally, nor could the view system find a new view in
5213 * the requested direction to give focus to.
5214 *
5215 * @param focused The currently focused view.
5216 * @param direction The direction focus wants to move. One of FOCUS_UP,
5217 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5218 * @return True if the this view consumed this unhandled move.
5219 */
5220 public boolean dispatchUnhandledMove(View focused, int direction) {
5221 return false;
5222 }
5223
5224 /**
5225 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005226 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005228 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5229 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 * @return The user specified next view, or null if there is none.
5231 */
5232 View findUserSetNextFocus(View root, int direction) {
5233 switch (direction) {
5234 case FOCUS_LEFT:
5235 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005236 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 case FOCUS_RIGHT:
5238 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005239 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 case FOCUS_UP:
5241 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005242 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 case FOCUS_DOWN:
5244 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005245 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005246 case FOCUS_FORWARD:
5247 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005248 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005249 case FOCUS_BACKWARD: {
5250 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005251 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005252 @Override
5253 public boolean apply(View t) {
5254 return t.mNextFocusForwardId == id;
5255 }
5256 });
5257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 }
5259 return null;
5260 }
5261
Jeff Brown4dfbec22011-08-15 14:55:37 -07005262 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5263 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5264 @Override
5265 public boolean apply(View t) {
5266 return t.mID == childViewId;
5267 }
5268 });
5269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 if (result == null) {
5271 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5272 + "by user for id " + childViewId);
5273 }
5274 return result;
5275 }
5276
5277 /**
5278 * Find and return all focusable views that are descendants of this view,
5279 * possibly including this view if it is focusable itself.
5280 *
5281 * @param direction The direction of the focus
5282 * @return A list of focusable views
5283 */
5284 public ArrayList<View> getFocusables(int direction) {
5285 ArrayList<View> result = new ArrayList<View>(24);
5286 addFocusables(result, direction);
5287 return result;
5288 }
5289
5290 /**
5291 * Add any focusable views that are descendants of this view (possibly
5292 * including this view if it is focusable itself) to views. If we are in touch mode,
5293 * only add views that are also focusable in touch mode.
5294 *
5295 * @param views Focusable views found so far
5296 * @param direction The direction of the focus
5297 */
5298 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005299 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301
svetoslavganov75986cf2009-05-14 22:28:01 -07005302 /**
5303 * Adds any focusable views that are descendants of this view (possibly
5304 * including this view if it is focusable itself) to views. This method
5305 * adds all focusable views regardless if we are in touch mode or
5306 * only views focusable in touch mode if we are in touch mode depending on
5307 * the focusable mode paramater.
5308 *
5309 * @param views Focusable views found so far or null if all we are interested is
5310 * the number of focusables.
5311 * @param direction The direction of the focus.
5312 * @param focusableMode The type of focusables to be added.
5313 *
5314 * @see #FOCUSABLES_ALL
5315 * @see #FOCUSABLES_TOUCH_MODE
5316 */
5317 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5318 if (!isFocusable()) {
5319 return;
5320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321
svetoslavganov75986cf2009-05-14 22:28:01 -07005322 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5323 isInTouchMode() && !isFocusableInTouchMode()) {
5324 return;
5325 }
5326
5327 if (views != null) {
5328 views.add(this);
5329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 }
5331
5332 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005333 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005334 * The search is performed by either the text that the View renders or the content
5335 * description that describes the view for accessibility purposes and the view does
5336 * not render or both. Clients can specify how the search is to be performed via
5337 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5338 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005339 *
5340 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005341 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005342 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005343 * @see #FIND_VIEWS_WITH_TEXT
5344 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5345 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005346 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005347 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005348 if (getAccessibilityNodeProvider() != null) {
5349 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5350 outViews.add(this);
5351 }
5352 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5353 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005354 String searchedLowerCase = searched.toString().toLowerCase();
5355 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5356 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5357 outViews.add(this);
5358 }
5359 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005360 }
5361
5362 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 * Find and return all touchable views that are descendants of this view,
5364 * possibly including this view if it is touchable itself.
5365 *
5366 * @return A list of touchable views
5367 */
5368 public ArrayList<View> getTouchables() {
5369 ArrayList<View> result = new ArrayList<View>();
5370 addTouchables(result);
5371 return result;
5372 }
5373
5374 /**
5375 * Add any touchable views that are descendants of this view (possibly
5376 * including this view if it is touchable itself) to views.
5377 *
5378 * @param views Touchable views found so far
5379 */
5380 public void addTouchables(ArrayList<View> views) {
5381 final int viewFlags = mViewFlags;
5382
5383 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5384 && (viewFlags & ENABLED_MASK) == ENABLED) {
5385 views.add(this);
5386 }
5387 }
5388
5389 /**
5390 * Call this to try to give focus to a specific view or to one of its
5391 * descendants.
5392 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005393 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5394 * false), or if it is focusable and it is not focusable in touch mode
5395 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005397 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 * have focus, and you want your parent to look for the next one.
5399 *
5400 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5401 * {@link #FOCUS_DOWN} and <code>null</code>.
5402 *
5403 * @return Whether this view or one of its descendants actually took focus.
5404 */
5405 public final boolean requestFocus() {
5406 return requestFocus(View.FOCUS_DOWN);
5407 }
5408
5409
5410 /**
5411 * Call this to try to give focus to a specific view or to one of its
5412 * descendants and give it a hint about what direction focus is heading.
5413 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005414 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5415 * false), or if it is focusable and it is not focusable in touch mode
5416 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005418 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 * have focus, and you want your parent to look for the next one.
5420 *
5421 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5422 * <code>null</code> set for the previously focused rectangle.
5423 *
5424 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5425 * @return Whether this view or one of its descendants actually took focus.
5426 */
5427 public final boolean requestFocus(int direction) {
5428 return requestFocus(direction, null);
5429 }
5430
5431 /**
5432 * Call this to try to give focus to a specific view or to one of its descendants
5433 * and give it hints about the direction and a specific rectangle that the focus
5434 * is coming from. The rectangle can help give larger views a finer grained hint
5435 * about where focus is coming from, and therefore, where to show selection, or
5436 * forward focus change internally.
5437 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005438 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5439 * false), or if it is focusable and it is not focusable in touch mode
5440 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 *
5442 * A View will not take focus if it is not visible.
5443 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005444 * A View will not take focus if one of its parents has
5445 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5446 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005448 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 * have focus, and you want your parent to look for the next one.
5450 *
5451 * You may wish to override this method if your custom {@link View} has an internal
5452 * {@link View} that it wishes to forward the request to.
5453 *
5454 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5455 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5456 * to give a finer grained hint about where focus is coming from. May be null
5457 * if there is no hint.
5458 * @return Whether this view or one of its descendants actually took focus.
5459 */
5460 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5461 // need to be focusable
5462 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5463 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5464 return false;
5465 }
5466
5467 // need to be focusable in touch mode if in touch mode
5468 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005469 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5470 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471 }
5472
5473 // need to not have any parents blocking us
5474 if (hasAncestorThatBlocksDescendantFocus()) {
5475 return false;
5476 }
5477
5478 handleFocusGainInternal(direction, previouslyFocusedRect);
5479 return true;
5480 }
5481
5482 /**
5483 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5484 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5485 * touch mode to request focus when they are touched.
5486 *
5487 * @return Whether this view or one of its descendants actually took focus.
5488 *
5489 * @see #isInTouchMode()
5490 *
5491 */
5492 public final boolean requestFocusFromTouch() {
5493 // Leave touch mode if we need to
5494 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005495 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005496 if (viewRoot != null) {
5497 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 }
5499 }
5500 return requestFocus(View.FOCUS_DOWN);
5501 }
5502
5503 /**
5504 * @return Whether any ancestor of this view blocks descendant focus.
5505 */
5506 private boolean hasAncestorThatBlocksDescendantFocus() {
5507 ViewParent ancestor = mParent;
5508 while (ancestor instanceof ViewGroup) {
5509 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5510 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5511 return true;
5512 } else {
5513 ancestor = vgAncestor.getParent();
5514 }
5515 }
5516 return false;
5517 }
5518
5519 /**
Romain Guya440b002010-02-24 15:57:54 -08005520 * @hide
5521 */
5522 public void dispatchStartTemporaryDetach() {
5523 onStartTemporaryDetach();
5524 }
5525
5526 /**
5527 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5529 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005530 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 */
5532 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005533 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005534 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005535 }
5536
5537 /**
5538 * @hide
5539 */
5540 public void dispatchFinishTemporaryDetach() {
5541 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 }
Romain Guy8506ab42009-06-11 17:35:47 -07005543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 /**
5545 * Called after {@link #onStartTemporaryDetach} when the container is done
5546 * changing the view.
5547 */
5548 public void onFinishTemporaryDetach() {
5549 }
Romain Guy8506ab42009-06-11 17:35:47 -07005550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005552 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5553 * for this view's window. Returns null if the view is not currently attached
5554 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005555 * just use the standard high-level event callbacks like
5556 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005557 */
5558 public KeyEvent.DispatcherState getKeyDispatcherState() {
5559 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5560 }
Joe Malin32736f02011-01-19 16:14:20 -08005561
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 * Dispatch a key event before it is processed by any input method
5564 * associated with the view hierarchy. This can be used to intercept
5565 * key events in special situations before the IME consumes them; a
5566 * typical example would be handling the BACK key to update the application's
5567 * UI instead of allowing the IME to see it and close itself.
5568 *
5569 * @param event The key event to be dispatched.
5570 * @return True if the event was handled, false otherwise.
5571 */
5572 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5573 return onKeyPreIme(event.getKeyCode(), event);
5574 }
5575
5576 /**
5577 * Dispatch a key event to the next view on the focus path. This path runs
5578 * from the top of the view tree down to the currently focused view. If this
5579 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5580 * the next node down the focus path. This method also fires any key
5581 * listeners.
5582 *
5583 * @param event The key event to be dispatched.
5584 * @return True if the event was handled, false otherwise.
5585 */
5586 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005587 if (mInputEventConsistencyVerifier != null) {
5588 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590
Jeff Brown21bc5c92011-02-28 18:27:14 -08005591 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005592 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005593 ListenerInfo li = mListenerInfo;
5594 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5595 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 return true;
5597 }
5598
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005599 if (event.dispatch(this, mAttachInfo != null
5600 ? mAttachInfo.mKeyDispatchState : null, this)) {
5601 return true;
5602 }
5603
5604 if (mInputEventConsistencyVerifier != null) {
5605 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5606 }
5607 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 }
5609
5610 /**
5611 * Dispatches a key shortcut event.
5612 *
5613 * @param event The key event to be dispatched.
5614 * @return True if the event was handled by the view, false otherwise.
5615 */
5616 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5617 return onKeyShortcut(event.getKeyCode(), event);
5618 }
5619
5620 /**
5621 * Pass the touch screen motion event down to the target view, or this
5622 * view if it is the target.
5623 *
5624 * @param event The motion event to be dispatched.
5625 * @return True if the event was handled by the view, false otherwise.
5626 */
5627 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005628 if (mInputEventConsistencyVerifier != null) {
5629 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5630 }
5631
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005632 if (onFilterTouchEventForSecurity(event)) {
5633 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005634 ListenerInfo li = mListenerInfo;
5635 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5636 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005637 return true;
5638 }
5639
5640 if (onTouchEvent(event)) {
5641 return true;
5642 }
Jeff Brown85a31762010-09-01 17:01:00 -07005643 }
5644
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005645 if (mInputEventConsistencyVerifier != null) {
5646 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005648 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 }
5650
5651 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005652 * Filter the touch event to apply security policies.
5653 *
5654 * @param event The motion event to be filtered.
5655 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005656 *
Jeff Brown85a31762010-09-01 17:01:00 -07005657 * @see #getFilterTouchesWhenObscured
5658 */
5659 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005660 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005661 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5662 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5663 // Window is obscured, drop this touch.
5664 return false;
5665 }
5666 return true;
5667 }
5668
5669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 * Pass a trackball motion event down to the focused view.
5671 *
5672 * @param event The motion event to be dispatched.
5673 * @return True if the event was handled by the view, false otherwise.
5674 */
5675 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005676 if (mInputEventConsistencyVerifier != null) {
5677 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5678 }
5679
Romain Guy02ccac62011-06-24 13:20:23 -07005680 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 }
5682
5683 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005684 * Dispatch a generic motion event.
5685 * <p>
5686 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5687 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005688 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005689 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005690 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005691 *
5692 * @param event The motion event to be dispatched.
5693 * @return True if the event was handled by the view, false otherwise.
5694 */
5695 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005696 if (mInputEventConsistencyVerifier != null) {
5697 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5698 }
5699
Jeff Browna032cc02011-03-07 16:56:21 -08005700 final int source = event.getSource();
5701 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5702 final int action = event.getAction();
5703 if (action == MotionEvent.ACTION_HOVER_ENTER
5704 || action == MotionEvent.ACTION_HOVER_MOVE
5705 || action == MotionEvent.ACTION_HOVER_EXIT) {
5706 if (dispatchHoverEvent(event)) {
5707 return true;
5708 }
5709 } else if (dispatchGenericPointerEvent(event)) {
5710 return true;
5711 }
5712 } else if (dispatchGenericFocusedEvent(event)) {
5713 return true;
5714 }
5715
Jeff Brown10b62902011-06-20 16:40:37 -07005716 if (dispatchGenericMotionEventInternal(event)) {
5717 return true;
5718 }
5719
5720 if (mInputEventConsistencyVerifier != null) {
5721 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5722 }
5723 return false;
5724 }
5725
5726 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005727 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005728 ListenerInfo li = mListenerInfo;
5729 if (li != null && li.mOnGenericMotionListener != null
5730 && (mViewFlags & ENABLED_MASK) == ENABLED
5731 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005732 return true;
5733 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005734
5735 if (onGenericMotionEvent(event)) {
5736 return true;
5737 }
5738
5739 if (mInputEventConsistencyVerifier != null) {
5740 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5741 }
5742 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005743 }
5744
5745 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005746 * Dispatch a hover event.
5747 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005748 * Do not call this method directly.
5749 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005750 * </p>
5751 *
5752 * @param event The motion event to be dispatched.
5753 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005754 */
5755 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005756 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005757 ListenerInfo li = mListenerInfo;
5758 if (li != null && li.mOnHoverListener != null
5759 && (mViewFlags & ENABLED_MASK) == ENABLED
5760 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005761 return true;
5762 }
5763
Jeff Browna032cc02011-03-07 16:56:21 -08005764 return onHoverEvent(event);
5765 }
5766
5767 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005768 * Returns true if the view has a child to which it has recently sent
5769 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5770 * it does not have a hovered child, then it must be the innermost hovered view.
5771 * @hide
5772 */
5773 protected boolean hasHoveredChild() {
5774 return false;
5775 }
5776
5777 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005778 * Dispatch a generic motion event to the view under the first pointer.
5779 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005780 * Do not call this method directly.
5781 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005782 * </p>
5783 *
5784 * @param event The motion event to be dispatched.
5785 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005786 */
5787 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5788 return false;
5789 }
5790
5791 /**
5792 * Dispatch a generic motion event to the currently focused view.
5793 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005794 * Do not call this method directly.
5795 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005796 * </p>
5797 *
5798 * @param event The motion event to be dispatched.
5799 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005800 */
5801 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5802 return false;
5803 }
5804
5805 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005806 * Dispatch a pointer event.
5807 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005808 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5809 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5810 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005811 * and should not be expected to handle other pointing device features.
5812 * </p>
5813 *
5814 * @param event The motion event to be dispatched.
5815 * @return True if the event was handled by the view, false otherwise.
5816 * @hide
5817 */
5818 public final boolean dispatchPointerEvent(MotionEvent event) {
5819 if (event.isTouchEvent()) {
5820 return dispatchTouchEvent(event);
5821 } else {
5822 return dispatchGenericMotionEvent(event);
5823 }
5824 }
5825
5826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005827 * Called when the window containing this view gains or loses window focus.
5828 * ViewGroups should override to route to their children.
5829 *
5830 * @param hasFocus True if the window containing this view now has focus,
5831 * false otherwise.
5832 */
5833 public void dispatchWindowFocusChanged(boolean hasFocus) {
5834 onWindowFocusChanged(hasFocus);
5835 }
5836
5837 /**
5838 * Called when the window containing this view gains or loses focus. Note
5839 * that this is separate from view focus: to receive key events, both
5840 * your view and its window must have focus. If a window is displayed
5841 * on top of yours that takes input focus, then your own window will lose
5842 * focus but the view focus will remain unchanged.
5843 *
5844 * @param hasWindowFocus True if the window containing this view now has
5845 * focus, false otherwise.
5846 */
5847 public void onWindowFocusChanged(boolean hasWindowFocus) {
5848 InputMethodManager imm = InputMethodManager.peekInstance();
5849 if (!hasWindowFocus) {
5850 if (isPressed()) {
5851 setPressed(false);
5852 }
5853 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5854 imm.focusOut(this);
5855 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005856 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005857 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005858 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5860 imm.focusIn(this);
5861 }
5862 refreshDrawableState();
5863 }
5864
5865 /**
5866 * Returns true if this view is in a window that currently has window focus.
5867 * Note that this is not the same as the view itself having focus.
5868 *
5869 * @return True if this view is in a window that currently has window focus.
5870 */
5871 public boolean hasWindowFocus() {
5872 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5873 }
5874
5875 /**
Adam Powell326d8082009-12-09 15:10:07 -08005876 * Dispatch a view visibility change down the view hierarchy.
5877 * ViewGroups should override to route to their children.
5878 * @param changedView The view whose visibility changed. Could be 'this' or
5879 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005880 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5881 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005882 */
5883 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5884 onVisibilityChanged(changedView, visibility);
5885 }
5886
5887 /**
5888 * Called when the visibility of the view or an ancestor of the view is changed.
5889 * @param changedView The view whose visibility changed. Could be 'this' or
5890 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005891 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5892 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005893 */
5894 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005895 if (visibility == VISIBLE) {
5896 if (mAttachInfo != null) {
5897 initialAwakenScrollBars();
5898 } else {
5899 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5900 }
5901 }
Adam Powell326d8082009-12-09 15:10:07 -08005902 }
5903
5904 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005905 * Dispatch a hint about whether this view is displayed. For instance, when
5906 * a View moves out of the screen, it might receives a display hint indicating
5907 * the view is not displayed. Applications should not <em>rely</em> on this hint
5908 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005909 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005910 * @param hint A hint about whether or not this view is displayed:
5911 * {@link #VISIBLE} or {@link #INVISIBLE}.
5912 */
5913 public void dispatchDisplayHint(int hint) {
5914 onDisplayHint(hint);
5915 }
5916
5917 /**
5918 * Gives this view a hint about whether is displayed or not. For instance, when
5919 * a View moves out of the screen, it might receives a display hint indicating
5920 * the view is not displayed. Applications should not <em>rely</em> on this hint
5921 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005922 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005923 * @param hint A hint about whether or not this view is displayed:
5924 * {@link #VISIBLE} or {@link #INVISIBLE}.
5925 */
5926 protected void onDisplayHint(int hint) {
5927 }
5928
5929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 * Dispatch a window visibility change down the view hierarchy.
5931 * ViewGroups should override to route to their children.
5932 *
5933 * @param visibility The new visibility of the window.
5934 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005935 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 */
5937 public void dispatchWindowVisibilityChanged(int visibility) {
5938 onWindowVisibilityChanged(visibility);
5939 }
5940
5941 /**
5942 * Called when the window containing has change its visibility
5943 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5944 * that this tells you whether or not your window is being made visible
5945 * to the window manager; this does <em>not</em> tell you whether or not
5946 * your window is obscured by other windows on the screen, even if it
5947 * is itself visible.
5948 *
5949 * @param visibility The new visibility of the window.
5950 */
5951 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005952 if (visibility == VISIBLE) {
5953 initialAwakenScrollBars();
5954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 }
5956
5957 /**
5958 * Returns the current visibility of the window this view is attached to
5959 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5960 *
5961 * @return Returns the current visibility of the view's window.
5962 */
5963 public int getWindowVisibility() {
5964 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5965 }
5966
5967 /**
5968 * Retrieve the overall visible display size in which the window this view is
5969 * attached to has been positioned in. This takes into account screen
5970 * decorations above the window, for both cases where the window itself
5971 * is being position inside of them or the window is being placed under
5972 * then and covered insets are used for the window to position its content
5973 * inside. In effect, this tells you the available area where content can
5974 * be placed and remain visible to users.
5975 *
5976 * <p>This function requires an IPC back to the window manager to retrieve
5977 * the requested information, so should not be used in performance critical
5978 * code like drawing.
5979 *
5980 * @param outRect Filled in with the visible display frame. If the view
5981 * is not attached to a window, this is simply the raw display size.
5982 */
5983 public void getWindowVisibleDisplayFrame(Rect outRect) {
5984 if (mAttachInfo != null) {
5985 try {
5986 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5987 } catch (RemoteException e) {
5988 return;
5989 }
5990 // XXX This is really broken, and probably all needs to be done
5991 // in the window manager, and we need to know more about whether
5992 // we want the area behind or in front of the IME.
5993 final Rect insets = mAttachInfo.mVisibleInsets;
5994 outRect.left += insets.left;
5995 outRect.top += insets.top;
5996 outRect.right -= insets.right;
5997 outRect.bottom -= insets.bottom;
5998 return;
5999 }
6000 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006001 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 }
6003
6004 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006005 * Dispatch a notification about a resource configuration change down
6006 * the view hierarchy.
6007 * ViewGroups should override to route to their children.
6008 *
6009 * @param newConfig The new resource configuration.
6010 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006011 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006012 */
6013 public void dispatchConfigurationChanged(Configuration newConfig) {
6014 onConfigurationChanged(newConfig);
6015 }
6016
6017 /**
6018 * Called when the current configuration of the resources being used
6019 * by the application have changed. You can use this to decide when
6020 * to reload resources that can changed based on orientation and other
6021 * configuration characterstics. You only need to use this if you are
6022 * not relying on the normal {@link android.app.Activity} mechanism of
6023 * recreating the activity instance upon a configuration change.
6024 *
6025 * @param newConfig The new resource configuration.
6026 */
6027 protected void onConfigurationChanged(Configuration newConfig) {
6028 }
6029
6030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 * Private function to aggregate all per-view attributes in to the view
6032 * root.
6033 */
6034 void dispatchCollectViewAttributes(int visibility) {
6035 performCollectViewAttributes(visibility);
6036 }
6037
6038 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006039 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006040 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6041 mAttachInfo.mKeepScreenOn = true;
6042 }
6043 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006044 ListenerInfo li = mListenerInfo;
6045 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006046 mAttachInfo.mHasSystemUiListeners = true;
6047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 }
6049 }
6050
6051 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006052 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006054 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6055 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 ai.mRecomputeGlobalAttributes = true;
6057 }
6058 }
6059 }
6060
6061 /**
6062 * Returns whether the device is currently in touch mode. Touch mode is entered
6063 * once the user begins interacting with the device by touch, and affects various
6064 * things like whether focus is always visible to the user.
6065 *
6066 * @return Whether the device is in touch mode.
6067 */
6068 @ViewDebug.ExportedProperty
6069 public boolean isInTouchMode() {
6070 if (mAttachInfo != null) {
6071 return mAttachInfo.mInTouchMode;
6072 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006073 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 }
6075 }
6076
6077 /**
6078 * Returns the context the view is running in, through which it can
6079 * access the current theme, resources, etc.
6080 *
6081 * @return The view's Context.
6082 */
6083 @ViewDebug.CapturedViewProperty
6084 public final Context getContext() {
6085 return mContext;
6086 }
6087
6088 /**
6089 * Handle a key event before it is processed by any input method
6090 * associated with the view hierarchy. This can be used to intercept
6091 * key events in special situations before the IME consumes them; a
6092 * typical example would be handling the BACK key to update the application's
6093 * UI instead of allowing the IME to see it and close itself.
6094 *
6095 * @param keyCode The value in event.getKeyCode().
6096 * @param event Description of the key event.
6097 * @return If you handled the event, return true. If you want to allow the
6098 * event to be handled by the next receiver, return false.
6099 */
6100 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6101 return false;
6102 }
6103
6104 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006105 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6106 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006107 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6108 * is released, if the view is enabled and clickable.
6109 *
6110 * @param keyCode A key code that represents the button pressed, from
6111 * {@link android.view.KeyEvent}.
6112 * @param event The KeyEvent object that defines the button action.
6113 */
6114 public boolean onKeyDown(int keyCode, KeyEvent event) {
6115 boolean result = false;
6116
6117 switch (keyCode) {
6118 case KeyEvent.KEYCODE_DPAD_CENTER:
6119 case KeyEvent.KEYCODE_ENTER: {
6120 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6121 return true;
6122 }
6123 // Long clickable items don't necessarily have to be clickable
6124 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6125 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6126 (event.getRepeatCount() == 0)) {
6127 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006128 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129 return true;
6130 }
6131 break;
6132 }
6133 }
6134 return result;
6135 }
6136
6137 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006138 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6139 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6140 * the event).
6141 */
6142 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6143 return false;
6144 }
6145
6146 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006147 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6148 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6150 * {@link KeyEvent#KEYCODE_ENTER} is released.
6151 *
6152 * @param keyCode A key code that represents the button pressed, from
6153 * {@link android.view.KeyEvent}.
6154 * @param event The KeyEvent object that defines the button action.
6155 */
6156 public boolean onKeyUp(int keyCode, KeyEvent event) {
6157 boolean result = false;
6158
6159 switch (keyCode) {
6160 case KeyEvent.KEYCODE_DPAD_CENTER:
6161 case KeyEvent.KEYCODE_ENTER: {
6162 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6163 return true;
6164 }
6165 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6166 setPressed(false);
6167
6168 if (!mHasPerformedLongPress) {
6169 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006170 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171
6172 result = performClick();
6173 }
6174 }
6175 break;
6176 }
6177 }
6178 return result;
6179 }
6180
6181 /**
6182 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6183 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6184 * the event).
6185 *
6186 * @param keyCode A key code that represents the button pressed, from
6187 * {@link android.view.KeyEvent}.
6188 * @param repeatCount The number of times the action was made.
6189 * @param event The KeyEvent object that defines the button action.
6190 */
6191 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6192 return false;
6193 }
6194
6195 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006196 * Called on the focused view when a key shortcut event is not handled.
6197 * Override this method to implement local key shortcuts for the View.
6198 * Key shortcuts can also be implemented by setting the
6199 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 *
6201 * @param keyCode The value in event.getKeyCode().
6202 * @param event Description of the key event.
6203 * @return If you handled the event, return true. If you want to allow the
6204 * event to be handled by the next receiver, return false.
6205 */
6206 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6207 return false;
6208 }
6209
6210 /**
6211 * Check whether the called view is a text editor, in which case it
6212 * would make sense to automatically display a soft input window for
6213 * it. Subclasses should override this if they implement
6214 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006215 * a call on that method would return a non-null InputConnection, and
6216 * they are really a first-class editor that the user would normally
6217 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006218 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006219 * <p>The default implementation always returns false. This does
6220 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6221 * will not be called or the user can not otherwise perform edits on your
6222 * view; it is just a hint to the system that this is not the primary
6223 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006224 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 * @return Returns true if this view is a text editor, else false.
6226 */
6227 public boolean onCheckIsTextEditor() {
6228 return false;
6229 }
Romain Guy8506ab42009-06-11 17:35:47 -07006230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 /**
6232 * Create a new InputConnection for an InputMethod to interact
6233 * with the view. The default implementation returns null, since it doesn't
6234 * support input methods. You can override this to implement such support.
6235 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 * <p>When implementing this, you probably also want to implement
6238 * {@link #onCheckIsTextEditor()} to indicate you will return a
6239 * non-null InputConnection.
6240 *
6241 * @param outAttrs Fill in with attribute information about the connection.
6242 */
6243 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6244 return null;
6245 }
6246
6247 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006248 * Called by the {@link android.view.inputmethod.InputMethodManager}
6249 * when a view who is not the current
6250 * input connection target is trying to make a call on the manager. The
6251 * default implementation returns false; you can override this to return
6252 * true for certain views if you are performing InputConnection proxying
6253 * to them.
6254 * @param view The View that is making the InputMethodManager call.
6255 * @return Return true to allow the call, false to reject.
6256 */
6257 public boolean checkInputConnectionProxy(View view) {
6258 return false;
6259 }
Romain Guy8506ab42009-06-11 17:35:47 -07006260
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 * Show the context menu for this view. It is not safe to hold on to the
6263 * menu after returning from this method.
6264 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006265 * You should normally not overload this method. Overload
6266 * {@link #onCreateContextMenu(ContextMenu)} or define an
6267 * {@link OnCreateContextMenuListener} to add items to the context menu.
6268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 * @param menu The context menu to populate
6270 */
6271 public void createContextMenu(ContextMenu menu) {
6272 ContextMenuInfo menuInfo = getContextMenuInfo();
6273
6274 // Sets the current menu info so all items added to menu will have
6275 // my extra info set.
6276 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6277
6278 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006279 ListenerInfo li = mListenerInfo;
6280 if (li != null && li.mOnCreateContextMenuListener != null) {
6281 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 }
6283
6284 // Clear the extra information so subsequent items that aren't mine don't
6285 // have my extra info.
6286 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6287
6288 if (mParent != null) {
6289 mParent.createContextMenu(menu);
6290 }
6291 }
6292
6293 /**
6294 * Views should implement this if they have extra information to associate
6295 * with the context menu. The return result is supplied as a parameter to
6296 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6297 * callback.
6298 *
6299 * @return Extra information about the item for which the context menu
6300 * should be shown. This information will vary across different
6301 * subclasses of View.
6302 */
6303 protected ContextMenuInfo getContextMenuInfo() {
6304 return null;
6305 }
6306
6307 /**
6308 * Views should implement this if the view itself is going to add items to
6309 * the context menu.
6310 *
6311 * @param menu the context menu to populate
6312 */
6313 protected void onCreateContextMenu(ContextMenu menu) {
6314 }
6315
6316 /**
6317 * Implement this method to handle trackball motion events. The
6318 * <em>relative</em> movement of the trackball since the last event
6319 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6320 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6321 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6322 * they will often be fractional values, representing the more fine-grained
6323 * movement information available from a trackball).
6324 *
6325 * @param event The motion event.
6326 * @return True if the event was handled, false otherwise.
6327 */
6328 public boolean onTrackballEvent(MotionEvent event) {
6329 return false;
6330 }
6331
6332 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006333 * Implement this method to handle generic motion events.
6334 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006335 * Generic motion events describe joystick movements, mouse hovers, track pad
6336 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006337 * {@link MotionEvent#getSource() source} of the motion event specifies
6338 * the class of input that was received. Implementations of this method
6339 * must examine the bits in the source before processing the event.
6340 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006341 * </p><p>
6342 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6343 * are delivered to the view under the pointer. All other generic motion events are
6344 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006345 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006346 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006347 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006348 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6349 * // process the joystick movement...
6350 * return true;
6351 * }
6352 * }
6353 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6354 * switch (event.getAction()) {
6355 * case MotionEvent.ACTION_HOVER_MOVE:
6356 * // process the mouse hover movement...
6357 * return true;
6358 * case MotionEvent.ACTION_SCROLL:
6359 * // process the scroll wheel movement...
6360 * return true;
6361 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006362 * }
6363 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006364 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006365 *
6366 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006367 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006368 */
6369 public boolean onGenericMotionEvent(MotionEvent event) {
6370 return false;
6371 }
6372
6373 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006374 * Implement this method to handle hover events.
6375 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006376 * This method is called whenever a pointer is hovering into, over, or out of the
6377 * bounds of a view and the view is not currently being touched.
6378 * Hover events are represented as pointer events with action
6379 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6380 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6381 * </p>
6382 * <ul>
6383 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6384 * when the pointer enters the bounds of the view.</li>
6385 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6386 * when the pointer has already entered the bounds of the view and has moved.</li>
6387 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6388 * when the pointer has exited the bounds of the view or when the pointer is
6389 * about to go down due to a button click, tap, or similar user action that
6390 * causes the view to be touched.</li>
6391 * </ul>
6392 * <p>
6393 * The view should implement this method to return true to indicate that it is
6394 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006395 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006396 * The default implementation calls {@link #setHovered} to update the hovered state
6397 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006398 * is enabled and is clickable. The default implementation also sends hover
6399 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006400 * </p>
6401 *
6402 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006403 * @return True if the view handled the hover event.
6404 *
6405 * @see #isHovered
6406 * @see #setHovered
6407 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006408 */
6409 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006410 // The root view may receive hover (or touch) events that are outside the bounds of
6411 // the window. This code ensures that we only send accessibility events for
6412 // hovers that are actually within the bounds of the root view.
6413 final int action = event.getAction();
6414 if (!mSendingHoverAccessibilityEvents) {
6415 if ((action == MotionEvent.ACTION_HOVER_ENTER
6416 || action == MotionEvent.ACTION_HOVER_MOVE)
6417 && !hasHoveredChild()
6418 && pointInView(event.getX(), event.getY())) {
6419 mSendingHoverAccessibilityEvents = true;
6420 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6421 }
6422 } else {
6423 if (action == MotionEvent.ACTION_HOVER_EXIT
6424 || (action == MotionEvent.ACTION_HOVER_MOVE
6425 && !pointInView(event.getX(), event.getY()))) {
6426 mSendingHoverAccessibilityEvents = false;
6427 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6428 }
Jeff Browna1b24182011-07-28 13:38:24 -07006429 }
6430
Jeff Brown87b7f802011-06-21 18:35:45 -07006431 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006432 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006433 case MotionEvent.ACTION_HOVER_ENTER:
6434 setHovered(true);
6435 break;
6436 case MotionEvent.ACTION_HOVER_EXIT:
6437 setHovered(false);
6438 break;
6439 }
Jeff Browna1b24182011-07-28 13:38:24 -07006440
6441 // Dispatch the event to onGenericMotionEvent before returning true.
6442 // This is to provide compatibility with existing applications that
6443 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6444 // break because of the new default handling for hoverable views
6445 // in onHoverEvent.
6446 // Note that onGenericMotionEvent will be called by default when
6447 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6448 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006449 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006450 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006451 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006452 }
6453
6454 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006455 * Returns true if the view should handle {@link #onHoverEvent}
6456 * by calling {@link #setHovered} to change its hovered state.
6457 *
6458 * @return True if the view is hoverable.
6459 */
6460 private boolean isHoverable() {
6461 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006462 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006463 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6464 return false;
6465 }
6466
6467 return (viewFlags & CLICKABLE) == CLICKABLE
6468 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6469 }
6470
6471 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006472 * Returns true if the view is currently hovered.
6473 *
6474 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006475 *
6476 * @see #setHovered
6477 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006478 */
Jeff Brown10b62902011-06-20 16:40:37 -07006479 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006480 public boolean isHovered() {
6481 return (mPrivateFlags & HOVERED) != 0;
6482 }
6483
6484 /**
6485 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006486 * <p>
6487 * Calling this method also changes the drawable state of the view. This
6488 * enables the view to react to hover by using different drawable resources
6489 * to change its appearance.
6490 * </p><p>
6491 * The {@link #onHoverChanged} method is called when the hovered state changes.
6492 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006493 *
6494 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006495 *
6496 * @see #isHovered
6497 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006498 */
6499 public void setHovered(boolean hovered) {
6500 if (hovered) {
6501 if ((mPrivateFlags & HOVERED) == 0) {
6502 mPrivateFlags |= HOVERED;
6503 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006504 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006505 }
6506 } else {
6507 if ((mPrivateFlags & HOVERED) != 0) {
6508 mPrivateFlags &= ~HOVERED;
6509 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006510 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006511 }
6512 }
6513 }
6514
6515 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006516 * Implement this method to handle hover state changes.
6517 * <p>
6518 * This method is called whenever the hover state changes as a result of a
6519 * call to {@link #setHovered}.
6520 * </p>
6521 *
6522 * @param hovered The current hover state, as returned by {@link #isHovered}.
6523 *
6524 * @see #isHovered
6525 * @see #setHovered
6526 */
6527 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006528 }
6529
6530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006531 * Implement this method to handle touch screen motion events.
6532 *
6533 * @param event The motion event.
6534 * @return True if the event was handled, false otherwise.
6535 */
6536 public boolean onTouchEvent(MotionEvent event) {
6537 final int viewFlags = mViewFlags;
6538
6539 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006540 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006541 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543 // A disabled view that is clickable still consumes the touch
6544 // events, it just doesn't respond to them.
6545 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6546 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6547 }
6548
6549 if (mTouchDelegate != null) {
6550 if (mTouchDelegate.onTouchEvent(event)) {
6551 return true;
6552 }
6553 }
6554
6555 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6556 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6557 switch (event.getAction()) {
6558 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006559 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6560 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 // take focus if we don't have it already and we should in
6562 // touch mode.
6563 boolean focusTaken = false;
6564 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6565 focusTaken = requestFocus();
6566 }
6567
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006568 if (prepressed) {
6569 // The button is being released before we actually
6570 // showed it as pressed. Make it show the pressed
6571 // state now (before scheduling the click) to ensure
6572 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006573 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006574 }
Joe Malin32736f02011-01-19 16:14:20 -08006575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 if (!mHasPerformedLongPress) {
6577 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006578 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579
6580 // Only perform take click actions if we were in the pressed state
6581 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006582 // Use a Runnable and post this rather than calling
6583 // performClick directly. This lets other visual state
6584 // of the view update before click actions start.
6585 if (mPerformClick == null) {
6586 mPerformClick = new PerformClick();
6587 }
6588 if (!post(mPerformClick)) {
6589 performClick();
6590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 }
6592 }
6593
6594 if (mUnsetPressedState == null) {
6595 mUnsetPressedState = new UnsetPressedState();
6596 }
6597
Adam Powelle14579b2009-12-16 18:39:52 -08006598 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006599 postDelayed(mUnsetPressedState,
6600 ViewConfiguration.getPressedStateDuration());
6601 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006602 // If the post failed, unpress right now
6603 mUnsetPressedState.run();
6604 }
Adam Powelle14579b2009-12-16 18:39:52 -08006605 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 }
6607 break;
6608
6609 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006610 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006611
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006612 if (performButtonActionOnTouchDown(event)) {
6613 break;
6614 }
6615
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006616 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006617 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006618
6619 // For views inside a scrolling container, delay the pressed feedback for
6620 // a short period in case this is a scroll.
6621 if (isInScrollingContainer) {
6622 mPrivateFlags |= PREPRESSED;
6623 if (mPendingCheckForTap == null) {
6624 mPendingCheckForTap = new CheckForTap();
6625 }
6626 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6627 } else {
6628 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006629 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006630 checkForLongClick(0);
6631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 break;
6633
6634 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006635 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006636 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 break;
6638
6639 case MotionEvent.ACTION_MOVE:
6640 final int x = (int) event.getX();
6641 final int y = (int) event.getY();
6642
6643 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006644 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006646 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006647 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006648 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006649 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006650
Adam Powell4d6f0662012-02-21 15:11:11 -08006651 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 }
6654 break;
6655 }
6656 return true;
6657 }
6658
6659 return false;
6660 }
6661
6662 /**
Adam Powell10298662011-08-14 18:26:30 -07006663 * @hide
6664 */
6665 public boolean isInScrollingContainer() {
6666 ViewParent p = getParent();
6667 while (p != null && p instanceof ViewGroup) {
6668 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6669 return true;
6670 }
6671 p = p.getParent();
6672 }
6673 return false;
6674 }
6675
6676 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006677 * Remove the longpress detection timer.
6678 */
6679 private void removeLongPressCallback() {
6680 if (mPendingCheckForLongPress != null) {
6681 removeCallbacks(mPendingCheckForLongPress);
6682 }
6683 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006684
6685 /**
6686 * Remove the pending click action
6687 */
6688 private void removePerformClickCallback() {
6689 if (mPerformClick != null) {
6690 removeCallbacks(mPerformClick);
6691 }
6692 }
6693
Adam Powelle14579b2009-12-16 18:39:52 -08006694 /**
Romain Guya440b002010-02-24 15:57:54 -08006695 * Remove the prepress detection timer.
6696 */
6697 private void removeUnsetPressCallback() {
6698 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6699 setPressed(false);
6700 removeCallbacks(mUnsetPressedState);
6701 }
6702 }
6703
6704 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006705 * Remove the tap detection timer.
6706 */
6707 private void removeTapCallback() {
6708 if (mPendingCheckForTap != null) {
6709 mPrivateFlags &= ~PREPRESSED;
6710 removeCallbacks(mPendingCheckForTap);
6711 }
6712 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006713
6714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 * Cancels a pending long press. Your subclass can use this if you
6716 * want the context menu to come up if the user presses and holds
6717 * at the same place, but you don't want it to come up if they press
6718 * and then move around enough to cause scrolling.
6719 */
6720 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006721 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006722
6723 /*
6724 * The prepressed state handled by the tap callback is a display
6725 * construct, but the tap callback will post a long press callback
6726 * less its own timeout. Remove it here.
6727 */
6728 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 }
6730
6731 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006732 * Remove the pending callback for sending a
6733 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6734 */
6735 private void removeSendViewScrolledAccessibilityEventCallback() {
6736 if (mSendViewScrolledAccessibilityEvent != null) {
6737 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6738 }
6739 }
6740
6741 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 * Sets the TouchDelegate for this View.
6743 */
6744 public void setTouchDelegate(TouchDelegate delegate) {
6745 mTouchDelegate = delegate;
6746 }
6747
6748 /**
6749 * Gets the TouchDelegate for this View.
6750 */
6751 public TouchDelegate getTouchDelegate() {
6752 return mTouchDelegate;
6753 }
6754
6755 /**
6756 * Set flags controlling behavior of this view.
6757 *
6758 * @param flags Constant indicating the value which should be set
6759 * @param mask Constant indicating the bit range that should be changed
6760 */
6761 void setFlags(int flags, int mask) {
6762 int old = mViewFlags;
6763 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6764
6765 int changed = mViewFlags ^ old;
6766 if (changed == 0) {
6767 return;
6768 }
6769 int privateFlags = mPrivateFlags;
6770
6771 /* Check if the FOCUSABLE bit has changed */
6772 if (((changed & FOCUSABLE_MASK) != 0) &&
6773 ((privateFlags & HAS_BOUNDS) !=0)) {
6774 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6775 && ((privateFlags & FOCUSED) != 0)) {
6776 /* Give up focus if we are no longer focusable */
6777 clearFocus();
6778 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6779 && ((privateFlags & FOCUSED) == 0)) {
6780 /*
6781 * Tell the view system that we are now available to take focus
6782 * if no one else already has it.
6783 */
6784 if (mParent != null) mParent.focusableViewAvailable(this);
6785 }
6786 }
6787
6788 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6789 if ((changed & VISIBILITY_MASK) != 0) {
6790 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006791 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006792 * it was not visible. Marking it drawn ensures that the invalidation will
6793 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006795 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006796 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797
6798 needGlobalAttributesUpdate(true);
6799
6800 // a view becoming visible is worth notifying the parent
6801 // about in case nothing has focus. even if this specific view
6802 // isn't focusable, it may contain something that is, so let
6803 // the root view try to give this focus if nothing else does.
6804 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6805 mParent.focusableViewAvailable(this);
6806 }
6807 }
6808 }
6809
6810 /* Check if the GONE bit has changed */
6811 if ((changed & GONE) != 0) {
6812 needGlobalAttributesUpdate(false);
6813 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814
Romain Guyecd80ee2009-12-03 17:13:02 -08006815 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6816 if (hasFocus()) clearFocus();
6817 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006818 if (mParent instanceof View) {
6819 // GONE views noop invalidation, so invalidate the parent
6820 ((View) mParent).invalidate(true);
6821 }
6822 // Mark the view drawn to ensure that it gets invalidated properly the next
6823 // time it is visible and gets invalidated
6824 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006825 }
6826 if (mAttachInfo != null) {
6827 mAttachInfo.mViewVisibilityChanged = true;
6828 }
6829 }
6830
6831 /* Check if the VISIBLE bit has changed */
6832 if ((changed & INVISIBLE) != 0) {
6833 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006834 /*
6835 * If this view is becoming invisible, set the DRAWN flag so that
6836 * the next invalidate() will not be skipped.
6837 */
6838 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839
6840 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6841 // root view becoming invisible shouldn't clear focus
6842 if (getRootView() != this) {
6843 clearFocus();
6844 }
6845 }
6846 if (mAttachInfo != null) {
6847 mAttachInfo.mViewVisibilityChanged = true;
6848 }
6849 }
6850
Adam Powell326d8082009-12-09 15:10:07 -08006851 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006852 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006853 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6854 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006855 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006856 } else if (mParent != null) {
6857 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006858 }
Adam Powell326d8082009-12-09 15:10:07 -08006859 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6860 }
6861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006862 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6863 destroyDrawingCache();
6864 }
6865
6866 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6867 destroyDrawingCache();
6868 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006869 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870 }
6871
6872 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6873 destroyDrawingCache();
6874 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6875 }
6876
6877 if ((changed & DRAW_MASK) != 0) {
6878 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6879 if (mBGDrawable != null) {
6880 mPrivateFlags &= ~SKIP_DRAW;
6881 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6882 } else {
6883 mPrivateFlags |= SKIP_DRAW;
6884 }
6885 } else {
6886 mPrivateFlags &= ~SKIP_DRAW;
6887 }
6888 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006889 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890 }
6891
6892 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006893 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 mParent.recomputeViewAttributes(this);
6895 }
6896 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006897
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006898 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006899 requestLayout();
6900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 }
6902
6903 /**
6904 * Change the view's z order in the tree, so it's on top of other sibling
6905 * views
6906 */
6907 public void bringToFront() {
6908 if (mParent != null) {
6909 mParent.bringChildToFront(this);
6910 }
6911 }
6912
6913 /**
6914 * This is called in response to an internal scroll in this view (i.e., the
6915 * view scrolled its own contents). This is typically as a result of
6916 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6917 * called.
6918 *
6919 * @param l Current horizontal scroll origin.
6920 * @param t Current vertical scroll origin.
6921 * @param oldl Previous horizontal scroll origin.
6922 * @param oldt Previous vertical scroll origin.
6923 */
6924 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006925 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6926 postSendViewScrolledAccessibilityEventCallback();
6927 }
6928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 mBackgroundSizeChanged = true;
6930
6931 final AttachInfo ai = mAttachInfo;
6932 if (ai != null) {
6933 ai.mViewScrollChanged = true;
6934 }
6935 }
6936
6937 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006938 * Interface definition for a callback to be invoked when the layout bounds of a view
6939 * changes due to layout processing.
6940 */
6941 public interface OnLayoutChangeListener {
6942 /**
6943 * Called when the focus state of a view has changed.
6944 *
6945 * @param v The view whose state has changed.
6946 * @param left The new value of the view's left property.
6947 * @param top The new value of the view's top property.
6948 * @param right The new value of the view's right property.
6949 * @param bottom The new value of the view's bottom property.
6950 * @param oldLeft The previous value of the view's left property.
6951 * @param oldTop The previous value of the view's top property.
6952 * @param oldRight The previous value of the view's right property.
6953 * @param oldBottom The previous value of the view's bottom property.
6954 */
6955 void onLayoutChange(View v, int left, int top, int right, int bottom,
6956 int oldLeft, int oldTop, int oldRight, int oldBottom);
6957 }
6958
6959 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 * This is called during layout when the size of this view has changed. If
6961 * you were just added to the view hierarchy, you're called with the old
6962 * values of 0.
6963 *
6964 * @param w Current width of this view.
6965 * @param h Current height of this view.
6966 * @param oldw Old width of this view.
6967 * @param oldh Old height of this view.
6968 */
6969 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6970 }
6971
6972 /**
6973 * Called by draw to draw the child views. This may be overridden
6974 * by derived classes to gain control just before its children are drawn
6975 * (but after its own view has been drawn).
6976 * @param canvas the canvas on which to draw the view
6977 */
6978 protected void dispatchDraw(Canvas canvas) {
6979 }
6980
6981 /**
6982 * Gets the parent of this view. Note that the parent is a
6983 * ViewParent and not necessarily a View.
6984 *
6985 * @return Parent of this view.
6986 */
6987 public final ViewParent getParent() {
6988 return mParent;
6989 }
6990
6991 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006992 * Set the horizontal scrolled position of your view. This will cause a call to
6993 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6994 * invalidated.
6995 * @param value the x position to scroll to
6996 */
6997 public void setScrollX(int value) {
6998 scrollTo(value, mScrollY);
6999 }
7000
7001 /**
7002 * Set the vertical scrolled position of your view. This will cause a call to
7003 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7004 * invalidated.
7005 * @param value the y position to scroll to
7006 */
7007 public void setScrollY(int value) {
7008 scrollTo(mScrollX, value);
7009 }
7010
7011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 * Return the scrolled left position of this view. This is the left edge of
7013 * the displayed part of your view. You do not need to draw any pixels
7014 * farther left, since those are outside of the frame of your view on
7015 * screen.
7016 *
7017 * @return The left edge of the displayed part of your view, in pixels.
7018 */
7019 public final int getScrollX() {
7020 return mScrollX;
7021 }
7022
7023 /**
7024 * Return the scrolled top position of this view. This is the top edge of
7025 * the displayed part of your view. You do not need to draw any pixels above
7026 * it, since those are outside of the frame of your view on screen.
7027 *
7028 * @return The top edge of the displayed part of your view, in pixels.
7029 */
7030 public final int getScrollY() {
7031 return mScrollY;
7032 }
7033
7034 /**
7035 * Return the width of the your view.
7036 *
7037 * @return The width of your view, in pixels.
7038 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007039 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007040 public final int getWidth() {
7041 return mRight - mLeft;
7042 }
7043
7044 /**
7045 * Return the height of your view.
7046 *
7047 * @return The height of your view, in pixels.
7048 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007049 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007050 public final int getHeight() {
7051 return mBottom - mTop;
7052 }
7053
7054 /**
7055 * Return the visible drawing bounds of your view. Fills in the output
7056 * rectangle with the values from getScrollX(), getScrollY(),
7057 * getWidth(), and getHeight().
7058 *
7059 * @param outRect The (scrolled) drawing bounds of the view.
7060 */
7061 public void getDrawingRect(Rect outRect) {
7062 outRect.left = mScrollX;
7063 outRect.top = mScrollY;
7064 outRect.right = mScrollX + (mRight - mLeft);
7065 outRect.bottom = mScrollY + (mBottom - mTop);
7066 }
7067
7068 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007069 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7070 * raw width component (that is the result is masked by
7071 * {@link #MEASURED_SIZE_MASK}).
7072 *
7073 * @return The raw measured width of this view.
7074 */
7075 public final int getMeasuredWidth() {
7076 return mMeasuredWidth & MEASURED_SIZE_MASK;
7077 }
7078
7079 /**
7080 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007081 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007082 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 * This should be used during measurement and layout calculations only. Use
7084 * {@link #getWidth()} to see how wide a view is after layout.
7085 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007086 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007087 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007088 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 return mMeasuredWidth;
7090 }
7091
7092 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007093 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7094 * raw width component (that is the result is masked by
7095 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007097 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007098 */
7099 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007100 return mMeasuredHeight & MEASURED_SIZE_MASK;
7101 }
7102
7103 /**
7104 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007105 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007106 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7107 * This should be used during measurement and layout calculations only. Use
7108 * {@link #getHeight()} to see how wide a view is after layout.
7109 *
7110 * @return The measured width of this view as a bit mask.
7111 */
7112 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 return mMeasuredHeight;
7114 }
7115
7116 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007117 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7118 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7119 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7120 * and the height component is at the shifted bits
7121 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7122 */
7123 public final int getMeasuredState() {
7124 return (mMeasuredWidth&MEASURED_STATE_MASK)
7125 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7126 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7127 }
7128
7129 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007130 * The transform matrix of this view, which is calculated based on the current
7131 * roation, scale, and pivot properties.
7132 *
7133 * @see #getRotation()
7134 * @see #getScaleX()
7135 * @see #getScaleY()
7136 * @see #getPivotX()
7137 * @see #getPivotY()
7138 * @return The current transform matrix for the view
7139 */
7140 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007141 if (mTransformationInfo != null) {
7142 updateMatrix();
7143 return mTransformationInfo.mMatrix;
7144 }
7145 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007146 }
7147
7148 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007149 * Utility function to determine if the value is far enough away from zero to be
7150 * considered non-zero.
7151 * @param value A floating point value to check for zero-ness
7152 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7153 */
7154 private static boolean nonzero(float value) {
7155 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7156 }
7157
7158 /**
Jeff Brown86671742010-09-30 20:00:15 -07007159 * Returns true if the transform matrix is the identity matrix.
7160 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007161 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007162 * @return True if the transform matrix is the identity matrix, false otherwise.
7163 */
Jeff Brown86671742010-09-30 20:00:15 -07007164 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007165 if (mTransformationInfo != null) {
7166 updateMatrix();
7167 return mTransformationInfo.mMatrixIsIdentity;
7168 }
7169 return true;
7170 }
7171
7172 void ensureTransformationInfo() {
7173 if (mTransformationInfo == null) {
7174 mTransformationInfo = new TransformationInfo();
7175 }
Jeff Brown86671742010-09-30 20:00:15 -07007176 }
7177
7178 /**
7179 * Recomputes the transform matrix if necessary.
7180 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007181 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007182 final TransformationInfo info = mTransformationInfo;
7183 if (info == null) {
7184 return;
7185 }
7186 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007187 // transform-related properties have changed since the last time someone
7188 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007189
7190 // Figure out if we need to update the pivot point
7191 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007192 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7193 info.mPrevWidth = mRight - mLeft;
7194 info.mPrevHeight = mBottom - mTop;
7195 info.mPivotX = info.mPrevWidth / 2f;
7196 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007197 }
7198 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007199 info.mMatrix.reset();
7200 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7201 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7202 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7203 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007204 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007205 if (info.mCamera == null) {
7206 info.mCamera = new Camera();
7207 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007208 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007209 info.mCamera.save();
7210 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7211 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7212 info.mCamera.getMatrix(info.matrix3D);
7213 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7214 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7215 info.mPivotY + info.mTranslationY);
7216 info.mMatrix.postConcat(info.matrix3D);
7217 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007218 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007219 info.mMatrixDirty = false;
7220 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7221 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007222 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007223 }
7224
7225 /**
7226 * Utility method to retrieve the inverse of the current mMatrix property.
7227 * We cache the matrix to avoid recalculating it when transform properties
7228 * have not changed.
7229 *
7230 * @return The inverse of the current matrix of this view.
7231 */
Jeff Brown86671742010-09-30 20:00:15 -07007232 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007233 final TransformationInfo info = mTransformationInfo;
7234 if (info != null) {
7235 updateMatrix();
7236 if (info.mInverseMatrixDirty) {
7237 if (info.mInverseMatrix == null) {
7238 info.mInverseMatrix = new Matrix();
7239 }
7240 info.mMatrix.invert(info.mInverseMatrix);
7241 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007242 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007243 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007244 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007245 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007246 }
7247
7248 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007249 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7250 * views are drawn) from the camera to this view. The camera's distance
7251 * affects 3D transformations, for instance rotations around the X and Y
7252 * axis. If the rotationX or rotationY properties are changed and this view is
7253 * large (more than half the size of the screen), it is recommended to always
7254 * use a camera distance that's greater than the height (X axis rotation) or
7255 * the width (Y axis rotation) of this view.</p>
7256 *
7257 * <p>The distance of the camera from the view plane can have an affect on the
7258 * perspective distortion of the view when it is rotated around the x or y axis.
7259 * For example, a large distance will result in a large viewing angle, and there
7260 * will not be much perspective distortion of the view as it rotates. A short
7261 * distance may cause much more perspective distortion upon rotation, and can
7262 * also result in some drawing artifacts if the rotated view ends up partially
7263 * behind the camera (which is why the recommendation is to use a distance at
7264 * least as far as the size of the view, if the view is to be rotated.)</p>
7265 *
7266 * <p>The distance is expressed in "depth pixels." The default distance depends
7267 * on the screen density. For instance, on a medium density display, the
7268 * default distance is 1280. On a high density display, the default distance
7269 * is 1920.</p>
7270 *
7271 * <p>If you want to specify a distance that leads to visually consistent
7272 * results across various densities, use the following formula:</p>
7273 * <pre>
7274 * float scale = context.getResources().getDisplayMetrics().density;
7275 * view.setCameraDistance(distance * scale);
7276 * </pre>
7277 *
7278 * <p>The density scale factor of a high density display is 1.5,
7279 * and 1920 = 1280 * 1.5.</p>
7280 *
7281 * @param distance The distance in "depth pixels", if negative the opposite
7282 * value is used
7283 *
7284 * @see #setRotationX(float)
7285 * @see #setRotationY(float)
7286 */
7287 public void setCameraDistance(float distance) {
7288 invalidateParentCaches();
7289 invalidate(false);
7290
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007291 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007292 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007293 final TransformationInfo info = mTransformationInfo;
7294 if (info.mCamera == null) {
7295 info.mCamera = new Camera();
7296 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007297 }
7298
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007299 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7300 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007301
7302 invalidate(false);
7303 }
7304
7305 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007306 * The degrees that the view is rotated around the pivot point.
7307 *
Romain Guya5364ee2011-02-24 14:46:04 -08007308 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007309 * @see #getPivotX()
7310 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007311 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007312 * @return The degrees of rotation.
7313 */
Chet Haasea5531132012-02-02 13:41:44 -08007314 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007315 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007316 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007317 }
7318
7319 /**
Chet Haase897247b2010-09-09 14:54:47 -07007320 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7321 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007322 *
7323 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007324 *
7325 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007326 * @see #getPivotX()
7327 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007328 * @see #setRotationX(float)
7329 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007330 *
7331 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007332 */
7333 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007334 ensureTransformationInfo();
7335 final TransformationInfo info = mTransformationInfo;
7336 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007337 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007338 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007339 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007340 info.mRotation = rotation;
7341 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007342 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007343 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007344 }
7345 }
7346
7347 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007348 * The degrees that the view is rotated around the vertical axis through the pivot point.
7349 *
7350 * @see #getPivotX()
7351 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007352 * @see #setRotationY(float)
7353 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007354 * @return The degrees of Y rotation.
7355 */
Chet Haasea5531132012-02-02 13:41:44 -08007356 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007357 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007358 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007359 }
7360
7361 /**
Chet Haase897247b2010-09-09 14:54:47 -07007362 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7363 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7364 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007365 *
7366 * When rotating large views, it is recommended to adjust the camera distance
7367 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007368 *
7369 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007370 *
7371 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007372 * @see #getPivotX()
7373 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007374 * @see #setRotation(float)
7375 * @see #setRotationX(float)
7376 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007377 *
7378 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007379 */
7380 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007381 ensureTransformationInfo();
7382 final TransformationInfo info = mTransformationInfo;
7383 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007384 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007385 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007386 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007387 info.mRotationY = rotationY;
7388 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007389 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007390 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007391 }
7392 }
7393
7394 /**
7395 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7396 *
7397 * @see #getPivotX()
7398 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007399 * @see #setRotationX(float)
7400 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007401 * @return The degrees of X rotation.
7402 */
Chet Haasea5531132012-02-02 13:41:44 -08007403 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007404 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007405 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007406 }
7407
7408 /**
Chet Haase897247b2010-09-09 14:54:47 -07007409 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7410 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7411 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007412 *
7413 * When rotating large views, it is recommended to adjust the camera distance
7414 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007415 *
7416 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007417 *
7418 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007419 * @see #getPivotX()
7420 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007421 * @see #setRotation(float)
7422 * @see #setRotationY(float)
7423 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007424 *
7425 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007426 */
7427 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007428 ensureTransformationInfo();
7429 final TransformationInfo info = mTransformationInfo;
7430 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007431 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007432 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007433 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007434 info.mRotationX = rotationX;
7435 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007436 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007437 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007438 }
7439 }
7440
7441 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007442 * The amount that the view is scaled in x around the pivot point, as a proportion of
7443 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7444 *
Joe Onorato93162322010-09-16 15:42:01 -04007445 * <p>By default, this is 1.0f.
7446 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007447 * @see #getPivotX()
7448 * @see #getPivotY()
7449 * @return The scaling factor.
7450 */
Chet Haasea5531132012-02-02 13:41:44 -08007451 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007452 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007453 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007454 }
7455
7456 /**
7457 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7458 * the view's unscaled width. A value of 1 means that no scaling is applied.
7459 *
7460 * @param scaleX The scaling factor.
7461 * @see #getPivotX()
7462 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007463 *
7464 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007465 */
7466 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007467 ensureTransformationInfo();
7468 final TransformationInfo info = mTransformationInfo;
7469 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007470 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007471 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007472 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007473 info.mScaleX = scaleX;
7474 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007475 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007476 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007477 }
7478 }
7479
7480 /**
7481 * The amount that the view is scaled in y around the pivot point, as a proportion of
7482 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7483 *
Joe Onorato93162322010-09-16 15:42:01 -04007484 * <p>By default, this is 1.0f.
7485 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007486 * @see #getPivotX()
7487 * @see #getPivotY()
7488 * @return The scaling factor.
7489 */
Chet Haasea5531132012-02-02 13:41:44 -08007490 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007491 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007492 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007493 }
7494
7495 /**
7496 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7497 * the view's unscaled width. A value of 1 means that no scaling is applied.
7498 *
7499 * @param scaleY The scaling factor.
7500 * @see #getPivotX()
7501 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007502 *
7503 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007504 */
7505 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007506 ensureTransformationInfo();
7507 final TransformationInfo info = mTransformationInfo;
7508 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007509 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007510 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007511 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007512 info.mScaleY = scaleY;
7513 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007514 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007515 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007516 }
7517 }
7518
7519 /**
7520 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7521 * and {@link #setScaleX(float) scaled}.
7522 *
7523 * @see #getRotation()
7524 * @see #getScaleX()
7525 * @see #getScaleY()
7526 * @see #getPivotY()
7527 * @return The x location of the pivot point.
7528 */
Chet Haasea5531132012-02-02 13:41:44 -08007529 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007530 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007531 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007532 }
7533
7534 /**
7535 * Sets the x location of the point around which the view is
7536 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007537 * By default, the pivot point is centered on the object.
7538 * Setting this property disables this behavior and causes the view to use only the
7539 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007540 *
7541 * @param pivotX The x location of the pivot point.
7542 * @see #getRotation()
7543 * @see #getScaleX()
7544 * @see #getScaleY()
7545 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007546 *
7547 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007548 */
7549 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007550 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007551 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007552 final TransformationInfo info = mTransformationInfo;
7553 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007554 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007555 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007556 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007557 info.mPivotX = pivotX;
7558 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007559 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007560 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007561 }
7562 }
7563
7564 /**
7565 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7566 * and {@link #setScaleY(float) scaled}.
7567 *
7568 * @see #getRotation()
7569 * @see #getScaleX()
7570 * @see #getScaleY()
7571 * @see #getPivotY()
7572 * @return The y location of the pivot point.
7573 */
Chet Haasea5531132012-02-02 13:41:44 -08007574 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007575 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007576 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007577 }
7578
7579 /**
7580 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007581 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7582 * Setting this property disables this behavior and causes the view to use only the
7583 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007584 *
7585 * @param pivotY The y location of the pivot point.
7586 * @see #getRotation()
7587 * @see #getScaleX()
7588 * @see #getScaleY()
7589 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007590 *
7591 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007592 */
7593 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007594 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007595 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007596 final TransformationInfo info = mTransformationInfo;
7597 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007598 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007599 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007600 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007601 info.mPivotY = pivotY;
7602 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007603 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007604 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007605 }
7606 }
7607
7608 /**
7609 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7610 * completely transparent and 1 means the view is completely opaque.
7611 *
Joe Onorato93162322010-09-16 15:42:01 -04007612 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007613 * @return The opacity of the view.
7614 */
Chet Haasea5531132012-02-02 13:41:44 -08007615 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007616 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007617 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007618 }
7619
7620 /**
Romain Guy171c5922011-01-06 10:04:23 -08007621 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7622 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007623 *
Romain Guy171c5922011-01-06 10:04:23 -08007624 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7625 * responsible for applying the opacity itself. Otherwise, calling this method is
7626 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007627 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007628 *
Chet Haasea5531132012-02-02 13:41:44 -08007629 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7630 * performance implications. It is generally best to use the alpha property sparingly and
7631 * transiently, as in the case of fading animations.</p>
7632 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007633 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007634 *
Joe Malin32736f02011-01-19 16:14:20 -08007635 * @see #setLayerType(int, android.graphics.Paint)
7636 *
Chet Haase73066682010-11-29 15:55:32 -08007637 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007638 */
7639 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007640 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007641 if (mTransformationInfo.mAlpha != alpha) {
7642 mTransformationInfo.mAlpha = alpha;
7643 invalidateParentCaches();
7644 if (onSetAlpha((int) (alpha * 255))) {
7645 mPrivateFlags |= ALPHA_SET;
7646 // subclass is handling alpha - don't optimize rendering cache invalidation
7647 invalidate(true);
7648 } else {
7649 mPrivateFlags &= ~ALPHA_SET;
7650 invalidate(false);
7651 }
Chet Haaseed032702010-10-01 14:05:54 -07007652 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007653 }
7654
7655 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007656 * Faster version of setAlpha() which performs the same steps except there are
7657 * no calls to invalidate(). The caller of this function should perform proper invalidation
7658 * on the parent and this object. The return value indicates whether the subclass handles
7659 * alpha (the return value for onSetAlpha()).
7660 *
7661 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007662 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7663 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007664 */
7665 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007666 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007667 if (mTransformationInfo.mAlpha != alpha) {
7668 mTransformationInfo.mAlpha = alpha;
7669 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7670 if (subclassHandlesAlpha) {
7671 mPrivateFlags |= ALPHA_SET;
7672 return true;
7673 } else {
7674 mPrivateFlags &= ~ALPHA_SET;
7675 }
Chet Haasea00f3862011-02-22 06:34:40 -08007676 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007677 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007678 }
7679
7680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 * Top position of this view relative to its parent.
7682 *
7683 * @return The top of this view, in pixels.
7684 */
7685 @ViewDebug.CapturedViewProperty
7686 public final int getTop() {
7687 return mTop;
7688 }
7689
7690 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007691 * Sets the top position of this view relative to its parent. This method is meant to be called
7692 * by the layout system and should not generally be called otherwise, because the property
7693 * may be changed at any time by the layout.
7694 *
7695 * @param top The top of this view, in pixels.
7696 */
7697 public final void setTop(int top) {
7698 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007699 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007700 final boolean matrixIsIdentity = mTransformationInfo == null
7701 || mTransformationInfo.mMatrixIsIdentity;
7702 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007703 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007704 int minTop;
7705 int yLoc;
7706 if (top < mTop) {
7707 minTop = top;
7708 yLoc = top - mTop;
7709 } else {
7710 minTop = mTop;
7711 yLoc = 0;
7712 }
Chet Haasee9140a72011-02-16 16:23:29 -08007713 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007714 }
7715 } else {
7716 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007717 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007718 }
7719
Chet Haaseed032702010-10-01 14:05:54 -07007720 int width = mRight - mLeft;
7721 int oldHeight = mBottom - mTop;
7722
Chet Haase21cd1382010-09-01 17:42:29 -07007723 mTop = top;
7724
Chet Haaseed032702010-10-01 14:05:54 -07007725 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7726
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007727 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007728 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7729 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007730 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007731 }
Chet Haase21cd1382010-09-01 17:42:29 -07007732 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007733 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007734 }
Chet Haase55dbb652010-12-21 20:15:08 -08007735 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007736 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007737 }
7738 }
7739
7740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 * Bottom position of this view relative to its parent.
7742 *
7743 * @return The bottom of this view, in pixels.
7744 */
7745 @ViewDebug.CapturedViewProperty
7746 public final int getBottom() {
7747 return mBottom;
7748 }
7749
7750 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007751 * True if this view has changed since the last time being drawn.
7752 *
7753 * @return The dirty state of this view.
7754 */
7755 public boolean isDirty() {
7756 return (mPrivateFlags & DIRTY_MASK) != 0;
7757 }
7758
7759 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007760 * Sets the bottom position of this view relative to its parent. This method is meant to be
7761 * called by the layout system and should not generally be called otherwise, because the
7762 * property may be changed at any time by the layout.
7763 *
7764 * @param bottom The bottom of this view, in pixels.
7765 */
7766 public final void setBottom(int bottom) {
7767 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007768 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007769 final boolean matrixIsIdentity = mTransformationInfo == null
7770 || mTransformationInfo.mMatrixIsIdentity;
7771 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007772 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007773 int maxBottom;
7774 if (bottom < mBottom) {
7775 maxBottom = mBottom;
7776 } else {
7777 maxBottom = bottom;
7778 }
Chet Haasee9140a72011-02-16 16:23:29 -08007779 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007780 }
7781 } else {
7782 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007783 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007784 }
7785
Chet Haaseed032702010-10-01 14:05:54 -07007786 int width = mRight - mLeft;
7787 int oldHeight = mBottom - mTop;
7788
Chet Haase21cd1382010-09-01 17:42:29 -07007789 mBottom = bottom;
7790
Chet Haaseed032702010-10-01 14:05:54 -07007791 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7792
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007793 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007794 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7795 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007796 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007797 }
Chet Haase21cd1382010-09-01 17:42:29 -07007798 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007799 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007800 }
Chet Haase55dbb652010-12-21 20:15:08 -08007801 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007802 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007803 }
7804 }
7805
7806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 * Left position of this view relative to its parent.
7808 *
7809 * @return The left edge of this view, in pixels.
7810 */
7811 @ViewDebug.CapturedViewProperty
7812 public final int getLeft() {
7813 return mLeft;
7814 }
7815
7816 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007817 * Sets the left position of this view relative to its parent. This method is meant to be called
7818 * by the layout system and should not generally be called otherwise, because the property
7819 * may be changed at any time by the layout.
7820 *
7821 * @param left The bottom of this view, in pixels.
7822 */
7823 public final void setLeft(int left) {
7824 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007825 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007826 final boolean matrixIsIdentity = mTransformationInfo == null
7827 || mTransformationInfo.mMatrixIsIdentity;
7828 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007829 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007830 int minLeft;
7831 int xLoc;
7832 if (left < mLeft) {
7833 minLeft = left;
7834 xLoc = left - mLeft;
7835 } else {
7836 minLeft = mLeft;
7837 xLoc = 0;
7838 }
Chet Haasee9140a72011-02-16 16:23:29 -08007839 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007840 }
7841 } else {
7842 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007843 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007844 }
7845
Chet Haaseed032702010-10-01 14:05:54 -07007846 int oldWidth = mRight - mLeft;
7847 int height = mBottom - mTop;
7848
Chet Haase21cd1382010-09-01 17:42:29 -07007849 mLeft = left;
7850
Chet Haaseed032702010-10-01 14:05:54 -07007851 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7852
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007853 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007854 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7855 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007856 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007857 }
Chet Haase21cd1382010-09-01 17:42:29 -07007858 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007859 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007860 }
Chet Haase55dbb652010-12-21 20:15:08 -08007861 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007862 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007863 }
7864 }
7865
7866 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 * Right position of this view relative to its parent.
7868 *
7869 * @return The right edge of this view, in pixels.
7870 */
7871 @ViewDebug.CapturedViewProperty
7872 public final int getRight() {
7873 return mRight;
7874 }
7875
7876 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007877 * Sets the right position of this view relative to its parent. This method is meant to be called
7878 * by the layout system and should not generally be called otherwise, because the property
7879 * may be changed at any time by the layout.
7880 *
7881 * @param right The bottom of this view, in pixels.
7882 */
7883 public final void setRight(int right) {
7884 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007885 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007886 final boolean matrixIsIdentity = mTransformationInfo == null
7887 || mTransformationInfo.mMatrixIsIdentity;
7888 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007889 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007890 int maxRight;
7891 if (right < mRight) {
7892 maxRight = mRight;
7893 } else {
7894 maxRight = right;
7895 }
Chet Haasee9140a72011-02-16 16:23:29 -08007896 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007897 }
7898 } else {
7899 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007900 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007901 }
7902
Chet Haaseed032702010-10-01 14:05:54 -07007903 int oldWidth = mRight - mLeft;
7904 int height = mBottom - mTop;
7905
Chet Haase21cd1382010-09-01 17:42:29 -07007906 mRight = right;
7907
Chet Haaseed032702010-10-01 14:05:54 -07007908 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7909
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007910 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007911 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7912 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007913 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007914 }
Chet Haase21cd1382010-09-01 17:42:29 -07007915 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007916 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007917 }
Chet Haase55dbb652010-12-21 20:15:08 -08007918 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007919 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007920 }
7921 }
7922
7923 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007924 * The visual x position of this view, in pixels. This is equivalent to the
7925 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007926 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007927 *
Chet Haasedf030d22010-07-30 17:22:38 -07007928 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007929 */
Chet Haasea5531132012-02-02 13:41:44 -08007930 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007931 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007932 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007933 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007934
Chet Haasedf030d22010-07-30 17:22:38 -07007935 /**
7936 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7937 * {@link #setTranslationX(float) translationX} property to be the difference between
7938 * the x value passed in and the current {@link #getLeft() left} property.
7939 *
7940 * @param x The visual x position of this view, in pixels.
7941 */
7942 public void setX(float x) {
7943 setTranslationX(x - mLeft);
7944 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007945
Chet Haasedf030d22010-07-30 17:22:38 -07007946 /**
7947 * The visual y position of this view, in pixels. This is equivalent to the
7948 * {@link #setTranslationY(float) translationY} property plus the current
7949 * {@link #getTop() top} property.
7950 *
7951 * @return The visual y position of this view, in pixels.
7952 */
Chet Haasea5531132012-02-02 13:41:44 -08007953 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007954 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007955 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007956 }
7957
7958 /**
7959 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7960 * {@link #setTranslationY(float) translationY} property to be the difference between
7961 * the y value passed in and the current {@link #getTop() top} property.
7962 *
7963 * @param y The visual y position of this view, in pixels.
7964 */
7965 public void setY(float y) {
7966 setTranslationY(y - mTop);
7967 }
7968
7969
7970 /**
7971 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7972 * This position is post-layout, in addition to wherever the object's
7973 * layout placed it.
7974 *
7975 * @return The horizontal position of this view relative to its left position, in pixels.
7976 */
Chet Haasea5531132012-02-02 13:41:44 -08007977 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007978 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007979 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007980 }
7981
7982 /**
7983 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7984 * This effectively positions the object post-layout, in addition to wherever the object's
7985 * layout placed it.
7986 *
7987 * @param translationX The horizontal position of this view relative to its left position,
7988 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007989 *
7990 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007991 */
7992 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007993 ensureTransformationInfo();
7994 final TransformationInfo info = mTransformationInfo;
7995 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007996 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007997 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007998 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007999 info.mTranslationX = translationX;
8000 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008001 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008002 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008003 }
8004 }
8005
8006 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008007 * The horizontal location of this view relative to its {@link #getTop() top} position.
8008 * This position is post-layout, in addition to wherever the object's
8009 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008010 *
Chet Haasedf030d22010-07-30 17:22:38 -07008011 * @return The vertical position of this view relative to its top position,
8012 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008013 */
Chet Haasea5531132012-02-02 13:41:44 -08008014 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008015 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008016 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008017 }
8018
8019 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008020 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8021 * This effectively positions the object post-layout, in addition to wherever the object's
8022 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008023 *
Chet Haasedf030d22010-07-30 17:22:38 -07008024 * @param translationY The vertical position of this view relative to its top position,
8025 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008026 *
8027 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008028 */
Chet Haasedf030d22010-07-30 17:22:38 -07008029 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008030 ensureTransformationInfo();
8031 final TransformationInfo info = mTransformationInfo;
8032 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008033 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008034 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008035 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008036 info.mTranslationY = translationY;
8037 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008038 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008039 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07008040 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008041 }
8042
8043 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 * Hit rectangle in parent's coordinates
8045 *
8046 * @param outRect The hit rectangle of the view.
8047 */
8048 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008049 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008050 final TransformationInfo info = mTransformationInfo;
8051 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008052 outRect.set(mLeft, mTop, mRight, mBottom);
8053 } else {
8054 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008055 tmpRect.set(-info.mPivotX, -info.mPivotY,
8056 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8057 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008058 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8059 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008060 }
8061 }
8062
8063 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008064 * Determines whether the given point, in local coordinates is inside the view.
8065 */
8066 /*package*/ final boolean pointInView(float localX, float localY) {
8067 return localX >= 0 && localX < (mRight - mLeft)
8068 && localY >= 0 && localY < (mBottom - mTop);
8069 }
8070
8071 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008072 * Utility method to determine whether the given point, in local coordinates,
8073 * is inside the view, where the area of the view is expanded by the slop factor.
8074 * This method is called while processing touch-move events to determine if the event
8075 * is still within the view.
8076 */
8077 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008078 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008079 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008080 }
8081
8082 /**
8083 * When a view has focus and the user navigates away from it, the next view is searched for
8084 * starting from the rectangle filled in by this method.
8085 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008086 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8087 * of the view. However, if your view maintains some idea of internal selection,
8088 * such as a cursor, or a selected row or column, you should override this method and
8089 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 *
8091 * @param r The rectangle to fill in, in this view's coordinates.
8092 */
8093 public void getFocusedRect(Rect r) {
8094 getDrawingRect(r);
8095 }
8096
8097 /**
8098 * If some part of this view is not clipped by any of its parents, then
8099 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008100 * coordinates (without taking possible View rotations into account), offset
8101 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8102 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103 *
8104 * @param r If true is returned, r holds the global coordinates of the
8105 * visible portion of this view.
8106 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8107 * between this view and its root. globalOffet may be null.
8108 * @return true if r is non-empty (i.e. part of the view is visible at the
8109 * root level.
8110 */
8111 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8112 int width = mRight - mLeft;
8113 int height = mBottom - mTop;
8114 if (width > 0 && height > 0) {
8115 r.set(0, 0, width, height);
8116 if (globalOffset != null) {
8117 globalOffset.set(-mScrollX, -mScrollY);
8118 }
8119 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8120 }
8121 return false;
8122 }
8123
8124 public final boolean getGlobalVisibleRect(Rect r) {
8125 return getGlobalVisibleRect(r, null);
8126 }
8127
8128 public final boolean getLocalVisibleRect(Rect r) {
8129 Point offset = new Point();
8130 if (getGlobalVisibleRect(r, offset)) {
8131 r.offset(-offset.x, -offset.y); // make r local
8132 return true;
8133 }
8134 return false;
8135 }
8136
8137 /**
8138 * Offset this view's vertical location by the specified number of pixels.
8139 *
8140 * @param offset the number of pixels to offset the view by
8141 */
8142 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008143 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008144 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008145 final boolean matrixIsIdentity = mTransformationInfo == null
8146 || mTransformationInfo.mMatrixIsIdentity;
8147 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008148 final ViewParent p = mParent;
8149 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008150 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008151 int minTop;
8152 int maxBottom;
8153 int yLoc;
8154 if (offset < 0) {
8155 minTop = mTop + offset;
8156 maxBottom = mBottom;
8157 yLoc = offset;
8158 } else {
8159 minTop = mTop;
8160 maxBottom = mBottom + offset;
8161 yLoc = 0;
8162 }
8163 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8164 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008165 }
8166 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008167 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008168 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008169
Chet Haasec3aa3612010-06-17 08:50:37 -07008170 mTop += offset;
8171 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008172
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008173 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008174 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008175 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008176 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008177 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 }
8180
8181 /**
8182 * Offset this view's horizontal location by the specified amount of pixels.
8183 *
8184 * @param offset the numer of pixels to offset the view by
8185 */
8186 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008187 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008188 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008189 final boolean matrixIsIdentity = mTransformationInfo == null
8190 || mTransformationInfo.mMatrixIsIdentity;
8191 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008192 final ViewParent p = mParent;
8193 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008194 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008195 int minLeft;
8196 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008197 if (offset < 0) {
8198 minLeft = mLeft + offset;
8199 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008200 } else {
8201 minLeft = mLeft;
8202 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008203 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008204 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008205 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008206 }
8207 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008208 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008209 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008210
Chet Haasec3aa3612010-06-17 08:50:37 -07008211 mLeft += offset;
8212 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008213
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008214 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008215 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008216 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008217 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008218 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008220 }
8221
8222 /**
8223 * Get the LayoutParams associated with this view. All views should have
8224 * layout parameters. These supply parameters to the <i>parent</i> of this
8225 * view specifying how it should be arranged. There are many subclasses of
8226 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8227 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008228 *
8229 * This method may return null if this View is not attached to a parent
8230 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8231 * was not invoked successfully. When a View is attached to a parent
8232 * ViewGroup, this method must not return null.
8233 *
8234 * @return The LayoutParams associated with this view, or null if no
8235 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008236 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008237 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 public ViewGroup.LayoutParams getLayoutParams() {
8239 return mLayoutParams;
8240 }
8241
8242 /**
8243 * Set the layout parameters associated with this view. These supply
8244 * parameters to the <i>parent</i> of this view specifying how it should be
8245 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8246 * correspond to the different subclasses of ViewGroup that are responsible
8247 * for arranging their children.
8248 *
Romain Guy01c174b2011-02-22 11:51:06 -08008249 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008250 */
8251 public void setLayoutParams(ViewGroup.LayoutParams params) {
8252 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008253 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 }
8255 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008256 if (mParent instanceof ViewGroup) {
8257 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 requestLayout();
8260 }
8261
8262 /**
8263 * Set the scrolled position of your view. This will cause a call to
8264 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8265 * invalidated.
8266 * @param x the x position to scroll to
8267 * @param y the y position to scroll to
8268 */
8269 public void scrollTo(int x, int y) {
8270 if (mScrollX != x || mScrollY != y) {
8271 int oldX = mScrollX;
8272 int oldY = mScrollY;
8273 mScrollX = x;
8274 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008275 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008276 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008277 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008278 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 }
8281 }
8282
8283 /**
8284 * Move the scrolled position of your view. This will cause a call to
8285 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8286 * invalidated.
8287 * @param x the amount of pixels to scroll by horizontally
8288 * @param y the amount of pixels to scroll by vertically
8289 */
8290 public void scrollBy(int x, int y) {
8291 scrollTo(mScrollX + x, mScrollY + y);
8292 }
8293
8294 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008295 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8296 * animation to fade the scrollbars out after a default delay. If a subclass
8297 * provides animated scrolling, the start delay should equal the duration
8298 * of the scrolling animation.</p>
8299 *
8300 * <p>The animation starts only if at least one of the scrollbars is
8301 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8302 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8303 * this method returns true, and false otherwise. If the animation is
8304 * started, this method calls {@link #invalidate()}; in that case the
8305 * caller should not call {@link #invalidate()}.</p>
8306 *
8307 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008308 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008309 *
8310 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8311 * and {@link #scrollTo(int, int)}.</p>
8312 *
8313 * @return true if the animation is played, false otherwise
8314 *
8315 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008316 * @see #scrollBy(int, int)
8317 * @see #scrollTo(int, int)
8318 * @see #isHorizontalScrollBarEnabled()
8319 * @see #isVerticalScrollBarEnabled()
8320 * @see #setHorizontalScrollBarEnabled(boolean)
8321 * @see #setVerticalScrollBarEnabled(boolean)
8322 */
8323 protected boolean awakenScrollBars() {
8324 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008325 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008326 }
8327
8328 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008329 * Trigger the scrollbars to draw.
8330 * This method differs from awakenScrollBars() only in its default duration.
8331 * initialAwakenScrollBars() will show the scroll bars for longer than
8332 * usual to give the user more of a chance to notice them.
8333 *
8334 * @return true if the animation is played, false otherwise.
8335 */
8336 private boolean initialAwakenScrollBars() {
8337 return mScrollCache != null &&
8338 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8339 }
8340
8341 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008342 * <p>
8343 * Trigger the scrollbars to draw. When invoked this method starts an
8344 * animation to fade the scrollbars out after a fixed delay. If a subclass
8345 * provides animated scrolling, the start delay should equal the duration of
8346 * the scrolling animation.
8347 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008348 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008349 * <p>
8350 * The animation starts only if at least one of the scrollbars is enabled,
8351 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8352 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8353 * this method returns true, and false otherwise. If the animation is
8354 * started, this method calls {@link #invalidate()}; in that case the caller
8355 * should not call {@link #invalidate()}.
8356 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008357 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008358 * <p>
8359 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008360 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008361 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008362 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008363 * @param startDelay the delay, in milliseconds, after which the animation
8364 * should start; when the delay is 0, the animation starts
8365 * immediately
8366 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008367 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008368 * @see #scrollBy(int, int)
8369 * @see #scrollTo(int, int)
8370 * @see #isHorizontalScrollBarEnabled()
8371 * @see #isVerticalScrollBarEnabled()
8372 * @see #setHorizontalScrollBarEnabled(boolean)
8373 * @see #setVerticalScrollBarEnabled(boolean)
8374 */
8375 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008376 return awakenScrollBars(startDelay, true);
8377 }
Joe Malin32736f02011-01-19 16:14:20 -08008378
Mike Cleron290947b2009-09-29 18:34:32 -07008379 /**
8380 * <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 Cleron290947b2009-09-29 18:34:32 -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
Joe Malin32736f02011-01-19 16:14:20 -08008392 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008393 * is set to true; in that case the caller
8394 * should not call {@link #invalidate()}.
8395 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008396 *
Mike Cleron290947b2009-09-29 18:34:32 -07008397 * <p>
8398 * This method should be invoked everytime a subclass directly updates the
8399 * scroll parameters.
8400 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008401 *
Mike Cleron290947b2009-09-29 18:34:32 -07008402 * @param startDelay the delay, in milliseconds, after which the animation
8403 * should start; when the delay is 0, the animation starts
8404 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008405 *
Mike Cleron290947b2009-09-29 18:34:32 -07008406 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008407 *
Mike Cleron290947b2009-09-29 18:34:32 -07008408 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008409 *
Mike Cleron290947b2009-09-29 18:34:32 -07008410 * @see #scrollBy(int, int)
8411 * @see #scrollTo(int, int)
8412 * @see #isHorizontalScrollBarEnabled()
8413 * @see #isVerticalScrollBarEnabled()
8414 * @see #setHorizontalScrollBarEnabled(boolean)
8415 * @see #setVerticalScrollBarEnabled(boolean)
8416 */
8417 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008418 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008419
Mike Cleronf116bf82009-09-27 19:14:12 -07008420 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8421 return false;
8422 }
8423
8424 if (scrollCache.scrollBar == null) {
8425 scrollCache.scrollBar = new ScrollBarDrawable();
8426 }
8427
8428 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8429
Mike Cleron290947b2009-09-29 18:34:32 -07008430 if (invalidate) {
8431 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008432 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008433 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008434
8435 if (scrollCache.state == ScrollabilityCache.OFF) {
8436 // FIXME: this is copied from WindowManagerService.
8437 // We should get this value from the system when it
8438 // is possible to do so.
8439 final int KEY_REPEAT_FIRST_DELAY = 750;
8440 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8441 }
8442
8443 // Tell mScrollCache when we should start fading. This may
8444 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008445 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008446 scrollCache.fadeStartTime = fadeStartTime;
8447 scrollCache.state = ScrollabilityCache.ON;
8448
8449 // Schedule our fader to run, unscheduling any old ones first
8450 if (mAttachInfo != null) {
8451 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8452 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8453 }
8454
8455 return true;
8456 }
8457
8458 return false;
8459 }
8460
8461 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008462 * Do not invalidate views which are not visible and which are not running an animation. They
8463 * will not get drawn and they should not set dirty flags as if they will be drawn
8464 */
8465 private boolean skipInvalidate() {
8466 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8467 (!(mParent instanceof ViewGroup) ||
8468 !((ViewGroup) mParent).isViewTransitioning(this));
8469 }
8470 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008471 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008472 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8473 * in the future. This must be called from a UI thread. To call from a non-UI
8474 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008475 *
8476 * WARNING: This method is destructive to dirty.
8477 * @param dirty the rectangle representing the bounds of the dirty region
8478 */
8479 public void invalidate(Rect dirty) {
8480 if (ViewDebug.TRACE_HIERARCHY) {
8481 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8482 }
8483
Chet Haaseaceafe62011-08-26 15:44:33 -07008484 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008485 return;
8486 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008487 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008488 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8489 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008491 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008492 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008493 final ViewParent p = mParent;
8494 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008495 //noinspection PointlessBooleanExpression,ConstantConditions
8496 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8497 if (p != null && ai != null && ai.mHardwareAccelerated) {
8498 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008499 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008500 p.invalidateChild(this, null);
8501 return;
8502 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008504 if (p != null && ai != null) {
8505 final int scrollX = mScrollX;
8506 final int scrollY = mScrollY;
8507 final Rect r = ai.mTmpInvalRect;
8508 r.set(dirty.left - scrollX, dirty.top - scrollY,
8509 dirty.right - scrollX, dirty.bottom - scrollY);
8510 mParent.invalidateChild(this, r);
8511 }
8512 }
8513 }
8514
8515 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008516 * 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 -08008517 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008518 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8519 * will be called at some point in the future. This must be called from
8520 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 * @param l the left position of the dirty region
8522 * @param t the top position of the dirty region
8523 * @param r the right position of the dirty region
8524 * @param b the bottom position of the dirty region
8525 */
8526 public void invalidate(int l, int t, int r, int b) {
8527 if (ViewDebug.TRACE_HIERARCHY) {
8528 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8529 }
8530
Chet Haaseaceafe62011-08-26 15:44:33 -07008531 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008532 return;
8533 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008534 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008535 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8536 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008537 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008538 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008539 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 final ViewParent p = mParent;
8541 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008542 //noinspection PointlessBooleanExpression,ConstantConditions
8543 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8544 if (p != null && ai != null && ai.mHardwareAccelerated) {
8545 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008546 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008547 p.invalidateChild(this, null);
8548 return;
8549 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 if (p != null && ai != null && l < r && t < b) {
8552 final int scrollX = mScrollX;
8553 final int scrollY = mScrollY;
8554 final Rect tmpr = ai.mTmpInvalRect;
8555 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8556 p.invalidateChild(this, tmpr);
8557 }
8558 }
8559 }
8560
8561 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008562 * Invalidate the whole view. If the view is visible,
8563 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8564 * the future. This must be called from a UI thread. To call from a non-UI thread,
8565 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 */
8567 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008568 invalidate(true);
8569 }
Joe Malin32736f02011-01-19 16:14:20 -08008570
Chet Haaseed032702010-10-01 14:05:54 -07008571 /**
8572 * This is where the invalidate() work actually happens. A full invalidate()
8573 * causes the drawing cache to be invalidated, but this function can be called with
8574 * invalidateCache set to false to skip that invalidation step for cases that do not
8575 * need it (for example, a component that remains at the same dimensions with the same
8576 * content).
8577 *
8578 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8579 * well. This is usually true for a full invalidate, but may be set to false if the
8580 * View's contents or dimensions have not changed.
8581 */
Romain Guy849d0a32011-02-01 17:20:48 -08008582 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008583 if (ViewDebug.TRACE_HIERARCHY) {
8584 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8585 }
8586
Chet Haaseaceafe62011-08-26 15:44:33 -07008587 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008588 return;
8589 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008590 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008591 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008592 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8593 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008594 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008595 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008596 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008597 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008598 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008600 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008601 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008602 //noinspection PointlessBooleanExpression,ConstantConditions
8603 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8604 if (p != null && ai != null && ai.mHardwareAccelerated) {
8605 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008606 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008607 p.invalidateChild(this, null);
8608 return;
8609 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008610 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008612 if (p != null && ai != null) {
8613 final Rect r = ai.mTmpInvalRect;
8614 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8615 // Don't call invalidate -- we don't want to internally scroll
8616 // our own bounds
8617 p.invalidateChild(this, r);
8618 }
8619 }
8620 }
8621
8622 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008623 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008624 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8625 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008626 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8627 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008628 *
8629 * @hide
8630 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008631 protected void invalidateParentCaches() {
8632 if (mParent instanceof View) {
8633 ((View) mParent).mPrivateFlags |= INVALIDATED;
8634 }
8635 }
Joe Malin32736f02011-01-19 16:14:20 -08008636
Romain Guy0fd89bf2011-01-26 15:41:30 -08008637 /**
8638 * Used to indicate that the parent of this view should be invalidated. This functionality
8639 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8640 * which is necessary when various parent-managed properties of the view change, such as
8641 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8642 * an invalidation event to the parent.
8643 *
8644 * @hide
8645 */
8646 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008647 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008648 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008649 }
8650 }
8651
8652 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008653 * Indicates whether this View is opaque. An opaque View guarantees that it will
8654 * draw all the pixels overlapping its bounds using a fully opaque color.
8655 *
8656 * Subclasses of View should override this method whenever possible to indicate
8657 * whether an instance is opaque. Opaque Views are treated in a special way by
8658 * the View hierarchy, possibly allowing it to perform optimizations during
8659 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008660 *
Romain Guy24443ea2009-05-11 11:56:30 -07008661 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008662 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008663 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008664 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008665 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008666 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8667 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008668 }
8669
Adam Powell20232d02010-12-08 21:08:53 -08008670 /**
8671 * @hide
8672 */
8673 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008674 // Opaque if:
8675 // - Has a background
8676 // - Background is opaque
8677 // - Doesn't have scrollbars or scrollbars are inside overlay
8678
8679 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8680 mPrivateFlags |= OPAQUE_BACKGROUND;
8681 } else {
8682 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8683 }
8684
8685 final int flags = mViewFlags;
8686 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8687 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8688 mPrivateFlags |= OPAQUE_SCROLLBARS;
8689 } else {
8690 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8691 }
8692 }
8693
8694 /**
8695 * @hide
8696 */
8697 protected boolean hasOpaqueScrollbars() {
8698 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008699 }
8700
8701 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 * @return A handler associated with the thread running the View. This
8703 * handler can be used to pump events in the UI events queue.
8704 */
8705 public Handler getHandler() {
8706 if (mAttachInfo != null) {
8707 return mAttachInfo.mHandler;
8708 }
8709 return null;
8710 }
8711
8712 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008713 * Gets the view root associated with the View.
8714 * @return The view root, or null if none.
8715 * @hide
8716 */
8717 public ViewRootImpl getViewRootImpl() {
8718 if (mAttachInfo != null) {
8719 return mAttachInfo.mViewRootImpl;
8720 }
8721 return null;
8722 }
8723
8724 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008725 * <p>Causes the Runnable to be added to the message queue.
8726 * The runnable will be run on the user interface thread.</p>
8727 *
8728 * <p>This method can be invoked from outside of the UI thread
8729 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 *
8731 * @param action The Runnable that will be executed.
8732 *
8733 * @return Returns true if the Runnable was successfully placed in to the
8734 * message queue. Returns false on failure, usually because the
8735 * looper processing the message queue is exiting.
8736 */
8737 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008738 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008739 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008740 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008742 // Assume that post will succeed later
8743 ViewRootImpl.getRunQueue().post(action);
8744 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008745 }
8746
8747 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008748 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008750 * The runnable will be run on the user interface thread.</p>
8751 *
8752 * <p>This method can be invoked from outside of the UI thread
8753 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008754 *
8755 * @param action The Runnable that will be executed.
8756 * @param delayMillis The delay (in milliseconds) until the Runnable
8757 * will be executed.
8758 *
8759 * @return true if the Runnable was successfully placed in to the
8760 * message queue. Returns false on failure, usually because the
8761 * looper processing the message queue is exiting. Note that a
8762 * result of true does not mean the Runnable will be processed --
8763 * if the looper is quit before the delivery time of the message
8764 * occurs then the message will be dropped.
8765 */
8766 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008767 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008768 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008769 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008770 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008771 // Assume that post will succeed later
8772 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8773 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774 }
8775
8776 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008777 * <p>Removes the specified Runnable from the message queue.</p>
8778 *
8779 * <p>This method can be invoked from outside of the UI thread
8780 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008781 *
8782 * @param action The Runnable to remove from the message handling queue
8783 *
8784 * @return true if this view could ask the Handler to remove the Runnable,
8785 * false otherwise. When the returned value is true, the Runnable
8786 * may or may not have been actually removed from the message queue
8787 * (for instance, if the Runnable was not in the queue already.)
8788 */
8789 public boolean removeCallbacks(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008790 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008791 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008792 attachInfo.mHandler.removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 } else {
8794 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008795 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 return true;
8798 }
8799
8800 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008801 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8802 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803 *
Romain Guye63a4f32011-08-11 11:33:31 -07008804 * <p>This method can be invoked from outside of the UI thread
8805 * only when this View is attached to a window.</p>
8806 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 * @see #invalidate()
8808 */
8809 public void postInvalidate() {
8810 postInvalidateDelayed(0);
8811 }
8812
8813 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008814 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8815 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8816 *
8817 * <p>This method can be invoked from outside of the UI thread
8818 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008819 *
8820 * @param left The left coordinate of the rectangle to invalidate.
8821 * @param top The top coordinate of the rectangle to invalidate.
8822 * @param right The right coordinate of the rectangle to invalidate.
8823 * @param bottom The bottom coordinate of the rectangle to invalidate.
8824 *
8825 * @see #invalidate(int, int, int, int)
8826 * @see #invalidate(Rect)
8827 */
8828 public void postInvalidate(int left, int top, int right, int bottom) {
8829 postInvalidateDelayed(0, left, top, right, bottom);
8830 }
8831
8832 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008833 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8834 * loop. Waits for the specified amount of time.</p>
8835 *
8836 * <p>This method can be invoked from outside of the UI thread
8837 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008838 *
8839 * @param delayMilliseconds the duration in milliseconds to delay the
8840 * invalidation by
8841 */
8842 public void postInvalidateDelayed(long delayMilliseconds) {
8843 // We try only with the AttachInfo because there's no point in invalidating
8844 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008845 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008846 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008847 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008848 }
8849 }
8850
8851 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008852 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8853 * through the event loop. Waits for the specified amount of time.</p>
8854 *
8855 * <p>This method can be invoked from outside of the UI thread
8856 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008857 *
8858 * @param delayMilliseconds the duration in milliseconds to delay the
8859 * invalidation by
8860 * @param left The left coordinate of the rectangle to invalidate.
8861 * @param top The top coordinate of the rectangle to invalidate.
8862 * @param right The right coordinate of the rectangle to invalidate.
8863 * @param bottom The bottom coordinate of the rectangle to invalidate.
8864 */
8865 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8866 int right, int bottom) {
8867
8868 // We try only with the AttachInfo because there's no point in invalidating
8869 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008870 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008871 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8873 info.target = this;
8874 info.left = left;
8875 info.top = top;
8876 info.right = right;
8877 info.bottom = bottom;
8878
Jeff Browna175a5b2012-02-15 19:18:31 -08008879 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008880 }
8881 }
8882
8883 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08008884 * <p>Cause an invalidate to happen on the next animation time step, typically the
8885 * next display frame.</p>
8886 *
8887 * <p>This method can be invoked from outside of the UI thread
8888 * only when this View is attached to a window.</p>
8889 *
8890 * @hide
8891 */
8892 public void postInvalidateOnAnimation() {
8893 // We try only with the AttachInfo because there's no point in invalidating
8894 // if we are not attached to our window
8895 final AttachInfo attachInfo = mAttachInfo;
8896 if (attachInfo != null) {
8897 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
8898 }
8899 }
8900
8901 /**
8902 * <p>Cause an invalidate of the specified area to happen on the next animation
8903 * time step, typically the next display frame.</p>
8904 *
8905 * <p>This method can be invoked from outside of the UI thread
8906 * only when this View is attached to a window.</p>
8907 *
8908 * @param left The left coordinate of the rectangle to invalidate.
8909 * @param top The top coordinate of the rectangle to invalidate.
8910 * @param right The right coordinate of the rectangle to invalidate.
8911 * @param bottom The bottom coordinate of the rectangle to invalidate.
8912 *
8913 * @hide
8914 */
8915 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
8916 // We try only with the AttachInfo because there's no point in invalidating
8917 // if we are not attached to our window
8918 final AttachInfo attachInfo = mAttachInfo;
8919 if (attachInfo != null) {
8920 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8921 info.target = this;
8922 info.left = left;
8923 info.top = top;
8924 info.right = right;
8925 info.bottom = bottom;
8926
8927 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
8928 }
8929 }
8930
8931 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008932 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8933 * This event is sent at most once every
8934 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8935 */
8936 private void postSendViewScrolledAccessibilityEventCallback() {
8937 if (mSendViewScrolledAccessibilityEvent == null) {
8938 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8939 }
8940 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8941 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8942 postDelayed(mSendViewScrolledAccessibilityEvent,
8943 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8944 }
8945 }
8946
8947 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 * Called by a parent to request that a child update its values for mScrollX
8949 * and mScrollY if necessary. This will typically be done if the child is
8950 * animating a scroll using a {@link android.widget.Scroller Scroller}
8951 * object.
8952 */
8953 public void computeScroll() {
8954 }
8955
8956 /**
8957 * <p>Indicate whether the horizontal edges are faded when the view is
8958 * scrolled horizontally.</p>
8959 *
8960 * @return true if the horizontal edges should are faded on scroll, false
8961 * otherwise
8962 *
8963 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008964 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008965 */
8966 public boolean isHorizontalFadingEdgeEnabled() {
8967 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8968 }
8969
8970 /**
8971 * <p>Define whether the horizontal edges should be faded when this view
8972 * is scrolled horizontally.</p>
8973 *
8974 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8975 * be faded when the view is scrolled
8976 * horizontally
8977 *
8978 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008979 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008980 */
8981 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8982 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8983 if (horizontalFadingEdgeEnabled) {
8984 initScrollCache();
8985 }
8986
8987 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8988 }
8989 }
8990
8991 /**
8992 * <p>Indicate whether the vertical edges are faded when the view is
8993 * scrolled horizontally.</p>
8994 *
8995 * @return true if the vertical edges should are faded on scroll, false
8996 * otherwise
8997 *
8998 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008999 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009000 */
9001 public boolean isVerticalFadingEdgeEnabled() {
9002 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9003 }
9004
9005 /**
9006 * <p>Define whether the vertical edges should be faded when this view
9007 * is scrolled vertically.</p>
9008 *
9009 * @param verticalFadingEdgeEnabled true if the vertical edges should
9010 * be faded when the view is scrolled
9011 * vertically
9012 *
9013 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009014 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009015 */
9016 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9017 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9018 if (verticalFadingEdgeEnabled) {
9019 initScrollCache();
9020 }
9021
9022 mViewFlags ^= FADING_EDGE_VERTICAL;
9023 }
9024 }
9025
9026 /**
9027 * Returns the strength, or intensity, of the top faded edge. The strength is
9028 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9029 * returns 0.0 or 1.0 but no value in between.
9030 *
9031 * Subclasses should override this method to provide a smoother fade transition
9032 * when scrolling occurs.
9033 *
9034 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9035 */
9036 protected float getTopFadingEdgeStrength() {
9037 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9038 }
9039
9040 /**
9041 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9042 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9043 * returns 0.0 or 1.0 but no value in between.
9044 *
9045 * Subclasses should override this method to provide a smoother fade transition
9046 * when scrolling occurs.
9047 *
9048 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9049 */
9050 protected float getBottomFadingEdgeStrength() {
9051 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9052 computeVerticalScrollRange() ? 1.0f : 0.0f;
9053 }
9054
9055 /**
9056 * Returns the strength, or intensity, of the left faded edge. The strength is
9057 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9058 * returns 0.0 or 1.0 but no value in between.
9059 *
9060 * Subclasses should override this method to provide a smoother fade transition
9061 * when scrolling occurs.
9062 *
9063 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9064 */
9065 protected float getLeftFadingEdgeStrength() {
9066 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9067 }
9068
9069 /**
9070 * Returns the strength, or intensity, of the right faded edge. The strength is
9071 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9072 * returns 0.0 or 1.0 but no value in between.
9073 *
9074 * Subclasses should override this method to provide a smoother fade transition
9075 * when scrolling occurs.
9076 *
9077 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9078 */
9079 protected float getRightFadingEdgeStrength() {
9080 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9081 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9082 }
9083
9084 /**
9085 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9086 * scrollbar is not drawn by default.</p>
9087 *
9088 * @return true if the horizontal scrollbar should be painted, false
9089 * otherwise
9090 *
9091 * @see #setHorizontalScrollBarEnabled(boolean)
9092 */
9093 public boolean isHorizontalScrollBarEnabled() {
9094 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9095 }
9096
9097 /**
9098 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9099 * scrollbar is not drawn by default.</p>
9100 *
9101 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9102 * be painted
9103 *
9104 * @see #isHorizontalScrollBarEnabled()
9105 */
9106 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9107 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9108 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009109 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009110 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009111 }
9112 }
9113
9114 /**
9115 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9116 * scrollbar is not drawn by default.</p>
9117 *
9118 * @return true if the vertical scrollbar should be painted, false
9119 * otherwise
9120 *
9121 * @see #setVerticalScrollBarEnabled(boolean)
9122 */
9123 public boolean isVerticalScrollBarEnabled() {
9124 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9125 }
9126
9127 /**
9128 * <p>Define whether the vertical scrollbar should be drawn or not. The
9129 * scrollbar is not drawn by default.</p>
9130 *
9131 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9132 * be painted
9133 *
9134 * @see #isVerticalScrollBarEnabled()
9135 */
9136 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9137 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9138 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009139 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009140 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009141 }
9142 }
9143
Adam Powell20232d02010-12-08 21:08:53 -08009144 /**
9145 * @hide
9146 */
9147 protected void recomputePadding() {
9148 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 }
Joe Malin32736f02011-01-19 16:14:20 -08009150
Mike Cleronfe81d382009-09-28 14:22:16 -07009151 /**
9152 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009153 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009154 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009155 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009156 */
9157 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9158 initScrollCache();
9159 final ScrollabilityCache scrollabilityCache = mScrollCache;
9160 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009161 if (fadeScrollbars) {
9162 scrollabilityCache.state = ScrollabilityCache.OFF;
9163 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009164 scrollabilityCache.state = ScrollabilityCache.ON;
9165 }
9166 }
Joe Malin32736f02011-01-19 16:14:20 -08009167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009168 /**
Joe Malin32736f02011-01-19 16:14:20 -08009169 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009170 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009171 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009172 * @return true if scrollbar fading is enabled
9173 */
9174 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009175 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009176 }
Joe Malin32736f02011-01-19 16:14:20 -08009177
Mike Cleron52f0a642009-09-28 18:21:37 -07009178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009179 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9180 * inset. When inset, they add to the padding of the view. And the scrollbars
9181 * can be drawn inside the padding area or on the edge of the view. For example,
9182 * if a view has a background drawable and you want to draw the scrollbars
9183 * inside the padding specified by the drawable, you can use
9184 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9185 * appear at the edge of the view, ignoring the padding, then you can use
9186 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9187 * @param style the style of the scrollbars. Should be one of
9188 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9189 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9190 * @see #SCROLLBARS_INSIDE_OVERLAY
9191 * @see #SCROLLBARS_INSIDE_INSET
9192 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9193 * @see #SCROLLBARS_OUTSIDE_INSET
9194 */
9195 public void setScrollBarStyle(int style) {
9196 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9197 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009198 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009199 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009200 }
9201 }
9202
9203 /**
9204 * <p>Returns the current scrollbar style.</p>
9205 * @return the current scrollbar style
9206 * @see #SCROLLBARS_INSIDE_OVERLAY
9207 * @see #SCROLLBARS_INSIDE_INSET
9208 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9209 * @see #SCROLLBARS_OUTSIDE_INSET
9210 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009211 @ViewDebug.ExportedProperty(mapping = {
9212 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9213 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9214 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9215 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9216 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009217 public int getScrollBarStyle() {
9218 return mViewFlags & SCROLLBARS_STYLE_MASK;
9219 }
9220
9221 /**
9222 * <p>Compute the horizontal range that the horizontal scrollbar
9223 * represents.</p>
9224 *
9225 * <p>The range is expressed in arbitrary units that must be the same as the
9226 * units used by {@link #computeHorizontalScrollExtent()} and
9227 * {@link #computeHorizontalScrollOffset()}.</p>
9228 *
9229 * <p>The default range is the drawing width of this view.</p>
9230 *
9231 * @return the total horizontal range represented by the horizontal
9232 * scrollbar
9233 *
9234 * @see #computeHorizontalScrollExtent()
9235 * @see #computeHorizontalScrollOffset()
9236 * @see android.widget.ScrollBarDrawable
9237 */
9238 protected int computeHorizontalScrollRange() {
9239 return getWidth();
9240 }
9241
9242 /**
9243 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9244 * within the horizontal range. This value is used to compute the position
9245 * of the thumb within the scrollbar's track.</p>
9246 *
9247 * <p>The range is expressed in arbitrary units that must be the same as the
9248 * units used by {@link #computeHorizontalScrollRange()} and
9249 * {@link #computeHorizontalScrollExtent()}.</p>
9250 *
9251 * <p>The default offset is the scroll offset of this view.</p>
9252 *
9253 * @return the horizontal offset of the scrollbar's thumb
9254 *
9255 * @see #computeHorizontalScrollRange()
9256 * @see #computeHorizontalScrollExtent()
9257 * @see android.widget.ScrollBarDrawable
9258 */
9259 protected int computeHorizontalScrollOffset() {
9260 return mScrollX;
9261 }
9262
9263 /**
9264 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9265 * within the horizontal range. This value is used to compute the length
9266 * of the thumb within the scrollbar's track.</p>
9267 *
9268 * <p>The range is expressed in arbitrary units that must be the same as the
9269 * units used by {@link #computeHorizontalScrollRange()} and
9270 * {@link #computeHorizontalScrollOffset()}.</p>
9271 *
9272 * <p>The default extent is the drawing width of this view.</p>
9273 *
9274 * @return the horizontal extent of the scrollbar's thumb
9275 *
9276 * @see #computeHorizontalScrollRange()
9277 * @see #computeHorizontalScrollOffset()
9278 * @see android.widget.ScrollBarDrawable
9279 */
9280 protected int computeHorizontalScrollExtent() {
9281 return getWidth();
9282 }
9283
9284 /**
9285 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9286 *
9287 * <p>The range is expressed in arbitrary units that must be the same as the
9288 * units used by {@link #computeVerticalScrollExtent()} and
9289 * {@link #computeVerticalScrollOffset()}.</p>
9290 *
9291 * @return the total vertical range represented by the vertical scrollbar
9292 *
9293 * <p>The default range is the drawing height of this view.</p>
9294 *
9295 * @see #computeVerticalScrollExtent()
9296 * @see #computeVerticalScrollOffset()
9297 * @see android.widget.ScrollBarDrawable
9298 */
9299 protected int computeVerticalScrollRange() {
9300 return getHeight();
9301 }
9302
9303 /**
9304 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9305 * within the horizontal range. This value is used to compute the position
9306 * of the thumb within the scrollbar's track.</p>
9307 *
9308 * <p>The range is expressed in arbitrary units that must be the same as the
9309 * units used by {@link #computeVerticalScrollRange()} and
9310 * {@link #computeVerticalScrollExtent()}.</p>
9311 *
9312 * <p>The default offset is the scroll offset of this view.</p>
9313 *
9314 * @return the vertical offset of the scrollbar's thumb
9315 *
9316 * @see #computeVerticalScrollRange()
9317 * @see #computeVerticalScrollExtent()
9318 * @see android.widget.ScrollBarDrawable
9319 */
9320 protected int computeVerticalScrollOffset() {
9321 return mScrollY;
9322 }
9323
9324 /**
9325 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9326 * within the vertical range. This value is used to compute the length
9327 * of the thumb within the scrollbar's track.</p>
9328 *
9329 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009330 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 * {@link #computeVerticalScrollOffset()}.</p>
9332 *
9333 * <p>The default extent is the drawing height of this view.</p>
9334 *
9335 * @return the vertical extent of the scrollbar's thumb
9336 *
9337 * @see #computeVerticalScrollRange()
9338 * @see #computeVerticalScrollOffset()
9339 * @see android.widget.ScrollBarDrawable
9340 */
9341 protected int computeVerticalScrollExtent() {
9342 return getHeight();
9343 }
9344
9345 /**
Adam Powell69159442011-06-13 17:53:06 -07009346 * Check if this view can be scrolled horizontally in a certain direction.
9347 *
9348 * @param direction Negative to check scrolling left, positive to check scrolling right.
9349 * @return true if this view can be scrolled in the specified direction, false otherwise.
9350 */
9351 public boolean canScrollHorizontally(int direction) {
9352 final int offset = computeHorizontalScrollOffset();
9353 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9354 if (range == 0) return false;
9355 if (direction < 0) {
9356 return offset > 0;
9357 } else {
9358 return offset < range - 1;
9359 }
9360 }
9361
9362 /**
9363 * Check if this view can be scrolled vertically in a certain direction.
9364 *
9365 * @param direction Negative to check scrolling up, positive to check scrolling down.
9366 * @return true if this view can be scrolled in the specified direction, false otherwise.
9367 */
9368 public boolean canScrollVertically(int direction) {
9369 final int offset = computeVerticalScrollOffset();
9370 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9371 if (range == 0) return false;
9372 if (direction < 0) {
9373 return offset > 0;
9374 } else {
9375 return offset < range - 1;
9376 }
9377 }
9378
9379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9381 * scrollbars are painted only if they have been awakened first.</p>
9382 *
9383 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009384 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009385 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009386 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009387 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009388 // scrollbars are drawn only when the animation is running
9389 final ScrollabilityCache cache = mScrollCache;
9390 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009391
Mike Cleronf116bf82009-09-27 19:14:12 -07009392 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009393
Mike Cleronf116bf82009-09-27 19:14:12 -07009394 if (state == ScrollabilityCache.OFF) {
9395 return;
9396 }
Joe Malin32736f02011-01-19 16:14:20 -08009397
Mike Cleronf116bf82009-09-27 19:14:12 -07009398 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009399
Mike Cleronf116bf82009-09-27 19:14:12 -07009400 if (state == ScrollabilityCache.FADING) {
9401 // We're fading -- get our fade interpolation
9402 if (cache.interpolatorValues == null) {
9403 cache.interpolatorValues = new float[1];
9404 }
Joe Malin32736f02011-01-19 16:14:20 -08009405
Mike Cleronf116bf82009-09-27 19:14:12 -07009406 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009407
Mike Cleronf116bf82009-09-27 19:14:12 -07009408 // Stops the animation if we're done
9409 if (cache.scrollBarInterpolator.timeToValues(values) ==
9410 Interpolator.Result.FREEZE_END) {
9411 cache.state = ScrollabilityCache.OFF;
9412 } else {
9413 cache.scrollBar.setAlpha(Math.round(values[0]));
9414 }
Joe Malin32736f02011-01-19 16:14:20 -08009415
9416 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009417 // drawing. We only want this when we're fading so that
9418 // we prevent excessive redraws
9419 invalidate = true;
9420 } else {
9421 // We're just on -- but we may have been fading before so
9422 // reset alpha
9423 cache.scrollBar.setAlpha(255);
9424 }
9425
Joe Malin32736f02011-01-19 16:14:20 -08009426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 final int viewFlags = mViewFlags;
9428
9429 final boolean drawHorizontalScrollBar =
9430 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9431 final boolean drawVerticalScrollBar =
9432 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9433 && !isVerticalScrollBarHidden();
9434
9435 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9436 final int width = mRight - mLeft;
9437 final int height = mBottom - mTop;
9438
9439 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440
Mike Reede8853fc2009-09-04 14:01:48 -04009441 final int scrollX = mScrollX;
9442 final int scrollY = mScrollY;
9443 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9444
Mike Cleronf116bf82009-09-27 19:14:12 -07009445 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009447 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009448 int size = scrollBar.getSize(false);
9449 if (size <= 0) {
9450 size = cache.scrollBarSize;
9451 }
9452
Mike Cleronf116bf82009-09-27 19:14:12 -07009453 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009454 computeHorizontalScrollOffset(),
9455 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009456 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009457 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009458 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009459 left = scrollX + (mPaddingLeft & inside);
9460 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9461 bottom = top + size;
9462 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9463 if (invalidate) {
9464 invalidate(left, top, right, bottom);
9465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009466 }
9467
9468 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009469 int size = scrollBar.getSize(true);
9470 if (size <= 0) {
9471 size = cache.scrollBarSize;
9472 }
9473
Mike Reede8853fc2009-09-04 14:01:48 -04009474 scrollBar.setParameters(computeVerticalScrollRange(),
9475 computeVerticalScrollOffset(),
9476 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009477 switch (mVerticalScrollbarPosition) {
9478 default:
9479 case SCROLLBAR_POSITION_DEFAULT:
9480 case SCROLLBAR_POSITION_RIGHT:
9481 left = scrollX + width - size - (mUserPaddingRight & inside);
9482 break;
9483 case SCROLLBAR_POSITION_LEFT:
9484 left = scrollX + (mUserPaddingLeft & inside);
9485 break;
9486 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009487 top = scrollY + (mPaddingTop & inside);
9488 right = left + size;
9489 bottom = scrollY + height - (mUserPaddingBottom & inside);
9490 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9491 if (invalidate) {
9492 invalidate(left, top, right, bottom);
9493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009494 }
9495 }
9496 }
9497 }
Romain Guy8506ab42009-06-11 17:35:47 -07009498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009500 * 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 -08009501 * FastScroller is visible.
9502 * @return whether to temporarily hide the vertical scrollbar
9503 * @hide
9504 */
9505 protected boolean isVerticalScrollBarHidden() {
9506 return false;
9507 }
9508
9509 /**
9510 * <p>Draw the horizontal scrollbar if
9511 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009513 * @param canvas the canvas on which to draw the scrollbar
9514 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009515 *
9516 * @see #isHorizontalScrollBarEnabled()
9517 * @see #computeHorizontalScrollRange()
9518 * @see #computeHorizontalScrollExtent()
9519 * @see #computeHorizontalScrollOffset()
9520 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009521 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009522 */
Romain Guy8fb95422010-08-17 18:38:51 -07009523 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9524 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009525 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009526 scrollBar.draw(canvas);
9527 }
Mike Reede8853fc2009-09-04 14:01:48 -04009528
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009530 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9531 * returns true.</p>
9532 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 * @param canvas the canvas on which to draw the scrollbar
9534 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 *
9536 * @see #isVerticalScrollBarEnabled()
9537 * @see #computeVerticalScrollRange()
9538 * @see #computeVerticalScrollExtent()
9539 * @see #computeVerticalScrollOffset()
9540 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009541 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009542 */
Romain Guy8fb95422010-08-17 18:38:51 -07009543 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9544 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009545 scrollBar.setBounds(l, t, r, b);
9546 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009547 }
9548
9549 /**
9550 * Implement this to do your drawing.
9551 *
9552 * @param canvas the canvas on which the background will be drawn
9553 */
9554 protected void onDraw(Canvas canvas) {
9555 }
9556
9557 /*
9558 * Caller is responsible for calling requestLayout if necessary.
9559 * (This allows addViewInLayout to not request a new layout.)
9560 */
9561 void assignParent(ViewParent parent) {
9562 if (mParent == null) {
9563 mParent = parent;
9564 } else if (parent == null) {
9565 mParent = null;
9566 } else {
9567 throw new RuntimeException("view " + this + " being added, but"
9568 + " it already has a parent");
9569 }
9570 }
9571
9572 /**
9573 * This is called when the view is attached to a window. At this point it
9574 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009575 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9576 * however it may be called any time before the first onDraw -- including
9577 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009578 *
9579 * @see #onDetachedFromWindow()
9580 */
9581 protected void onAttachedToWindow() {
9582 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9583 mParent.requestTransparentRegion(this);
9584 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009585 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9586 initialAwakenScrollBars();
9587 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9588 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009589 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009590 // Order is important here: LayoutDirection MUST be resolved before Padding
9591 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009592 resolveLayoutDirectionIfNeeded();
9593 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009594 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009595 if (isFocused()) {
9596 InputMethodManager imm = InputMethodManager.peekInstance();
9597 imm.focusIn(this);
9598 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009599 }
Cibu Johny86666632010-02-22 13:01:02 -08009600
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009601 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009602 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9603 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009604 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009605 private void resolveLayoutDirectionIfNeeded() {
9606 // Do not resolve if it is not needed
9607 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9608
9609 // Clear any previous layout direction resolution
9610 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9611
9612 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009613 switch (getLayoutDirection()) {
9614 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009615 // We cannot do the resolution if there is no parent
9616 if (mParent == null) return;
9617
Cibu Johny86666632010-02-22 13:01:02 -08009618 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009619 if (mParent instanceof ViewGroup) {
9620 ViewGroup viewGroup = ((ViewGroup) mParent);
9621
9622 // Check if the parent view group can resolve
9623 if (! viewGroup.canResolveLayoutDirection()) {
9624 return;
9625 }
9626 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9627 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9628 }
Cibu Johny86666632010-02-22 13:01:02 -08009629 }
9630 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009631 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009632 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009633 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009634 case LAYOUT_DIRECTION_LOCALE:
9635 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009636 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009637 }
9638 break;
9639 default:
9640 // Nothing to do, LTR by default
9641 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009642
9643 // Set to resolved
9644 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009645 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009646 }
9647
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009648 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009649 * Called when layout direction has been resolved.
9650 *
9651 * The default implementation does nothing.
9652 */
9653 public void onResolvedLayoutDirectionChanged() {
9654 }
9655
9656 /**
9657 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009658 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009659 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009660 // If the user specified the absolute padding (either with android:padding or
9661 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9662 // use the default padding or the padding from the background drawable
9663 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009664 int resolvedLayoutDirection = getResolvedLayoutDirection();
9665 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009666 case LAYOUT_DIRECTION_RTL:
9667 // Start user padding override Right user padding. Otherwise, if Right user
9668 // padding is not defined, use the default Right padding. If Right user padding
9669 // is defined, just use it.
9670 if (mUserPaddingStart >= 0) {
9671 mUserPaddingRight = mUserPaddingStart;
9672 } else if (mUserPaddingRight < 0) {
9673 mUserPaddingRight = mPaddingRight;
9674 }
9675 if (mUserPaddingEnd >= 0) {
9676 mUserPaddingLeft = mUserPaddingEnd;
9677 } else if (mUserPaddingLeft < 0) {
9678 mUserPaddingLeft = mPaddingLeft;
9679 }
9680 break;
9681 case LAYOUT_DIRECTION_LTR:
9682 default:
9683 // Start user padding override Left user padding. Otherwise, if Left user
9684 // padding is not defined, use the default left padding. If Left user padding
9685 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009686 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009687 mUserPaddingLeft = mUserPaddingStart;
9688 } else if (mUserPaddingLeft < 0) {
9689 mUserPaddingLeft = mPaddingLeft;
9690 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009691 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009692 mUserPaddingRight = mUserPaddingEnd;
9693 } else if (mUserPaddingRight < 0) {
9694 mUserPaddingRight = mPaddingRight;
9695 }
9696 }
9697
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009698 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9699
9700 recomputePadding();
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009701 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009702 }
9703
9704 /**
9705 * Resolve padding depending on the layout direction. Subclasses that care about
9706 * padding resolution should override this method. The default implementation does
9707 * nothing.
9708 *
9709 * @param layoutDirection the direction of the layout
9710 *
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009711 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009712 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009713 }
9714
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009715 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009716 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009717 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009718 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009719 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009720 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009721 switch (getLayoutDirection()) {
9722 case LAYOUT_DIRECTION_INHERIT:
9723 return (mParent != null);
9724 default:
9725 return true;
9726 }
9727 }
9728
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009729 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009730 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
9731 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009732 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009733 public void resetResolvedLayoutDirection() {
9734 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009735 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009736 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009737 // Reset also the text direction
9738 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009739 }
9740
9741 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009742 * Called during reset of resolved layout direction.
9743 *
9744 * Subclasses need to override this method to clear cached information that depends on the
9745 * resolved layout direction, or to inform child views that inherit their layout direction.
9746 *
9747 * The default implementation does nothing.
9748 */
9749 public void onResolvedLayoutDirectionReset() {
9750 }
9751
9752 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009753 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009754 *
9755 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009756 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009757 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009758 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -08009759 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009760 }
9761
9762 /**
9763 * This is called when the view is detached from a window. At this point it
9764 * no longer has a surface for drawing.
9765 *
9766 * @see #onAttachedToWindow()
9767 */
9768 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009769 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009770
Romain Guya440b002010-02-24 15:57:54 -08009771 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009772 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009773 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009774 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009776 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009777
Romain Guy6d7475d2011-07-27 16:28:21 -07009778 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009779
Chet Haasedaf98e92011-01-10 14:10:36 -08009780 if (mDisplayList != null) {
9781 mDisplayList.invalidate();
9782 }
9783
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009784 if (mAttachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009785 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009786 }
9787
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009788 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009789
9790 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009791 }
9792
9793 /**
9794 * @return The number of times this view has been attached to a window
9795 */
9796 protected int getWindowAttachCount() {
9797 return mWindowAttachCount;
9798 }
9799
9800 /**
9801 * Retrieve a unique token identifying the window this view is attached to.
9802 * @return Return the window's token for use in
9803 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9804 */
9805 public IBinder getWindowToken() {
9806 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9807 }
9808
9809 /**
9810 * Retrieve a unique token identifying the top-level "real" window of
9811 * the window that this view is attached to. That is, this is like
9812 * {@link #getWindowToken}, except if the window this view in is a panel
9813 * window (attached to another containing window), then the token of
9814 * the containing window is returned instead.
9815 *
9816 * @return Returns the associated window token, either
9817 * {@link #getWindowToken()} or the containing window's token.
9818 */
9819 public IBinder getApplicationWindowToken() {
9820 AttachInfo ai = mAttachInfo;
9821 if (ai != null) {
9822 IBinder appWindowToken = ai.mPanelParentWindowToken;
9823 if (appWindowToken == null) {
9824 appWindowToken = ai.mWindowToken;
9825 }
9826 return appWindowToken;
9827 }
9828 return null;
9829 }
9830
9831 /**
9832 * Retrieve private session object this view hierarchy is using to
9833 * communicate with the window manager.
9834 * @return the session object to communicate with the window manager
9835 */
9836 /*package*/ IWindowSession getWindowSession() {
9837 return mAttachInfo != null ? mAttachInfo.mSession : null;
9838 }
9839
9840 /**
9841 * @param info the {@link android.view.View.AttachInfo} to associated with
9842 * this view
9843 */
9844 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9845 //System.out.println("Attached! " + this);
9846 mAttachInfo = info;
9847 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009848 // We will need to evaluate the drawable state at least once.
9849 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009850 if (mFloatingTreeObserver != null) {
9851 info.mTreeObserver.merge(mFloatingTreeObserver);
9852 mFloatingTreeObserver = null;
9853 }
9854 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9855 mAttachInfo.mScrollContainers.add(this);
9856 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9857 }
9858 performCollectViewAttributes(visibility);
9859 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009860
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009861 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009862 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009863 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009864 if (listeners != null && listeners.size() > 0) {
9865 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9866 // perform the dispatching. The iterator is a safe guard against listeners that
9867 // could mutate the list by calling the various add/remove methods. This prevents
9868 // the array from being modified while we iterate it.
9869 for (OnAttachStateChangeListener listener : listeners) {
9870 listener.onViewAttachedToWindow(this);
9871 }
9872 }
9873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009874 int vis = info.mWindowVisibility;
9875 if (vis != GONE) {
9876 onWindowVisibilityChanged(vis);
9877 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009878 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9879 // If nobody has evaluated the drawable state yet, then do it now.
9880 refreshDrawableState();
9881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009882 }
9883
9884 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009885 AttachInfo info = mAttachInfo;
9886 if (info != null) {
9887 int vis = info.mWindowVisibility;
9888 if (vis != GONE) {
9889 onWindowVisibilityChanged(GONE);
9890 }
9891 }
9892
9893 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009894
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009895 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009896 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009897 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009898 if (listeners != null && listeners.size() > 0) {
9899 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9900 // perform the dispatching. The iterator is a safe guard against listeners that
9901 // could mutate the list by calling the various add/remove methods. This prevents
9902 // the array from being modified while we iterate it.
9903 for (OnAttachStateChangeListener listener : listeners) {
9904 listener.onViewDetachedFromWindow(this);
9905 }
9906 }
9907
Romain Guy01d5edc2011-01-28 11:28:53 -08009908 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 mAttachInfo.mScrollContainers.remove(this);
9910 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9911 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009913 mAttachInfo = null;
9914 }
9915
9916 /**
9917 * Store this view hierarchy's frozen state into the given container.
9918 *
9919 * @param container The SparseArray in which to save the view's state.
9920 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009921 * @see #restoreHierarchyState(android.util.SparseArray)
9922 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9923 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 */
9925 public void saveHierarchyState(SparseArray<Parcelable> container) {
9926 dispatchSaveInstanceState(container);
9927 }
9928
9929 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009930 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9931 * this view and its children. May be overridden to modify how freezing happens to a
9932 * 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 -08009933 *
9934 * @param container The SparseArray in which to save the view's state.
9935 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009936 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9937 * @see #saveHierarchyState(android.util.SparseArray)
9938 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009939 */
9940 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9941 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9942 mPrivateFlags &= ~SAVE_STATE_CALLED;
9943 Parcelable state = onSaveInstanceState();
9944 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9945 throw new IllegalStateException(
9946 "Derived class did not call super.onSaveInstanceState()");
9947 }
9948 if (state != null) {
9949 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9950 // + ": " + state);
9951 container.put(mID, state);
9952 }
9953 }
9954 }
9955
9956 /**
9957 * Hook allowing a view to generate a representation of its internal state
9958 * that can later be used to create a new instance with that same state.
9959 * This state should only contain information that is not persistent or can
9960 * not be reconstructed later. For example, you will never store your
9961 * current position on screen because that will be computed again when a
9962 * new instance of the view is placed in its view hierarchy.
9963 * <p>
9964 * Some examples of things you may store here: the current cursor position
9965 * in a text view (but usually not the text itself since that is stored in a
9966 * content provider or other persistent storage), the currently selected
9967 * item in a list view.
9968 *
9969 * @return Returns a Parcelable object containing the view's current dynamic
9970 * state, or null if there is nothing interesting to save. The
9971 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009972 * @see #onRestoreInstanceState(android.os.Parcelable)
9973 * @see #saveHierarchyState(android.util.SparseArray)
9974 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009975 * @see #setSaveEnabled(boolean)
9976 */
9977 protected Parcelable onSaveInstanceState() {
9978 mPrivateFlags |= SAVE_STATE_CALLED;
9979 return BaseSavedState.EMPTY_STATE;
9980 }
9981
9982 /**
9983 * Restore this view hierarchy's frozen state from the given container.
9984 *
9985 * @param container The SparseArray which holds previously frozen states.
9986 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009987 * @see #saveHierarchyState(android.util.SparseArray)
9988 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9989 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009990 */
9991 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9992 dispatchRestoreInstanceState(container);
9993 }
9994
9995 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009996 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9997 * state for this view and its children. May be overridden to modify how restoring
9998 * happens to a view's children; for example, some views may want to not store state
9999 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 *
10001 * @param container The SparseArray which holds previously saved state.
10002 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010003 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10004 * @see #restoreHierarchyState(android.util.SparseArray)
10005 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010006 */
10007 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10008 if (mID != NO_ID) {
10009 Parcelable state = container.get(mID);
10010 if (state != null) {
10011 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10012 // + ": " + state);
10013 mPrivateFlags &= ~SAVE_STATE_CALLED;
10014 onRestoreInstanceState(state);
10015 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10016 throw new IllegalStateException(
10017 "Derived class did not call super.onRestoreInstanceState()");
10018 }
10019 }
10020 }
10021 }
10022
10023 /**
10024 * Hook allowing a view to re-apply a representation of its internal state that had previously
10025 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10026 * null state.
10027 *
10028 * @param state The frozen state that had previously been returned by
10029 * {@link #onSaveInstanceState}.
10030 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010031 * @see #onSaveInstanceState()
10032 * @see #restoreHierarchyState(android.util.SparseArray)
10033 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010034 */
10035 protected void onRestoreInstanceState(Parcelable state) {
10036 mPrivateFlags |= SAVE_STATE_CALLED;
10037 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010038 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10039 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010040 + "when two views of different type have the same id in the same hierarchy. "
10041 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010042 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010043 }
10044 }
10045
10046 /**
10047 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10048 *
10049 * @return the drawing start time in milliseconds
10050 */
10051 public long getDrawingTime() {
10052 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10053 }
10054
10055 /**
10056 * <p>Enables or disables the duplication of the parent's state into this view. When
10057 * duplication is enabled, this view gets its drawable state from its parent rather
10058 * than from its own internal properties.</p>
10059 *
10060 * <p>Note: in the current implementation, setting this property to true after the
10061 * view was added to a ViewGroup might have no effect at all. This property should
10062 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10063 *
10064 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10065 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010066 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010067 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10068 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010069 *
10070 * @param enabled True to enable duplication of the parent's drawable state, false
10071 * to disable it.
10072 *
10073 * @see #getDrawableState()
10074 * @see #isDuplicateParentStateEnabled()
10075 */
10076 public void setDuplicateParentStateEnabled(boolean enabled) {
10077 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10078 }
10079
10080 /**
10081 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10082 *
10083 * @return True if this view's drawable state is duplicated from the parent,
10084 * false otherwise
10085 *
10086 * @see #getDrawableState()
10087 * @see #setDuplicateParentStateEnabled(boolean)
10088 */
10089 public boolean isDuplicateParentStateEnabled() {
10090 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10091 }
10092
10093 /**
Romain Guy171c5922011-01-06 10:04:23 -080010094 * <p>Specifies the type of layer backing this view. The layer can be
10095 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10096 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010097 *
Romain Guy171c5922011-01-06 10:04:23 -080010098 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10099 * instance that controls how the layer is composed on screen. The following
10100 * properties of the paint are taken into account when composing the layer:</p>
10101 * <ul>
10102 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10103 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10104 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10105 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010106 *
Romain Guy171c5922011-01-06 10:04:23 -080010107 * <p>If this view has an alpha value set to < 1.0 by calling
10108 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10109 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10110 * equivalent to setting a hardware layer on this view and providing a paint with
10111 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010112 *
Romain Guy171c5922011-01-06 10:04:23 -080010113 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10114 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10115 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010116 *
Romain Guy171c5922011-01-06 10:04:23 -080010117 * @param layerType The ype of layer to use with this view, must be one of
10118 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10119 * {@link #LAYER_TYPE_HARDWARE}
10120 * @param paint The paint used to compose the layer. This argument is optional
10121 * and can be null. It is ignored when the layer type is
10122 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010123 *
10124 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010125 * @see #LAYER_TYPE_NONE
10126 * @see #LAYER_TYPE_SOFTWARE
10127 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010128 * @see #setAlpha(float)
10129 *
Romain Guy171c5922011-01-06 10:04:23 -080010130 * @attr ref android.R.styleable#View_layerType
10131 */
10132 public void setLayerType(int layerType, Paint paint) {
10133 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010134 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010135 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10136 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010137
Romain Guyd6cd5722011-01-17 14:42:41 -080010138 if (layerType == mLayerType) {
10139 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10140 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010141 invalidateParentCaches();
10142 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010143 }
10144 return;
10145 }
Romain Guy171c5922011-01-06 10:04:23 -080010146
10147 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010148 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010149 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010150 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010151 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010152 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010153 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010154 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010155 default:
10156 break;
Romain Guy171c5922011-01-06 10:04:23 -080010157 }
10158
10159 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010160 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10161 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10162 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010163
Romain Guy0fd89bf2011-01-26 15:41:30 -080010164 invalidateParentCaches();
10165 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010166 }
10167
10168 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010169 * Indicates whether this view has a static layer. A view with layer type
10170 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10171 * dynamic.
10172 */
10173 boolean hasStaticLayer() {
10174 return mLayerType == LAYER_TYPE_NONE;
10175 }
10176
10177 /**
Romain Guy171c5922011-01-06 10:04:23 -080010178 * Indicates what type of layer is currently associated with this view. By default
10179 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10180 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10181 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010182 *
Romain Guy171c5922011-01-06 10:04:23 -080010183 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10184 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010185 *
10186 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010187 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010188 * @see #LAYER_TYPE_NONE
10189 * @see #LAYER_TYPE_SOFTWARE
10190 * @see #LAYER_TYPE_HARDWARE
10191 */
10192 public int getLayerType() {
10193 return mLayerType;
10194 }
Joe Malin32736f02011-01-19 16:14:20 -080010195
Romain Guy6c319ca2011-01-11 14:29:25 -080010196 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010197 * Forces this view's layer to be created and this view to be rendered
10198 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10199 * invoking this method will have no effect.
10200 *
10201 * This method can for instance be used to render a view into its layer before
10202 * starting an animation. If this view is complex, rendering into the layer
10203 * before starting the animation will avoid skipping frames.
10204 *
10205 * @throws IllegalStateException If this view is not attached to a window
10206 *
10207 * @see #setLayerType(int, android.graphics.Paint)
10208 */
10209 public void buildLayer() {
10210 if (mLayerType == LAYER_TYPE_NONE) return;
10211
10212 if (mAttachInfo == null) {
10213 throw new IllegalStateException("This view must be attached to a window first");
10214 }
10215
10216 switch (mLayerType) {
10217 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010218 if (mAttachInfo.mHardwareRenderer != null &&
10219 mAttachInfo.mHardwareRenderer.isEnabled() &&
10220 mAttachInfo.mHardwareRenderer.validate()) {
10221 getHardwareLayer();
10222 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010223 break;
10224 case LAYER_TYPE_SOFTWARE:
10225 buildDrawingCache(true);
10226 break;
10227 }
10228 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010229
10230 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10231 void flushLayer() {
10232 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10233 mHardwareLayer.flush();
10234 }
10235 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010236
10237 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010238 * <p>Returns a hardware layer that can be used to draw this view again
10239 * without executing its draw method.</p>
10240 *
10241 * @return A HardwareLayer ready to render, or null if an error occurred.
10242 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010243 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010244 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10245 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010246 return null;
10247 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010248
10249 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010250
10251 final int width = mRight - mLeft;
10252 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010253
Romain Guy6c319ca2011-01-11 14:29:25 -080010254 if (width == 0 || height == 0) {
10255 return null;
10256 }
10257
10258 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10259 if (mHardwareLayer == null) {
10260 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10261 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010262 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010263 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10264 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010265 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010266 }
10267
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010268 // The layer is not valid if the underlying GPU resources cannot be allocated
10269 if (!mHardwareLayer.isValid()) {
10270 return null;
10271 }
10272
Romain Guy59a12ca2011-06-09 17:48:21 -070010273 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010274 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010275
10276 // Make sure all the GPU resources have been properly allocated
10277 if (canvas == null) {
10278 mHardwareLayer.end(currentCanvas);
10279 return null;
10280 }
10281
Romain Guy5e7f7662011-01-24 22:35:56 -080010282 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010283 try {
10284 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010285 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010286 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010287
Chet Haase88172fe2011-03-07 17:36:33 -080010288 final int restoreCount = canvas.save();
10289
Romain Guy6c319ca2011-01-11 14:29:25 -080010290 computeScroll();
10291 canvas.translate(-mScrollX, -mScrollY);
10292
Romain Guy6c319ca2011-01-11 14:29:25 -080010293 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010294
Romain Guy6c319ca2011-01-11 14:29:25 -080010295 // Fast path for layouts with no backgrounds
10296 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10297 mPrivateFlags &= ~DIRTY_MASK;
10298 dispatchDraw(canvas);
10299 } else {
10300 draw(canvas);
10301 }
Joe Malin32736f02011-01-19 16:14:20 -080010302
Chet Haase88172fe2011-03-07 17:36:33 -080010303 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010304 } finally {
10305 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010306 mHardwareLayer.end(currentCanvas);
10307 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010308 }
10309 }
10310
10311 return mHardwareLayer;
10312 }
Romain Guy171c5922011-01-06 10:04:23 -080010313
Romain Guy589b0bb2011-10-10 13:57:47 -070010314 /**
10315 * Destroys this View's hardware layer if possible.
10316 *
10317 * @return True if the layer was destroyed, false otherwise.
10318 *
10319 * @see #setLayerType(int, android.graphics.Paint)
10320 * @see #LAYER_TYPE_HARDWARE
10321 */
Romain Guy65b345f2011-07-27 18:51:50 -070010322 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010323 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010324 AttachInfo info = mAttachInfo;
10325 if (info != null && info.mHardwareRenderer != null &&
10326 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10327 mHardwareLayer.destroy();
10328 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010329
Romain Guy9c4b79a2011-11-10 19:23:58 -080010330 invalidate(true);
10331 invalidateParentCaches();
10332 }
Romain Guy65b345f2011-07-27 18:51:50 -070010333 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010334 }
Romain Guy65b345f2011-07-27 18:51:50 -070010335 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010336 }
10337
Romain Guy171c5922011-01-06 10:04:23 -080010338 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010339 * Destroys all hardware rendering resources. This method is invoked
10340 * when the system needs to reclaim resources. Upon execution of this
10341 * method, you should free any OpenGL resources created by the view.
10342 *
10343 * Note: you <strong>must</strong> call
10344 * <code>super.destroyHardwareResources()</code> when overriding
10345 * this method.
10346 *
10347 * @hide
10348 */
10349 protected void destroyHardwareResources() {
10350 destroyLayer();
10351 }
10352
10353 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010354 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10355 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10356 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10357 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10358 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10359 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010360 *
Romain Guy171c5922011-01-06 10:04:23 -080010361 * <p>Enabling the drawing cache is similar to
10362 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010363 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10364 * drawing cache has no effect on rendering because the system uses a different mechanism
10365 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10366 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10367 * for information on how to enable software and hardware layers.</p>
10368 *
10369 * <p>This API can be used to manually generate
10370 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10371 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 *
10373 * @param enabled true to enable the drawing cache, false otherwise
10374 *
10375 * @see #isDrawingCacheEnabled()
10376 * @see #getDrawingCache()
10377 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010378 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 */
10380 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010381 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10383 }
10384
10385 /**
10386 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10387 *
10388 * @return true if the drawing cache is enabled
10389 *
10390 * @see #setDrawingCacheEnabled(boolean)
10391 * @see #getDrawingCache()
10392 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010393 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 public boolean isDrawingCacheEnabled() {
10395 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10396 }
10397
10398 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010399 * Debugging utility which recursively outputs the dirty state of a view and its
10400 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010401 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010402 * @hide
10403 */
Romain Guy676b1732011-02-14 14:45:33 -080010404 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010405 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10406 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10407 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10408 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10409 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10410 if (clear) {
10411 mPrivateFlags &= clearMask;
10412 }
10413 if (this instanceof ViewGroup) {
10414 ViewGroup parent = (ViewGroup) this;
10415 final int count = parent.getChildCount();
10416 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010417 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010418 child.outputDirtyFlags(indent + " ", clear, clearMask);
10419 }
10420 }
10421 }
10422
10423 /**
10424 * This method is used by ViewGroup to cause its children to restore or recreate their
10425 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10426 * to recreate its own display list, which would happen if it went through the normal
10427 * draw/dispatchDraw mechanisms.
10428 *
10429 * @hide
10430 */
10431 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010432
10433 /**
10434 * A view that is not attached or hardware accelerated cannot create a display list.
10435 * This method checks these conditions and returns the appropriate result.
10436 *
10437 * @return true if view has the ability to create a display list, false otherwise.
10438 *
10439 * @hide
10440 */
10441 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010442 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010443 }
Joe Malin32736f02011-01-19 16:14:20 -080010444
Chet Haasedaf98e92011-01-10 14:10:36 -080010445 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010446 * @return The HardwareRenderer associated with that view or null if hardware rendering
10447 * is not supported or this this has not been attached to a window.
10448 *
10449 * @hide
10450 */
10451 public HardwareRenderer getHardwareRenderer() {
10452 if (mAttachInfo != null) {
10453 return mAttachInfo.mHardwareRenderer;
10454 }
10455 return null;
10456 }
10457
10458 /**
Romain Guyb051e892010-09-28 19:09:36 -070010459 * <p>Returns a display list that can be used to draw this view again
10460 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010461 *
Romain Guyb051e892010-09-28 19:09:36 -070010462 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010463 *
10464 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010465 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010466 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010467 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010468 return null;
10469 }
10470
Chet Haasedaf98e92011-01-10 14:10:36 -080010471 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10472 mDisplayList == null || !mDisplayList.isValid() ||
10473 mRecreateDisplayList)) {
10474 // Don't need to recreate the display list, just need to tell our
10475 // children to restore/recreate theirs
10476 if (mDisplayList != null && mDisplayList.isValid() &&
10477 !mRecreateDisplayList) {
10478 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10479 mPrivateFlags &= ~DIRTY_MASK;
10480 dispatchGetDisplayList();
10481
10482 return mDisplayList;
10483 }
10484
10485 // If we got here, we're recreating it. Mark it as such to ensure that
10486 // we copy in child display lists into ours in drawChild()
10487 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010488 if (mDisplayList == null) {
Romain Guy13631f32012-01-30 17:41:55 -080010489 final String name = getClass().getSimpleName();
10490 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
Chet Haasedaf98e92011-01-10 14:10:36 -080010491 // If we're creating a new display list, make sure our parent gets invalidated
10492 // since they will need to recreate their display list to account for this
10493 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010494 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010495 }
Romain Guyb051e892010-09-28 19:09:36 -070010496
10497 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010498 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010499 try {
10500 int width = mRight - mLeft;
10501 int height = mBottom - mTop;
10502
10503 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010504 // The dirty rect should always be null for a display list
10505 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010506
Chet Haasedaf98e92011-01-10 14:10:36 -080010507 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010508
10509 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010510 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010511 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010512 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010513
Romain Guyb051e892010-09-28 19:09:36 -070010514 // Fast path for layouts with no backgrounds
10515 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010516 dispatchDraw(canvas);
10517 } else {
10518 draw(canvas);
10519 }
Romain Guyb051e892010-09-28 19:09:36 -070010520 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010521 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010522 canvas.onPostDraw();
10523
10524 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010525 }
Chet Haase77785f92011-01-25 23:22:09 -080010526 } else {
10527 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10528 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010529 }
10530
10531 return mDisplayList;
10532 }
10533
10534 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010535 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010536 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010537 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010538 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010539 * @see #getDrawingCache(boolean)
10540 */
10541 public Bitmap getDrawingCache() {
10542 return getDrawingCache(false);
10543 }
10544
10545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10547 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10548 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10549 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10550 * request the drawing cache by calling this method and draw it on screen if the
10551 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010552 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010553 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10554 * this method will create a bitmap of the same size as this view. Because this bitmap
10555 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10556 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10557 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10558 * size than the view. This implies that your application must be able to handle this
10559 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010560 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010561 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10562 * the current density of the screen when the application is in compatibility
10563 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010565 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010566 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010567 * @see #setDrawingCacheEnabled(boolean)
10568 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010569 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 * @see #destroyDrawingCache()
10571 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010572 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10574 return null;
10575 }
10576 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010577 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010578 }
Romain Guy02890fd2010-08-06 17:58:44 -070010579 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580 }
10581
10582 /**
10583 * <p>Frees the resources used by the drawing cache. If you call
10584 * {@link #buildDrawingCache()} manually without calling
10585 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10586 * should cleanup the cache with this method afterwards.</p>
10587 *
10588 * @see #setDrawingCacheEnabled(boolean)
10589 * @see #buildDrawingCache()
10590 * @see #getDrawingCache()
10591 */
10592 public void destroyDrawingCache() {
10593 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010594 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 mDrawingCache = null;
10596 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010597 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010598 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010599 mUnscaledDrawingCache = null;
10600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 }
10602
10603 /**
10604 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010605 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010606 * view will always be drawn on top of a solid color.
10607 *
10608 * @param color The background color to use for the drawing cache's bitmap
10609 *
10610 * @see #setDrawingCacheEnabled(boolean)
10611 * @see #buildDrawingCache()
10612 * @see #getDrawingCache()
10613 */
10614 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010615 if (color != mDrawingCacheBackgroundColor) {
10616 mDrawingCacheBackgroundColor = color;
10617 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619 }
10620
10621 /**
10622 * @see #setDrawingCacheBackgroundColor(int)
10623 *
10624 * @return The background color to used for the drawing cache's bitmap
10625 */
10626 public int getDrawingCacheBackgroundColor() {
10627 return mDrawingCacheBackgroundColor;
10628 }
10629
10630 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010631 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010632 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010633 * @see #buildDrawingCache(boolean)
10634 */
10635 public void buildDrawingCache() {
10636 buildDrawingCache(false);
10637 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010638
Romain Guyfbd8f692009-06-26 14:51:58 -070010639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10641 *
10642 * <p>If you call {@link #buildDrawingCache()} manually without calling
10643 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10644 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010645 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010646 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10647 * this method will create a bitmap of the same size as this view. Because this bitmap
10648 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10649 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10650 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10651 * size than the view. This implies that your application must be able to handle this
10652 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010653 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010654 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10655 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010656 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010657 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010658 *
10659 * @see #getDrawingCache()
10660 * @see #destroyDrawingCache()
10661 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010662 public void buildDrawingCache(boolean autoScale) {
10663 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010664 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010665 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666
10667 if (ViewDebug.TRACE_HIERARCHY) {
10668 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010670
Romain Guy8506ab42009-06-11 17:35:47 -070010671 int width = mRight - mLeft;
10672 int height = mBottom - mTop;
10673
10674 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010675 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010676
Romain Guye1123222009-06-29 14:24:56 -070010677 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010678 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10679 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010681
10682 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010683 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010684 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010685
10686 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010687 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010688 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10690 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010691 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010692 return;
10693 }
10694
10695 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010696 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010697
10698 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 Bitmap.Config quality;
10700 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010701 // Never pick ARGB_4444 because it looks awful
10702 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10704 case DRAWING_CACHE_QUALITY_AUTO:
10705 quality = Bitmap.Config.ARGB_8888;
10706 break;
10707 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010708 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010709 break;
10710 case DRAWING_CACHE_QUALITY_HIGH:
10711 quality = Bitmap.Config.ARGB_8888;
10712 break;
10713 default:
10714 quality = Bitmap.Config.ARGB_8888;
10715 break;
10716 }
10717 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010718 // Optimization for translucent windows
10719 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010720 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 }
10722
10723 // Try to cleanup memory
10724 if (bitmap != null) bitmap.recycle();
10725
10726 try {
10727 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010728 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010729 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010730 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010731 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010732 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010733 }
Adam Powell26153a32010-11-08 15:22:27 -080010734 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 } catch (OutOfMemoryError e) {
10736 // If there is not enough memory to create the bitmap cache, just
10737 // ignore the issue as bitmap caches are not required to draw the
10738 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010739 if (autoScale) {
10740 mDrawingCache = null;
10741 } else {
10742 mUnscaledDrawingCache = null;
10743 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010744 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745 return;
10746 }
10747
10748 clear = drawingCacheBackgroundColor != 0;
10749 }
10750
10751 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010752 if (attachInfo != null) {
10753 canvas = attachInfo.mCanvas;
10754 if (canvas == null) {
10755 canvas = new Canvas();
10756 }
10757 canvas.setBitmap(bitmap);
10758 // Temporarily clobber the cached Canvas in case one of our children
10759 // is also using a drawing cache. Without this, the children would
10760 // steal the canvas by attaching their own bitmap to it and bad, bad
10761 // thing would happen (invisible views, corrupted drawings, etc.)
10762 attachInfo.mCanvas = null;
10763 } else {
10764 // This case should hopefully never or seldom happen
10765 canvas = new Canvas(bitmap);
10766 }
10767
10768 if (clear) {
10769 bitmap.eraseColor(drawingCacheBackgroundColor);
10770 }
10771
10772 computeScroll();
10773 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010774
Romain Guye1123222009-06-29 14:24:56 -070010775 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010776 final float scale = attachInfo.mApplicationScale;
10777 canvas.scale(scale, scale);
10778 }
Joe Malin32736f02011-01-19 16:14:20 -080010779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 canvas.translate(-mScrollX, -mScrollY);
10781
Romain Guy5bcdff42009-05-14 21:27:18 -070010782 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010783 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10784 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010785 mPrivateFlags |= DRAWING_CACHE_VALID;
10786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010787
10788 // Fast path for layouts with no backgrounds
10789 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10790 if (ViewDebug.TRACE_HIERARCHY) {
10791 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10792 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010793 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 dispatchDraw(canvas);
10795 } else {
10796 draw(canvas);
10797 }
10798
10799 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010800 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801
10802 if (attachInfo != null) {
10803 // Restore the cached Canvas for our siblings
10804 attachInfo.mCanvas = canvas;
10805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010806 }
10807 }
10808
10809 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010810 * Create a snapshot of the view into a bitmap. We should probably make
10811 * some form of this public, but should think about the API.
10812 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010813 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010814 int width = mRight - mLeft;
10815 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010816
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010817 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010818 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010819 width = (int) ((width * scale) + 0.5f);
10820 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010821
Romain Guy8c11e312009-09-14 15:15:30 -070010822 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010823 if (bitmap == null) {
10824 throw new OutOfMemoryError();
10825 }
10826
Romain Guyc529d8d2011-09-06 15:01:39 -070010827 Resources resources = getResources();
10828 if (resources != null) {
10829 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10830 }
Joe Malin32736f02011-01-19 16:14:20 -080010831
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010832 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010833 if (attachInfo != null) {
10834 canvas = attachInfo.mCanvas;
10835 if (canvas == null) {
10836 canvas = new Canvas();
10837 }
10838 canvas.setBitmap(bitmap);
10839 // Temporarily clobber the cached Canvas in case one of our children
10840 // is also using a drawing cache. Without this, the children would
10841 // steal the canvas by attaching their own bitmap to it and bad, bad
10842 // things would happen (invisible views, corrupted drawings, etc.)
10843 attachInfo.mCanvas = null;
10844 } else {
10845 // This case should hopefully never or seldom happen
10846 canvas = new Canvas(bitmap);
10847 }
10848
Romain Guy5bcdff42009-05-14 21:27:18 -070010849 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010850 bitmap.eraseColor(backgroundColor);
10851 }
10852
10853 computeScroll();
10854 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010855 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010856 canvas.translate(-mScrollX, -mScrollY);
10857
Romain Guy5bcdff42009-05-14 21:27:18 -070010858 // Temporarily remove the dirty mask
10859 int flags = mPrivateFlags;
10860 mPrivateFlags &= ~DIRTY_MASK;
10861
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010862 // Fast path for layouts with no backgrounds
10863 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10864 dispatchDraw(canvas);
10865 } else {
10866 draw(canvas);
10867 }
10868
Romain Guy5bcdff42009-05-14 21:27:18 -070010869 mPrivateFlags = flags;
10870
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010871 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010872 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010873
10874 if (attachInfo != null) {
10875 // Restore the cached Canvas for our siblings
10876 attachInfo.mCanvas = canvas;
10877 }
Romain Guy8506ab42009-06-11 17:35:47 -070010878
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010879 return bitmap;
10880 }
10881
10882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010883 * Indicates whether this View is currently in edit mode. A View is usually
10884 * in edit mode when displayed within a developer tool. For instance, if
10885 * this View is being drawn by a visual user interface builder, this method
10886 * should return true.
10887 *
10888 * Subclasses should check the return value of this method to provide
10889 * different behaviors if their normal behavior might interfere with the
10890 * host environment. For instance: the class spawns a thread in its
10891 * constructor, the drawing code relies on device-specific features, etc.
10892 *
10893 * This method is usually checked in the drawing code of custom widgets.
10894 *
10895 * @return True if this View is in edit mode, false otherwise.
10896 */
10897 public boolean isInEditMode() {
10898 return false;
10899 }
10900
10901 /**
10902 * If the View draws content inside its padding and enables fading edges,
10903 * it needs to support padding offsets. Padding offsets are added to the
10904 * fading edges to extend the length of the fade so that it covers pixels
10905 * drawn inside the padding.
10906 *
10907 * Subclasses of this class should override this method if they need
10908 * to draw content inside the padding.
10909 *
10910 * @return True if padding offset must be applied, false otherwise.
10911 *
10912 * @see #getLeftPaddingOffset()
10913 * @see #getRightPaddingOffset()
10914 * @see #getTopPaddingOffset()
10915 * @see #getBottomPaddingOffset()
10916 *
10917 * @since CURRENT
10918 */
10919 protected boolean isPaddingOffsetRequired() {
10920 return false;
10921 }
10922
10923 /**
10924 * Amount by which to extend the left fading region. Called only when
10925 * {@link #isPaddingOffsetRequired()} returns true.
10926 *
10927 * @return The left padding offset in pixels.
10928 *
10929 * @see #isPaddingOffsetRequired()
10930 *
10931 * @since CURRENT
10932 */
10933 protected int getLeftPaddingOffset() {
10934 return 0;
10935 }
10936
10937 /**
10938 * Amount by which to extend the right fading region. Called only when
10939 * {@link #isPaddingOffsetRequired()} returns true.
10940 *
10941 * @return The right padding offset in pixels.
10942 *
10943 * @see #isPaddingOffsetRequired()
10944 *
10945 * @since CURRENT
10946 */
10947 protected int getRightPaddingOffset() {
10948 return 0;
10949 }
10950
10951 /**
10952 * Amount by which to extend the top fading region. Called only when
10953 * {@link #isPaddingOffsetRequired()} returns true.
10954 *
10955 * @return The top padding offset in pixels.
10956 *
10957 * @see #isPaddingOffsetRequired()
10958 *
10959 * @since CURRENT
10960 */
10961 protected int getTopPaddingOffset() {
10962 return 0;
10963 }
10964
10965 /**
10966 * Amount by which to extend the bottom fading region. Called only when
10967 * {@link #isPaddingOffsetRequired()} returns true.
10968 *
10969 * @return The bottom padding offset in pixels.
10970 *
10971 * @see #isPaddingOffsetRequired()
10972 *
10973 * @since CURRENT
10974 */
10975 protected int getBottomPaddingOffset() {
10976 return 0;
10977 }
10978
10979 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010980 * @hide
10981 * @param offsetRequired
10982 */
10983 protected int getFadeTop(boolean offsetRequired) {
10984 int top = mPaddingTop;
10985 if (offsetRequired) top += getTopPaddingOffset();
10986 return top;
10987 }
10988
10989 /**
10990 * @hide
10991 * @param offsetRequired
10992 */
10993 protected int getFadeHeight(boolean offsetRequired) {
10994 int padding = mPaddingTop;
10995 if (offsetRequired) padding += getTopPaddingOffset();
10996 return mBottom - mTop - mPaddingBottom - padding;
10997 }
10998
10999 /**
Romain Guy2bffd262010-09-12 17:40:02 -070011000 * <p>Indicates whether this view is attached to an hardware accelerated
11001 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011002 *
Romain Guy2bffd262010-09-12 17:40:02 -070011003 * <p>Even if this method returns true, it does not mean that every call
11004 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11005 * accelerated {@link android.graphics.Canvas}. For instance, if this view
11006 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
11007 * window is hardware accelerated,
11008 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11009 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011010 *
Romain Guy2bffd262010-09-12 17:40:02 -070011011 * @return True if the view is attached to a window and the window is
11012 * hardware accelerated; false in any other case.
11013 */
11014 public boolean isHardwareAccelerated() {
11015 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11016 }
Joe Malin32736f02011-01-19 16:14:20 -080011017
Romain Guy2bffd262010-09-12 17:40:02 -070011018 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011019 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11020 * case of an active Animation being run on the view.
11021 */
11022 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11023 Animation a, boolean scalingRequired) {
11024 Transformation invalidationTransform;
11025 final int flags = parent.mGroupFlags;
11026 final boolean initialized = a.isInitialized();
11027 if (!initialized) {
11028 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11029 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11030 onAnimationStart();
11031 }
11032
11033 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11034 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11035 if (parent.mInvalidationTransformation == null) {
11036 parent.mInvalidationTransformation = new Transformation();
11037 }
11038 invalidationTransform = parent.mInvalidationTransformation;
11039 a.getTransformation(drawingTime, invalidationTransform, 1f);
11040 } else {
11041 invalidationTransform = parent.mChildTransformation;
11042 }
11043 if (more) {
11044 if (!a.willChangeBounds()) {
11045 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11046 parent.FLAG_OPTIMIZE_INVALIDATE) {
11047 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11048 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11049 // The child need to draw an animation, potentially offscreen, so
11050 // make sure we do not cancel invalidate requests
11051 parent.mPrivateFlags |= DRAW_ANIMATION;
11052 parent.invalidate(mLeft, mTop, mRight, mBottom);
11053 }
11054 } else {
11055 if (parent.mInvalidateRegion == null) {
11056 parent.mInvalidateRegion = new RectF();
11057 }
11058 final RectF region = parent.mInvalidateRegion;
11059 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11060 invalidationTransform);
11061
11062 // The child need to draw an animation, potentially offscreen, so
11063 // make sure we do not cancel invalidate requests
11064 parent.mPrivateFlags |= DRAW_ANIMATION;
11065
11066 final int left = mLeft + (int) region.left;
11067 final int top = mTop + (int) region.top;
11068 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11069 top + (int) (region.height() + .5f));
11070 }
11071 }
11072 return more;
11073 }
11074
11075 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011076 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11077 * This draw() method is an implementation detail and is not intended to be overridden or
11078 * to be called from anywhere else other than ViewGroup.drawChild().
11079 */
11080 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
11081 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011082 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011083 final int flags = parent.mGroupFlags;
11084
11085 if ((flags & parent.FLAG_CLEAR_TRANSFORMATION) == parent.FLAG_CLEAR_TRANSFORMATION) {
11086 parent.mChildTransformation.clear();
11087 parent.mGroupFlags &= ~parent.FLAG_CLEAR_TRANSFORMATION;
11088 }
11089
11090 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011091 boolean concatMatrix = false;
11092
11093 boolean scalingRequired = false;
11094 boolean caching;
11095 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11096
11097 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
11098 if ((flags & parent.FLAG_CHILDREN_DRAWN_WITH_CACHE) == parent.FLAG_CHILDREN_DRAWN_WITH_CACHE ||
11099 (flags & parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) == parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) {
11100 caching = true;
11101 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11102 } else {
11103 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11104 }
11105
Chet Haasebcca79a2012-02-14 08:45:14 -080011106 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011107 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011108 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011109 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011110 transformToApply = parent.mChildTransformation;
Chet Haase64a48c12012-02-13 16:33:29 -080011111 } else if ((flags & parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
11112 parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011113 final boolean hasTransform =
11114 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011115 if (hasTransform) {
11116 final int transformType = parent.mChildTransformation.getTransformationType();
11117 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11118 parent.mChildTransformation : null;
11119 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11120 }
11121 }
11122
11123 concatMatrix |= !childHasIdentityMatrix;
11124
11125 // Sets the flag as early as possible to allow draw() implementations
11126 // to call invalidate() successfully when doing animations
11127 mPrivateFlags |= DRAWN;
11128
Chet Haasebcca79a2012-02-14 08:45:14 -080011129 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011130 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11131 return more;
11132 }
11133
11134 if (hardwareAccelerated) {
11135 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11136 // retain the flag's value temporarily in the mRecreateDisplayList flag
11137 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11138 mPrivateFlags &= ~INVALIDATED;
11139 }
11140
11141 computeScroll();
11142
11143 final int sx = mScrollX;
11144 final int sy = mScrollY;
11145
11146 DisplayList displayList = null;
11147 Bitmap cache = null;
11148 boolean hasDisplayList = false;
11149 if (caching) {
11150 if (!hardwareAccelerated) {
11151 if (layerType != LAYER_TYPE_NONE) {
11152 layerType = LAYER_TYPE_SOFTWARE;
11153 buildDrawingCache(true);
11154 }
11155 cache = getDrawingCache(true);
11156 } else {
11157 switch (layerType) {
11158 case LAYER_TYPE_SOFTWARE:
11159 buildDrawingCache(true);
11160 cache = getDrawingCache(true);
11161 break;
11162 case LAYER_TYPE_NONE:
11163 // Delay getting the display list until animation-driven alpha values are
11164 // set up and possibly passed on to the view
11165 hasDisplayList = canHaveDisplayList();
11166 break;
11167 }
11168 }
11169 }
11170
11171 final boolean hasNoCache = cache == null || hasDisplayList;
11172 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11173 layerType != LAYER_TYPE_HARDWARE;
11174
11175 final int restoreTo = canvas.save();
11176 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011177 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011178 } else {
Chet Haasebcca79a2012-02-14 08:45:14 -080011179 canvas.translate(mLeft, mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011180 if (scalingRequired) {
11181 // mAttachInfo cannot be null, otherwise scalingRequired == false
11182 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11183 canvas.scale(scale, scale);
11184 }
11185 }
11186
11187 float alpha = getAlpha();
11188 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11189 if (transformToApply != null || !childHasIdentityMatrix) {
11190 int transX = 0;
11191 int transY = 0;
11192
11193 if (offsetForScroll) {
11194 transX = -sx;
11195 transY = -sy;
11196 }
11197
11198 if (transformToApply != null) {
11199 if (concatMatrix) {
11200 // Undo the scroll translation, apply the transformation matrix,
11201 // then redo the scroll translate to get the correct result.
11202 canvas.translate(-transX, -transY);
11203 canvas.concat(transformToApply.getMatrix());
11204 canvas.translate(transX, transY);
11205 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11206 }
11207
11208 float transformAlpha = transformToApply.getAlpha();
11209 if (transformAlpha < 1.0f) {
11210 alpha *= transformToApply.getAlpha();
11211 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11212 }
11213 }
11214
11215 if (!childHasIdentityMatrix) {
11216 canvas.translate(-transX, -transY);
11217 canvas.concat(getMatrix());
11218 canvas.translate(transX, transY);
11219 }
11220 }
11221
11222 if (alpha < 1.0f) {
11223 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11224 if (hasNoCache) {
11225 final int multipliedAlpha = (int) (255 * alpha);
11226 if (!onSetAlpha(multipliedAlpha)) {
11227 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11228 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN ||
11229 layerType != LAYER_TYPE_NONE) {
11230 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11231 }
11232 if (layerType == LAYER_TYPE_NONE) {
11233 final int scrollX = hasDisplayList ? 0 : sx;
11234 final int scrollY = hasDisplayList ? 0 : sy;
Chet Haasebcca79a2012-02-14 08:45:14 -080011235 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11236 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011237 }
11238 } else {
11239 // Alpha is handled by the child directly, clobber the layer's alpha
11240 mPrivateFlags |= ALPHA_SET;
11241 }
11242 }
11243 }
11244 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11245 onSetAlpha(255);
11246 mPrivateFlags &= ~ALPHA_SET;
11247 }
11248
11249 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN) {
11250 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011251 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011252 } else {
11253 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011254 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011255 } else {
11256 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11257 }
11258 }
11259 }
11260
11261 if (hasDisplayList) {
11262 displayList = getDisplayList();
11263 if (!displayList.isValid()) {
11264 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11265 // to getDisplayList(), the display list will be marked invalid and we should not
11266 // try to use it again.
11267 displayList = null;
11268 hasDisplayList = false;
11269 }
11270 }
11271
11272 if (hasNoCache) {
11273 boolean layerRendered = false;
11274 if (layerType == LAYER_TYPE_HARDWARE) {
11275 final HardwareLayer layer = getHardwareLayer();
11276 if (layer != null && layer.isValid()) {
11277 mLayerPaint.setAlpha((int) (alpha * 255));
11278 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11279 layerRendered = true;
11280 } else {
11281 final int scrollX = hasDisplayList ? 0 : sx;
11282 final int scrollY = hasDisplayList ? 0 : sy;
11283 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011284 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011285 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11286 }
11287 }
11288
11289 if (!layerRendered) {
11290 if (!hasDisplayList) {
11291 // Fast path for layouts with no backgrounds
11292 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11293 if (ViewDebug.TRACE_HIERARCHY) {
11294 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11295 }
11296 mPrivateFlags &= ~DIRTY_MASK;
11297 dispatchDraw(canvas);
11298 } else {
11299 draw(canvas);
11300 }
11301 } else {
11302 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011303 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011304 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011305 }
11306 }
11307 } else if (cache != null) {
11308 mPrivateFlags &= ~DIRTY_MASK;
11309 Paint cachePaint;
11310
11311 if (layerType == LAYER_TYPE_NONE) {
11312 cachePaint = parent.mCachePaint;
11313 if (cachePaint == null) {
11314 cachePaint = new Paint();
11315 cachePaint.setDither(false);
11316 parent.mCachePaint = cachePaint;
11317 }
11318 if (alpha < 1.0f) {
11319 cachePaint.setAlpha((int) (alpha * 255));
11320 parent.mGroupFlags |= parent.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haasebcca79a2012-02-14 08:45:14 -080011321 } else if ((flags & parent.FLAG_ALPHA_LOWER_THAN_ONE) ==
11322 parent.FLAG_ALPHA_LOWER_THAN_ONE) {
Chet Haase64a48c12012-02-13 16:33:29 -080011323 cachePaint.setAlpha(255);
11324 parent.mGroupFlags &= ~parent.FLAG_ALPHA_LOWER_THAN_ONE;
11325 }
11326 } else {
11327 cachePaint = mLayerPaint;
11328 cachePaint.setAlpha((int) (alpha * 255));
11329 }
11330 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11331 }
11332
11333 canvas.restoreToCount(restoreTo);
11334
11335 if (a != null && !more) {
11336 if (!hardwareAccelerated && !a.getFillAfter()) {
11337 onSetAlpha(255);
11338 }
11339 parent.finishAnimatingView(this, a);
11340 }
11341
11342 if (more && hardwareAccelerated) {
11343 // invalidation is the trigger to recreate display lists, so if we're using
11344 // display lists to render, force an invalidate to allow the animation to
11345 // continue drawing another frame
11346 parent.invalidate(true);
11347 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11348 // alpha animations should cause the child to recreate its display list
11349 invalidate(true);
11350 }
11351 }
11352
11353 mRecreateDisplayList = false;
11354
11355 return more;
11356 }
11357
11358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011359 * Manually render this view (and all of its children) to the given Canvas.
11360 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011361 * called. When implementing a view, implement
11362 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11363 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011364 *
11365 * @param canvas The Canvas to which the View is rendered.
11366 */
11367 public void draw(Canvas canvas) {
11368 if (ViewDebug.TRACE_HIERARCHY) {
11369 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11370 }
11371
Romain Guy5bcdff42009-05-14 21:27:18 -070011372 final int privateFlags = mPrivateFlags;
11373 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11374 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11375 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011377 /*
11378 * Draw traversal performs several drawing steps which must be executed
11379 * in the appropriate order:
11380 *
11381 * 1. Draw the background
11382 * 2. If necessary, save the canvas' layers to prepare for fading
11383 * 3. Draw view's content
11384 * 4. Draw children
11385 * 5. If necessary, draw the fading edges and restore layers
11386 * 6. Draw decorations (scrollbars for instance)
11387 */
11388
11389 // Step 1, draw the background, if needed
11390 int saveCount;
11391
Romain Guy24443ea2009-05-11 11:56:30 -070011392 if (!dirtyOpaque) {
11393 final Drawable background = mBGDrawable;
11394 if (background != null) {
11395 final int scrollX = mScrollX;
11396 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011397
Romain Guy24443ea2009-05-11 11:56:30 -070011398 if (mBackgroundSizeChanged) {
11399 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11400 mBackgroundSizeChanged = false;
11401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011402
Romain Guy24443ea2009-05-11 11:56:30 -070011403 if ((scrollX | scrollY) == 0) {
11404 background.draw(canvas);
11405 } else {
11406 canvas.translate(scrollX, scrollY);
11407 background.draw(canvas);
11408 canvas.translate(-scrollX, -scrollY);
11409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011410 }
11411 }
11412
11413 // skip step 2 & 5 if possible (common case)
11414 final int viewFlags = mViewFlags;
11415 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11416 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11417 if (!verticalEdges && !horizontalEdges) {
11418 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011419 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011420
11421 // Step 4, draw the children
11422 dispatchDraw(canvas);
11423
11424 // Step 6, draw decorations (scrollbars)
11425 onDrawScrollBars(canvas);
11426
11427 // we're done...
11428 return;
11429 }
11430
11431 /*
11432 * Here we do the full fledged routine...
11433 * (this is an uncommon case where speed matters less,
11434 * this is why we repeat some of the tests that have been
11435 * done above)
11436 */
11437
11438 boolean drawTop = false;
11439 boolean drawBottom = false;
11440 boolean drawLeft = false;
11441 boolean drawRight = false;
11442
11443 float topFadeStrength = 0.0f;
11444 float bottomFadeStrength = 0.0f;
11445 float leftFadeStrength = 0.0f;
11446 float rightFadeStrength = 0.0f;
11447
11448 // Step 2, save the canvas' layers
11449 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011450
11451 final boolean offsetRequired = isPaddingOffsetRequired();
11452 if (offsetRequired) {
11453 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011454 }
11455
11456 int left = mScrollX + paddingLeft;
11457 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011458 int top = mScrollY + getFadeTop(offsetRequired);
11459 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011460
11461 if (offsetRequired) {
11462 right += getRightPaddingOffset();
11463 bottom += getBottomPaddingOffset();
11464 }
11465
11466 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011467 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11468 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011469
11470 // clip the fade length if top and bottom fades overlap
11471 // overlapping fades produce odd-looking artifacts
11472 if (verticalEdges && (top + length > bottom - length)) {
11473 length = (bottom - top) / 2;
11474 }
11475
11476 // also clip horizontal fades if necessary
11477 if (horizontalEdges && (left + length > right - length)) {
11478 length = (right - left) / 2;
11479 }
11480
11481 if (verticalEdges) {
11482 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011483 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011484 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011485 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011486 }
11487
11488 if (horizontalEdges) {
11489 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011490 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011491 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011492 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011493 }
11494
11495 saveCount = canvas.getSaveCount();
11496
11497 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011498 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011499 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11500
11501 if (drawTop) {
11502 canvas.saveLayer(left, top, right, top + length, null, flags);
11503 }
11504
11505 if (drawBottom) {
11506 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11507 }
11508
11509 if (drawLeft) {
11510 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11511 }
11512
11513 if (drawRight) {
11514 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11515 }
11516 } else {
11517 scrollabilityCache.setFadeColor(solidColor);
11518 }
11519
11520 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011521 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011522
11523 // Step 4, draw the children
11524 dispatchDraw(canvas);
11525
11526 // Step 5, draw the fade effect and restore layers
11527 final Paint p = scrollabilityCache.paint;
11528 final Matrix matrix = scrollabilityCache.matrix;
11529 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011530
11531 if (drawTop) {
11532 matrix.setScale(1, fadeHeight * topFadeStrength);
11533 matrix.postTranslate(left, top);
11534 fade.setLocalMatrix(matrix);
11535 canvas.drawRect(left, top, right, top + length, p);
11536 }
11537
11538 if (drawBottom) {
11539 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11540 matrix.postRotate(180);
11541 matrix.postTranslate(left, bottom);
11542 fade.setLocalMatrix(matrix);
11543 canvas.drawRect(left, bottom - length, right, bottom, p);
11544 }
11545
11546 if (drawLeft) {
11547 matrix.setScale(1, fadeHeight * leftFadeStrength);
11548 matrix.postRotate(-90);
11549 matrix.postTranslate(left, top);
11550 fade.setLocalMatrix(matrix);
11551 canvas.drawRect(left, top, left + length, bottom, p);
11552 }
11553
11554 if (drawRight) {
11555 matrix.setScale(1, fadeHeight * rightFadeStrength);
11556 matrix.postRotate(90);
11557 matrix.postTranslate(right, top);
11558 fade.setLocalMatrix(matrix);
11559 canvas.drawRect(right - length, top, right, bottom, p);
11560 }
11561
11562 canvas.restoreToCount(saveCount);
11563
11564 // Step 6, draw decorations (scrollbars)
11565 onDrawScrollBars(canvas);
11566 }
11567
11568 /**
11569 * Override this if your view is known to always be drawn on top of a solid color background,
11570 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11571 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11572 * should be set to 0xFF.
11573 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011574 * @see #setVerticalFadingEdgeEnabled(boolean)
11575 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011576 *
11577 * @return The known solid color background for this view, or 0 if the color may vary
11578 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011579 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 public int getSolidColor() {
11581 return 0;
11582 }
11583
11584 /**
11585 * Build a human readable string representation of the specified view flags.
11586 *
11587 * @param flags the view flags to convert to a string
11588 * @return a String representing the supplied flags
11589 */
11590 private static String printFlags(int flags) {
11591 String output = "";
11592 int numFlags = 0;
11593 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11594 output += "TAKES_FOCUS";
11595 numFlags++;
11596 }
11597
11598 switch (flags & VISIBILITY_MASK) {
11599 case INVISIBLE:
11600 if (numFlags > 0) {
11601 output += " ";
11602 }
11603 output += "INVISIBLE";
11604 // USELESS HERE numFlags++;
11605 break;
11606 case GONE:
11607 if (numFlags > 0) {
11608 output += " ";
11609 }
11610 output += "GONE";
11611 // USELESS HERE numFlags++;
11612 break;
11613 default:
11614 break;
11615 }
11616 return output;
11617 }
11618
11619 /**
11620 * Build a human readable string representation of the specified private
11621 * view flags.
11622 *
11623 * @param privateFlags the private view flags to convert to a string
11624 * @return a String representing the supplied flags
11625 */
11626 private static String printPrivateFlags(int privateFlags) {
11627 String output = "";
11628 int numFlags = 0;
11629
11630 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11631 output += "WANTS_FOCUS";
11632 numFlags++;
11633 }
11634
11635 if ((privateFlags & FOCUSED) == FOCUSED) {
11636 if (numFlags > 0) {
11637 output += " ";
11638 }
11639 output += "FOCUSED";
11640 numFlags++;
11641 }
11642
11643 if ((privateFlags & SELECTED) == SELECTED) {
11644 if (numFlags > 0) {
11645 output += " ";
11646 }
11647 output += "SELECTED";
11648 numFlags++;
11649 }
11650
11651 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11652 if (numFlags > 0) {
11653 output += " ";
11654 }
11655 output += "IS_ROOT_NAMESPACE";
11656 numFlags++;
11657 }
11658
11659 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11660 if (numFlags > 0) {
11661 output += " ";
11662 }
11663 output += "HAS_BOUNDS";
11664 numFlags++;
11665 }
11666
11667 if ((privateFlags & DRAWN) == DRAWN) {
11668 if (numFlags > 0) {
11669 output += " ";
11670 }
11671 output += "DRAWN";
11672 // USELESS HERE numFlags++;
11673 }
11674 return output;
11675 }
11676
11677 /**
11678 * <p>Indicates whether or not this view's layout will be requested during
11679 * the next hierarchy layout pass.</p>
11680 *
11681 * @return true if the layout will be forced during next layout pass
11682 */
11683 public boolean isLayoutRequested() {
11684 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11685 }
11686
11687 /**
11688 * Assign a size and position to a view and all of its
11689 * descendants
11690 *
11691 * <p>This is the second phase of the layout mechanism.
11692 * (The first is measuring). In this phase, each parent calls
11693 * layout on all of its children to position them.
11694 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011695 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011696 *
Chet Haase9c087442011-01-12 16:20:16 -080011697 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011698 * Derived classes with children should override
11699 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011700 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011701 *
11702 * @param l Left position, relative to parent
11703 * @param t Top position, relative to parent
11704 * @param r Right position, relative to parent
11705 * @param b Bottom position, relative to parent
11706 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011707 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011708 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011709 int oldL = mLeft;
11710 int oldT = mTop;
11711 int oldB = mBottom;
11712 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011713 boolean changed = setFrame(l, t, r, b);
11714 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11715 if (ViewDebug.TRACE_HIERARCHY) {
11716 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11717 }
11718
11719 onLayout(changed, l, t, r, b);
11720 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011721
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011722 ListenerInfo li = mListenerInfo;
11723 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011724 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011725 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011726 int numListeners = listenersCopy.size();
11727 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011728 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011729 }
11730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011731 }
11732 mPrivateFlags &= ~FORCE_LAYOUT;
11733 }
11734
11735 /**
11736 * Called from layout when this view should
11737 * assign a size and position to each of its children.
11738 *
11739 * Derived classes with children should override
11740 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011741 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011742 * @param changed This is a new size or position for this view
11743 * @param left Left position, relative to parent
11744 * @param top Top position, relative to parent
11745 * @param right Right position, relative to parent
11746 * @param bottom Bottom position, relative to parent
11747 */
11748 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11749 }
11750
11751 /**
11752 * Assign a size and position to this view.
11753 *
11754 * This is called from layout.
11755 *
11756 * @param left Left position, relative to parent
11757 * @param top Top position, relative to parent
11758 * @param right Right position, relative to parent
11759 * @param bottom Bottom position, relative to parent
11760 * @return true if the new size and position are different than the
11761 * previous ones
11762 * {@hide}
11763 */
11764 protected boolean setFrame(int left, int top, int right, int bottom) {
11765 boolean changed = false;
11766
11767 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011768 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011769 + right + "," + bottom + ")");
11770 }
11771
11772 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11773 changed = true;
11774
11775 // Remember our drawn bit
11776 int drawn = mPrivateFlags & DRAWN;
11777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 int oldWidth = mRight - mLeft;
11779 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011780 int newWidth = right - left;
11781 int newHeight = bottom - top;
11782 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11783
11784 // Invalidate our old position
11785 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011786
11787 mLeft = left;
11788 mTop = top;
11789 mRight = right;
11790 mBottom = bottom;
11791
11792 mPrivateFlags |= HAS_BOUNDS;
11793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011794
Chet Haase75755e22011-07-18 17:48:25 -070011795 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011796 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11797 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011798 if (mTransformationInfo != null) {
11799 mTransformationInfo.mMatrixDirty = true;
11800 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011802 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11803 }
11804
11805 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11806 // If we are visible, force the DRAWN bit to on so that
11807 // this invalidate will go through (at least to our parent).
11808 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011809 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011810 // the DRAWN bit.
11811 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011812 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011813 // parent display list may need to be recreated based on a change in the bounds
11814 // of any child
11815 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011816 }
11817
11818 // Reset drawn bit to original value (invalidate turns it off)
11819 mPrivateFlags |= drawn;
11820
11821 mBackgroundSizeChanged = true;
11822 }
11823 return changed;
11824 }
11825
11826 /**
11827 * Finalize inflating a view from XML. This is called as the last phase
11828 * of inflation, after all child views have been added.
11829 *
11830 * <p>Even if the subclass overrides onFinishInflate, they should always be
11831 * sure to call the super method, so that we get called.
11832 */
11833 protected void onFinishInflate() {
11834 }
11835
11836 /**
11837 * Returns the resources associated with this view.
11838 *
11839 * @return Resources object.
11840 */
11841 public Resources getResources() {
11842 return mResources;
11843 }
11844
11845 /**
11846 * Invalidates the specified Drawable.
11847 *
11848 * @param drawable the drawable to invalidate
11849 */
11850 public void invalidateDrawable(Drawable drawable) {
11851 if (verifyDrawable(drawable)) {
11852 final Rect dirty = drawable.getBounds();
11853 final int scrollX = mScrollX;
11854 final int scrollY = mScrollY;
11855
11856 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11857 dirty.right + scrollX, dirty.bottom + scrollY);
11858 }
11859 }
11860
11861 /**
11862 * Schedules an action on a drawable to occur at a specified time.
11863 *
11864 * @param who the recipient of the action
11865 * @param what the action to run on the drawable
11866 * @param when the time at which the action must occur. Uses the
11867 * {@link SystemClock#uptimeMillis} timebase.
11868 */
11869 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011870 if (verifyDrawable(who) && what != null) {
11871 if (mAttachInfo != null) {
11872 mAttachInfo.mHandler.postAtTime(what, who, when);
11873 } else {
11874 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011876 }
11877 }
11878
11879 /**
11880 * Cancels a scheduled action on a drawable.
11881 *
11882 * @param who the recipient of the action
11883 * @param what the action to cancel
11884 */
11885 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011886 if (verifyDrawable(who) && what != null) {
11887 if (mAttachInfo != null) {
11888 mAttachInfo.mHandler.removeCallbacks(what, who);
11889 } else {
11890 ViewRootImpl.getRunQueue().removeCallbacks(what);
11891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 }
11893 }
11894
11895 /**
11896 * Unschedule any events associated with the given Drawable. This can be
11897 * used when selecting a new Drawable into a view, so that the previous
11898 * one is completely unscheduled.
11899 *
11900 * @param who The Drawable to unschedule.
11901 *
11902 * @see #drawableStateChanged
11903 */
11904 public void unscheduleDrawable(Drawable who) {
11905 if (mAttachInfo != null) {
11906 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11907 }
11908 }
11909
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011910 /**
11911 * Return the layout direction of a given Drawable.
11912 *
11913 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011914 */
11915 public int getResolvedLayoutDirection(Drawable who) {
11916 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011917 }
11918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011919 /**
11920 * If your view subclass is displaying its own Drawable objects, it should
11921 * override this function and return true for any Drawable it is
11922 * displaying. This allows animations for those drawables to be
11923 * scheduled.
11924 *
11925 * <p>Be sure to call through to the super class when overriding this
11926 * function.
11927 *
11928 * @param who The Drawable to verify. Return true if it is one you are
11929 * displaying, else return the result of calling through to the
11930 * super class.
11931 *
11932 * @return boolean If true than the Drawable is being displayed in the
11933 * view; else false and it is not allowed to animate.
11934 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011935 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11936 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937 */
11938 protected boolean verifyDrawable(Drawable who) {
11939 return who == mBGDrawable;
11940 }
11941
11942 /**
11943 * This function is called whenever the state of the view changes in such
11944 * a way that it impacts the state of drawables being shown.
11945 *
11946 * <p>Be sure to call through to the superclass when overriding this
11947 * function.
11948 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011949 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011950 */
11951 protected void drawableStateChanged() {
11952 Drawable d = mBGDrawable;
11953 if (d != null && d.isStateful()) {
11954 d.setState(getDrawableState());
11955 }
11956 }
11957
11958 /**
11959 * Call this to force a view to update its drawable state. This will cause
11960 * drawableStateChanged to be called on this view. Views that are interested
11961 * in the new state should call getDrawableState.
11962 *
11963 * @see #drawableStateChanged
11964 * @see #getDrawableState
11965 */
11966 public void refreshDrawableState() {
11967 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11968 drawableStateChanged();
11969
11970 ViewParent parent = mParent;
11971 if (parent != null) {
11972 parent.childDrawableStateChanged(this);
11973 }
11974 }
11975
11976 /**
11977 * Return an array of resource IDs of the drawable states representing the
11978 * current state of the view.
11979 *
11980 * @return The current drawable state
11981 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011982 * @see Drawable#setState(int[])
11983 * @see #drawableStateChanged()
11984 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011985 */
11986 public final int[] getDrawableState() {
11987 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11988 return mDrawableState;
11989 } else {
11990 mDrawableState = onCreateDrawableState(0);
11991 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11992 return mDrawableState;
11993 }
11994 }
11995
11996 /**
11997 * Generate the new {@link android.graphics.drawable.Drawable} state for
11998 * this view. This is called by the view
11999 * system when the cached Drawable state is determined to be invalid. To
12000 * retrieve the current state, you should use {@link #getDrawableState}.
12001 *
12002 * @param extraSpace if non-zero, this is the number of extra entries you
12003 * would like in the returned array in which you can place your own
12004 * states.
12005 *
12006 * @return Returns an array holding the current {@link Drawable} state of
12007 * the view.
12008 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012009 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 */
12011 protected int[] onCreateDrawableState(int extraSpace) {
12012 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12013 mParent instanceof View) {
12014 return ((View) mParent).onCreateDrawableState(extraSpace);
12015 }
12016
12017 int[] drawableState;
12018
12019 int privateFlags = mPrivateFlags;
12020
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012021 int viewStateIndex = 0;
12022 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12023 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12024 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012025 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012026 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12027 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012028 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12029 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012030 // This is set if HW acceleration is requested, even if the current
12031 // process doesn't allow it. This is just to allow app preview
12032 // windows to better match their app.
12033 viewStateIndex |= VIEW_STATE_ACCELERATED;
12034 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012035 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012036
Christopher Tate3d4bf172011-03-28 16:16:46 -070012037 final int privateFlags2 = mPrivateFlags2;
12038 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12039 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012041 drawableState = VIEW_STATE_SETS[viewStateIndex];
12042
12043 //noinspection ConstantIfStatement
12044 if (false) {
12045 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12046 Log.i("View", toString()
12047 + " pressed=" + ((privateFlags & PRESSED) != 0)
12048 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12049 + " fo=" + hasFocus()
12050 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012051 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012052 + ": " + Arrays.toString(drawableState));
12053 }
12054
12055 if (extraSpace == 0) {
12056 return drawableState;
12057 }
12058
12059 final int[] fullState;
12060 if (drawableState != null) {
12061 fullState = new int[drawableState.length + extraSpace];
12062 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12063 } else {
12064 fullState = new int[extraSpace];
12065 }
12066
12067 return fullState;
12068 }
12069
12070 /**
12071 * Merge your own state values in <var>additionalState</var> into the base
12072 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012073 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012074 *
12075 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012076 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012077 * own additional state values.
12078 *
12079 * @param additionalState The additional state values you would like
12080 * added to <var>baseState</var>; this array is not modified.
12081 *
12082 * @return As a convenience, the <var>baseState</var> array you originally
12083 * passed into the function is returned.
12084 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012085 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012086 */
12087 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12088 final int N = baseState.length;
12089 int i = N - 1;
12090 while (i >= 0 && baseState[i] == 0) {
12091 i--;
12092 }
12093 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12094 return baseState;
12095 }
12096
12097 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012098 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12099 * on all Drawable objects associated with this view.
12100 */
12101 public void jumpDrawablesToCurrentState() {
12102 if (mBGDrawable != null) {
12103 mBGDrawable.jumpToCurrentState();
12104 }
12105 }
12106
12107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012108 * Sets the background color for this view.
12109 * @param color the color of the background
12110 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012111 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012112 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012113 if (mBGDrawable instanceof ColorDrawable) {
12114 ((ColorDrawable) mBGDrawable).setColor(color);
12115 } else {
12116 setBackgroundDrawable(new ColorDrawable(color));
12117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012118 }
12119
12120 /**
12121 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012122 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012123 * @param resid The identifier of the resource.
12124 * @attr ref android.R.styleable#View_background
12125 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012126 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012127 public void setBackgroundResource(int resid) {
12128 if (resid != 0 && resid == mBackgroundResource) {
12129 return;
12130 }
12131
12132 Drawable d= null;
12133 if (resid != 0) {
12134 d = mResources.getDrawable(resid);
12135 }
12136 setBackgroundDrawable(d);
12137
12138 mBackgroundResource = resid;
12139 }
12140
12141 /**
12142 * Set the background to a given Drawable, or remove the background. If the
12143 * background has padding, this View's padding is set to the background's
12144 * padding. However, when a background is removed, this View's padding isn't
12145 * touched. If setting the padding is desired, please use
12146 * {@link #setPadding(int, int, int, int)}.
12147 *
12148 * @param d The Drawable to use as the background, or null to remove the
12149 * background
12150 */
12151 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012152 if (d == mBGDrawable) {
12153 return;
12154 }
12155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012156 boolean requestLayout = false;
12157
12158 mBackgroundResource = 0;
12159
12160 /*
12161 * Regardless of whether we're setting a new background or not, we want
12162 * to clear the previous drawable.
12163 */
12164 if (mBGDrawable != null) {
12165 mBGDrawable.setCallback(null);
12166 unscheduleDrawable(mBGDrawable);
12167 }
12168
12169 if (d != null) {
12170 Rect padding = sThreadLocal.get();
12171 if (padding == null) {
12172 padding = new Rect();
12173 sThreadLocal.set(padding);
12174 }
12175 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012176 switch (d.getResolvedLayoutDirectionSelf()) {
12177 case LAYOUT_DIRECTION_RTL:
12178 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12179 break;
12180 case LAYOUT_DIRECTION_LTR:
12181 default:
12182 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012184 }
12185
12186 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12187 // if it has a different minimum size, we should layout again
12188 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12189 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12190 requestLayout = true;
12191 }
12192
12193 d.setCallback(this);
12194 if (d.isStateful()) {
12195 d.setState(getDrawableState());
12196 }
12197 d.setVisible(getVisibility() == VISIBLE, false);
12198 mBGDrawable = d;
12199
12200 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12201 mPrivateFlags &= ~SKIP_DRAW;
12202 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12203 requestLayout = true;
12204 }
12205 } else {
12206 /* Remove the background */
12207 mBGDrawable = null;
12208
12209 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12210 /*
12211 * This view ONLY drew the background before and we're removing
12212 * the background, so now it won't draw anything
12213 * (hence we SKIP_DRAW)
12214 */
12215 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12216 mPrivateFlags |= SKIP_DRAW;
12217 }
12218
12219 /*
12220 * When the background is set, we try to apply its padding to this
12221 * View. When the background is removed, we don't touch this View's
12222 * padding. This is noted in the Javadocs. Hence, we don't need to
12223 * requestLayout(), the invalidate() below is sufficient.
12224 */
12225
12226 // The old background's minimum size could have affected this
12227 // View's layout, so let's requestLayout
12228 requestLayout = true;
12229 }
12230
Romain Guy8f1344f52009-05-15 16:03:59 -070012231 computeOpaqueFlags();
12232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012233 if (requestLayout) {
12234 requestLayout();
12235 }
12236
12237 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012238 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012239 }
12240
12241 /**
12242 * Gets the background drawable
12243 * @return The drawable used as the background for this view, if any.
12244 */
12245 public Drawable getBackground() {
12246 return mBGDrawable;
12247 }
12248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012249 /**
12250 * Sets the padding. The view may add on the space required to display
12251 * the scrollbars, depending on the style and visibility of the scrollbars.
12252 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12253 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12254 * from the values set in this call.
12255 *
12256 * @attr ref android.R.styleable#View_padding
12257 * @attr ref android.R.styleable#View_paddingBottom
12258 * @attr ref android.R.styleable#View_paddingLeft
12259 * @attr ref android.R.styleable#View_paddingRight
12260 * @attr ref android.R.styleable#View_paddingTop
12261 * @param left the left padding in pixels
12262 * @param top the top padding in pixels
12263 * @param right the right padding in pixels
12264 * @param bottom the bottom padding in pixels
12265 */
12266 public void setPadding(int left, int top, int right, int bottom) {
12267 boolean changed = false;
12268
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012269 mUserPaddingRelative = false;
12270
Adam Powell20232d02010-12-08 21:08:53 -080012271 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012272 mUserPaddingRight = right;
12273 mUserPaddingBottom = bottom;
12274
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012275 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070012276
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012277 // Common case is there are no scroll bars.
12278 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012279 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012280 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012281 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012282 switch (mVerticalScrollbarPosition) {
12283 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012284 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12285 left += offset;
12286 } else {
12287 right += offset;
12288 }
12289 break;
Adam Powell20232d02010-12-08 21:08:53 -080012290 case SCROLLBAR_POSITION_RIGHT:
12291 right += offset;
12292 break;
12293 case SCROLLBAR_POSITION_LEFT:
12294 left += offset;
12295 break;
12296 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012297 }
Adam Powell20232d02010-12-08 21:08:53 -080012298 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012299 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12300 ? 0 : getHorizontalScrollbarHeight();
12301 }
12302 }
Romain Guy8506ab42009-06-11 17:35:47 -070012303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012304 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012305 changed = true;
12306 mPaddingLeft = left;
12307 }
12308 if (mPaddingTop != top) {
12309 changed = true;
12310 mPaddingTop = top;
12311 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012312 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012313 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012314 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012315 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012316 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012317 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012318 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012319 }
12320
12321 if (changed) {
12322 requestLayout();
12323 }
12324 }
12325
12326 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012327 * Sets the relative padding. The view may add on the space required to display
12328 * the scrollbars, depending on the style and visibility of the scrollbars.
12329 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12330 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12331 * from the values set in this call.
12332 *
12333 * @attr ref android.R.styleable#View_padding
12334 * @attr ref android.R.styleable#View_paddingBottom
12335 * @attr ref android.R.styleable#View_paddingStart
12336 * @attr ref android.R.styleable#View_paddingEnd
12337 * @attr ref android.R.styleable#View_paddingTop
12338 * @param start the start padding in pixels
12339 * @param top the top padding in pixels
12340 * @param end the end padding in pixels
12341 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012342 */
12343 public void setPaddingRelative(int start, int top, int end, int bottom) {
12344 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012345
12346 mUserPaddingStart = start;
12347 mUserPaddingEnd = end;
12348
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012349 switch(getResolvedLayoutDirection()) {
12350 case LAYOUT_DIRECTION_RTL:
12351 setPadding(end, top, start, bottom);
12352 break;
12353 case LAYOUT_DIRECTION_LTR:
12354 default:
12355 setPadding(start, top, end, bottom);
12356 }
12357 }
12358
12359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012360 * Returns the top padding of this view.
12361 *
12362 * @return the top padding in pixels
12363 */
12364 public int getPaddingTop() {
12365 return mPaddingTop;
12366 }
12367
12368 /**
12369 * Returns the bottom padding of this view. If there are inset and enabled
12370 * scrollbars, this value may include the space required to display the
12371 * scrollbars as well.
12372 *
12373 * @return the bottom padding in pixels
12374 */
12375 public int getPaddingBottom() {
12376 return mPaddingBottom;
12377 }
12378
12379 /**
12380 * Returns the left padding of this view. If there are inset and enabled
12381 * scrollbars, this value may include the space required to display the
12382 * scrollbars as well.
12383 *
12384 * @return the left padding in pixels
12385 */
12386 public int getPaddingLeft() {
12387 return mPaddingLeft;
12388 }
12389
12390 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012391 * Returns the start padding of this view. If there are inset and enabled
12392 * scrollbars, this value may include the space required to display the
12393 * scrollbars as well.
12394 *
12395 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012396 */
12397 public int getPaddingStart() {
12398 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12399 mPaddingRight : mPaddingLeft;
12400 }
12401
12402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012403 * Returns the right padding of this view. If there are inset and enabled
12404 * scrollbars, this value may include the space required to display the
12405 * scrollbars as well.
12406 *
12407 * @return the right padding in pixels
12408 */
12409 public int getPaddingRight() {
12410 return mPaddingRight;
12411 }
12412
12413 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012414 * Returns the end padding of this view. If there are inset and enabled
12415 * scrollbars, this value may include the space required to display the
12416 * scrollbars as well.
12417 *
12418 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012419 */
12420 public int getPaddingEnd() {
12421 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12422 mPaddingLeft : mPaddingRight;
12423 }
12424
12425 /**
12426 * Return if the padding as been set thru relative values
12427 * {@link #setPaddingRelative(int, int, int, int)} or thru
12428 * @attr ref android.R.styleable#View_paddingStart or
12429 * @attr ref android.R.styleable#View_paddingEnd
12430 *
12431 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012432 */
12433 public boolean isPaddingRelative() {
12434 return mUserPaddingRelative;
12435 }
12436
12437 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012438 * Changes the selection state of this view. A view can be selected or not.
12439 * Note that selection is not the same as focus. Views are typically
12440 * selected in the context of an AdapterView like ListView or GridView;
12441 * the selected view is the view that is highlighted.
12442 *
12443 * @param selected true if the view must be selected, false otherwise
12444 */
12445 public void setSelected(boolean selected) {
12446 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12447 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012448 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012449 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012450 refreshDrawableState();
12451 dispatchSetSelected(selected);
12452 }
12453 }
12454
12455 /**
12456 * Dispatch setSelected to all of this View's children.
12457 *
12458 * @see #setSelected(boolean)
12459 *
12460 * @param selected The new selected state
12461 */
12462 protected void dispatchSetSelected(boolean selected) {
12463 }
12464
12465 /**
12466 * Indicates the selection state of this view.
12467 *
12468 * @return true if the view is selected, false otherwise
12469 */
12470 @ViewDebug.ExportedProperty
12471 public boolean isSelected() {
12472 return (mPrivateFlags & SELECTED) != 0;
12473 }
12474
12475 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012476 * Changes the activated state of this view. A view can be activated or not.
12477 * Note that activation is not the same as selection. Selection is
12478 * a transient property, representing the view (hierarchy) the user is
12479 * currently interacting with. Activation is a longer-term state that the
12480 * user can move views in and out of. For example, in a list view with
12481 * single or multiple selection enabled, the views in the current selection
12482 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12483 * here.) The activated state is propagated down to children of the view it
12484 * is set on.
12485 *
12486 * @param activated true if the view must be activated, false otherwise
12487 */
12488 public void setActivated(boolean activated) {
12489 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12490 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012491 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012492 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012493 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012494 }
12495 }
12496
12497 /**
12498 * Dispatch setActivated to all of this View's children.
12499 *
12500 * @see #setActivated(boolean)
12501 *
12502 * @param activated The new activated state
12503 */
12504 protected void dispatchSetActivated(boolean activated) {
12505 }
12506
12507 /**
12508 * Indicates the activation state of this view.
12509 *
12510 * @return true if the view is activated, false otherwise
12511 */
12512 @ViewDebug.ExportedProperty
12513 public boolean isActivated() {
12514 return (mPrivateFlags & ACTIVATED) != 0;
12515 }
12516
12517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012518 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12519 * observer can be used to get notifications when global events, like
12520 * layout, happen.
12521 *
12522 * The returned ViewTreeObserver observer is not guaranteed to remain
12523 * valid for the lifetime of this View. If the caller of this method keeps
12524 * a long-lived reference to ViewTreeObserver, it should always check for
12525 * the return value of {@link ViewTreeObserver#isAlive()}.
12526 *
12527 * @return The ViewTreeObserver for this view's hierarchy.
12528 */
12529 public ViewTreeObserver getViewTreeObserver() {
12530 if (mAttachInfo != null) {
12531 return mAttachInfo.mTreeObserver;
12532 }
12533 if (mFloatingTreeObserver == null) {
12534 mFloatingTreeObserver = new ViewTreeObserver();
12535 }
12536 return mFloatingTreeObserver;
12537 }
12538
12539 /**
12540 * <p>Finds the topmost view in the current view hierarchy.</p>
12541 *
12542 * @return the topmost view containing this view
12543 */
12544 public View getRootView() {
12545 if (mAttachInfo != null) {
12546 final View v = mAttachInfo.mRootView;
12547 if (v != null) {
12548 return v;
12549 }
12550 }
Romain Guy8506ab42009-06-11 17:35:47 -070012551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012552 View parent = this;
12553
12554 while (parent.mParent != null && parent.mParent instanceof View) {
12555 parent = (View) parent.mParent;
12556 }
12557
12558 return parent;
12559 }
12560
12561 /**
12562 * <p>Computes the coordinates of this view on the screen. The argument
12563 * must be an array of two integers. After the method returns, the array
12564 * contains the x and y location in that order.</p>
12565 *
12566 * @param location an array of two integers in which to hold the coordinates
12567 */
12568 public void getLocationOnScreen(int[] location) {
12569 getLocationInWindow(location);
12570
12571 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012572 if (info != null) {
12573 location[0] += info.mWindowLeft;
12574 location[1] += info.mWindowTop;
12575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012576 }
12577
12578 /**
12579 * <p>Computes the coordinates of this view in its window. The argument
12580 * must be an array of two integers. After the method returns, the array
12581 * contains the x and y location in that order.</p>
12582 *
12583 * @param location an array of two integers in which to hold the coordinates
12584 */
12585 public void getLocationInWindow(int[] location) {
12586 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012587 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012588 }
12589
Gilles Debunne6583ce52011-12-06 18:09:02 -080012590 if (mAttachInfo == null) {
12591 // When the view is not attached to a window, this method does not make sense
12592 location[0] = location[1] = 0;
12593 return;
12594 }
12595
Gilles Debunnecea45132011-11-24 02:19:27 +010012596 float[] position = mAttachInfo.mTmpTransformLocation;
12597 position[0] = position[1] = 0.0f;
12598
12599 if (!hasIdentityMatrix()) {
12600 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012602
Gilles Debunnecea45132011-11-24 02:19:27 +010012603 position[0] += mLeft;
12604 position[1] += mTop;
12605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012606 ViewParent viewParent = mParent;
12607 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012608 final View view = (View) viewParent;
12609
12610 position[0] -= view.mScrollX;
12611 position[1] -= view.mScrollY;
12612
12613 if (!view.hasIdentityMatrix()) {
12614 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012615 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012616
12617 position[0] += view.mLeft;
12618 position[1] += view.mTop;
12619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012620 viewParent = view.mParent;
12621 }
Romain Guy8506ab42009-06-11 17:35:47 -070012622
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012623 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012624 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012625 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12626 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012627 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012628
12629 location[0] = (int) (position[0] + 0.5f);
12630 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012631 }
12632
12633 /**
12634 * {@hide}
12635 * @param id the id of the view to be found
12636 * @return the view of the specified id, null if cannot be found
12637 */
12638 protected View findViewTraversal(int id) {
12639 if (id == mID) {
12640 return this;
12641 }
12642 return null;
12643 }
12644
12645 /**
12646 * {@hide}
12647 * @param tag the tag of the view to be found
12648 * @return the view of specified tag, null if cannot be found
12649 */
12650 protected View findViewWithTagTraversal(Object tag) {
12651 if (tag != null && tag.equals(mTag)) {
12652 return this;
12653 }
12654 return null;
12655 }
12656
12657 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012658 * {@hide}
12659 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012660 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012661 * @return The first view that matches the predicate or null.
12662 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012663 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012664 if (predicate.apply(this)) {
12665 return this;
12666 }
12667 return null;
12668 }
12669
12670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012671 * Look for a child view with the given id. If this view has the given
12672 * id, return this view.
12673 *
12674 * @param id The id to search for.
12675 * @return The view that has the given id in the hierarchy or null
12676 */
12677 public final View findViewById(int id) {
12678 if (id < 0) {
12679 return null;
12680 }
12681 return findViewTraversal(id);
12682 }
12683
12684 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012685 * Finds a view by its unuque and stable accessibility id.
12686 *
12687 * @param accessibilityId The searched accessibility id.
12688 * @return The found view.
12689 */
12690 final View findViewByAccessibilityId(int accessibilityId) {
12691 if (accessibilityId < 0) {
12692 return null;
12693 }
12694 return findViewByAccessibilityIdTraversal(accessibilityId);
12695 }
12696
12697 /**
12698 * Performs the traversal to find a view by its unuque and stable accessibility id.
12699 *
12700 * <strong>Note:</strong>This method does not stop at the root namespace
12701 * boundary since the user can touch the screen at an arbitrary location
12702 * potentially crossing the root namespace bounday which will send an
12703 * accessibility event to accessibility services and they should be able
12704 * to obtain the event source. Also accessibility ids are guaranteed to be
12705 * unique in the window.
12706 *
12707 * @param accessibilityId The accessibility id.
12708 * @return The found view.
12709 */
12710 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12711 if (getAccessibilityViewId() == accessibilityId) {
12712 return this;
12713 }
12714 return null;
12715 }
12716
12717 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012718 * Look for a child view with the given tag. If this view has the given
12719 * tag, return this view.
12720 *
12721 * @param tag The tag to search for, using "tag.equals(getTag())".
12722 * @return The View that has the given tag in the hierarchy or null
12723 */
12724 public final View findViewWithTag(Object tag) {
12725 if (tag == null) {
12726 return null;
12727 }
12728 return findViewWithTagTraversal(tag);
12729 }
12730
12731 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012732 * {@hide}
12733 * Look for a child view that matches the specified predicate.
12734 * If this view matches the predicate, return this view.
12735 *
12736 * @param predicate The predicate to evaluate.
12737 * @return The first view that matches the predicate or null.
12738 */
12739 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012740 return findViewByPredicateTraversal(predicate, null);
12741 }
12742
12743 /**
12744 * {@hide}
12745 * Look for a child view that matches the specified predicate,
12746 * starting with the specified view and its descendents and then
12747 * recusively searching the ancestors and siblings of that view
12748 * until this view is reached.
12749 *
12750 * This method is useful in cases where the predicate does not match
12751 * a single unique view (perhaps multiple views use the same id)
12752 * and we are trying to find the view that is "closest" in scope to the
12753 * starting view.
12754 *
12755 * @param start The view to start from.
12756 * @param predicate The predicate to evaluate.
12757 * @return The first view that matches the predicate or null.
12758 */
12759 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12760 View childToSkip = null;
12761 for (;;) {
12762 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12763 if (view != null || start == this) {
12764 return view;
12765 }
12766
12767 ViewParent parent = start.getParent();
12768 if (parent == null || !(parent instanceof View)) {
12769 return null;
12770 }
12771
12772 childToSkip = start;
12773 start = (View) parent;
12774 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012775 }
12776
12777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012778 * Sets the identifier for this view. The identifier does not have to be
12779 * unique in this view's hierarchy. The identifier should be a positive
12780 * number.
12781 *
12782 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012783 * @see #getId()
12784 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012785 *
12786 * @param id a number used to identify the view
12787 *
12788 * @attr ref android.R.styleable#View_id
12789 */
12790 public void setId(int id) {
12791 mID = id;
12792 }
12793
12794 /**
12795 * {@hide}
12796 *
12797 * @param isRoot true if the view belongs to the root namespace, false
12798 * otherwise
12799 */
12800 public void setIsRootNamespace(boolean isRoot) {
12801 if (isRoot) {
12802 mPrivateFlags |= IS_ROOT_NAMESPACE;
12803 } else {
12804 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12805 }
12806 }
12807
12808 /**
12809 * {@hide}
12810 *
12811 * @return true if the view belongs to the root namespace, false otherwise
12812 */
12813 public boolean isRootNamespace() {
12814 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12815 }
12816
12817 /**
12818 * Returns this view's identifier.
12819 *
12820 * @return a positive integer used to identify the view or {@link #NO_ID}
12821 * if the view has no ID
12822 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012823 * @see #setId(int)
12824 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012825 * @attr ref android.R.styleable#View_id
12826 */
12827 @ViewDebug.CapturedViewProperty
12828 public int getId() {
12829 return mID;
12830 }
12831
12832 /**
12833 * Returns this view's tag.
12834 *
12835 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012836 *
12837 * @see #setTag(Object)
12838 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012839 */
12840 @ViewDebug.ExportedProperty
12841 public Object getTag() {
12842 return mTag;
12843 }
12844
12845 /**
12846 * Sets the tag associated with this view. A tag can be used to mark
12847 * a view in its hierarchy and does not have to be unique within the
12848 * hierarchy. Tags can also be used to store data within a view without
12849 * resorting to another data structure.
12850 *
12851 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012852 *
12853 * @see #getTag()
12854 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012855 */
12856 public void setTag(final Object tag) {
12857 mTag = tag;
12858 }
12859
12860 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012861 * Returns the tag associated with this view and the specified key.
12862 *
12863 * @param key The key identifying the tag
12864 *
12865 * @return the Object stored in this view as a tag
12866 *
12867 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012868 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012869 */
12870 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012871 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012872 return null;
12873 }
12874
12875 /**
12876 * Sets a tag associated with this view and a key. A tag can be used
12877 * to mark a view in its hierarchy and does not have to be unique within
12878 * the hierarchy. Tags can also be used to store data within a view
12879 * without resorting to another data structure.
12880 *
12881 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012882 * application to ensure it is unique (see the <a
12883 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12884 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012885 * the Android framework or not associated with any package will cause
12886 * an {@link IllegalArgumentException} to be thrown.
12887 *
12888 * @param key The key identifying the tag
12889 * @param tag An Object to tag the view with
12890 *
12891 * @throws IllegalArgumentException If they specified key is not valid
12892 *
12893 * @see #setTag(Object)
12894 * @see #getTag(int)
12895 */
12896 public void setTag(int key, final Object tag) {
12897 // If the package id is 0x00 or 0x01, it's either an undefined package
12898 // or a framework id
12899 if ((key >>> 24) < 2) {
12900 throw new IllegalArgumentException("The key must be an application-specific "
12901 + "resource id.");
12902 }
12903
Adam Powell2b2f6d62011-09-23 11:15:39 -070012904 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012905 }
12906
12907 /**
12908 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12909 * framework id.
12910 *
12911 * @hide
12912 */
12913 public void setTagInternal(int key, Object tag) {
12914 if ((key >>> 24) != 0x1) {
12915 throw new IllegalArgumentException("The key must be a framework-specific "
12916 + "resource id.");
12917 }
12918
Adam Powell2b2f6d62011-09-23 11:15:39 -070012919 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012920 }
12921
Adam Powell2b2f6d62011-09-23 11:15:39 -070012922 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012923 if (mKeyedTags == null) {
12924 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012925 }
12926
Adam Powell7db82ac2011-09-22 19:44:04 -070012927 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012928 }
12929
12930 /**
Romain Guy13922e02009-05-12 17:56:14 -070012931 * @param consistency The type of consistency. See ViewDebug for more information.
12932 *
12933 * @hide
12934 */
12935 protected boolean dispatchConsistencyCheck(int consistency) {
12936 return onConsistencyCheck(consistency);
12937 }
12938
12939 /**
12940 * Method that subclasses should implement to check their consistency. The type of
12941 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012942 *
Romain Guy13922e02009-05-12 17:56:14 -070012943 * @param consistency The type of consistency. See ViewDebug for more information.
12944 *
12945 * @throws IllegalStateException if the view is in an inconsistent state.
12946 *
12947 * @hide
12948 */
12949 protected boolean onConsistencyCheck(int consistency) {
12950 boolean result = true;
12951
12952 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12953 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12954
12955 if (checkLayout) {
12956 if (getParent() == null) {
12957 result = false;
12958 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12959 "View " + this + " does not have a parent.");
12960 }
12961
12962 if (mAttachInfo == null) {
12963 result = false;
12964 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12965 "View " + this + " is not attached to a window.");
12966 }
12967 }
12968
12969 if (checkDrawing) {
12970 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12971 // from their draw() method
12972
12973 if ((mPrivateFlags & DRAWN) != DRAWN &&
12974 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12975 result = false;
12976 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12977 "View " + this + " was invalidated but its drawing cache is valid.");
12978 }
12979 }
12980
12981 return result;
12982 }
12983
12984 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012985 * Prints information about this view in the log output, with the tag
12986 * {@link #VIEW_LOG_TAG}.
12987 *
12988 * @hide
12989 */
12990 public void debug() {
12991 debug(0);
12992 }
12993
12994 /**
12995 * Prints information about this view in the log output, with the tag
12996 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12997 * indentation defined by the <code>depth</code>.
12998 *
12999 * @param depth the indentation level
13000 *
13001 * @hide
13002 */
13003 protected void debug(int depth) {
13004 String output = debugIndent(depth - 1);
13005
13006 output += "+ " + this;
13007 int id = getId();
13008 if (id != -1) {
13009 output += " (id=" + id + ")";
13010 }
13011 Object tag = getTag();
13012 if (tag != null) {
13013 output += " (tag=" + tag + ")";
13014 }
13015 Log.d(VIEW_LOG_TAG, output);
13016
13017 if ((mPrivateFlags & FOCUSED) != 0) {
13018 output = debugIndent(depth) + " FOCUSED";
13019 Log.d(VIEW_LOG_TAG, output);
13020 }
13021
13022 output = debugIndent(depth);
13023 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13024 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13025 + "} ";
13026 Log.d(VIEW_LOG_TAG, output);
13027
13028 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13029 || mPaddingBottom != 0) {
13030 output = debugIndent(depth);
13031 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13032 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13033 Log.d(VIEW_LOG_TAG, output);
13034 }
13035
13036 output = debugIndent(depth);
13037 output += "mMeasureWidth=" + mMeasuredWidth +
13038 " mMeasureHeight=" + mMeasuredHeight;
13039 Log.d(VIEW_LOG_TAG, output);
13040
13041 output = debugIndent(depth);
13042 if (mLayoutParams == null) {
13043 output += "BAD! no layout params";
13044 } else {
13045 output = mLayoutParams.debug(output);
13046 }
13047 Log.d(VIEW_LOG_TAG, output);
13048
13049 output = debugIndent(depth);
13050 output += "flags={";
13051 output += View.printFlags(mViewFlags);
13052 output += "}";
13053 Log.d(VIEW_LOG_TAG, output);
13054
13055 output = debugIndent(depth);
13056 output += "privateFlags={";
13057 output += View.printPrivateFlags(mPrivateFlags);
13058 output += "}";
13059 Log.d(VIEW_LOG_TAG, output);
13060 }
13061
13062 /**
13063 * Creates an string of whitespaces used for indentation.
13064 *
13065 * @param depth the indentation level
13066 * @return a String containing (depth * 2 + 3) * 2 white spaces
13067 *
13068 * @hide
13069 */
13070 protected static String debugIndent(int depth) {
13071 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13072 for (int i = 0; i < (depth * 2) + 3; i++) {
13073 spaces.append(' ').append(' ');
13074 }
13075 return spaces.toString();
13076 }
13077
13078 /**
13079 * <p>Return the offset of the widget's text baseline from the widget's top
13080 * boundary. If this widget does not support baseline alignment, this
13081 * method returns -1. </p>
13082 *
13083 * @return the offset of the baseline within the widget's bounds or -1
13084 * if baseline alignment is not supported
13085 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013086 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013087 public int getBaseline() {
13088 return -1;
13089 }
13090
13091 /**
13092 * Call this when something has changed which has invalidated the
13093 * layout of this view. This will schedule a layout pass of the view
13094 * tree.
13095 */
13096 public void requestLayout() {
13097 if (ViewDebug.TRACE_HIERARCHY) {
13098 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13099 }
13100
13101 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013102 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013103
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013104 if (mParent != null) {
13105 if (mLayoutParams != null) {
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080013106 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013107 }
13108 if (!mParent.isLayoutRequested()) {
13109 mParent.requestLayout();
13110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013111 }
13112 }
13113
13114 /**
13115 * Forces this view to be laid out during the next layout pass.
13116 * This method does not call requestLayout() or forceLayout()
13117 * on the parent.
13118 */
13119 public void forceLayout() {
13120 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013121 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013122 }
13123
13124 /**
13125 * <p>
13126 * This is called to find out how big a view should be. The parent
13127 * supplies constraint information in the width and height parameters.
13128 * </p>
13129 *
13130 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013131 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013132 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013133 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013134 * </p>
13135 *
13136 *
13137 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13138 * parent
13139 * @param heightMeasureSpec Vertical space requirements as imposed by the
13140 * parent
13141 *
13142 * @see #onMeasure(int, int)
13143 */
13144 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13145 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13146 widthMeasureSpec != mOldWidthMeasureSpec ||
13147 heightMeasureSpec != mOldHeightMeasureSpec) {
13148
13149 // first clears the measured dimension flag
13150 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13151
13152 if (ViewDebug.TRACE_HIERARCHY) {
13153 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13154 }
13155
13156 // measure ourselves, this should set the measured dimension flag back
13157 onMeasure(widthMeasureSpec, heightMeasureSpec);
13158
13159 // flag not set, setMeasuredDimension() was not invoked, we raise
13160 // an exception to warn the developer
13161 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13162 throw new IllegalStateException("onMeasure() did not set the"
13163 + " measured dimension by calling"
13164 + " setMeasuredDimension()");
13165 }
13166
13167 mPrivateFlags |= LAYOUT_REQUIRED;
13168 }
13169
13170 mOldWidthMeasureSpec = widthMeasureSpec;
13171 mOldHeightMeasureSpec = heightMeasureSpec;
13172 }
13173
13174 /**
13175 * <p>
13176 * Measure the view and its content to determine the measured width and the
13177 * measured height. This method is invoked by {@link #measure(int, int)} and
13178 * should be overriden by subclasses to provide accurate and efficient
13179 * measurement of their contents.
13180 * </p>
13181 *
13182 * <p>
13183 * <strong>CONTRACT:</strong> When overriding this method, you
13184 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13185 * measured width and height of this view. Failure to do so will trigger an
13186 * <code>IllegalStateException</code>, thrown by
13187 * {@link #measure(int, int)}. Calling the superclass'
13188 * {@link #onMeasure(int, int)} is a valid use.
13189 * </p>
13190 *
13191 * <p>
13192 * The base class implementation of measure defaults to the background size,
13193 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13194 * override {@link #onMeasure(int, int)} to provide better measurements of
13195 * their content.
13196 * </p>
13197 *
13198 * <p>
13199 * If this method is overridden, it is the subclass's responsibility to make
13200 * sure the measured height and width are at least the view's minimum height
13201 * and width ({@link #getSuggestedMinimumHeight()} and
13202 * {@link #getSuggestedMinimumWidth()}).
13203 * </p>
13204 *
13205 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13206 * The requirements are encoded with
13207 * {@link android.view.View.MeasureSpec}.
13208 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13209 * The requirements are encoded with
13210 * {@link android.view.View.MeasureSpec}.
13211 *
13212 * @see #getMeasuredWidth()
13213 * @see #getMeasuredHeight()
13214 * @see #setMeasuredDimension(int, int)
13215 * @see #getSuggestedMinimumHeight()
13216 * @see #getSuggestedMinimumWidth()
13217 * @see android.view.View.MeasureSpec#getMode(int)
13218 * @see android.view.View.MeasureSpec#getSize(int)
13219 */
13220 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13221 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13222 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13223 }
13224
13225 /**
13226 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13227 * measured width and measured height. Failing to do so will trigger an
13228 * exception at measurement time.</p>
13229 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013230 * @param measuredWidth The measured width of this view. May be a complex
13231 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13232 * {@link #MEASURED_STATE_TOO_SMALL}.
13233 * @param measuredHeight The measured height of this view. May be a complex
13234 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13235 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013236 */
13237 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13238 mMeasuredWidth = measuredWidth;
13239 mMeasuredHeight = measuredHeight;
13240
13241 mPrivateFlags |= MEASURED_DIMENSION_SET;
13242 }
13243
13244 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013245 * Merge two states as returned by {@link #getMeasuredState()}.
13246 * @param curState The current state as returned from a view or the result
13247 * of combining multiple views.
13248 * @param newState The new view state to combine.
13249 * @return Returns a new integer reflecting the combination of the two
13250 * states.
13251 */
13252 public static int combineMeasuredStates(int curState, int newState) {
13253 return curState | newState;
13254 }
13255
13256 /**
13257 * Version of {@link #resolveSizeAndState(int, int, int)}
13258 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13259 */
13260 public static int resolveSize(int size, int measureSpec) {
13261 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13262 }
13263
13264 /**
13265 * Utility to reconcile a desired size and state, with constraints imposed
13266 * by a MeasureSpec. Will take the desired size, unless a different size
13267 * is imposed by the constraints. The returned value is a compound integer,
13268 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13269 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13270 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013271 *
13272 * @param size How big the view wants to be
13273 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013274 * @return Size information bit mask as defined by
13275 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013276 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013277 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013278 int result = size;
13279 int specMode = MeasureSpec.getMode(measureSpec);
13280 int specSize = MeasureSpec.getSize(measureSpec);
13281 switch (specMode) {
13282 case MeasureSpec.UNSPECIFIED:
13283 result = size;
13284 break;
13285 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013286 if (specSize < size) {
13287 result = specSize | MEASURED_STATE_TOO_SMALL;
13288 } else {
13289 result = size;
13290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013291 break;
13292 case MeasureSpec.EXACTLY:
13293 result = specSize;
13294 break;
13295 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013296 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013297 }
13298
13299 /**
13300 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013301 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013302 * by the MeasureSpec.
13303 *
13304 * @param size Default size for this view
13305 * @param measureSpec Constraints imposed by the parent
13306 * @return The size this view should be.
13307 */
13308 public static int getDefaultSize(int size, int measureSpec) {
13309 int result = size;
13310 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013311 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013312
13313 switch (specMode) {
13314 case MeasureSpec.UNSPECIFIED:
13315 result = size;
13316 break;
13317 case MeasureSpec.AT_MOST:
13318 case MeasureSpec.EXACTLY:
13319 result = specSize;
13320 break;
13321 }
13322 return result;
13323 }
13324
13325 /**
13326 * Returns the suggested minimum height that the view should use. This
13327 * returns the maximum of the view's minimum height
13328 * and the background's minimum height
13329 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13330 * <p>
13331 * When being used in {@link #onMeasure(int, int)}, the caller should still
13332 * ensure the returned height is within the requirements of the parent.
13333 *
13334 * @return The suggested minimum height of the view.
13335 */
13336 protected int getSuggestedMinimumHeight() {
13337 int suggestedMinHeight = mMinHeight;
13338
13339 if (mBGDrawable != null) {
13340 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13341 if (suggestedMinHeight < bgMinHeight) {
13342 suggestedMinHeight = bgMinHeight;
13343 }
13344 }
13345
13346 return suggestedMinHeight;
13347 }
13348
13349 /**
13350 * Returns the suggested minimum width that the view should use. This
13351 * returns the maximum of the view's minimum width)
13352 * and the background's minimum width
13353 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13354 * <p>
13355 * When being used in {@link #onMeasure(int, int)}, the caller should still
13356 * ensure the returned width is within the requirements of the parent.
13357 *
13358 * @return The suggested minimum width of the view.
13359 */
13360 protected int getSuggestedMinimumWidth() {
13361 int suggestedMinWidth = mMinWidth;
13362
13363 if (mBGDrawable != null) {
13364 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13365 if (suggestedMinWidth < bgMinWidth) {
13366 suggestedMinWidth = bgMinWidth;
13367 }
13368 }
13369
13370 return suggestedMinWidth;
13371 }
13372
13373 /**
13374 * Sets the minimum height of the view. It is not guaranteed the view will
13375 * be able to achieve this minimum height (for example, if its parent layout
13376 * constrains it with less available height).
13377 *
13378 * @param minHeight The minimum height the view will try to be.
13379 */
13380 public void setMinimumHeight(int minHeight) {
13381 mMinHeight = minHeight;
13382 }
13383
13384 /**
13385 * Sets the minimum width of the view. It is not guaranteed the view will
13386 * be able to achieve this minimum width (for example, if its parent layout
13387 * constrains it with less available width).
13388 *
13389 * @param minWidth The minimum width the view will try to be.
13390 */
13391 public void setMinimumWidth(int minWidth) {
13392 mMinWidth = minWidth;
13393 }
13394
13395 /**
13396 * Get the animation currently associated with this view.
13397 *
13398 * @return The animation that is currently playing or
13399 * scheduled to play for this view.
13400 */
13401 public Animation getAnimation() {
13402 return mCurrentAnimation;
13403 }
13404
13405 /**
13406 * Start the specified animation now.
13407 *
13408 * @param animation the animation to start now
13409 */
13410 public void startAnimation(Animation animation) {
13411 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13412 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013413 invalidateParentCaches();
13414 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013415 }
13416
13417 /**
13418 * Cancels any animations for this view.
13419 */
13420 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013421 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013422 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013424 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013425 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013426 }
13427
13428 /**
13429 * Sets the next animation to play for this view.
13430 * If you want the animation to play immediately, use
13431 * startAnimation. This method provides allows fine-grained
13432 * control over the start time and invalidation, but you
13433 * must make sure that 1) the animation has a start time set, and
13434 * 2) the view will be invalidated when the animation is supposed to
13435 * start.
13436 *
13437 * @param animation The next animation, or null.
13438 */
13439 public void setAnimation(Animation animation) {
13440 mCurrentAnimation = animation;
13441 if (animation != null) {
13442 animation.reset();
13443 }
13444 }
13445
13446 /**
13447 * Invoked by a parent ViewGroup to notify the start of the animation
13448 * currently associated with this view. If you override this method,
13449 * always call super.onAnimationStart();
13450 *
13451 * @see #setAnimation(android.view.animation.Animation)
13452 * @see #getAnimation()
13453 */
13454 protected void onAnimationStart() {
13455 mPrivateFlags |= ANIMATION_STARTED;
13456 }
13457
13458 /**
13459 * Invoked by a parent ViewGroup to notify the end of the animation
13460 * currently associated with this view. If you override this method,
13461 * always call super.onAnimationEnd();
13462 *
13463 * @see #setAnimation(android.view.animation.Animation)
13464 * @see #getAnimation()
13465 */
13466 protected void onAnimationEnd() {
13467 mPrivateFlags &= ~ANIMATION_STARTED;
13468 }
13469
13470 /**
13471 * Invoked if there is a Transform that involves alpha. Subclass that can
13472 * draw themselves with the specified alpha should return true, and then
13473 * respect that alpha when their onDraw() is called. If this returns false
13474 * then the view may be redirected to draw into an offscreen buffer to
13475 * fulfill the request, which will look fine, but may be slower than if the
13476 * subclass handles it internally. The default implementation returns false.
13477 *
13478 * @param alpha The alpha (0..255) to apply to the view's drawing
13479 * @return true if the view can draw with the specified alpha.
13480 */
13481 protected boolean onSetAlpha(int alpha) {
13482 return false;
13483 }
13484
13485 /**
13486 * This is used by the RootView to perform an optimization when
13487 * the view hierarchy contains one or several SurfaceView.
13488 * SurfaceView is always considered transparent, but its children are not,
13489 * therefore all View objects remove themselves from the global transparent
13490 * region (passed as a parameter to this function).
13491 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013492 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013493 *
13494 * @return Returns true if the effective visibility of the view at this
13495 * point is opaque, regardless of the transparent region; returns false
13496 * if it is possible for underlying windows to be seen behind the view.
13497 *
13498 * {@hide}
13499 */
13500 public boolean gatherTransparentRegion(Region region) {
13501 final AttachInfo attachInfo = mAttachInfo;
13502 if (region != null && attachInfo != null) {
13503 final int pflags = mPrivateFlags;
13504 if ((pflags & SKIP_DRAW) == 0) {
13505 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13506 // remove it from the transparent region.
13507 final int[] location = attachInfo.mTransparentLocation;
13508 getLocationInWindow(location);
13509 region.op(location[0], location[1], location[0] + mRight - mLeft,
13510 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13511 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13512 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13513 // exists, so we remove the background drawable's non-transparent
13514 // parts from this transparent region.
13515 applyDrawableToTransparentRegion(mBGDrawable, region);
13516 }
13517 }
13518 return true;
13519 }
13520
13521 /**
13522 * Play a sound effect for this view.
13523 *
13524 * <p>The framework will play sound effects for some built in actions, such as
13525 * clicking, but you may wish to play these effects in your widget,
13526 * for instance, for internal navigation.
13527 *
13528 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13529 * {@link #isSoundEffectsEnabled()} is true.
13530 *
13531 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13532 */
13533 public void playSoundEffect(int soundConstant) {
13534 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13535 return;
13536 }
13537 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13538 }
13539
13540 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013541 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013542 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013543 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013544 *
13545 * <p>The framework will provide haptic feedback for some built in actions,
13546 * such as long presses, but you may wish to provide feedback for your
13547 * own widget.
13548 *
13549 * <p>The feedback will only be performed if
13550 * {@link #isHapticFeedbackEnabled()} is true.
13551 *
13552 * @param feedbackConstant One of the constants defined in
13553 * {@link HapticFeedbackConstants}
13554 */
13555 public boolean performHapticFeedback(int feedbackConstant) {
13556 return performHapticFeedback(feedbackConstant, 0);
13557 }
13558
13559 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013560 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013561 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013562 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013563 *
13564 * @param feedbackConstant One of the constants defined in
13565 * {@link HapticFeedbackConstants}
13566 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13567 */
13568 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13569 if (mAttachInfo == null) {
13570 return false;
13571 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013572 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013573 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013574 && !isHapticFeedbackEnabled()) {
13575 return false;
13576 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013577 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13578 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013579 }
13580
13581 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013582 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013583 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13584 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013585 */
13586 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013587 if (visibility != mSystemUiVisibility) {
13588 mSystemUiVisibility = visibility;
13589 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13590 mParent.recomputeViewAttributes(this);
13591 }
Joe Onorato664644d2011-01-23 17:53:23 -080013592 }
13593 }
13594
13595 /**
13596 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013597 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13598 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013599 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013600 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013601 return mSystemUiVisibility;
13602 }
13603
Scott Mainec6331b2011-05-24 16:55:56 -070013604 /**
13605 * Set a listener to receive callbacks when the visibility of the system bar changes.
13606 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13607 */
Joe Onorato664644d2011-01-23 17:53:23 -080013608 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013609 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013610 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13611 mParent.recomputeViewAttributes(this);
13612 }
13613 }
13614
13615 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013616 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13617 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013618 */
13619 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013620 ListenerInfo li = mListenerInfo;
13621 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13622 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013623 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013624 }
13625 }
13626
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013627 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13628 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13629 if (val != mSystemUiVisibility) {
13630 setSystemUiVisibility(val);
13631 }
13632 }
13633
Joe Onorato664644d2011-01-23 17:53:23 -080013634 /**
Joe Malin32736f02011-01-19 16:14:20 -080013635 * Creates an image that the system displays during the drag and drop
13636 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13637 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13638 * appearance as the given View. The default also positions the center of the drag shadow
13639 * directly under the touch point. If no View is provided (the constructor with no parameters
13640 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13641 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13642 * default is an invisible drag shadow.
13643 * <p>
13644 * You are not required to use the View you provide to the constructor as the basis of the
13645 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13646 * anything you want as the drag shadow.
13647 * </p>
13648 * <p>
13649 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13650 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13651 * size and position of the drag shadow. It uses this data to construct a
13652 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13653 * so that your application can draw the shadow image in the Canvas.
13654 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013655 *
13656 * <div class="special reference">
13657 * <h3>Developer Guides</h3>
13658 * <p>For a guide to implementing drag and drop features, read the
13659 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13660 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013661 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013662 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013663 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013664
13665 /**
Joe Malin32736f02011-01-19 16:14:20 -080013666 * Constructs a shadow image builder based on a View. By default, the resulting drag
13667 * shadow will have the same appearance and dimensions as the View, with the touch point
13668 * over the center of the View.
13669 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013670 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013671 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013672 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013673 }
13674
Christopher Tate17ed60c2011-01-18 12:50:26 -080013675 /**
13676 * Construct a shadow builder object with no associated View. This
13677 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13678 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13679 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013680 * reference to any View object. If they are not overridden, then the result is an
13681 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013682 */
13683 public DragShadowBuilder() {
13684 mView = new WeakReference<View>(null);
13685 }
13686
13687 /**
13688 * Returns the View object that had been passed to the
13689 * {@link #View.DragShadowBuilder(View)}
13690 * constructor. If that View parameter was {@code null} or if the
13691 * {@link #View.DragShadowBuilder()}
13692 * constructor was used to instantiate the builder object, this method will return
13693 * null.
13694 *
13695 * @return The View object associate with this builder object.
13696 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013697 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013698 final public View getView() {
13699 return mView.get();
13700 }
13701
Christopher Tate2c095f32010-10-04 14:13:40 -070013702 /**
Joe Malin32736f02011-01-19 16:14:20 -080013703 * Provides the metrics for the shadow image. These include the dimensions of
13704 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013705 * be centered under the touch location while dragging.
13706 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013707 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013708 * same as the dimensions of the View itself and centers the shadow under
13709 * the touch point.
13710 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013711 *
Joe Malin32736f02011-01-19 16:14:20 -080013712 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13713 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13714 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13715 * image.
13716 *
13717 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13718 * shadow image that should be underneath the touch point during the drag and drop
13719 * operation. Your application must set {@link android.graphics.Point#x} to the
13720 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013721 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013722 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013723 final View view = mView.get();
13724 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013725 shadowSize.set(view.getWidth(), view.getHeight());
13726 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013727 } else {
13728 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13729 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013730 }
13731
13732 /**
Joe Malin32736f02011-01-19 16:14:20 -080013733 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13734 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013735 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013736 *
Joe Malin32736f02011-01-19 16:14:20 -080013737 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013738 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013739 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013740 final View view = mView.get();
13741 if (view != null) {
13742 view.draw(canvas);
13743 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013744 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013745 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013746 }
13747 }
13748
13749 /**
Joe Malin32736f02011-01-19 16:14:20 -080013750 * Starts a drag and drop operation. When your application calls this method, it passes a
13751 * {@link android.view.View.DragShadowBuilder} object to the system. The
13752 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13753 * to get metrics for the drag shadow, and then calls the object's
13754 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13755 * <p>
13756 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13757 * drag events to all the View objects in your application that are currently visible. It does
13758 * this either by calling the View object's drag listener (an implementation of
13759 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13760 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13761 * Both are passed a {@link android.view.DragEvent} object that has a
13762 * {@link android.view.DragEvent#getAction()} value of
13763 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13764 * </p>
13765 * <p>
13766 * Your application can invoke startDrag() on any attached View object. The View object does not
13767 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13768 * be related to the View the user selected for dragging.
13769 * </p>
13770 * @param data A {@link android.content.ClipData} object pointing to the data to be
13771 * transferred by the drag and drop operation.
13772 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13773 * drag shadow.
13774 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13775 * drop operation. This Object is put into every DragEvent object sent by the system during the
13776 * current drag.
13777 * <p>
13778 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13779 * to the target Views. For example, it can contain flags that differentiate between a
13780 * a copy operation and a move operation.
13781 * </p>
13782 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13783 * so the parameter should be set to 0.
13784 * @return {@code true} if the method completes successfully, or
13785 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13786 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013787 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013788 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013789 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013790 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013791 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013792 }
13793 boolean okay = false;
13794
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013795 Point shadowSize = new Point();
13796 Point shadowTouchPoint = new Point();
13797 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013798
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013799 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13800 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13801 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013802 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013803
Chris Tatea32dcf72010-10-14 12:13:50 -070013804 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013805 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13806 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013807 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013808 Surface surface = new Surface();
13809 try {
13810 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013811 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013812 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013813 + " surface=" + surface);
13814 if (token != null) {
13815 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013816 try {
Chris Tate6b391282010-10-14 15:48:59 -070013817 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013818 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013819 } finally {
13820 surface.unlockCanvasAndPost(canvas);
13821 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013822
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013823 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013824
13825 // Cache the local state object for delivery with DragEvents
13826 root.setLocalDragState(myLocalState);
13827
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013828 // repurpose 'shadowSize' for the last touch point
13829 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013830
Christopher Tatea53146c2010-09-07 11:57:52 -070013831 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013832 shadowSize.x, shadowSize.y,
13833 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013834 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013835
13836 // Off and running! Release our local surface instance; the drag
13837 // shadow surface is now managed by the system process.
13838 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013839 }
13840 } catch (Exception e) {
13841 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13842 surface.destroy();
13843 }
13844
13845 return okay;
13846 }
13847
Christopher Tatea53146c2010-09-07 11:57:52 -070013848 /**
Joe Malin32736f02011-01-19 16:14:20 -080013849 * Handles drag events sent by the system following a call to
13850 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13851 *<p>
13852 * When the system calls this method, it passes a
13853 * {@link android.view.DragEvent} object. A call to
13854 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13855 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13856 * operation.
13857 * @param event The {@link android.view.DragEvent} sent by the system.
13858 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13859 * in DragEvent, indicating the type of drag event represented by this object.
13860 * @return {@code true} if the method was successful, otherwise {@code false}.
13861 * <p>
13862 * The method should return {@code true} in response to an action type of
13863 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13864 * operation.
13865 * </p>
13866 * <p>
13867 * The method should also return {@code true} in response to an action type of
13868 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13869 * {@code false} if it didn't.
13870 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013871 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013872 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013873 return false;
13874 }
13875
13876 /**
Joe Malin32736f02011-01-19 16:14:20 -080013877 * Detects if this View is enabled and has a drag event listener.
13878 * If both are true, then it calls the drag event listener with the
13879 * {@link android.view.DragEvent} it received. If the drag event listener returns
13880 * {@code true}, then dispatchDragEvent() returns {@code true}.
13881 * <p>
13882 * For all other cases, the method calls the
13883 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13884 * method and returns its result.
13885 * </p>
13886 * <p>
13887 * This ensures that a drag event is always consumed, even if the View does not have a drag
13888 * event listener. However, if the View has a listener and the listener returns true, then
13889 * onDragEvent() is not called.
13890 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013891 */
13892 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013893 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013894 ListenerInfo li = mListenerInfo;
13895 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13896 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013897 return true;
13898 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013899 return onDragEvent(event);
13900 }
13901
Christopher Tate3d4bf172011-03-28 16:16:46 -070013902 boolean canAcceptDrag() {
13903 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13904 }
13905
Christopher Tatea53146c2010-09-07 11:57:52 -070013906 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013907 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13908 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013909 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013910 */
13911 public void onCloseSystemDialogs(String reason) {
13912 }
Joe Malin32736f02011-01-19 16:14:20 -080013913
Dianne Hackbornffa42482009-09-23 22:20:11 -070013914 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013915 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013916 * update a Region being computed for
13917 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013918 * that any non-transparent parts of the Drawable are removed from the
13919 * given transparent region.
13920 *
13921 * @param dr The Drawable whose transparency is to be applied to the region.
13922 * @param region A Region holding the current transparency information,
13923 * where any parts of the region that are set are considered to be
13924 * transparent. On return, this region will be modified to have the
13925 * transparency information reduced by the corresponding parts of the
13926 * Drawable that are not transparent.
13927 * {@hide}
13928 */
13929 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13930 if (DBG) {
13931 Log.i("View", "Getting transparent region for: " + this);
13932 }
13933 final Region r = dr.getTransparentRegion();
13934 final Rect db = dr.getBounds();
13935 final AttachInfo attachInfo = mAttachInfo;
13936 if (r != null && attachInfo != null) {
13937 final int w = getRight()-getLeft();
13938 final int h = getBottom()-getTop();
13939 if (db.left > 0) {
13940 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13941 r.op(0, 0, db.left, h, Region.Op.UNION);
13942 }
13943 if (db.right < w) {
13944 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13945 r.op(db.right, 0, w, h, Region.Op.UNION);
13946 }
13947 if (db.top > 0) {
13948 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13949 r.op(0, 0, w, db.top, Region.Op.UNION);
13950 }
13951 if (db.bottom < h) {
13952 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13953 r.op(0, db.bottom, w, h, Region.Op.UNION);
13954 }
13955 final int[] location = attachInfo.mTransparentLocation;
13956 getLocationInWindow(location);
13957 r.translate(location[0], location[1]);
13958 region.op(r, Region.Op.INTERSECT);
13959 } else {
13960 region.op(db, Region.Op.DIFFERENCE);
13961 }
13962 }
13963
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013964 private void checkForLongClick(int delayOffset) {
13965 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13966 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013967
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013968 if (mPendingCheckForLongPress == null) {
13969 mPendingCheckForLongPress = new CheckForLongPress();
13970 }
13971 mPendingCheckForLongPress.rememberWindowAttachCount();
13972 postDelayed(mPendingCheckForLongPress,
13973 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013975 }
13976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013977 /**
13978 * Inflate a view from an XML resource. This convenience method wraps the {@link
13979 * LayoutInflater} class, which provides a full range of options for view inflation.
13980 *
13981 * @param context The Context object for your activity or application.
13982 * @param resource The resource ID to inflate
13983 * @param root A view group that will be the parent. Used to properly inflate the
13984 * layout_* parameters.
13985 * @see LayoutInflater
13986 */
13987 public static View inflate(Context context, int resource, ViewGroup root) {
13988 LayoutInflater factory = LayoutInflater.from(context);
13989 return factory.inflate(resource, root);
13990 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013992 /**
Adam Powell637d3372010-08-25 14:37:03 -070013993 * Scroll the view with standard behavior for scrolling beyond the normal
13994 * content boundaries. Views that call this method should override
13995 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13996 * results of an over-scroll operation.
13997 *
13998 * Views can use this method to handle any touch or fling-based scrolling.
13999 *
14000 * @param deltaX Change in X in pixels
14001 * @param deltaY Change in Y in pixels
14002 * @param scrollX Current X scroll value in pixels before applying deltaX
14003 * @param scrollY Current Y scroll value in pixels before applying deltaY
14004 * @param scrollRangeX Maximum content scroll range along the X axis
14005 * @param scrollRangeY Maximum content scroll range along the Y axis
14006 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14007 * along the X axis.
14008 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14009 * along the Y axis.
14010 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14011 * @return true if scrolling was clamped to an over-scroll boundary along either
14012 * axis, false otherwise.
14013 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014014 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014015 protected boolean overScrollBy(int deltaX, int deltaY,
14016 int scrollX, int scrollY,
14017 int scrollRangeX, int scrollRangeY,
14018 int maxOverScrollX, int maxOverScrollY,
14019 boolean isTouchEvent) {
14020 final int overScrollMode = mOverScrollMode;
14021 final boolean canScrollHorizontal =
14022 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14023 final boolean canScrollVertical =
14024 computeVerticalScrollRange() > computeVerticalScrollExtent();
14025 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14026 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14027 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14028 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14029
14030 int newScrollX = scrollX + deltaX;
14031 if (!overScrollHorizontal) {
14032 maxOverScrollX = 0;
14033 }
14034
14035 int newScrollY = scrollY + deltaY;
14036 if (!overScrollVertical) {
14037 maxOverScrollY = 0;
14038 }
14039
14040 // Clamp values if at the limits and record
14041 final int left = -maxOverScrollX;
14042 final int right = maxOverScrollX + scrollRangeX;
14043 final int top = -maxOverScrollY;
14044 final int bottom = maxOverScrollY + scrollRangeY;
14045
14046 boolean clampedX = false;
14047 if (newScrollX > right) {
14048 newScrollX = right;
14049 clampedX = true;
14050 } else if (newScrollX < left) {
14051 newScrollX = left;
14052 clampedX = true;
14053 }
14054
14055 boolean clampedY = false;
14056 if (newScrollY > bottom) {
14057 newScrollY = bottom;
14058 clampedY = true;
14059 } else if (newScrollY < top) {
14060 newScrollY = top;
14061 clampedY = true;
14062 }
14063
14064 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14065
14066 return clampedX || clampedY;
14067 }
14068
14069 /**
14070 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14071 * respond to the results of an over-scroll operation.
14072 *
14073 * @param scrollX New X scroll value in pixels
14074 * @param scrollY New Y scroll value in pixels
14075 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14076 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14077 */
14078 protected void onOverScrolled(int scrollX, int scrollY,
14079 boolean clampedX, boolean clampedY) {
14080 // Intentionally empty.
14081 }
14082
14083 /**
14084 * Returns the over-scroll mode for this view. The result will be
14085 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14086 * (allow over-scrolling only if the view content is larger than the container),
14087 * or {@link #OVER_SCROLL_NEVER}.
14088 *
14089 * @return This view's over-scroll mode.
14090 */
14091 public int getOverScrollMode() {
14092 return mOverScrollMode;
14093 }
14094
14095 /**
14096 * Set the over-scroll mode for this view. Valid over-scroll modes are
14097 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14098 * (allow over-scrolling only if the view content is larger than the container),
14099 * or {@link #OVER_SCROLL_NEVER}.
14100 *
14101 * Setting the over-scroll mode of a view will have an effect only if the
14102 * view is capable of scrolling.
14103 *
14104 * @param overScrollMode The new over-scroll mode for this view.
14105 */
14106 public void setOverScrollMode(int overScrollMode) {
14107 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14108 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14109 overScrollMode != OVER_SCROLL_NEVER) {
14110 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14111 }
14112 mOverScrollMode = overScrollMode;
14113 }
14114
14115 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014116 * Gets a scale factor that determines the distance the view should scroll
14117 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14118 * @return The vertical scroll scale factor.
14119 * @hide
14120 */
14121 protected float getVerticalScrollFactor() {
14122 if (mVerticalScrollFactor == 0) {
14123 TypedValue outValue = new TypedValue();
14124 if (!mContext.getTheme().resolveAttribute(
14125 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14126 throw new IllegalStateException(
14127 "Expected theme to define listPreferredItemHeight.");
14128 }
14129 mVerticalScrollFactor = outValue.getDimension(
14130 mContext.getResources().getDisplayMetrics());
14131 }
14132 return mVerticalScrollFactor;
14133 }
14134
14135 /**
14136 * Gets a scale factor that determines the distance the view should scroll
14137 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14138 * @return The horizontal scroll scale factor.
14139 * @hide
14140 */
14141 protected float getHorizontalScrollFactor() {
14142 // TODO: Should use something else.
14143 return getVerticalScrollFactor();
14144 }
14145
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014146 /**
14147 * Return the value specifying the text direction or policy that was set with
14148 * {@link #setTextDirection(int)}.
14149 *
14150 * @return the defined text direction. It can be one of:
14151 *
14152 * {@link #TEXT_DIRECTION_INHERIT},
14153 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14154 * {@link #TEXT_DIRECTION_ANY_RTL},
14155 * {@link #TEXT_DIRECTION_LTR},
14156 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014157 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014158 */
14159 public int getTextDirection() {
14160 return mTextDirection;
14161 }
14162
14163 /**
14164 * Set the text direction.
14165 *
14166 * @param textDirection the direction to set. Should be one of:
14167 *
14168 * {@link #TEXT_DIRECTION_INHERIT},
14169 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14170 * {@link #TEXT_DIRECTION_ANY_RTL},
14171 * {@link #TEXT_DIRECTION_LTR},
14172 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014173 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014174 */
14175 public void setTextDirection(int textDirection) {
14176 if (textDirection != mTextDirection) {
14177 mTextDirection = textDirection;
14178 resetResolvedTextDirection();
14179 requestLayout();
14180 }
14181 }
14182
14183 /**
14184 * Return the resolved text direction.
14185 *
14186 * @return the resolved text direction. Return one of:
14187 *
Doug Feltcb3791202011-07-07 11:57:48 -070014188 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14189 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014190 * {@link #TEXT_DIRECTION_LTR},
14191 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014192 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014193 */
14194 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014195 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014196 resolveTextDirection();
14197 }
14198 return mResolvedTextDirection;
14199 }
14200
14201 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014202 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14203 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014204 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014205 public void resolveTextDirection() {
14206 if (mResolvedTextDirection != TEXT_DIRECTION_INHERIT) {
14207 // Resolution has already been done.
14208 return;
14209 }
Doug Feltcb3791202011-07-07 11:57:48 -070014210 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
14211 mResolvedTextDirection = mTextDirection;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014212 } else if (mParent != null && mParent instanceof ViewGroup) {
Doug Feltcb3791202011-07-07 11:57:48 -070014213 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014214 } else {
14215 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Doug Feltcb3791202011-07-07 11:57:48 -070014216 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014217 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014218 }
14219
14220 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014221 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014222 * resolution should override this method.
14223 *
14224 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014225 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014226 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014227 }
14228
14229 /**
14230 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014231 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014232 * reset is done.
14233 */
14234 public void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014235 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014236 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014237 }
14238
14239 /**
14240 * Called when text direction is reset. Subclasses that care about text direction reset should
14241 * override this method and do a reset of the text direction of their children. The default
14242 * implementation does nothing.
14243 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014244 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014245 }
14246
Chet Haaseb39f0512011-05-24 14:36:40 -070014247 //
14248 // Properties
14249 //
14250 /**
14251 * A Property wrapper around the <code>alpha</code> functionality handled by the
14252 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14253 */
Chet Haased47f1532011-12-16 11:18:52 -080014254 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014255 @Override
14256 public void setValue(View object, float value) {
14257 object.setAlpha(value);
14258 }
14259
14260 @Override
14261 public Float get(View object) {
14262 return object.getAlpha();
14263 }
14264 };
14265
14266 /**
14267 * A Property wrapper around the <code>translationX</code> functionality handled by the
14268 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14269 */
Chet Haased47f1532011-12-16 11:18:52 -080014270 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014271 @Override
14272 public void setValue(View object, float value) {
14273 object.setTranslationX(value);
14274 }
14275
14276 @Override
14277 public Float get(View object) {
14278 return object.getTranslationX();
14279 }
14280 };
14281
14282 /**
14283 * A Property wrapper around the <code>translationY</code> functionality handled by the
14284 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14285 */
Chet Haased47f1532011-12-16 11:18:52 -080014286 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014287 @Override
14288 public void setValue(View object, float value) {
14289 object.setTranslationY(value);
14290 }
14291
14292 @Override
14293 public Float get(View object) {
14294 return object.getTranslationY();
14295 }
14296 };
14297
14298 /**
14299 * A Property wrapper around the <code>x</code> functionality handled by the
14300 * {@link View#setX(float)} and {@link View#getX()} methods.
14301 */
Chet Haased47f1532011-12-16 11:18:52 -080014302 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014303 @Override
14304 public void setValue(View object, float value) {
14305 object.setX(value);
14306 }
14307
14308 @Override
14309 public Float get(View object) {
14310 return object.getX();
14311 }
14312 };
14313
14314 /**
14315 * A Property wrapper around the <code>y</code> functionality handled by the
14316 * {@link View#setY(float)} and {@link View#getY()} methods.
14317 */
Chet Haased47f1532011-12-16 11:18:52 -080014318 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014319 @Override
14320 public void setValue(View object, float value) {
14321 object.setY(value);
14322 }
14323
14324 @Override
14325 public Float get(View object) {
14326 return object.getY();
14327 }
14328 };
14329
14330 /**
14331 * A Property wrapper around the <code>rotation</code> functionality handled by the
14332 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14333 */
Chet Haased47f1532011-12-16 11:18:52 -080014334 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014335 @Override
14336 public void setValue(View object, float value) {
14337 object.setRotation(value);
14338 }
14339
14340 @Override
14341 public Float get(View object) {
14342 return object.getRotation();
14343 }
14344 };
14345
14346 /**
14347 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14348 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14349 */
Chet Haased47f1532011-12-16 11:18:52 -080014350 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014351 @Override
14352 public void setValue(View object, float value) {
14353 object.setRotationX(value);
14354 }
14355
14356 @Override
14357 public Float get(View object) {
14358 return object.getRotationX();
14359 }
14360 };
14361
14362 /**
14363 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14364 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14365 */
Chet Haased47f1532011-12-16 11:18:52 -080014366 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014367 @Override
14368 public void setValue(View object, float value) {
14369 object.setRotationY(value);
14370 }
14371
14372 @Override
14373 public Float get(View object) {
14374 return object.getRotationY();
14375 }
14376 };
14377
14378 /**
14379 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14380 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14381 */
Chet Haased47f1532011-12-16 11:18:52 -080014382 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014383 @Override
14384 public void setValue(View object, float value) {
14385 object.setScaleX(value);
14386 }
14387
14388 @Override
14389 public Float get(View object) {
14390 return object.getScaleX();
14391 }
14392 };
14393
14394 /**
14395 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14396 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14397 */
Chet Haased47f1532011-12-16 11:18:52 -080014398 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014399 @Override
14400 public void setValue(View object, float value) {
14401 object.setScaleY(value);
14402 }
14403
14404 @Override
14405 public Float get(View object) {
14406 return object.getScaleY();
14407 }
14408 };
14409
Jeff Brown33bbfd22011-02-24 20:55:35 -080014410 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014411 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14412 * Each MeasureSpec represents a requirement for either the width or the height.
14413 * A MeasureSpec is comprised of a size and a mode. There are three possible
14414 * modes:
14415 * <dl>
14416 * <dt>UNSPECIFIED</dt>
14417 * <dd>
14418 * The parent has not imposed any constraint on the child. It can be whatever size
14419 * it wants.
14420 * </dd>
14421 *
14422 * <dt>EXACTLY</dt>
14423 * <dd>
14424 * The parent has determined an exact size for the child. The child is going to be
14425 * given those bounds regardless of how big it wants to be.
14426 * </dd>
14427 *
14428 * <dt>AT_MOST</dt>
14429 * <dd>
14430 * The child can be as large as it wants up to the specified size.
14431 * </dd>
14432 * </dl>
14433 *
14434 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14435 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14436 */
14437 public static class MeasureSpec {
14438 private static final int MODE_SHIFT = 30;
14439 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14440
14441 /**
14442 * Measure specification mode: The parent has not imposed any constraint
14443 * on the child. It can be whatever size it wants.
14444 */
14445 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14446
14447 /**
14448 * Measure specification mode: The parent has determined an exact size
14449 * for the child. The child is going to be given those bounds regardless
14450 * of how big it wants to be.
14451 */
14452 public static final int EXACTLY = 1 << MODE_SHIFT;
14453
14454 /**
14455 * Measure specification mode: The child can be as large as it wants up
14456 * to the specified size.
14457 */
14458 public static final int AT_MOST = 2 << MODE_SHIFT;
14459
14460 /**
14461 * Creates a measure specification based on the supplied size and mode.
14462 *
14463 * The mode must always be one of the following:
14464 * <ul>
14465 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14466 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14467 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14468 * </ul>
14469 *
14470 * @param size the size of the measure specification
14471 * @param mode the mode of the measure specification
14472 * @return the measure specification based on size and mode
14473 */
14474 public static int makeMeasureSpec(int size, int mode) {
14475 return size + mode;
14476 }
14477
14478 /**
14479 * Extracts the mode from the supplied measure specification.
14480 *
14481 * @param measureSpec the measure specification to extract the mode from
14482 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14483 * {@link android.view.View.MeasureSpec#AT_MOST} or
14484 * {@link android.view.View.MeasureSpec#EXACTLY}
14485 */
14486 public static int getMode(int measureSpec) {
14487 return (measureSpec & MODE_MASK);
14488 }
14489
14490 /**
14491 * Extracts the size from the supplied measure specification.
14492 *
14493 * @param measureSpec the measure specification to extract the size from
14494 * @return the size in pixels defined in the supplied measure specification
14495 */
14496 public static int getSize(int measureSpec) {
14497 return (measureSpec & ~MODE_MASK);
14498 }
14499
14500 /**
14501 * Returns a String representation of the specified measure
14502 * specification.
14503 *
14504 * @param measureSpec the measure specification to convert to a String
14505 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14506 */
14507 public static String toString(int measureSpec) {
14508 int mode = getMode(measureSpec);
14509 int size = getSize(measureSpec);
14510
14511 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14512
14513 if (mode == UNSPECIFIED)
14514 sb.append("UNSPECIFIED ");
14515 else if (mode == EXACTLY)
14516 sb.append("EXACTLY ");
14517 else if (mode == AT_MOST)
14518 sb.append("AT_MOST ");
14519 else
14520 sb.append(mode).append(" ");
14521
14522 sb.append(size);
14523 return sb.toString();
14524 }
14525 }
14526
14527 class CheckForLongPress implements Runnable {
14528
14529 private int mOriginalWindowAttachCount;
14530
14531 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014532 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014533 && mOriginalWindowAttachCount == mWindowAttachCount) {
14534 if (performLongClick()) {
14535 mHasPerformedLongPress = true;
14536 }
14537 }
14538 }
14539
14540 public void rememberWindowAttachCount() {
14541 mOriginalWindowAttachCount = mWindowAttachCount;
14542 }
14543 }
Joe Malin32736f02011-01-19 16:14:20 -080014544
Adam Powelle14579b2009-12-16 18:39:52 -080014545 private final class CheckForTap implements Runnable {
14546 public void run() {
14547 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014548 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014549 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014550 }
14551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014552
Adam Powella35d7682010-03-12 14:48:13 -080014553 private final class PerformClick implements Runnable {
14554 public void run() {
14555 performClick();
14556 }
14557 }
14558
Dianne Hackborn63042d62011-01-26 18:56:29 -080014559 /** @hide */
14560 public void hackTurnOffWindowResizeAnim(boolean off) {
14561 mAttachInfo.mTurnOffWindowResizeAnim = off;
14562 }
Joe Malin32736f02011-01-19 16:14:20 -080014563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014564 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014565 * This method returns a ViewPropertyAnimator object, which can be used to animate
14566 * specific properties on this View.
14567 *
14568 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14569 */
14570 public ViewPropertyAnimator animate() {
14571 if (mAnimator == null) {
14572 mAnimator = new ViewPropertyAnimator(this);
14573 }
14574 return mAnimator;
14575 }
14576
14577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014578 * Interface definition for a callback to be invoked when a key event is
14579 * dispatched to this view. The callback will be invoked before the key
14580 * event is given to the view.
14581 */
14582 public interface OnKeyListener {
14583 /**
14584 * Called when a key is dispatched to a view. This allows listeners to
14585 * get a chance to respond before the target view.
14586 *
14587 * @param v The view the key has been dispatched to.
14588 * @param keyCode The code for the physical key that was pressed
14589 * @param event The KeyEvent object containing full information about
14590 * the event.
14591 * @return True if the listener has consumed the event, false otherwise.
14592 */
14593 boolean onKey(View v, int keyCode, KeyEvent event);
14594 }
14595
14596 /**
14597 * Interface definition for a callback to be invoked when a touch event is
14598 * dispatched to this view. The callback will be invoked before the touch
14599 * event is given to the view.
14600 */
14601 public interface OnTouchListener {
14602 /**
14603 * Called when a touch event is dispatched to a view. This allows listeners to
14604 * get a chance to respond before the target view.
14605 *
14606 * @param v The view the touch event has been dispatched to.
14607 * @param event The MotionEvent object containing full information about
14608 * the event.
14609 * @return True if the listener has consumed the event, false otherwise.
14610 */
14611 boolean onTouch(View v, MotionEvent event);
14612 }
14613
14614 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014615 * Interface definition for a callback to be invoked when a hover event is
14616 * dispatched to this view. The callback will be invoked before the hover
14617 * event is given to the view.
14618 */
14619 public interface OnHoverListener {
14620 /**
14621 * Called when a hover event is dispatched to a view. This allows listeners to
14622 * get a chance to respond before the target view.
14623 *
14624 * @param v The view the hover event has been dispatched to.
14625 * @param event The MotionEvent object containing full information about
14626 * the event.
14627 * @return True if the listener has consumed the event, false otherwise.
14628 */
14629 boolean onHover(View v, MotionEvent event);
14630 }
14631
14632 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014633 * Interface definition for a callback to be invoked when a generic motion event is
14634 * dispatched to this view. The callback will be invoked before the generic motion
14635 * event is given to the view.
14636 */
14637 public interface OnGenericMotionListener {
14638 /**
14639 * Called when a generic motion event is dispatched to a view. This allows listeners to
14640 * get a chance to respond before the target view.
14641 *
14642 * @param v The view the generic motion event has been dispatched to.
14643 * @param event The MotionEvent object containing full information about
14644 * the event.
14645 * @return True if the listener has consumed the event, false otherwise.
14646 */
14647 boolean onGenericMotion(View v, MotionEvent event);
14648 }
14649
14650 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014651 * Interface definition for a callback to be invoked when a view has been clicked and held.
14652 */
14653 public interface OnLongClickListener {
14654 /**
14655 * Called when a view has been clicked and held.
14656 *
14657 * @param v The view that was clicked and held.
14658 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014659 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014660 */
14661 boolean onLongClick(View v);
14662 }
14663
14664 /**
Chris Tate32affef2010-10-18 15:29:21 -070014665 * Interface definition for a callback to be invoked when a drag is being dispatched
14666 * to this view. The callback will be invoked before the hosting view's own
14667 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14668 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014669 *
14670 * <div class="special reference">
14671 * <h3>Developer Guides</h3>
14672 * <p>For a guide to implementing drag and drop features, read the
14673 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14674 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014675 */
14676 public interface OnDragListener {
14677 /**
14678 * Called when a drag event is dispatched to a view. This allows listeners
14679 * to get a chance to override base View behavior.
14680 *
Joe Malin32736f02011-01-19 16:14:20 -080014681 * @param v The View that received the drag event.
14682 * @param event The {@link android.view.DragEvent} object for the drag event.
14683 * @return {@code true} if the drag event was handled successfully, or {@code false}
14684 * if the drag event was not handled. Note that {@code false} will trigger the View
14685 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014686 */
14687 boolean onDrag(View v, DragEvent event);
14688 }
14689
14690 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014691 * Interface definition for a callback to be invoked when the focus state of
14692 * a view changed.
14693 */
14694 public interface OnFocusChangeListener {
14695 /**
14696 * Called when the focus state of a view has changed.
14697 *
14698 * @param v The view whose state has changed.
14699 * @param hasFocus The new focus state of v.
14700 */
14701 void onFocusChange(View v, boolean hasFocus);
14702 }
14703
14704 /**
14705 * Interface definition for a callback to be invoked when a view is clicked.
14706 */
14707 public interface OnClickListener {
14708 /**
14709 * Called when a view has been clicked.
14710 *
14711 * @param v The view that was clicked.
14712 */
14713 void onClick(View v);
14714 }
14715
14716 /**
14717 * Interface definition for a callback to be invoked when the context menu
14718 * for this view is being built.
14719 */
14720 public interface OnCreateContextMenuListener {
14721 /**
14722 * Called when the context menu for this view is being built. It is not
14723 * safe to hold onto the menu after this method returns.
14724 *
14725 * @param menu The context menu that is being built
14726 * @param v The view for which the context menu is being built
14727 * @param menuInfo Extra information about the item for which the
14728 * context menu should be shown. This information will vary
14729 * depending on the class of v.
14730 */
14731 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14732 }
14733
Joe Onorato664644d2011-01-23 17:53:23 -080014734 /**
14735 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014736 * visibility. This reports <strong>global</strong> changes to the system UI
14737 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014738 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014739 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014740 */
14741 public interface OnSystemUiVisibilityChangeListener {
14742 /**
14743 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014744 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014745 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014746 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014747 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14748 * <strong>global</strong> state of the UI visibility flags, not what your
14749 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014750 */
14751 public void onSystemUiVisibilityChange(int visibility);
14752 }
14753
Adam Powell4afd62b2011-02-18 15:02:18 -080014754 /**
14755 * Interface definition for a callback to be invoked when this view is attached
14756 * or detached from its window.
14757 */
14758 public interface OnAttachStateChangeListener {
14759 /**
14760 * Called when the view is attached to a window.
14761 * @param v The view that was attached
14762 */
14763 public void onViewAttachedToWindow(View v);
14764 /**
14765 * Called when the view is detached from a window.
14766 * @param v The view that was detached
14767 */
14768 public void onViewDetachedFromWindow(View v);
14769 }
14770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014771 private final class UnsetPressedState implements Runnable {
14772 public void run() {
14773 setPressed(false);
14774 }
14775 }
14776
14777 /**
14778 * Base class for derived classes that want to save and restore their own
14779 * state in {@link android.view.View#onSaveInstanceState()}.
14780 */
14781 public static class BaseSavedState extends AbsSavedState {
14782 /**
14783 * Constructor used when reading from a parcel. Reads the state of the superclass.
14784 *
14785 * @param source
14786 */
14787 public BaseSavedState(Parcel source) {
14788 super(source);
14789 }
14790
14791 /**
14792 * Constructor called by derived classes when creating their SavedState objects
14793 *
14794 * @param superState The state of the superclass of this view
14795 */
14796 public BaseSavedState(Parcelable superState) {
14797 super(superState);
14798 }
14799
14800 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14801 new Parcelable.Creator<BaseSavedState>() {
14802 public BaseSavedState createFromParcel(Parcel in) {
14803 return new BaseSavedState(in);
14804 }
14805
14806 public BaseSavedState[] newArray(int size) {
14807 return new BaseSavedState[size];
14808 }
14809 };
14810 }
14811
14812 /**
14813 * A set of information given to a view when it is attached to its parent
14814 * window.
14815 */
14816 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014817 interface Callbacks {
14818 void playSoundEffect(int effectId);
14819 boolean performHapticFeedback(int effectId, boolean always);
14820 }
14821
14822 /**
14823 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14824 * to a Handler. This class contains the target (View) to invalidate and
14825 * the coordinates of the dirty rectangle.
14826 *
14827 * For performance purposes, this class also implements a pool of up to
14828 * POOL_LIMIT objects that get reused. This reduces memory allocations
14829 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014830 */
Romain Guyd928d682009-03-31 17:52:16 -070014831 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014832 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014833 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14834 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014835 public InvalidateInfo newInstance() {
14836 return new InvalidateInfo();
14837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014838
Romain Guyd928d682009-03-31 17:52:16 -070014839 public void onAcquired(InvalidateInfo element) {
14840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014841
Romain Guyd928d682009-03-31 17:52:16 -070014842 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014843 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014844 }
14845 }, POOL_LIMIT)
14846 );
14847
14848 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014849 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014850
14851 View target;
14852
14853 int left;
14854 int top;
14855 int right;
14856 int bottom;
14857
Romain Guyd928d682009-03-31 17:52:16 -070014858 public void setNextPoolable(InvalidateInfo element) {
14859 mNext = element;
14860 }
14861
14862 public InvalidateInfo getNextPoolable() {
14863 return mNext;
14864 }
14865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014866 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014867 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014868 }
14869
14870 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014871 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014872 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014873
14874 public boolean isPooled() {
14875 return mIsPooled;
14876 }
14877
14878 public void setPooled(boolean isPooled) {
14879 mIsPooled = isPooled;
14880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014881 }
14882
14883 final IWindowSession mSession;
14884
14885 final IWindow mWindow;
14886
14887 final IBinder mWindowToken;
14888
14889 final Callbacks mRootCallbacks;
14890
Romain Guy59a12ca2011-06-09 17:48:21 -070014891 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014893 /**
14894 * The top view of the hierarchy.
14895 */
14896 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014898 IBinder mPanelParentWindowToken;
14899 Surface mSurface;
14900
Romain Guyb051e892010-09-28 19:09:36 -070014901 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014902 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014903 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014905 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014906 * Scale factor used by the compatibility mode
14907 */
14908 float mApplicationScale;
14909
14910 /**
14911 * Indicates whether the application is in compatibility mode
14912 */
14913 boolean mScalingRequired;
14914
14915 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014916 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014917 */
14918 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014919
Dianne Hackborn63042d62011-01-26 18:56:29 -080014920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014921 * Left position of this view's window
14922 */
14923 int mWindowLeft;
14924
14925 /**
14926 * Top position of this view's window
14927 */
14928 int mWindowTop;
14929
14930 /**
Adam Powell26153a32010-11-08 15:22:27 -080014931 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014932 */
Adam Powell26153a32010-11-08 15:22:27 -080014933 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014934
14935 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014936 * For windows that are full-screen but using insets to layout inside
14937 * of the screen decorations, these are the current insets for the
14938 * content of the window.
14939 */
14940 final Rect mContentInsets = new Rect();
14941
14942 /**
14943 * For windows that are full-screen but using insets to layout inside
14944 * of the screen decorations, these are the current insets for the
14945 * actual visible parts of the window.
14946 */
14947 final Rect mVisibleInsets = new Rect();
14948
14949 /**
14950 * The internal insets given by this window. This value is
14951 * supplied by the client (through
14952 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14953 * be given to the window manager when changed to be used in laying
14954 * out windows behind it.
14955 */
14956 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14957 = new ViewTreeObserver.InternalInsetsInfo();
14958
14959 /**
14960 * All views in the window's hierarchy that serve as scroll containers,
14961 * used to determine if the window can be resized or must be panned
14962 * to adjust for a soft input area.
14963 */
14964 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14965
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014966 final KeyEvent.DispatcherState mKeyDispatchState
14967 = new KeyEvent.DispatcherState();
14968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014969 /**
14970 * Indicates whether the view's window currently has the focus.
14971 */
14972 boolean mHasWindowFocus;
14973
14974 /**
14975 * The current visibility of the window.
14976 */
14977 int mWindowVisibility;
14978
14979 /**
14980 * Indicates the time at which drawing started to occur.
14981 */
14982 long mDrawingTime;
14983
14984 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014985 * Indicates whether or not ignoring the DIRTY_MASK flags.
14986 */
14987 boolean mIgnoreDirtyState;
14988
14989 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014990 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14991 * to avoid clearing that flag prematurely.
14992 */
14993 boolean mSetIgnoreDirtyState = false;
14994
14995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014996 * Indicates whether the view's window is currently in touch mode.
14997 */
14998 boolean mInTouchMode;
14999
15000 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015001 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015002 * the next time it performs a traversal
15003 */
15004 boolean mRecomputeGlobalAttributes;
15005
15006 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015007 * Always report new attributes at next traversal.
15008 */
15009 boolean mForceReportNewAttributes;
15010
15011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015012 * Set during a traveral if any views want to keep the screen on.
15013 */
15014 boolean mKeepScreenOn;
15015
15016 /**
Joe Onorato664644d2011-01-23 17:53:23 -080015017 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
15018 */
15019 int mSystemUiVisibility;
15020
15021 /**
15022 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
15023 * attached.
15024 */
15025 boolean mHasSystemUiListeners;
15026
15027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015028 * Set if the visibility of any views has changed.
15029 */
15030 boolean mViewVisibilityChanged;
15031
15032 /**
15033 * Set to true if a view has been scrolled.
15034 */
15035 boolean mViewScrollChanged;
15036
15037 /**
15038 * Global to the view hierarchy used as a temporary for dealing with
15039 * x/y points in the transparent region computations.
15040 */
15041 final int[] mTransparentLocation = new int[2];
15042
15043 /**
15044 * Global to the view hierarchy used as a temporary for dealing with
15045 * x/y points in the ViewGroup.invalidateChild implementation.
15046 */
15047 final int[] mInvalidateChildLocation = new int[2];
15048
Chet Haasec3aa3612010-06-17 08:50:37 -070015049
15050 /**
15051 * Global to the view hierarchy used as a temporary for dealing with
15052 * x/y location when view is transformed.
15053 */
15054 final float[] mTmpTransformLocation = new float[2];
15055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015056 /**
15057 * The view tree observer used to dispatch global events like
15058 * layout, pre-draw, touch mode change, etc.
15059 */
15060 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15061
15062 /**
15063 * A Canvas used by the view hierarchy to perform bitmap caching.
15064 */
15065 Canvas mCanvas;
15066
15067 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015068 * The view root impl.
15069 */
15070 final ViewRootImpl mViewRootImpl;
15071
15072 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015073 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015074 * handler can be used to pump events in the UI events queue.
15075 */
15076 final Handler mHandler;
15077
15078 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015079 * Temporary for use in computing invalidate rectangles while
15080 * calling up the hierarchy.
15081 */
15082 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015083
15084 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015085 * Temporary for use in computing hit areas with transformed views
15086 */
15087 final RectF mTmpTransformRect = new RectF();
15088
15089 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015090 * Temporary list for use in collecting focusable descendents of a view.
15091 */
15092 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015094 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015095 * The id of the window for accessibility purposes.
15096 */
15097 int mAccessibilityWindowId = View.NO_ID;
15098
15099 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015100 * Creates a new set of attachment information with the specified
15101 * events handler and thread.
15102 *
15103 * @param handler the events handler the view must use
15104 */
15105 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015106 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015107 mSession = session;
15108 mWindow = window;
15109 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015110 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015111 mHandler = handler;
15112 mRootCallbacks = effectPlayer;
15113 }
15114 }
15115
15116 /**
15117 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15118 * is supported. This avoids keeping too many unused fields in most
15119 * instances of View.</p>
15120 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015121 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015122
Mike Cleronf116bf82009-09-27 19:14:12 -070015123 /**
15124 * Scrollbars are not visible
15125 */
15126 public static final int OFF = 0;
15127
15128 /**
15129 * Scrollbars are visible
15130 */
15131 public static final int ON = 1;
15132
15133 /**
15134 * Scrollbars are fading away
15135 */
15136 public static final int FADING = 2;
15137
15138 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015140 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015141 public int scrollBarDefaultDelayBeforeFade;
15142 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015143
15144 public int scrollBarSize;
15145 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015146 public float[] interpolatorValues;
15147 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015148
15149 public final Paint paint;
15150 public final Matrix matrix;
15151 public Shader shader;
15152
Mike Cleronf116bf82009-09-27 19:14:12 -070015153 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15154
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015155 private static final float[] OPAQUE = { 255 };
15156 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015157
Mike Cleronf116bf82009-09-27 19:14:12 -070015158 /**
15159 * When fading should start. This time moves into the future every time
15160 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15161 */
15162 public long fadeStartTime;
15163
15164
15165 /**
15166 * The current state of the scrollbars: ON, OFF, or FADING
15167 */
15168 public int state = OFF;
15169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015170 private int mLastColor;
15171
Mike Cleronf116bf82009-09-27 19:14:12 -070015172 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015173 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15174 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015175 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15176 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015177
15178 paint = new Paint();
15179 matrix = new Matrix();
15180 // use use a height of 1, and then wack the matrix each time we
15181 // actually use it.
15182 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015184 paint.setShader(shader);
15185 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015186 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015187 }
Romain Guy8506ab42009-06-11 17:35:47 -070015188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015189 public void setFadeColor(int color) {
15190 if (color != 0 && color != mLastColor) {
15191 mLastColor = color;
15192 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015193
Romain Guye55e1a72009-08-27 10:42:26 -070015194 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15195 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015197 paint.setShader(shader);
15198 // Restore the default transfer mode (src_over)
15199 paint.setXfermode(null);
15200 }
15201 }
Joe Malin32736f02011-01-19 16:14:20 -080015202
Mike Cleronf116bf82009-09-27 19:14:12 -070015203 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015204 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015205 if (now >= fadeStartTime) {
15206
15207 // the animation fades the scrollbars out by changing
15208 // the opacity (alpha) from fully opaque to fully
15209 // transparent
15210 int nextFrame = (int) now;
15211 int framesCount = 0;
15212
15213 Interpolator interpolator = scrollBarInterpolator;
15214
15215 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015216 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015217
15218 // End transparent
15219 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015220 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015221
15222 state = FADING;
15223
15224 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015225 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015226 }
15227 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015228 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015229
Svetoslav Ganova0156172011-06-26 17:55:44 -070015230 /**
15231 * Resuable callback for sending
15232 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15233 */
15234 private class SendViewScrolledAccessibilityEvent implements Runnable {
15235 public volatile boolean mIsPending;
15236
15237 public void run() {
15238 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15239 mIsPending = false;
15240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015241 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015242
15243 /**
15244 * <p>
15245 * This class represents a delegate that can be registered in a {@link View}
15246 * to enhance accessibility support via composition rather via inheritance.
15247 * It is specifically targeted to widget developers that extend basic View
15248 * classes i.e. classes in package android.view, that would like their
15249 * applications to be backwards compatible.
15250 * </p>
15251 * <p>
15252 * A scenario in which a developer would like to use an accessibility delegate
15253 * is overriding a method introduced in a later API version then the minimal API
15254 * version supported by the application. For example, the method
15255 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15256 * in API version 4 when the accessibility APIs were first introduced. If a
15257 * developer would like his application to run on API version 4 devices (assuming
15258 * all other APIs used by the application are version 4 or lower) and take advantage
15259 * of this method, instead of overriding the method which would break the application's
15260 * backwards compatibility, he can override the corresponding method in this
15261 * delegate and register the delegate in the target View if the API version of
15262 * the system is high enough i.e. the API version is same or higher to the API
15263 * version that introduced
15264 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15265 * </p>
15266 * <p>
15267 * Here is an example implementation:
15268 * </p>
15269 * <code><pre><p>
15270 * if (Build.VERSION.SDK_INT >= 14) {
15271 * // If the API version is equal of higher than the version in
15272 * // which onInitializeAccessibilityNodeInfo was introduced we
15273 * // register a delegate with a customized implementation.
15274 * View view = findViewById(R.id.view_id);
15275 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15276 * public void onInitializeAccessibilityNodeInfo(View host,
15277 * AccessibilityNodeInfo info) {
15278 * // Let the default implementation populate the info.
15279 * super.onInitializeAccessibilityNodeInfo(host, info);
15280 * // Set some other information.
15281 * info.setEnabled(host.isEnabled());
15282 * }
15283 * });
15284 * }
15285 * </code></pre></p>
15286 * <p>
15287 * This delegate contains methods that correspond to the accessibility methods
15288 * in View. If a delegate has been specified the implementation in View hands
15289 * off handling to the corresponding method in this delegate. The default
15290 * implementation the delegate methods behaves exactly as the corresponding
15291 * method in View for the case of no accessibility delegate been set. Hence,
15292 * to customize the behavior of a View method, clients can override only the
15293 * corresponding delegate method without altering the behavior of the rest
15294 * accessibility related methods of the host view.
15295 * </p>
15296 */
15297 public static class AccessibilityDelegate {
15298
15299 /**
15300 * Sends an accessibility event of the given type. If accessibility is not
15301 * enabled this method has no effect.
15302 * <p>
15303 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15304 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15305 * been set.
15306 * </p>
15307 *
15308 * @param host The View hosting the delegate.
15309 * @param eventType The type of the event to send.
15310 *
15311 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15312 */
15313 public void sendAccessibilityEvent(View host, int eventType) {
15314 host.sendAccessibilityEventInternal(eventType);
15315 }
15316
15317 /**
15318 * Sends an accessibility event. This method behaves exactly as
15319 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15320 * empty {@link AccessibilityEvent} and does not perform a check whether
15321 * accessibility is enabled.
15322 * <p>
15323 * The default implementation behaves as
15324 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15325 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15326 * the case of no accessibility delegate been set.
15327 * </p>
15328 *
15329 * @param host The View hosting the delegate.
15330 * @param event The event to send.
15331 *
15332 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15333 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15334 */
15335 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15336 host.sendAccessibilityEventUncheckedInternal(event);
15337 }
15338
15339 /**
15340 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15341 * to its children for adding their text content to the event.
15342 * <p>
15343 * The default implementation behaves as
15344 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15345 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15346 * the case of no accessibility delegate been set.
15347 * </p>
15348 *
15349 * @param host The View hosting the delegate.
15350 * @param event The event.
15351 * @return True if the event population was completed.
15352 *
15353 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15354 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15355 */
15356 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15357 return host.dispatchPopulateAccessibilityEventInternal(event);
15358 }
15359
15360 /**
15361 * Gives a chance to the host View to populate the accessibility event with its
15362 * text content.
15363 * <p>
15364 * The default implementation behaves as
15365 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15366 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15367 * the case of no accessibility delegate been set.
15368 * </p>
15369 *
15370 * @param host The View hosting the delegate.
15371 * @param event The accessibility event which to populate.
15372 *
15373 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15374 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15375 */
15376 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15377 host.onPopulateAccessibilityEventInternal(event);
15378 }
15379
15380 /**
15381 * Initializes an {@link AccessibilityEvent} with information about the
15382 * the host View which is the event source.
15383 * <p>
15384 * The default implementation behaves as
15385 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15386 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15387 * the case of no accessibility delegate been set.
15388 * </p>
15389 *
15390 * @param host The View hosting the delegate.
15391 * @param event The event to initialize.
15392 *
15393 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15394 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15395 */
15396 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15397 host.onInitializeAccessibilityEventInternal(event);
15398 }
15399
15400 /**
15401 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15402 * <p>
15403 * The default implementation behaves as
15404 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15405 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15406 * the case of no accessibility delegate been set.
15407 * </p>
15408 *
15409 * @param host The View hosting the delegate.
15410 * @param info The instance to initialize.
15411 *
15412 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15413 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15414 */
15415 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15416 host.onInitializeAccessibilityNodeInfoInternal(info);
15417 }
15418
15419 /**
15420 * Called when a child of the host View has requested sending an
15421 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15422 * to augment the event.
15423 * <p>
15424 * The default implementation behaves as
15425 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15426 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15427 * the case of no accessibility delegate been set.
15428 * </p>
15429 *
15430 * @param host The View hosting the delegate.
15431 * @param child The child which requests sending the event.
15432 * @param event The event to be sent.
15433 * @return True if the event should be sent
15434 *
15435 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15436 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15437 */
15438 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15439 AccessibilityEvent event) {
15440 return host.onRequestSendAccessibilityEventInternal(child, event);
15441 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015442
15443 /**
15444 * Gets the provider for managing a virtual view hierarchy rooted at this View
15445 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15446 * that explore the window content.
15447 * <p>
15448 * The default implementation behaves as
15449 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15450 * the case of no accessibility delegate been set.
15451 * </p>
15452 *
15453 * @return The provider.
15454 *
15455 * @see AccessibilityNodeProvider
15456 */
15457 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15458 return null;
15459 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015461}