blob: e6b41da2ba8548d340cb266b25eafd9ade8b6f54 [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;
43import android.os.Message;
44import android.os.Parcel;
45import android.os.Parcelable;
46import android.os.RemoteException;
47import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070048import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070050import android.util.FloatProperty;
51import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070053import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070054import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070057import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080059import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070064import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070065import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070067import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080068import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070069import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.inputmethod.InputConnection;
71import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.widget.ScrollBarDrawable;
73
Romain Guy1ef3fdb2011-09-09 15:30:30 -070074import static android.os.Build.VERSION_CODES.*;
75
Doug Feltcb3791202011-07-07 11:57:48 -070076import com.android.internal.R;
77import com.android.internal.util.Predicate;
78import com.android.internal.view.menu.MenuBuilder;
79
Christopher Tatea0374192010-10-05 13:06:41 -070080import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070081import java.lang.reflect.InvocationTargetException;
82import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.ArrayList;
84import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070085import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080086import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/**
89 * <p>
90 * This class represents the basic building block for user interface components. A View
91 * occupies a rectangular area on the screen and is responsible for drawing and
92 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070093 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
95 * are invisible containers that hold other Views (or other ViewGroups) and define
96 * their layout properties.
97 * </p>
98 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070099 * <div class="special reference">
100 * <h3>Developer Guides</h3>
101 * <p>For information about using this class to develop your application's user interface,
102 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <a name="Using"></a>
106 * <h3>Using Views</h3>
107 * <p>
108 * All of the views in a window are arranged in a single tree. You can add views
109 * either from code or by specifying a tree of views in one or more XML layout
110 * files. There are many specialized subclasses of views that act as controls or
111 * are capable of displaying text, images, or other content.
112 * </p>
113 * <p>
114 * Once you have created a tree of views, there are typically a few types of
115 * common operations you may wish to perform:
116 * <ul>
117 * <li><strong>Set properties:</strong> for example setting the text of a
118 * {@link android.widget.TextView}. The available properties and the methods
119 * that set them will vary among the different subclasses of views. Note that
120 * properties that are known at build time can be set in the XML layout
121 * files.</li>
122 * <li><strong>Set focus:</strong> The framework will handled moving focus in
123 * response to user input. To force focus to a specific view, call
124 * {@link #requestFocus}.</li>
125 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
126 * that will be notified when something interesting happens to the view. For
127 * example, all views will let you set a listener to be notified when the view
128 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700129 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
130 * Other view subclasses offer more specialized listeners. For example, a Button
131 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * </ul>
135 * </p>
136 * <p><em>
137 * Note: The Android framework is responsible for measuring, laying out and
138 * drawing views. You should not call methods that perform these actions on
139 * views yourself unless you are actually implementing a
140 * {@link android.view.ViewGroup}.
141 * </em></p>
142 *
143 * <a name="Lifecycle"></a>
144 * <h3>Implementing a Custom View</h3>
145 *
146 * <p>
147 * To implement a custom view, you will usually begin by providing overrides for
148 * some of the standard methods that the framework calls on all views. You do
149 * not need to override all of these methods. In fact, you can start by just
150 * overriding {@link #onDraw(android.graphics.Canvas)}.
151 * <table border="2" width="85%" align="center" cellpadding="5">
152 * <thead>
153 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
154 * </thead>
155 *
156 * <tbody>
157 * <tr>
158 * <td rowspan="2">Creation</td>
159 * <td>Constructors</td>
160 * <td>There is a form of the constructor that are called when the view
161 * is created from code and a form that is called when the view is
162 * inflated from a layout file. The second form should parse and apply
163 * any attributes defined in the layout file.
164 * </td>
165 * </tr>
166 * <tr>
167 * <td><code>{@link #onFinishInflate()}</code></td>
168 * <td>Called after a view and all of its children has been inflated
169 * from XML.</td>
170 * </tr>
171 *
172 * <tr>
173 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700174 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * <td>Called to determine the size requirements for this view and all
176 * of its children.
177 * </td>
178 * </tr>
179 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called when this view should assign a size and position to all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when the size of this view has changed.
188 * </td>
189 * </tr>
190 *
191 * <tr>
192 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when the view should render its content.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when a new key event occurs.
202 * </td>
203 * </tr>
204 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700205 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * <td>Called when a key up event occurs.
207 * </td>
208 * </tr>
209 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700210 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * <td>Called when a trackball motion event occurs.
212 * </td>
213 * </tr>
214 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700215 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * <td>Called when a touch screen motion event occurs.
217 * </td>
218 * </tr>
219 *
220 * <tr>
221 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when the view gains or loses focus.
224 * </td>
225 * </tr>
226 *
227 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the window containing the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="3">Attaching</td>
235 * <td><code>{@link #onAttachedToWindow()}</code></td>
236 * <td>Called when the view is attached to a window.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td><code>{@link #onDetachedFromWindow}</code></td>
242 * <td>Called when the view is detached from its window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700247 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * <td>Called when the visibility of the window containing the view
249 * has changed.
250 * </td>
251 * </tr>
252 * </tbody>
253 *
254 * </table>
255 * </p>
256 *
257 * <a name="IDs"></a>
258 * <h3>IDs</h3>
259 * Views may have an integer id associated with them. These ids are typically
260 * assigned in the layout XML files, and are used to find specific views within
261 * the view tree. A common pattern is to:
262 * <ul>
263 * <li>Define a Button in the layout file and assign it a unique ID.
264 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700265 * &lt;Button
266 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * android:layout_width="wrap_content"
268 * android:layout_height="wrap_content"
269 * android:text="@string/my_button_text"/&gt;
270 * </pre></li>
271 * <li>From the onCreate method of an Activity, find the Button
272 * <pre class="prettyprint">
273 * Button myButton = (Button) findViewById(R.id.my_button);
274 * </pre></li>
275 * </ul>
276 * <p>
277 * View IDs need not be unique throughout the tree, but it is good practice to
278 * ensure that they are at least unique within the part of the tree you are
279 * searching.
280 * </p>
281 *
282 * <a name="Position"></a>
283 * <h3>Position</h3>
284 * <p>
285 * The geometry of a view is that of a rectangle. A view has a location,
286 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
287 * two dimensions, expressed as a width and a height. The unit for location
288 * and dimensions is the pixel.
289 * </p>
290 *
291 * <p>
292 * It is possible to retrieve the location of a view by invoking the methods
293 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
294 * coordinate of the rectangle representing the view. The latter returns the
295 * top, or Y, coordinate of the rectangle representing the view. These methods
296 * both return the location of the view relative to its parent. For instance,
297 * when getLeft() returns 20, that means the view is located 20 pixels to the
298 * right of the left edge of its direct parent.
299 * </p>
300 *
301 * <p>
302 * In addition, several convenience methods are offered to avoid unnecessary
303 * computations, namely {@link #getRight()} and {@link #getBottom()}.
304 * These methods return the coordinates of the right and bottom edges of the
305 * rectangle representing the view. For instance, calling {@link #getRight()}
306 * is similar to the following computation: <code>getLeft() + getWidth()</code>
307 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
308 * </p>
309 *
310 * <a name="SizePaddingMargins"></a>
311 * <h3>Size, padding and margins</h3>
312 * <p>
313 * The size of a view is expressed with a width and a height. A view actually
314 * possess two pairs of width and height values.
315 * </p>
316 *
317 * <p>
318 * The first pair is known as <em>measured width</em> and
319 * <em>measured height</em>. These dimensions define how big a view wants to be
320 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
321 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
322 * and {@link #getMeasuredHeight()}.
323 * </p>
324 *
325 * <p>
326 * The second pair is simply known as <em>width</em> and <em>height</em>, or
327 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
328 * dimensions define the actual size of the view on screen, at drawing time and
329 * after layout. These values may, but do not have to, be different from the
330 * measured width and height. The width and height can be obtained by calling
331 * {@link #getWidth()} and {@link #getHeight()}.
332 * </p>
333 *
334 * <p>
335 * To measure its dimensions, a view takes into account its padding. The padding
336 * is expressed in pixels for the left, top, right and bottom parts of the view.
337 * Padding can be used to offset the content of the view by a specific amount of
338 * pixels. For instance, a left padding of 2 will push the view's content by
339 * 2 pixels to the right of the left edge. Padding can be set using the
340 * {@link #setPadding(int, int, int, int)} method and queried by calling
341 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700342 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 * </p>
344 *
345 * <p>
346 * Even though a view can define a padding, it does not provide any support for
347 * margins. However, view groups provide such a support. Refer to
348 * {@link android.view.ViewGroup} and
349 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
350 * </p>
351 *
352 * <a name="Layout"></a>
353 * <h3>Layout</h3>
354 * <p>
355 * Layout is a two pass process: a measure pass and a layout pass. The measuring
356 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
357 * of the view tree. Each view pushes dimension specifications down the tree
358 * during the recursion. At the end of the measure pass, every view has stored
359 * its measurements. The second pass happens in
360 * {@link #layout(int,int,int,int)} and is also top-down. During
361 * this pass each parent is responsible for positioning all of its children
362 * using the sizes computed in the measure pass.
363 * </p>
364 *
365 * <p>
366 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
367 * {@link #getMeasuredHeight()} values must be set, along with those for all of
368 * that view's descendants. A view's measured width and measured height values
369 * must respect the constraints imposed by the view's parents. This guarantees
370 * that at the end of the measure pass, all parents accept all of their
371 * children's measurements. A parent view may call measure() more than once on
372 * its children. For example, the parent may measure each child once with
373 * unspecified dimensions to find out how big they want to be, then call
374 * measure() on them again with actual numbers if the sum of all the children's
375 * unconstrained sizes is too big or too small.
376 * </p>
377 *
378 * <p>
379 * The measure pass uses two classes to communicate dimensions. The
380 * {@link MeasureSpec} class is used by views to tell their parents how they
381 * want to be measured and positioned. The base LayoutParams class just
382 * describes how big the view wants to be for both width and height. For each
383 * dimension, it can specify one of:
384 * <ul>
385 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800386 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * (minus padding)
388 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
389 * enclose its content (plus padding).
390 * </ul>
391 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
392 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
393 * an X and Y value.
394 * </p>
395 *
396 * <p>
397 * MeasureSpecs are used to push requirements down the tree from parent to
398 * child. A MeasureSpec can be in one of three modes:
399 * <ul>
400 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
401 * of a child view. For example, a LinearLayout may call measure() on its child
402 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
403 * tall the child view wants to be given a width of 240 pixels.
404 * <li>EXACTLY: This is used by the parent to impose an exact size on the
405 * child. The child must use this size, and guarantee that all of its
406 * descendants will fit within this size.
407 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
408 * child. The child must gurantee that it and all of its descendants will fit
409 * within this size.
410 * </ul>
411 * </p>
412 *
413 * <p>
414 * To intiate a layout, call {@link #requestLayout}. This method is typically
415 * called by a view on itself when it believes that is can no longer fit within
416 * its current bounds.
417 * </p>
418 *
419 * <a name="Drawing"></a>
420 * <h3>Drawing</h3>
421 * <p>
422 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700423 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 * this means that parents will draw before (i.e., behind) their children, with
425 * siblings drawn in the order they appear in the tree.
426 * If you set a background drawable for a View, then the View will draw it for you
427 * before calling back to its <code>onDraw()</code> method.
428 * </p>
429 *
430 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700431 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 * </p>
433 *
434 * <p>
435 * To force a view to draw, call {@link #invalidate()}.
436 * </p>
437 *
438 * <a name="EventHandlingThreading"></a>
439 * <h3>Event Handling and Threading</h3>
440 * <p>
441 * The basic cycle of a view is as follows:
442 * <ol>
443 * <li>An event comes in and is dispatched to the appropriate view. The view
444 * handles the event and notifies any listeners.</li>
445 * <li>If in the course of processing the event, the view's bounds may need
446 * to be changed, the view will call {@link #requestLayout()}.</li>
447 * <li>Similarly, if in the course of processing the event the view's appearance
448 * may need to be changed, the view will call {@link #invalidate()}.</li>
449 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
450 * the framework will take care of measuring, laying out, and drawing the tree
451 * as appropriate.</li>
452 * </ol>
453 * </p>
454 *
455 * <p><em>Note: The entire view tree is single threaded. You must always be on
456 * the UI thread when calling any method on any view.</em>
457 * If you are doing work on other threads and want to update the state of a view
458 * from that thread, you should use a {@link Handler}.
459 * </p>
460 *
461 * <a name="FocusHandling"></a>
462 * <h3>Focus Handling</h3>
463 * <p>
464 * The framework will handle routine focus movement in response to user input.
465 * This includes changing the focus as views are removed or hidden, or as new
466 * views become available. Views indicate their willingness to take focus
467 * through the {@link #isFocusable} method. To change whether a view can take
468 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
469 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
470 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
471 * </p>
472 * <p>
473 * Focus movement is based on an algorithm which finds the nearest neighbor in a
474 * given direction. In rare cases, the default algorithm may not match the
475 * intended behavior of the developer. In these situations, you can provide
476 * explicit overrides by using these XML attributes in the layout file:
477 * <pre>
478 * nextFocusDown
479 * nextFocusLeft
480 * nextFocusRight
481 * nextFocusUp
482 * </pre>
483 * </p>
484 *
485 *
486 * <p>
487 * To get a particular view to take focus, call {@link #requestFocus()}.
488 * </p>
489 *
490 * <a name="TouchMode"></a>
491 * <h3>Touch Mode</h3>
492 * <p>
493 * When a user is navigating a user interface via directional keys such as a D-pad, it is
494 * necessary to give focus to actionable items such as buttons so the user can see
495 * what will take input. If the device has touch capabilities, however, and the user
496 * begins interacting with the interface by touching it, it is no longer necessary to
497 * always highlight, or give focus to, a particular view. This motivates a mode
498 * for interaction named 'touch mode'.
499 * </p>
500 * <p>
501 * For a touch capable device, once the user touches the screen, the device
502 * will enter touch mode. From this point onward, only views for which
503 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
504 * Other views that are touchable, like buttons, will not take focus when touched; they will
505 * only fire the on click listeners.
506 * </p>
507 * <p>
508 * Any time a user hits a directional key, such as a D-pad direction, the view device will
509 * exit touch mode, and find a view to take focus, so that the user may resume interacting
510 * with the user interface without touching the screen again.
511 * </p>
512 * <p>
513 * The touch mode state is maintained across {@link android.app.Activity}s. Call
514 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
515 * </p>
516 *
517 * <a name="Scrolling"></a>
518 * <h3>Scrolling</h3>
519 * <p>
520 * The framework provides basic support for views that wish to internally
521 * scroll their content. This includes keeping track of the X and Y scroll
522 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800523 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700524 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * </p>
526 *
527 * <a name="Tags"></a>
528 * <h3>Tags</h3>
529 * <p>
530 * Unlike IDs, tags are not used to identify views. Tags are essentially an
531 * extra piece of information that can be associated with a view. They are most
532 * often used as a convenience to store data related to views in the views
533 * themselves rather than by putting them in a separate structure.
534 * </p>
535 *
536 * <a name="Animation"></a>
537 * <h3>Animation</h3>
538 * <p>
539 * You can attach an {@link Animation} object to a view using
540 * {@link #setAnimation(Animation)} or
541 * {@link #startAnimation(Animation)}. The animation can alter the scale,
542 * rotation, translation and alpha of a view over time. If the animation is
543 * attached to a view that has children, the animation will affect the entire
544 * subtree rooted by that node. When an animation is started, the framework will
545 * take care of redrawing the appropriate views until the animation completes.
546 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800547 * <p>
548 * Starting with Android 3.0, the preferred way of animating views is to use the
549 * {@link android.animation} package APIs.
550 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 *
Jeff Brown85a31762010-09-01 17:01:00 -0700552 * <a name="Security"></a>
553 * <h3>Security</h3>
554 * <p>
555 * Sometimes it is essential that an application be able to verify that an action
556 * is being performed with the full knowledge and consent of the user, such as
557 * granting a permission request, making a purchase or clicking on an advertisement.
558 * Unfortunately, a malicious application could try to spoof the user into
559 * performing these actions, unaware, by concealing the intended purpose of the view.
560 * As a remedy, the framework offers a touch filtering mechanism that can be used to
561 * improve the security of views that provide access to sensitive functionality.
562 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700563 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800564 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700565 * will discard touches that are received whenever the view's window is obscured by
566 * another visible window. As a result, the view will not receive touches whenever a
567 * toast, dialog or other window appears above the view's window.
568 * </p><p>
569 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700570 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
571 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700572 * </p>
573 *
Romain Guy171c5922011-01-06 10:04:23 -0800574 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700575 * @attr ref android.R.styleable#View_background
576 * @attr ref android.R.styleable#View_clickable
577 * @attr ref android.R.styleable#View_contentDescription
578 * @attr ref android.R.styleable#View_drawingCacheQuality
579 * @attr ref android.R.styleable#View_duplicateParentState
580 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700581 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700582 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700583 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_isScrollContainer
586 * @attr ref android.R.styleable#View_focusable
587 * @attr ref android.R.styleable#View_focusableInTouchMode
588 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
589 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800590 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_longClickable
592 * @attr ref android.R.styleable#View_minHeight
593 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * @attr ref android.R.styleable#View_nextFocusDown
595 * @attr ref android.R.styleable#View_nextFocusLeft
596 * @attr ref android.R.styleable#View_nextFocusRight
597 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700598 * @attr ref android.R.styleable#View_onClick
599 * @attr ref android.R.styleable#View_padding
600 * @attr ref android.R.styleable#View_paddingBottom
601 * @attr ref android.R.styleable#View_paddingLeft
602 * @attr ref android.R.styleable#View_paddingRight
603 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800604 * @attr ref android.R.styleable#View_paddingStart
605 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700635public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
636 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 private static final boolean DBG = false;
638
639 /**
640 * The logging tag used by this class with android.util.Log.
641 */
642 protected static final String VIEW_LOG_TAG = "View";
643
644 /**
645 * Used to mark a View that has no ID.
646 */
647 public static final int NO_ID = -1;
648
649 /**
650 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
651 * calling setFlags.
652 */
653 private static final int NOT_FOCUSABLE = 0x00000000;
654
655 /**
656 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
657 * setFlags.
658 */
659 private static final int FOCUSABLE = 0x00000001;
660
661 /**
662 * Mask for use with setFlags indicating bits used for focus.
663 */
664 private static final int FOCUSABLE_MASK = 0x00000001;
665
666 /**
667 * This view will adjust its padding to fit sytem windows (e.g. status bar)
668 */
669 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
670
671 /**
Scott Main812634c22011-07-27 13:22:35 -0700672 * This view is visible.
673 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
674 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 */
676 public static final int VISIBLE = 0x00000000;
677
678 /**
679 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700680 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
681 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 public static final int INVISIBLE = 0x00000004;
684
685 /**
686 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700687 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
688 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 */
690 public static final int GONE = 0x00000008;
691
692 /**
693 * Mask for use with setFlags indicating bits used for visibility.
694 * {@hide}
695 */
696 static final int VISIBILITY_MASK = 0x0000000C;
697
698 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
699
700 /**
701 * This view is enabled. Intrepretation varies by subclass.
702 * Use with ENABLED_MASK when calling setFlags.
703 * {@hide}
704 */
705 static final int ENABLED = 0x00000000;
706
707 /**
708 * This view is disabled. Intrepretation varies by subclass.
709 * Use with ENABLED_MASK when calling setFlags.
710 * {@hide}
711 */
712 static final int DISABLED = 0x00000020;
713
714 /**
715 * Mask for use with setFlags indicating bits used for indicating whether
716 * this view is enabled
717 * {@hide}
718 */
719 static final int ENABLED_MASK = 0x00000020;
720
721 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700722 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
723 * called and further optimizations will be performed. It is okay to have
724 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 * {@hide}
726 */
727 static final int WILL_NOT_DRAW = 0x00000080;
728
729 /**
730 * Mask for use with setFlags indicating bits used for indicating whether
731 * this view is will draw
732 * {@hide}
733 */
734 static final int DRAW_MASK = 0x00000080;
735
736 /**
737 * <p>This view doesn't show scrollbars.</p>
738 * {@hide}
739 */
740 static final int SCROLLBARS_NONE = 0x00000000;
741
742 /**
743 * <p>This view shows horizontal scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
747
748 /**
749 * <p>This view shows vertical scrollbars.</p>
750 * {@hide}
751 */
752 static final int SCROLLBARS_VERTICAL = 0x00000200;
753
754 /**
755 * <p>Mask for use with setFlags indicating bits used for indicating which
756 * scrollbars are enabled.</p>
757 * {@hide}
758 */
759 static final int SCROLLBARS_MASK = 0x00000300;
760
Jeff Brown85a31762010-09-01 17:01:00 -0700761 /**
762 * Indicates that the view should filter touches when its window is obscured.
763 * Refer to the class comments for more information about this security feature.
764 * {@hide}
765 */
766 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
767
768 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
770 /**
771 * <p>This view doesn't show fading edges.</p>
772 * {@hide}
773 */
774 static final int FADING_EDGE_NONE = 0x00000000;
775
776 /**
777 * <p>This view shows horizontal fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
781
782 /**
783 * <p>This view shows vertical fading edges.</p>
784 * {@hide}
785 */
786 static final int FADING_EDGE_VERTICAL = 0x00002000;
787
788 /**
789 * <p>Mask for use with setFlags indicating bits used for indicating which
790 * fading edges are enabled.</p>
791 * {@hide}
792 */
793 static final int FADING_EDGE_MASK = 0x00003000;
794
795 /**
796 * <p>Indicates this view can be clicked. When clickable, a View reacts
797 * to clicks by notifying the OnClickListener.<p>
798 * {@hide}
799 */
800 static final int CLICKABLE = 0x00004000;
801
802 /**
803 * <p>Indicates this view is caching its drawing into a bitmap.</p>
804 * {@hide}
805 */
806 static final int DRAWING_CACHE_ENABLED = 0x00008000;
807
808 /**
809 * <p>Indicates that no icicle should be saved for this view.<p>
810 * {@hide}
811 */
812 static final int SAVE_DISABLED = 0x000010000;
813
814 /**
815 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
816 * property.</p>
817 * {@hide}
818 */
819 static final int SAVE_DISABLED_MASK = 0x000010000;
820
821 /**
822 * <p>Indicates that no drawing cache should ever be created for this view.<p>
823 * {@hide}
824 */
825 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
826
827 /**
828 * <p>Indicates this view can take / keep focus when int touch mode.</p>
829 * {@hide}
830 */
831 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
832
833 /**
834 * <p>Enables low quality mode for the drawing cache.</p>
835 */
836 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
837
838 /**
839 * <p>Enables high quality mode for the drawing cache.</p>
840 */
841 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
842
843 /**
844 * <p>Enables automatic quality mode for the drawing cache.</p>
845 */
846 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
847
848 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
849 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
850 };
851
852 /**
853 * <p>Mask for use with setFlags indicating bits used for the cache
854 * quality property.</p>
855 * {@hide}
856 */
857 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
858
859 /**
860 * <p>
861 * Indicates this view can be long clicked. When long clickable, a View
862 * reacts to long clicks by notifying the OnLongClickListener or showing a
863 * context menu.
864 * </p>
865 * {@hide}
866 */
867 static final int LONG_CLICKABLE = 0x00200000;
868
869 /**
870 * <p>Indicates that this view gets its drawable states from its direct parent
871 * and ignores its original internal states.</p>
872 *
873 * @hide
874 */
875 static final int DUPLICATE_PARENT_STATE = 0x00400000;
876
877 /**
878 * The scrollbar style to display the scrollbars inside the content area,
879 * without increasing the padding. The scrollbars will be overlaid with
880 * translucency on the view's content.
881 */
882 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
883
884 /**
885 * The scrollbar style to display the scrollbars inside the padded area,
886 * increasing the padding of the view. The scrollbars will not overlap the
887 * content area of the view.
888 */
889 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
890
891 /**
892 * The scrollbar style to display the scrollbars at the edge of the view,
893 * without increasing the padding. The scrollbars will be overlaid with
894 * translucency.
895 */
896 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
897
898 /**
899 * The scrollbar style to display the scrollbars at the edge of the view,
900 * increasing the padding of the view. The scrollbars will only overlap the
901 * background, if any.
902 */
903 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
904
905 /**
906 * Mask to check if the scrollbar style is overlay or inset.
907 * {@hide}
908 */
909 static final int SCROLLBARS_INSET_MASK = 0x01000000;
910
911 /**
912 * Mask to check if the scrollbar style is inside or outside.
913 * {@hide}
914 */
915 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
916
917 /**
918 * Mask for scrollbar style.
919 * {@hide}
920 */
921 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
922
923 /**
924 * View flag indicating that the screen should remain on while the
925 * window containing this view is visible to the user. This effectively
926 * takes care of automatically setting the WindowManager's
927 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
928 */
929 public static final int KEEP_SCREEN_ON = 0x04000000;
930
931 /**
932 * View flag indicating whether this view should have sound effects enabled
933 * for events such as clicking and touching.
934 */
935 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
936
937 /**
938 * View flag indicating whether this view should have haptic feedback
939 * enabled for events such as long presses.
940 */
941 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
942
943 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700944 * <p>Indicates that the view hierarchy should stop saving state when
945 * it reaches this view. If state saving is initiated immediately at
946 * the view, it will be allowed.
947 * {@hide}
948 */
949 static final int PARENT_SAVE_DISABLED = 0x20000000;
950
951 /**
952 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
953 * {@hide}
954 */
955 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
956
957 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800958 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700959 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800960 * {@hide}
961 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700962 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800963
964 /**
965 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700966 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800967 * {@hide}
968 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700969 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800970
971 /**
972 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700973 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800974 * {@hide}
975 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700976 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800977
978 /**
979 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700980 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800981 * {@hide}
982 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700983 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800984
985 /**
986 * Mask for use with setFlags indicating bits used for horizontalDirection.
987 * {@hide}
988 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700989 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800990
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700991 /*
992 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
993 * flag value.
994 * {@hide}
995 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700996 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
997 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800998
999 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001000 * Default horizontalDirection.
1001 * {@hide}
1002 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001003 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001004
1005 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add all focusable Views regardless if they are focusable in touch mode.
1008 */
1009 public static final int FOCUSABLES_ALL = 0x00000000;
1010
1011 /**
1012 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1013 * should add only Views focusable in touch mode.
1014 */
1015 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * item.
1020 */
1021 public static final int FOCUS_BACKWARD = 0x00000001;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * item.
1026 */
1027 public static final int FOCUS_FORWARD = 0x00000002;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final int FOCUS_LEFT = 0x00000011;
1033
1034 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001035 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public static final int FOCUS_UP = 0x00000021;
1038
1039 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001040 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public static final int FOCUS_RIGHT = 0x00000042;
1043
1044 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001045 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 */
1047 public static final int FOCUS_DOWN = 0x00000082;
1048
1049 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001050 * Bits of {@link #getMeasuredWidthAndState()} and
1051 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1052 */
1053 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1054
1055 /**
1056 * Bits of {@link #getMeasuredWidthAndState()} and
1057 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1058 */
1059 public static final int MEASURED_STATE_MASK = 0xff000000;
1060
1061 /**
1062 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1063 * for functions that combine both width and height into a single int,
1064 * such as {@link #getMeasuredState()} and the childState argument of
1065 * {@link #resolveSizeAndState(int, int, int)}.
1066 */
1067 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1068
1069 /**
1070 * Bit of {@link #getMeasuredWidthAndState()} and
1071 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1072 * is smaller that the space the view would like to have.
1073 */
1074 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1075
1076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 * Base View state sets
1078 */
1079 // Singles
1080 /**
1081 * Indicates the view has no states set. States are used with
1082 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1083 * view depending on its state.
1084 *
1085 * @see android.graphics.drawable.Drawable
1086 * @see #getDrawableState()
1087 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001088 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 /**
1090 * Indicates the view is enabled. States are used with
1091 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1092 * view depending on its state.
1093 *
1094 * @see android.graphics.drawable.Drawable
1095 * @see #getDrawableState()
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is focused. States are used with
1100 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1101 * view depending on its state.
1102 *
1103 * @see android.graphics.drawable.Drawable
1104 * @see #getDrawableState()
1105 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001106 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 /**
1108 * Indicates the view is selected. States are used with
1109 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1110 * view depending on its state.
1111 *
1112 * @see android.graphics.drawable.Drawable
1113 * @see #getDrawableState()
1114 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001115 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 /**
1117 * Indicates the view is pressed. States are used with
1118 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1119 * view depending on its state.
1120 *
1121 * @see android.graphics.drawable.Drawable
1122 * @see #getDrawableState()
1123 * @hide
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 /**
1127 * Indicates the view's window has focus. States are used with
1128 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1129 * view depending on its state.
1130 *
1131 * @see android.graphics.drawable.Drawable
1132 * @see #getDrawableState()
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 // Doubles
1136 /**
1137 * Indicates the view is enabled and has the focus.
1138 *
1139 * @see #ENABLED_STATE_SET
1140 * @see #FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled and selected.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is enabled and that its window has focus.
1152 *
1153 * @see #ENABLED_STATE_SET
1154 * @see #WINDOW_FOCUSED_STATE_SET
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view is focused and selected.
1159 *
1160 * @see #FOCUSED_STATE_SET
1161 * @see #SELECTED_STATE_SET
1162 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001163 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 /**
1165 * Indicates the view has the focus and that its window has the focus.
1166 *
1167 * @see #FOCUSED_STATE_SET
1168 * @see #WINDOW_FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 /**
1172 * Indicates the view is selected and that its window has the focus.
1173 *
1174 * @see #SELECTED_STATE_SET
1175 * @see #WINDOW_FOCUSED_STATE_SET
1176 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001177 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 // Triples
1179 /**
1180 * Indicates the view is enabled, focused and selected.
1181 *
1182 * @see #ENABLED_STATE_SET
1183 * @see #FOCUSED_STATE_SET
1184 * @see #SELECTED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is enabled, focused and its window has the focus.
1189 *
1190 * @see #ENABLED_STATE_SET
1191 * @see #FOCUSED_STATE_SET
1192 * @see #WINDOW_FOCUSED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is enabled, selected and its window has the focus.
1197 *
1198 * @see #ENABLED_STATE_SET
1199 * @see #SELECTED_STATE_SET
1200 * @see #WINDOW_FOCUSED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is focused, selected and its window has the focus.
1205 *
1206 * @see #FOCUSED_STATE_SET
1207 * @see #SELECTED_STATE_SET
1208 * @see #WINDOW_FOCUSED_STATE_SET
1209 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001210 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 /**
1212 * Indicates the view is enabled, focused, selected and its window
1213 * has the focus.
1214 *
1215 * @see #ENABLED_STATE_SET
1216 * @see #FOCUSED_STATE_SET
1217 * @see #SELECTED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is pressed and its window has the focus.
1223 *
1224 * @see #PRESSED_STATE_SET
1225 * @see #WINDOW_FOCUSED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed and selected.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #SELECTED_STATE_SET
1233 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001234 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 /**
1236 * Indicates the view is pressed, selected and its window has the focus.
1237 *
1238 * @see #PRESSED_STATE_SET
1239 * @see #SELECTED_STATE_SET
1240 * @see #WINDOW_FOCUSED_STATE_SET
1241 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001242 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 /**
1244 * Indicates the view is pressed and focused.
1245 *
1246 * @see #PRESSED_STATE_SET
1247 * @see #FOCUSED_STATE_SET
1248 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001249 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 /**
1251 * Indicates the view is pressed, focused and its window has the focus.
1252 *
1253 * @see #PRESSED_STATE_SET
1254 * @see #FOCUSED_STATE_SET
1255 * @see #WINDOW_FOCUSED_STATE_SET
1256 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001257 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 /**
1259 * Indicates the view is pressed, focused and selected.
1260 *
1261 * @see #PRESSED_STATE_SET
1262 * @see #SELECTED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001265 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 /**
1267 * Indicates the view is pressed, focused, selected and its window has the focus.
1268 *
1269 * @see #PRESSED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #SELECTED_STATE_SET
1272 * @see #WINDOW_FOCUSED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed and enabled.
1277 *
1278 * @see #PRESSED_STATE_SET
1279 * @see #ENABLED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, enabled and its window has the focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #ENABLED_STATE_SET
1287 * @see #WINDOW_FOCUSED_STATE_SET
1288 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001289 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 /**
1291 * Indicates the view is pressed, enabled and selected.
1292 *
1293 * @see #PRESSED_STATE_SET
1294 * @see #ENABLED_STATE_SET
1295 * @see #SELECTED_STATE_SET
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 /**
1299 * Indicates the view is pressed, enabled, selected and its window has the
1300 * focus.
1301 *
1302 * @see #PRESSED_STATE_SET
1303 * @see #ENABLED_STATE_SET
1304 * @see #SELECTED_STATE_SET
1305 * @see #WINDOW_FOCUSED_STATE_SET
1306 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 /**
1309 * Indicates the view is pressed, enabled and focused.
1310 *
1311 * @see #PRESSED_STATE_SET
1312 * @see #ENABLED_STATE_SET
1313 * @see #FOCUSED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled, focused and its window has the
1318 * focus.
1319 *
1320 * @see #PRESSED_STATE_SET
1321 * @see #ENABLED_STATE_SET
1322 * @see #FOCUSED_STATE_SET
1323 * @see #WINDOW_FOCUSED_STATE_SET
1324 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 /**
1327 * Indicates the view is pressed, enabled, focused and selected.
1328 *
1329 * @see #PRESSED_STATE_SET
1330 * @see #ENABLED_STATE_SET
1331 * @see #SELECTED_STATE_SET
1332 * @see #FOCUSED_STATE_SET
1333 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001334 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 /**
1336 * Indicates the view is pressed, enabled, focused, selected and its window
1337 * has the focus.
1338 *
1339 * @see #PRESSED_STATE_SET
1340 * @see #ENABLED_STATE_SET
1341 * @see #SELECTED_STATE_SET
1342 * @see #FOCUSED_STATE_SET
1343 * @see #WINDOW_FOCUSED_STATE_SET
1344 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346
1347 /**
1348 * The order here is very important to {@link #getDrawableState()}
1349 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001350 private static final int[][] VIEW_STATE_SETS;
1351
Romain Guyb051e892010-09-28 19:09:36 -07001352 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1353 static final int VIEW_STATE_SELECTED = 1 << 1;
1354 static final int VIEW_STATE_FOCUSED = 1 << 2;
1355 static final int VIEW_STATE_ENABLED = 1 << 3;
1356 static final int VIEW_STATE_PRESSED = 1 << 4;
1357 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001358 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001359 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001360 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1361 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001362
1363 static final int[] VIEW_STATE_IDS = new int[] {
1364 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1365 R.attr.state_selected, VIEW_STATE_SELECTED,
1366 R.attr.state_focused, VIEW_STATE_FOCUSED,
1367 R.attr.state_enabled, VIEW_STATE_ENABLED,
1368 R.attr.state_pressed, VIEW_STATE_PRESSED,
1369 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001370 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001371 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001372 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1373 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 };
1375
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001377 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1378 throw new IllegalStateException(
1379 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1380 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001382 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001384 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001385 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001386 orderedIds[i * 2] = viewState;
1387 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 }
1389 }
1390 }
Romain Guyb051e892010-09-28 19:09:36 -07001391 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1392 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1393 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 int numBits = Integer.bitCount(i);
1395 int[] set = new int[numBits];
1396 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001397 for (int j = 0; j < orderedIds.length; j += 2) {
1398 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001399 set[pos++] = orderedIds[j];
1400 }
1401 }
1402 VIEW_STATE_SETS[i] = set;
1403 }
1404
1405 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1406 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1407 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1408 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1410 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1411 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1413 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1415 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1417 | VIEW_STATE_FOCUSED];
1418 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1419 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1421 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1423 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1430 | VIEW_STATE_ENABLED];
1431 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED];
1434 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1436 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1437
1438 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1439 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1441 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1443 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1450 | VIEW_STATE_PRESSED];
1451 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1456 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1461 | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1467 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1470 | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1473 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1476 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1480 | VIEW_STATE_PRESSED];
1481 }
1482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001484 * Accessibility event types that are dispatched for text population.
1485 */
1486 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1487 AccessibilityEvent.TYPE_VIEW_CLICKED
1488 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1489 | AccessibilityEvent.TYPE_VIEW_SELECTED
1490 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1491 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1492 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001493 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001494 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1495 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001496
1497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 * Temporary Rect currently for use in setBackground(). This will probably
1499 * be extended in the future to hold our own class with more than just
1500 * a Rect. :)
1501 */
1502 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001503
1504 /**
1505 * Map used to store views' tags.
1506 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001507 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001510 * The next available accessiiblity id.
1511 */
1512 private static int sNextAccessibilityViewId;
1513
1514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 * The animation currently associated with this view.
1516 * @hide
1517 */
1518 protected Animation mCurrentAnimation = null;
1519
1520 /**
1521 * Width as measured during measure pass.
1522 * {@hide}
1523 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001524 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001525 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526
1527 /**
1528 * Height as measured during measure pass.
1529 * {@hide}
1530 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001531 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001532 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533
1534 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001535 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1536 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1537 * its display list. This flag, used only when hw accelerated, allows us to clear the
1538 * flag while retaining this information until it's needed (at getDisplayList() time and
1539 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1540 *
1541 * {@hide}
1542 */
1543 boolean mRecreateDisplayList = false;
1544
1545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 * The view's identifier.
1547 * {@hide}
1548 *
1549 * @see #setId(int)
1550 * @see #getId()
1551 */
1552 @ViewDebug.ExportedProperty(resolveId = true)
1553 int mID = NO_ID;
1554
1555 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001556 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001557 */
1558 int mAccessibilityViewId = NO_ID;
1559
1560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 * The view's tag.
1562 * {@hide}
1563 *
1564 * @see #setTag(Object)
1565 * @see #getTag()
1566 */
1567 protected Object mTag;
1568
1569 // for mPrivateFlags:
1570 /** {@hide} */
1571 static final int WANTS_FOCUS = 0x00000001;
1572 /** {@hide} */
1573 static final int FOCUSED = 0x00000002;
1574 /** {@hide} */
1575 static final int SELECTED = 0x00000004;
1576 /** {@hide} */
1577 static final int IS_ROOT_NAMESPACE = 0x00000008;
1578 /** {@hide} */
1579 static final int HAS_BOUNDS = 0x00000010;
1580 /** {@hide} */
1581 static final int DRAWN = 0x00000020;
1582 /**
1583 * When this flag is set, this view is running an animation on behalf of its
1584 * children and should therefore not cancel invalidate requests, even if they
1585 * lie outside of this view's bounds.
1586 *
1587 * {@hide}
1588 */
1589 static final int DRAW_ANIMATION = 0x00000040;
1590 /** {@hide} */
1591 static final int SKIP_DRAW = 0x00000080;
1592 /** {@hide} */
1593 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1594 /** {@hide} */
1595 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1596 /** {@hide} */
1597 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1598 /** {@hide} */
1599 static final int MEASURED_DIMENSION_SET = 0x00000800;
1600 /** {@hide} */
1601 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001602 /** {@hide} */
1603 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604
1605 private static final int PRESSED = 0x00004000;
1606
1607 /** {@hide} */
1608 static final int DRAWING_CACHE_VALID = 0x00008000;
1609 /**
1610 * Flag used to indicate that this view should be drawn once more (and only once
1611 * more) after its animation has completed.
1612 * {@hide}
1613 */
1614 static final int ANIMATION_STARTED = 0x00010000;
1615
1616 private static final int SAVE_STATE_CALLED = 0x00020000;
1617
1618 /**
1619 * Indicates that the View returned true when onSetAlpha() was called and that
1620 * the alpha must be restored.
1621 * {@hide}
1622 */
1623 static final int ALPHA_SET = 0x00040000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER = 0x00080000;
1629
1630 /**
1631 * Set by {@link #setScrollContainer(boolean)}.
1632 */
1633 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1634
1635 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001636 * View flag indicating whether this view was invalidated (fully or partially.)
1637 *
1638 * @hide
1639 */
1640 static final int DIRTY = 0x00200000;
1641
1642 /**
1643 * View flag indicating whether this view was invalidated by an opaque
1644 * invalidate request.
1645 *
1646 * @hide
1647 */
1648 static final int DIRTY_OPAQUE = 0x00400000;
1649
1650 /**
1651 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1652 *
1653 * @hide
1654 */
1655 static final int DIRTY_MASK = 0x00600000;
1656
1657 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001658 * Indicates whether the background is opaque.
1659 *
1660 * @hide
1661 */
1662 static final int OPAQUE_BACKGROUND = 0x00800000;
1663
1664 /**
1665 * Indicates whether the scrollbars are opaque.
1666 *
1667 * @hide
1668 */
1669 static final int OPAQUE_SCROLLBARS = 0x01000000;
1670
1671 /**
1672 * Indicates whether the view is opaque.
1673 *
1674 * @hide
1675 */
1676 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001677
Adam Powelle14579b2009-12-16 18:39:52 -08001678 /**
1679 * Indicates a prepressed state;
1680 * the short time between ACTION_DOWN and recognizing
1681 * a 'real' press. Prepressed is used to recognize quick taps
1682 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001683 *
Adam Powelle14579b2009-12-16 18:39:52 -08001684 * @hide
1685 */
1686 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001687
Adam Powellc9fbaab2010-02-16 17:16:19 -08001688 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001689 * Indicates whether the view is temporarily detached.
1690 *
1691 * @hide
1692 */
1693 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powell8568c3a2010-04-19 14:26:11 -07001695 /**
1696 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001697 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001698 * @hide
1699 */
1700 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001701
1702 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001703 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1704 * @hide
1705 */
1706 private static final int HOVERED = 0x10000000;
1707
1708 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001709 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1710 * for transform operations
1711 *
1712 * @hide
1713 */
Adam Powellf37df072010-09-17 16:22:49 -07001714 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001715
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001716 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001717 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001718
Chet Haasefd2b0022010-08-06 13:08:56 -07001719 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001720 * Indicates that this view was specifically invalidated, not just dirtied because some
1721 * child view was invalidated. The flag is used to determine when we need to recreate
1722 * a view's display list (as opposed to just returning a reference to its existing
1723 * display list).
1724 *
1725 * @hide
1726 */
1727 static final int INVALIDATED = 0x80000000;
1728
Christopher Tate3d4bf172011-03-28 16:16:46 -07001729 /* Masks for mPrivateFlags2 */
1730
1731 /**
1732 * Indicates that this view has reported that it can accept the current drag's content.
1733 * Cleared when the drag operation concludes.
1734 * @hide
1735 */
1736 static final int DRAG_CAN_ACCEPT = 0x00000001;
1737
1738 /**
1739 * Indicates that this view is currently directly under the drag location in a
1740 * drag-and-drop operation involving content that it can accept. Cleared when
1741 * the drag exits the view, or when the drag operation concludes.
1742 * @hide
1743 */
1744 static final int DRAG_HOVERED = 0x00000002;
1745
Cibu Johny86666632010-02-22 13:01:02 -08001746 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001747 * Indicates whether the view layout direction has been resolved and drawn to the
1748 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001749 *
1750 * @hide
1751 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001752 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1753
1754 /**
1755 * Indicates whether the view layout direction has been resolved.
1756 *
1757 * @hide
1758 */
1759 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1760
Cibu Johny86666632010-02-22 13:01:02 -08001761
Adam Powell539ee872012-02-03 19:00:49 -08001762 /**
1763 * Indicates that the view is tracking some sort of transient state
1764 * that the app should not need to be aware of, but that the framework
1765 * should take special care to preserve.
1766 *
1767 * @hide
1768 */
1769 static final int HAS_TRANSIENT_STATE = 0x00000010;
1770
1771
Christopher Tate3d4bf172011-03-28 16:16:46 -07001772 /* End of masks for mPrivateFlags2 */
1773
1774 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1775
Chet Haasedaf98e92011-01-10 14:10:36 -08001776 /**
Adam Powell637d3372010-08-25 14:37:03 -07001777 * Always allow a user to over-scroll this view, provided it is a
1778 * view that can scroll.
1779 *
1780 * @see #getOverScrollMode()
1781 * @see #setOverScrollMode(int)
1782 */
1783 public static final int OVER_SCROLL_ALWAYS = 0;
1784
1785 /**
1786 * Allow a user to over-scroll this view only if the content is large
1787 * enough to meaningfully scroll, provided it is a view that can scroll.
1788 *
1789 * @see #getOverScrollMode()
1790 * @see #setOverScrollMode(int)
1791 */
1792 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1793
1794 /**
1795 * Never allow a user to over-scroll this view.
1796 *
1797 * @see #getOverScrollMode()
1798 * @see #setOverScrollMode(int)
1799 */
1800 public static final int OVER_SCROLL_NEVER = 2;
1801
1802 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001803 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001804 *
Joe Malin32736f02011-01-19 16:14:20 -08001805 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001806 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001807 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001808
1809 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001810 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1811 *
1812 * This is for use in games, book readers, video players, or any other "immersive" application
1813 * where the usual system chrome is deemed too distracting.
1814 *
1815 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001816 *
Joe Malin32736f02011-01-19 16:14:20 -08001817 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001818 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001819 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1820
1821 /**
1822 * View has requested that the system navigation be temporarily hidden.
1823 *
1824 * This is an even less obtrusive state than that called for by
1825 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1826 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1827 * those to disappear. This is useful (in conjunction with the
1828 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1829 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1830 * window flags) for displaying content using every last pixel on the display.
1831 *
1832 * There is a limitation: because navigation controls are so important, the least user
1833 * interaction will cause them to reappear immediately.
1834 *
1835 * @see #setSystemUiVisibility(int)
1836 */
1837 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1838
1839 /**
1840 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1841 */
1842 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1843
1844 /**
1845 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1846 */
1847 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001848
1849 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001850 * @hide
1851 *
1852 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1853 * out of the public fields to keep the undefined bits out of the developer's way.
1854 *
1855 * Flag to make the status bar not expandable. Unless you also
1856 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1857 */
1858 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1859
1860 /**
1861 * @hide
1862 *
1863 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1864 * out of the public fields to keep the undefined bits out of the developer's way.
1865 *
1866 * Flag to hide notification icons and scrolling ticker text.
1867 */
1868 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1869
1870 /**
1871 * @hide
1872 *
1873 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1874 * out of the public fields to keep the undefined bits out of the developer's way.
1875 *
1876 * Flag to disable incoming notification alerts. This will not block
1877 * icons, but it will block sound, vibrating and other visual or aural notifications.
1878 */
1879 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1880
1881 /**
1882 * @hide
1883 *
1884 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1885 * out of the public fields to keep the undefined bits out of the developer's way.
1886 *
1887 * Flag to hide only the scrolling ticker. Note that
1888 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1889 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1890 */
1891 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1892
1893 /**
1894 * @hide
1895 *
1896 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1897 * out of the public fields to keep the undefined bits out of the developer's way.
1898 *
1899 * Flag to hide the center system info area.
1900 */
1901 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1902
1903 /**
1904 * @hide
1905 *
1906 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1907 * out of the public fields to keep the undefined bits out of the developer's way.
1908 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001909 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001910 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1911 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001912 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001913
1914 /**
1915 * @hide
1916 *
1917 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1918 * out of the public fields to keep the undefined bits out of the developer's way.
1919 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001920 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001921 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1922 */
1923 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1924
1925 /**
1926 * @hide
1927 *
1928 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1929 * out of the public fields to keep the undefined bits out of the developer's way.
1930 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001931 * Flag to hide only the clock. You might use this if your activity has
1932 * its own clock making the status bar's clock redundant.
1933 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001934 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1935
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001936 /**
1937 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001938 *
1939 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1940 * out of the public fields to keep the undefined bits out of the developer's way.
1941 *
1942 * Flag to hide only the recent apps button. Don't use this
1943 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1944 */
1945 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1946
1947 /**
1948 * @hide
1949 *
1950 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1951 *
1952 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1953 */
1954 @Deprecated
1955 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1956 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1957
1958 /**
1959 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001960 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001961 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001962
1963 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001964 * These are the system UI flags that can be cleared by events outside
1965 * of an application. Currently this is just the ability to tap on the
1966 * screen while hiding the navigation bar to have it return.
1967 * @hide
1968 */
1969 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1970 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1971
1972 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001973 * Find views that render the specified text.
1974 *
1975 * @see #findViewsWithText(ArrayList, CharSequence, int)
1976 */
1977 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1978
1979 /**
1980 * Find find views that contain the specified content description.
1981 *
1982 * @see #findViewsWithText(ArrayList, CharSequence, int)
1983 */
1984 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1985
1986 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07001987 * Find views that contain {@link AccessibilityNodeProvider}. Such
1988 * a View is a root of virtual view hierarchy and may contain the searched
1989 * text. If this flag is set Views with providers are automatically
1990 * added and it is a responsibility of the client to call the APIs of
1991 * the provider to determine whether the virtual tree rooted at this View
1992 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
1993 * represeting the virtual views with this text.
1994 *
1995 * @see #findViewsWithText(ArrayList, CharSequence, int)
1996 *
1997 * @hide
1998 */
1999 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2000
2001 /**
Adam Powell637d3372010-08-25 14:37:03 -07002002 * Controls the over-scroll mode for this view.
2003 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2004 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2005 * and {@link #OVER_SCROLL_NEVER}.
2006 */
2007 private int mOverScrollMode;
2008
2009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 * The parent this view is attached to.
2011 * {@hide}
2012 *
2013 * @see #getParent()
2014 */
2015 protected ViewParent mParent;
2016
2017 /**
2018 * {@hide}
2019 */
2020 AttachInfo mAttachInfo;
2021
2022 /**
2023 * {@hide}
2024 */
Romain Guy809a7f62009-05-14 15:44:42 -07002025 @ViewDebug.ExportedProperty(flagMapping = {
2026 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2027 name = "FORCE_LAYOUT"),
2028 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2029 name = "LAYOUT_REQUIRED"),
2030 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002031 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002032 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2033 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2034 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2035 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2036 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002038 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039
2040 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002041 * This view's request for the visibility of the status bar.
2042 * @hide
2043 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002044 @ViewDebug.ExportedProperty(flagMapping = {
2045 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2046 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2047 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2048 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2049 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2050 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2051 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2052 equals = SYSTEM_UI_FLAG_VISIBLE,
2053 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2054 })
Joe Onorato664644d2011-01-23 17:53:23 -08002055 int mSystemUiVisibility;
2056
2057 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 * Count of how many windows this view has been attached to.
2059 */
2060 int mWindowAttachCount;
2061
2062 /**
2063 * The layout parameters associated with this view and used by the parent
2064 * {@link android.view.ViewGroup} to determine how this view should be
2065 * laid out.
2066 * {@hide}
2067 */
2068 protected ViewGroup.LayoutParams mLayoutParams;
2069
2070 /**
2071 * The view flags hold various views states.
2072 * {@hide}
2073 */
2074 @ViewDebug.ExportedProperty
2075 int mViewFlags;
2076
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002077 static class TransformationInfo {
2078 /**
2079 * The transform matrix for the View. This transform is calculated internally
2080 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2081 * is used by default. Do *not* use this variable directly; instead call
2082 * getMatrix(), which will automatically recalculate the matrix if necessary
2083 * to get the correct matrix based on the latest rotation and scale properties.
2084 */
2085 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002086
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002087 /**
2088 * The transform matrix for the View. This transform is calculated internally
2089 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2090 * is used by default. Do *not* use this variable directly; instead call
2091 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2092 * to get the correct matrix based on the latest rotation and scale properties.
2093 */
2094 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002095
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002096 /**
2097 * An internal variable that tracks whether we need to recalculate the
2098 * transform matrix, based on whether the rotation or scaleX/Y properties
2099 * have changed since the matrix was last calculated.
2100 */
2101 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002102
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002103 /**
2104 * An internal variable that tracks whether we need to recalculate the
2105 * transform matrix, based on whether the rotation or scaleX/Y properties
2106 * have changed since the matrix was last calculated.
2107 */
2108 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002109
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002110 /**
2111 * A variable that tracks whether we need to recalculate the
2112 * transform matrix, based on whether the rotation or scaleX/Y properties
2113 * have changed since the matrix was last calculated. This variable
2114 * is only valid after a call to updateMatrix() or to a function that
2115 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2116 */
2117 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002118
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002119 /**
2120 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2121 */
2122 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002123
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002124 /**
2125 * This matrix is used when computing the matrix for 3D rotations.
2126 */
2127 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002128
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002129 /**
2130 * These prev values are used to recalculate a centered pivot point when necessary. The
2131 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2132 * set), so thes values are only used then as well.
2133 */
2134 private int mPrevWidth = -1;
2135 private int mPrevHeight = -1;
2136
2137 /**
2138 * The degrees rotation around the vertical axis through the pivot point.
2139 */
2140 @ViewDebug.ExportedProperty
2141 float mRotationY = 0f;
2142
2143 /**
2144 * The degrees rotation around the horizontal axis through the pivot point.
2145 */
2146 @ViewDebug.ExportedProperty
2147 float mRotationX = 0f;
2148
2149 /**
2150 * The degrees rotation around the pivot point.
2151 */
2152 @ViewDebug.ExportedProperty
2153 float mRotation = 0f;
2154
2155 /**
2156 * The amount of translation of the object away from its left property (post-layout).
2157 */
2158 @ViewDebug.ExportedProperty
2159 float mTranslationX = 0f;
2160
2161 /**
2162 * The amount of translation of the object away from its top property (post-layout).
2163 */
2164 @ViewDebug.ExportedProperty
2165 float mTranslationY = 0f;
2166
2167 /**
2168 * The amount of scale in the x direction around the pivot point. A
2169 * value of 1 means no scaling is applied.
2170 */
2171 @ViewDebug.ExportedProperty
2172 float mScaleX = 1f;
2173
2174 /**
2175 * The amount of scale in the y direction around the pivot point. A
2176 * value of 1 means no scaling is applied.
2177 */
2178 @ViewDebug.ExportedProperty
2179 float mScaleY = 1f;
2180
2181 /**
Chet Haasea33de552012-02-03 16:28:24 -08002182 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002183 */
2184 @ViewDebug.ExportedProperty
2185 float mPivotX = 0f;
2186
2187 /**
Chet Haasea33de552012-02-03 16:28:24 -08002188 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002189 */
2190 @ViewDebug.ExportedProperty
2191 float mPivotY = 0f;
2192
2193 /**
2194 * The opacity of the View. This is a value from 0 to 1, where 0 means
2195 * completely transparent and 1 means completely opaque.
2196 */
2197 @ViewDebug.ExportedProperty
2198 float mAlpha = 1f;
2199 }
2200
2201 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002202
Joe Malin32736f02011-01-19 16:14:20 -08002203 private boolean mLastIsOpaque;
2204
Chet Haasefd2b0022010-08-06 13:08:56 -07002205 /**
2206 * Convenience value to check for float values that are close enough to zero to be considered
2207 * zero.
2208 */
Romain Guy2542d192010-08-18 11:47:12 -07002209 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002210
2211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 * The distance in pixels from the left edge of this view's parent
2213 * to the left edge of this view.
2214 * {@hide}
2215 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002216 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 protected int mLeft;
2218 /**
2219 * The distance in pixels from the left edge of this view's parent
2220 * to the right edge of this view.
2221 * {@hide}
2222 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002223 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 protected int mRight;
2225 /**
2226 * The distance in pixels from the top edge of this view's parent
2227 * to the top edge of this view.
2228 * {@hide}
2229 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002230 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 protected int mTop;
2232 /**
2233 * The distance in pixels from the top edge of this view's parent
2234 * to the bottom edge of this view.
2235 * {@hide}
2236 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002237 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 protected int mBottom;
2239
2240 /**
2241 * The offset, in pixels, by which the content of this view is scrolled
2242 * horizontally.
2243 * {@hide}
2244 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002245 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 protected int mScrollX;
2247 /**
2248 * The offset, in pixels, by which the content of this view is scrolled
2249 * vertically.
2250 * {@hide}
2251 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002252 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 protected int mScrollY;
2254
2255 /**
2256 * The left padding in pixels, that is the distance in pixels between the
2257 * left edge of this view and the left edge of its content.
2258 * {@hide}
2259 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002260 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 protected int mPaddingLeft;
2262 /**
2263 * The right padding in pixels, that is the distance in pixels between the
2264 * right edge of this view and the right edge of its content.
2265 * {@hide}
2266 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002267 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 protected int mPaddingRight;
2269 /**
2270 * The top padding in pixels, that is the distance in pixels between the
2271 * top edge of this view and the top edge of its content.
2272 * {@hide}
2273 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002274 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 protected int mPaddingTop;
2276 /**
2277 * The bottom padding in pixels, that is the distance in pixels between the
2278 * bottom edge of this view and the bottom edge of its content.
2279 * {@hide}
2280 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002281 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 protected int mPaddingBottom;
2283
2284 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002285 * Briefly describes the view and is primarily used for accessibility support.
2286 */
2287 private CharSequence mContentDescription;
2288
2289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002291 *
2292 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002294 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002295 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296
2297 /**
2298 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002299 *
2300 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002302 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002303 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002305 /**
Adam Powell20232d02010-12-08 21:08:53 -08002306 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002307 *
2308 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002309 */
2310 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002311 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002312
2313 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002314 * Cache if the user padding is relative.
2315 *
2316 */
2317 @ViewDebug.ExportedProperty(category = "padding")
2318 boolean mUserPaddingRelative;
2319
2320 /**
2321 * Cache the paddingStart set by the user to append to the scrollbar's size.
2322 *
2323 */
2324 @ViewDebug.ExportedProperty(category = "padding")
2325 int mUserPaddingStart;
2326
2327 /**
2328 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2329 *
2330 */
2331 @ViewDebug.ExportedProperty(category = "padding")
2332 int mUserPaddingEnd;
2333
2334 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002335 * @hide
2336 */
2337 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2338 /**
2339 * @hide
2340 */
2341 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 private Drawable mBGDrawable;
2344
2345 private int mBackgroundResource;
2346 private boolean mBackgroundSizeChanged;
2347
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002348 static class ListenerInfo {
2349 /**
2350 * Listener used to dispatch focus change events.
2351 * This field should be made private, so it is hidden from the SDK.
2352 * {@hide}
2353 */
2354 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002356 /**
2357 * Listeners for layout change events.
2358 */
2359 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002360
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002361 /**
2362 * Listeners for attach events.
2363 */
2364 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002365
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002366 /**
2367 * Listener used to dispatch click events.
2368 * This field should be made private, so it is hidden from the SDK.
2369 * {@hide}
2370 */
2371 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002373 /**
2374 * Listener used to dispatch long click events.
2375 * This field should be made private, so it is hidden from the SDK.
2376 * {@hide}
2377 */
2378 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002380 /**
2381 * Listener used to build the context menu.
2382 * This field should be made private, so it is hidden from the SDK.
2383 * {@hide}
2384 */
2385 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002387 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002389 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002391 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002392
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002393 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002394
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002395 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002396
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002397 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2398 }
2399
2400 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 /**
2403 * The application environment this view lives in.
2404 * This field should be made private, so it is hidden from the SDK.
2405 * {@hide}
2406 */
2407 protected Context mContext;
2408
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002409 private final Resources mResources;
2410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 private ScrollabilityCache mScrollCache;
2412
2413 private int[] mDrawableState = null;
2414
Romain Guy0211a0a2011-02-14 16:34:59 -08002415 /**
2416 * Set to true when drawing cache is enabled and cannot be created.
2417 *
2418 * @hide
2419 */
2420 public boolean mCachingFailed;
2421
Romain Guy02890fd2010-08-06 17:58:44 -07002422 private Bitmap mDrawingCache;
2423 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002424 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002425 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426
2427 /**
2428 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2429 * the user may specify which view to go to next.
2430 */
2431 private int mNextFocusLeftId = View.NO_ID;
2432
2433 /**
2434 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2435 * the user may specify which view to go to next.
2436 */
2437 private int mNextFocusRightId = View.NO_ID;
2438
2439 /**
2440 * When this view has focus and the next focus is {@link #FOCUS_UP},
2441 * the user may specify which view to go to next.
2442 */
2443 private int mNextFocusUpId = View.NO_ID;
2444
2445 /**
2446 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2447 * the user may specify which view to go to next.
2448 */
2449 private int mNextFocusDownId = View.NO_ID;
2450
Jeff Brown4e6319b2010-12-13 10:36:51 -08002451 /**
2452 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2453 * the user may specify which view to go to next.
2454 */
2455 int mNextFocusForwardId = View.NO_ID;
2456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002458 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002459 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002460 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 private UnsetPressedState mUnsetPressedState;
2463
2464 /**
2465 * Whether the long press's action has been invoked. The tap's action is invoked on the
2466 * up event while a long press is invoked as soon as the long press duration is reached, so
2467 * a long press could be performed before the tap is checked, in which case the tap's action
2468 * should not be invoked.
2469 */
2470 private boolean mHasPerformedLongPress;
2471
2472 /**
2473 * The minimum height of the view. We'll try our best to have the height
2474 * of this view to at least this amount.
2475 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002476 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 private int mMinHeight;
2478
2479 /**
2480 * The minimum width of the view. We'll try our best to have the width
2481 * of this view to at least this amount.
2482 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002483 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 private int mMinWidth;
2485
2486 /**
2487 * The delegate to handle touch events that are physically in this view
2488 * but should be handled by another view.
2489 */
2490 private TouchDelegate mTouchDelegate = null;
2491
2492 /**
2493 * Solid color to use as a background when creating the drawing cache. Enables
2494 * the cache to use 16 bit bitmaps instead of 32 bit.
2495 */
2496 private int mDrawingCacheBackgroundColor = 0;
2497
2498 /**
2499 * Special tree observer used when mAttachInfo is null.
2500 */
2501 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002502
Adam Powelle14579b2009-12-16 18:39:52 -08002503 /**
2504 * Cache the touch slop from the context that created the view.
2505 */
2506 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002509 * Object that handles automatic animation of view properties.
2510 */
2511 private ViewPropertyAnimator mAnimator = null;
2512
2513 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002514 * Flag indicating that a drag can cross window boundaries. When
2515 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2516 * with this flag set, all visible applications will be able to participate
2517 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002518 *
2519 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002520 */
2521 public static final int DRAG_FLAG_GLOBAL = 1;
2522
2523 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002524 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2525 */
2526 private float mVerticalScrollFactor;
2527
2528 /**
Adam Powell20232d02010-12-08 21:08:53 -08002529 * Position of the vertical scroll bar.
2530 */
2531 private int mVerticalScrollbarPosition;
2532
2533 /**
2534 * Position the scroll bar at the default position as determined by the system.
2535 */
2536 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2537
2538 /**
2539 * Position the scroll bar along the left edge.
2540 */
2541 public static final int SCROLLBAR_POSITION_LEFT = 1;
2542
2543 /**
2544 * Position the scroll bar along the right edge.
2545 */
2546 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2547
2548 /**
Romain Guy171c5922011-01-06 10:04:23 -08002549 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002550 *
2551 * @see #getLayerType()
2552 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002553 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002554 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002555 */
2556 public static final int LAYER_TYPE_NONE = 0;
2557
2558 /**
2559 * <p>Indicates that the view has a software layer. A software layer is backed
2560 * by a bitmap and causes the view to be rendered using Android's software
2561 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002562 *
Romain Guy171c5922011-01-06 10:04:23 -08002563 * <p>Software layers have various usages:</p>
2564 * <p>When the application is not using hardware acceleration, a software layer
2565 * is useful to apply a specific color filter and/or blending mode and/or
2566 * translucency to a view and all its children.</p>
2567 * <p>When the application is using hardware acceleration, a software layer
2568 * is useful to render drawing primitives not supported by the hardware
2569 * accelerated pipeline. It can also be used to cache a complex view tree
2570 * into a texture and reduce the complexity of drawing operations. For instance,
2571 * when animating a complex view tree with a translation, a software layer can
2572 * be used to render the view tree only once.</p>
2573 * <p>Software layers should be avoided when the affected view tree updates
2574 * often. Every update will require to re-render the software layer, which can
2575 * potentially be slow (particularly when hardware acceleration is turned on
2576 * since the layer will have to be uploaded into a hardware texture after every
2577 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002578 *
2579 * @see #getLayerType()
2580 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002581 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002582 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002583 */
2584 public static final int LAYER_TYPE_SOFTWARE = 1;
2585
2586 /**
2587 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2588 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2589 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2590 * rendering pipeline, but only if hardware acceleration is turned on for the
2591 * view hierarchy. When hardware acceleration is turned off, hardware layers
2592 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002593 *
Romain Guy171c5922011-01-06 10:04:23 -08002594 * <p>A hardware layer is useful to apply a specific color filter and/or
2595 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002596 * <p>A hardware layer can be used to cache a complex view tree into a
2597 * texture and reduce the complexity of drawing operations. For instance,
2598 * when animating a complex view tree with a translation, a hardware layer can
2599 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002600 * <p>A hardware layer can also be used to increase the rendering quality when
2601 * rotation transformations are applied on a view. It can also be used to
2602 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002603 *
2604 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002605 * @see #setLayerType(int, android.graphics.Paint)
2606 * @see #LAYER_TYPE_NONE
2607 * @see #LAYER_TYPE_SOFTWARE
2608 */
2609 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002610
Romain Guy3aaff3a2011-01-12 14:18:47 -08002611 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2612 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2613 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2614 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2615 })
Romain Guy171c5922011-01-06 10:04:23 -08002616 int mLayerType = LAYER_TYPE_NONE;
2617 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002618 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002619
2620 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002621 * Set to true when the view is sending hover accessibility events because it
2622 * is the innermost hovered view.
2623 */
2624 private boolean mSendingHoverAccessibilityEvents;
2625
2626 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002627 * Delegate for injecting accessiblity functionality.
2628 */
2629 AccessibilityDelegate mAccessibilityDelegate;
2630
2631 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002632 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002633 */
2634 public static final int TEXT_DIRECTION_INHERIT = 0;
2635
2636 /**
2637 * Text direction is using "first strong algorithm". The first strong directional character
2638 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002639 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002640 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002641 */
2642 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2643
2644 /**
2645 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2646 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002647 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002648 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002649 */
2650 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2651
2652 /**
2653 * Text direction is forced to LTR.
2654 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002655 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002656 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002657
2658 /**
2659 * Text direction is forced to RTL.
2660 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002661 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002662 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002663
2664 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002665 * Text direction is coming from the system Locale.
2666 *
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002667 */
2668 public static final int TEXT_DIRECTION_LOCALE = 5;
2669
2670 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002671 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002672 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002673 */
2674 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2675
2676 /**
2677 * The text direction that has been defined by {@link #setTextDirection(int)}.
2678 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002679 */
2680 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002681 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2682 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2683 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2684 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002685 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2686 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002687 })
Doug Feltcb3791202011-07-07 11:57:48 -07002688 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002689
2690 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002691 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002692 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002693 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2694 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002695 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002696 */
2697 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002698 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2699 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2700 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002701 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002702 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2703 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002704 })
Doug Feltcb3791202011-07-07 11:57:48 -07002705 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002706
2707 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002708 * Consistency verifier for debugging purposes.
2709 * @hide
2710 */
2711 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2712 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2713 new InputEventConsistencyVerifier(this, 0) : null;
2714
2715 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 * Simple constructor to use when creating a view from code.
2717 *
2718 * @param context The Context the view is running in, through which it can
2719 * access the current theme, resources, etc.
2720 */
2721 public View(Context context) {
2722 mContext = context;
2723 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002724 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002725 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002726 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002727 mUserPaddingStart = -1;
2728 mUserPaddingEnd = -1;
2729 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 }
2731
2732 /**
2733 * Constructor that is called when inflating a view from XML. This is called
2734 * when a view is being constructed from an XML file, supplying attributes
2735 * that were specified in the XML file. This version uses a default style of
2736 * 0, so the only attribute values applied are those in the Context's Theme
2737 * and the given AttributeSet.
2738 *
2739 * <p>
2740 * The method onFinishInflate() will be called after all children have been
2741 * added.
2742 *
2743 * @param context The Context the view is running in, through which it can
2744 * access the current theme, resources, etc.
2745 * @param attrs The attributes of the XML tag that is inflating the view.
2746 * @see #View(Context, AttributeSet, int)
2747 */
2748 public View(Context context, AttributeSet attrs) {
2749 this(context, attrs, 0);
2750 }
2751
2752 /**
2753 * Perform inflation from XML and apply a class-specific base style. This
2754 * constructor of View allows subclasses to use their own base style when
2755 * they are inflating. For example, a Button class's constructor would call
2756 * this version of the super class constructor and supply
2757 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2758 * the theme's button style to modify all of the base view attributes (in
2759 * particular its background) as well as the Button class's attributes.
2760 *
2761 * @param context The Context the view is running in, through which it can
2762 * access the current theme, resources, etc.
2763 * @param attrs The attributes of the XML tag that is inflating the view.
2764 * @param defStyle The default style to apply to this view. If 0, no style
2765 * will be applied (beyond what is included in the theme). This may
2766 * either be an attribute resource, whose value will be retrieved
2767 * from the current theme, or an explicit style resource.
2768 * @see #View(Context, AttributeSet)
2769 */
2770 public View(Context context, AttributeSet attrs, int defStyle) {
2771 this(context);
2772
2773 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2774 defStyle, 0);
2775
2776 Drawable background = null;
2777
2778 int leftPadding = -1;
2779 int topPadding = -1;
2780 int rightPadding = -1;
2781 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002782 int startPadding = -1;
2783 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784
2785 int padding = -1;
2786
2787 int viewFlagValues = 0;
2788 int viewFlagMasks = 0;
2789
2790 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 int x = 0;
2793 int y = 0;
2794
Chet Haase73066682010-11-29 15:55:32 -08002795 float tx = 0;
2796 float ty = 0;
2797 float rotation = 0;
2798 float rotationX = 0;
2799 float rotationY = 0;
2800 float sx = 1f;
2801 float sy = 1f;
2802 boolean transformSet = false;
2803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2805
Adam Powell637d3372010-08-25 14:37:03 -07002806 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 final int N = a.getIndexCount();
2808 for (int i = 0; i < N; i++) {
2809 int attr = a.getIndex(i);
2810 switch (attr) {
2811 case com.android.internal.R.styleable.View_background:
2812 background = a.getDrawable(attr);
2813 break;
2814 case com.android.internal.R.styleable.View_padding:
2815 padding = a.getDimensionPixelSize(attr, -1);
2816 break;
2817 case com.android.internal.R.styleable.View_paddingLeft:
2818 leftPadding = a.getDimensionPixelSize(attr, -1);
2819 break;
2820 case com.android.internal.R.styleable.View_paddingTop:
2821 topPadding = a.getDimensionPixelSize(attr, -1);
2822 break;
2823 case com.android.internal.R.styleable.View_paddingRight:
2824 rightPadding = a.getDimensionPixelSize(attr, -1);
2825 break;
2826 case com.android.internal.R.styleable.View_paddingBottom:
2827 bottomPadding = a.getDimensionPixelSize(attr, -1);
2828 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002829 case com.android.internal.R.styleable.View_paddingStart:
2830 startPadding = a.getDimensionPixelSize(attr, -1);
2831 break;
2832 case com.android.internal.R.styleable.View_paddingEnd:
2833 endPadding = a.getDimensionPixelSize(attr, -1);
2834 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 case com.android.internal.R.styleable.View_scrollX:
2836 x = a.getDimensionPixelOffset(attr, 0);
2837 break;
2838 case com.android.internal.R.styleable.View_scrollY:
2839 y = a.getDimensionPixelOffset(attr, 0);
2840 break;
Chet Haase73066682010-11-29 15:55:32 -08002841 case com.android.internal.R.styleable.View_alpha:
2842 setAlpha(a.getFloat(attr, 1f));
2843 break;
2844 case com.android.internal.R.styleable.View_transformPivotX:
2845 setPivotX(a.getDimensionPixelOffset(attr, 0));
2846 break;
2847 case com.android.internal.R.styleable.View_transformPivotY:
2848 setPivotY(a.getDimensionPixelOffset(attr, 0));
2849 break;
2850 case com.android.internal.R.styleable.View_translationX:
2851 tx = a.getDimensionPixelOffset(attr, 0);
2852 transformSet = true;
2853 break;
2854 case com.android.internal.R.styleable.View_translationY:
2855 ty = a.getDimensionPixelOffset(attr, 0);
2856 transformSet = true;
2857 break;
2858 case com.android.internal.R.styleable.View_rotation:
2859 rotation = a.getFloat(attr, 0);
2860 transformSet = true;
2861 break;
2862 case com.android.internal.R.styleable.View_rotationX:
2863 rotationX = a.getFloat(attr, 0);
2864 transformSet = true;
2865 break;
2866 case com.android.internal.R.styleable.View_rotationY:
2867 rotationY = a.getFloat(attr, 0);
2868 transformSet = true;
2869 break;
2870 case com.android.internal.R.styleable.View_scaleX:
2871 sx = a.getFloat(attr, 1f);
2872 transformSet = true;
2873 break;
2874 case com.android.internal.R.styleable.View_scaleY:
2875 sy = a.getFloat(attr, 1f);
2876 transformSet = true;
2877 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 case com.android.internal.R.styleable.View_id:
2879 mID = a.getResourceId(attr, NO_ID);
2880 break;
2881 case com.android.internal.R.styleable.View_tag:
2882 mTag = a.getText(attr);
2883 break;
2884 case com.android.internal.R.styleable.View_fitsSystemWindows:
2885 if (a.getBoolean(attr, false)) {
2886 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2887 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2888 }
2889 break;
2890 case com.android.internal.R.styleable.View_focusable:
2891 if (a.getBoolean(attr, false)) {
2892 viewFlagValues |= FOCUSABLE;
2893 viewFlagMasks |= FOCUSABLE_MASK;
2894 }
2895 break;
2896 case com.android.internal.R.styleable.View_focusableInTouchMode:
2897 if (a.getBoolean(attr, false)) {
2898 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2899 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2900 }
2901 break;
2902 case com.android.internal.R.styleable.View_clickable:
2903 if (a.getBoolean(attr, false)) {
2904 viewFlagValues |= CLICKABLE;
2905 viewFlagMasks |= CLICKABLE;
2906 }
2907 break;
2908 case com.android.internal.R.styleable.View_longClickable:
2909 if (a.getBoolean(attr, false)) {
2910 viewFlagValues |= LONG_CLICKABLE;
2911 viewFlagMasks |= LONG_CLICKABLE;
2912 }
2913 break;
2914 case com.android.internal.R.styleable.View_saveEnabled:
2915 if (!a.getBoolean(attr, true)) {
2916 viewFlagValues |= SAVE_DISABLED;
2917 viewFlagMasks |= SAVE_DISABLED_MASK;
2918 }
2919 break;
2920 case com.android.internal.R.styleable.View_duplicateParentState:
2921 if (a.getBoolean(attr, false)) {
2922 viewFlagValues |= DUPLICATE_PARENT_STATE;
2923 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2924 }
2925 break;
2926 case com.android.internal.R.styleable.View_visibility:
2927 final int visibility = a.getInt(attr, 0);
2928 if (visibility != 0) {
2929 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2930 viewFlagMasks |= VISIBILITY_MASK;
2931 }
2932 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002933 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002934 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002935 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002936 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002937 final int layoutDirection = a.getInt(attr, -1);
2938 if (layoutDirection != -1) {
2939 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002940 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002941 // Set to default (LAYOUT_DIRECTION_INHERIT)
2942 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002943 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002944 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002945 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 case com.android.internal.R.styleable.View_drawingCacheQuality:
2947 final int cacheQuality = a.getInt(attr, 0);
2948 if (cacheQuality != 0) {
2949 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2950 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2951 }
2952 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002953 case com.android.internal.R.styleable.View_contentDescription:
2954 mContentDescription = a.getString(attr);
2955 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2957 if (!a.getBoolean(attr, true)) {
2958 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2959 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2960 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002961 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2963 if (!a.getBoolean(attr, true)) {
2964 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2965 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2966 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002967 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 case R.styleable.View_scrollbars:
2969 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2970 if (scrollbars != SCROLLBARS_NONE) {
2971 viewFlagValues |= scrollbars;
2972 viewFlagMasks |= SCROLLBARS_MASK;
2973 initializeScrollbars(a);
2974 }
2975 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002976 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002978 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2979 // Ignore the attribute starting with ICS
2980 break;
2981 }
2982 // With builds < ICS, fall through and apply fading edges
2983 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2985 if (fadingEdge != FADING_EDGE_NONE) {
2986 viewFlagValues |= fadingEdge;
2987 viewFlagMasks |= FADING_EDGE_MASK;
2988 initializeFadingEdge(a);
2989 }
2990 break;
2991 case R.styleable.View_scrollbarStyle:
2992 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2993 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2994 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2995 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2996 }
2997 break;
2998 case R.styleable.View_isScrollContainer:
2999 setScrollContainer = true;
3000 if (a.getBoolean(attr, false)) {
3001 setScrollContainer(true);
3002 }
3003 break;
3004 case com.android.internal.R.styleable.View_keepScreenOn:
3005 if (a.getBoolean(attr, false)) {
3006 viewFlagValues |= KEEP_SCREEN_ON;
3007 viewFlagMasks |= KEEP_SCREEN_ON;
3008 }
3009 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003010 case R.styleable.View_filterTouchesWhenObscured:
3011 if (a.getBoolean(attr, false)) {
3012 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3013 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3014 }
3015 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 case R.styleable.View_nextFocusLeft:
3017 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3018 break;
3019 case R.styleable.View_nextFocusRight:
3020 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3021 break;
3022 case R.styleable.View_nextFocusUp:
3023 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3024 break;
3025 case R.styleable.View_nextFocusDown:
3026 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3027 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003028 case R.styleable.View_nextFocusForward:
3029 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3030 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 case R.styleable.View_minWidth:
3032 mMinWidth = a.getDimensionPixelSize(attr, 0);
3033 break;
3034 case R.styleable.View_minHeight:
3035 mMinHeight = a.getDimensionPixelSize(attr, 0);
3036 break;
Romain Guy9a817362009-05-01 10:57:14 -07003037 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003038 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003039 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003040 + "be used within a restricted context");
3041 }
3042
Romain Guy9a817362009-05-01 10:57:14 -07003043 final String handlerName = a.getString(attr);
3044 if (handlerName != null) {
3045 setOnClickListener(new OnClickListener() {
3046 private Method mHandler;
3047
3048 public void onClick(View v) {
3049 if (mHandler == null) {
3050 try {
3051 mHandler = getContext().getClass().getMethod(handlerName,
3052 View.class);
3053 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003054 int id = getId();
3055 String idText = id == NO_ID ? "" : " with id '"
3056 + getContext().getResources().getResourceEntryName(
3057 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003058 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003059 handlerName + "(View) in the activity "
3060 + getContext().getClass() + " for onClick handler"
3061 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003062 }
3063 }
3064
3065 try {
3066 mHandler.invoke(getContext(), View.this);
3067 } catch (IllegalAccessException e) {
3068 throw new IllegalStateException("Could not execute non "
3069 + "public method of the activity", e);
3070 } catch (InvocationTargetException e) {
3071 throw new IllegalStateException("Could not execute "
3072 + "method of the activity", e);
3073 }
3074 }
3075 });
3076 }
3077 break;
Adam Powell637d3372010-08-25 14:37:03 -07003078 case R.styleable.View_overScrollMode:
3079 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3080 break;
Adam Powell20232d02010-12-08 21:08:53 -08003081 case R.styleable.View_verticalScrollbarPosition:
3082 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3083 break;
Romain Guy171c5922011-01-06 10:04:23 -08003084 case R.styleable.View_layerType:
3085 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3086 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003087 case R.styleable.View_textDirection:
3088 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3089 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 }
3091 }
3092
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003093 a.recycle();
3094
Adam Powell637d3372010-08-25 14:37:03 -07003095 setOverScrollMode(overScrollMode);
3096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 if (background != null) {
3098 setBackgroundDrawable(background);
3099 }
3100
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003101 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3102
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003103 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3104 // layout direction). Those cached values will be used later during padding resolution.
3105 mUserPaddingStart = startPadding;
3106 mUserPaddingEnd = endPadding;
3107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 if (padding >= 0) {
3109 leftPadding = padding;
3110 topPadding = padding;
3111 rightPadding = padding;
3112 bottomPadding = padding;
3113 }
3114
3115 // If the user specified the padding (either with android:padding or
3116 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3117 // use the default padding or the padding from the background drawable
3118 // (stored at this point in mPadding*)
3119 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3120 topPadding >= 0 ? topPadding : mPaddingTop,
3121 rightPadding >= 0 ? rightPadding : mPaddingRight,
3122 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3123
3124 if (viewFlagMasks != 0) {
3125 setFlags(viewFlagValues, viewFlagMasks);
3126 }
3127
3128 // Needs to be called after mViewFlags is set
3129 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3130 recomputePadding();
3131 }
3132
3133 if (x != 0 || y != 0) {
3134 scrollTo(x, y);
3135 }
3136
Chet Haase73066682010-11-29 15:55:32 -08003137 if (transformSet) {
3138 setTranslationX(tx);
3139 setTranslationY(ty);
3140 setRotation(rotation);
3141 setRotationX(rotationX);
3142 setRotationY(rotationY);
3143 setScaleX(sx);
3144 setScaleY(sy);
3145 }
3146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3148 setScrollContainer(true);
3149 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003150
3151 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 }
3153
3154 /**
3155 * Non-public constructor for use in testing
3156 */
3157 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003158 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 }
3160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 /**
3162 * <p>
3163 * Initializes the fading edges from a given set of styled attributes. This
3164 * method should be called by subclasses that need fading edges and when an
3165 * instance of these subclasses is created programmatically rather than
3166 * being inflated from XML. This method is automatically called when the XML
3167 * is inflated.
3168 * </p>
3169 *
3170 * @param a the styled attributes set to initialize the fading edges from
3171 */
3172 protected void initializeFadingEdge(TypedArray a) {
3173 initScrollCache();
3174
3175 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3176 R.styleable.View_fadingEdgeLength,
3177 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3178 }
3179
3180 /**
3181 * Returns the size of the vertical faded edges used to indicate that more
3182 * content in this view is visible.
3183 *
3184 * @return The size in pixels of the vertical faded edge or 0 if vertical
3185 * faded edges are not enabled for this view.
3186 * @attr ref android.R.styleable#View_fadingEdgeLength
3187 */
3188 public int getVerticalFadingEdgeLength() {
3189 if (isVerticalFadingEdgeEnabled()) {
3190 ScrollabilityCache cache = mScrollCache;
3191 if (cache != null) {
3192 return cache.fadingEdgeLength;
3193 }
3194 }
3195 return 0;
3196 }
3197
3198 /**
3199 * Set the size of the faded edge used to indicate that more content in this
3200 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003201 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3202 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3203 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 *
3205 * @param length The size in pixels of the faded edge used to indicate that more
3206 * content in this view is visible.
3207 */
3208 public void setFadingEdgeLength(int length) {
3209 initScrollCache();
3210 mScrollCache.fadingEdgeLength = length;
3211 }
3212
3213 /**
3214 * Returns the size of the horizontal faded edges used to indicate that more
3215 * content in this view is visible.
3216 *
3217 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3218 * faded edges are not enabled for this view.
3219 * @attr ref android.R.styleable#View_fadingEdgeLength
3220 */
3221 public int getHorizontalFadingEdgeLength() {
3222 if (isHorizontalFadingEdgeEnabled()) {
3223 ScrollabilityCache cache = mScrollCache;
3224 if (cache != null) {
3225 return cache.fadingEdgeLength;
3226 }
3227 }
3228 return 0;
3229 }
3230
3231 /**
3232 * Returns the width of the vertical scrollbar.
3233 *
3234 * @return The width in pixels of the vertical scrollbar or 0 if there
3235 * is no vertical scrollbar.
3236 */
3237 public int getVerticalScrollbarWidth() {
3238 ScrollabilityCache cache = mScrollCache;
3239 if (cache != null) {
3240 ScrollBarDrawable scrollBar = cache.scrollBar;
3241 if (scrollBar != null) {
3242 int size = scrollBar.getSize(true);
3243 if (size <= 0) {
3244 size = cache.scrollBarSize;
3245 }
3246 return size;
3247 }
3248 return 0;
3249 }
3250 return 0;
3251 }
3252
3253 /**
3254 * Returns the height of the horizontal scrollbar.
3255 *
3256 * @return The height in pixels of the horizontal scrollbar or 0 if
3257 * there is no horizontal scrollbar.
3258 */
3259 protected int getHorizontalScrollbarHeight() {
3260 ScrollabilityCache cache = mScrollCache;
3261 if (cache != null) {
3262 ScrollBarDrawable scrollBar = cache.scrollBar;
3263 if (scrollBar != null) {
3264 int size = scrollBar.getSize(false);
3265 if (size <= 0) {
3266 size = cache.scrollBarSize;
3267 }
3268 return size;
3269 }
3270 return 0;
3271 }
3272 return 0;
3273 }
3274
3275 /**
3276 * <p>
3277 * Initializes the scrollbars from a given set of styled attributes. This
3278 * method should be called by subclasses that need scrollbars and when an
3279 * instance of these subclasses is created programmatically rather than
3280 * being inflated from XML. This method is automatically called when the XML
3281 * is inflated.
3282 * </p>
3283 *
3284 * @param a the styled attributes set to initialize the scrollbars from
3285 */
3286 protected void initializeScrollbars(TypedArray a) {
3287 initScrollCache();
3288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003290
Mike Cleronf116bf82009-09-27 19:14:12 -07003291 if (scrollabilityCache.scrollBar == null) {
3292 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3293 }
Joe Malin32736f02011-01-19 16:14:20 -08003294
Romain Guy8bda2482010-03-02 11:42:11 -08003295 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296
Mike Cleronf116bf82009-09-27 19:14:12 -07003297 if (!fadeScrollbars) {
3298 scrollabilityCache.state = ScrollabilityCache.ON;
3299 }
3300 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003301
3302
Mike Cleronf116bf82009-09-27 19:14:12 -07003303 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3304 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3305 .getScrollBarFadeDuration());
3306 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3307 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3308 ViewConfiguration.getScrollDefaultDelay());
3309
Joe Malin32736f02011-01-19 16:14:20 -08003310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3312 com.android.internal.R.styleable.View_scrollbarSize,
3313 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3314
3315 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3316 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3317
3318 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3319 if (thumb != null) {
3320 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3321 }
3322
3323 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3324 false);
3325 if (alwaysDraw) {
3326 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3327 }
3328
3329 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3330 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3331
3332 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3333 if (thumb != null) {
3334 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3335 }
3336
3337 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3338 false);
3339 if (alwaysDraw) {
3340 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3341 }
3342
3343 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003344 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 }
3346
3347 /**
3348 * <p>
3349 * Initalizes the scrollability cache if necessary.
3350 * </p>
3351 */
3352 private void initScrollCache() {
3353 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003354 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 }
3356 }
3357
3358 /**
Adam Powell20232d02010-12-08 21:08:53 -08003359 * Set the position of the vertical scroll bar. Should be one of
3360 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3361 * {@link #SCROLLBAR_POSITION_RIGHT}.
3362 *
3363 * @param position Where the vertical scroll bar should be positioned.
3364 */
3365 public void setVerticalScrollbarPosition(int position) {
3366 if (mVerticalScrollbarPosition != position) {
3367 mVerticalScrollbarPosition = position;
3368 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003369 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003370 }
3371 }
3372
3373 /**
3374 * @return The position where the vertical scroll bar will show, if applicable.
3375 * @see #setVerticalScrollbarPosition(int)
3376 */
3377 public int getVerticalScrollbarPosition() {
3378 return mVerticalScrollbarPosition;
3379 }
3380
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003381 ListenerInfo getListenerInfo() {
3382 if (mListenerInfo != null) {
3383 return mListenerInfo;
3384 }
3385 mListenerInfo = new ListenerInfo();
3386 return mListenerInfo;
3387 }
3388
Adam Powell20232d02010-12-08 21:08:53 -08003389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 * Register a callback to be invoked when focus of this view changed.
3391 *
3392 * @param l The callback that will run.
3393 */
3394 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003395 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 }
3397
3398 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003399 * Add a listener that will be called when the bounds of the view change due to
3400 * layout processing.
3401 *
3402 * @param listener The listener that will be called when layout bounds change.
3403 */
3404 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003405 ListenerInfo li = getListenerInfo();
3406 if (li.mOnLayoutChangeListeners == null) {
3407 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003408 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003409 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3410 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003411 }
Chet Haase21cd1382010-09-01 17:42:29 -07003412 }
3413
3414 /**
3415 * Remove a listener for layout changes.
3416 *
3417 * @param listener The listener for layout bounds change.
3418 */
3419 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003420 ListenerInfo li = mListenerInfo;
3421 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003422 return;
3423 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003424 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003425 }
3426
3427 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003428 * Add a listener for attach state changes.
3429 *
3430 * This listener will be called whenever this view is attached or detached
3431 * from a window. Remove the listener using
3432 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3433 *
3434 * @param listener Listener to attach
3435 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3436 */
3437 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003438 ListenerInfo li = getListenerInfo();
3439 if (li.mOnAttachStateChangeListeners == null) {
3440 li.mOnAttachStateChangeListeners
3441 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003442 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003443 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003444 }
3445
3446 /**
3447 * Remove a listener for attach state changes. The listener will receive no further
3448 * notification of window attach/detach events.
3449 *
3450 * @param listener Listener to remove
3451 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3452 */
3453 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003454 ListenerInfo li = mListenerInfo;
3455 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003456 return;
3457 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003458 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003459 }
3460
3461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 * Returns the focus-change callback registered for this view.
3463 *
3464 * @return The callback, or null if one is not registered.
3465 */
3466 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003467 ListenerInfo li = mListenerInfo;
3468 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 }
3470
3471 /**
3472 * Register a callback to be invoked when this view is clicked. If this view is not
3473 * clickable, it becomes clickable.
3474 *
3475 * @param l The callback that will run
3476 *
3477 * @see #setClickable(boolean)
3478 */
3479 public void setOnClickListener(OnClickListener l) {
3480 if (!isClickable()) {
3481 setClickable(true);
3482 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003483 getListenerInfo().mOnClickListener = l;
3484 }
3485
3486 /**
3487 * Return whether this view has an attached OnClickListener. Returns
3488 * true if there is a listener, false if there is none.
3489 */
3490 public boolean hasOnClickListeners() {
3491 ListenerInfo li = mListenerInfo;
3492 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
3494
3495 /**
3496 * Register a callback to be invoked when this view is clicked and held. If this view is not
3497 * long clickable, it becomes long clickable.
3498 *
3499 * @param l The callback that will run
3500 *
3501 * @see #setLongClickable(boolean)
3502 */
3503 public void setOnLongClickListener(OnLongClickListener l) {
3504 if (!isLongClickable()) {
3505 setLongClickable(true);
3506 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003507 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 }
3509
3510 /**
3511 * Register a callback to be invoked when the context menu for this view is
3512 * being built. If this view is not long clickable, it becomes long clickable.
3513 *
3514 * @param l The callback that will run
3515 *
3516 */
3517 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3518 if (!isLongClickable()) {
3519 setLongClickable(true);
3520 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003521 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 }
3523
3524 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003525 * Call this view's OnClickListener, if it is defined. Performs all normal
3526 * actions associated with clicking: reporting accessibility event, playing
3527 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 *
3529 * @return True there was an assigned OnClickListener that was called, false
3530 * otherwise is returned.
3531 */
3532 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003533 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3534
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003535 ListenerInfo li = mListenerInfo;
3536 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003538 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 return true;
3540 }
3541
3542 return false;
3543 }
3544
3545 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003546 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3547 * this only calls the listener, and does not do any associated clicking
3548 * actions like reporting an accessibility event.
3549 *
3550 * @return True there was an assigned OnClickListener that was called, false
3551 * otherwise is returned.
3552 */
3553 public boolean callOnClick() {
3554 ListenerInfo li = mListenerInfo;
3555 if (li != null && li.mOnClickListener != null) {
3556 li.mOnClickListener.onClick(this);
3557 return true;
3558 }
3559 return false;
3560 }
3561
3562 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003563 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3564 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003566 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 */
3568 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003569 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003572 ListenerInfo li = mListenerInfo;
3573 if (li != null && li.mOnLongClickListener != null) {
3574 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 }
3576 if (!handled) {
3577 handled = showContextMenu();
3578 }
3579 if (handled) {
3580 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3581 }
3582 return handled;
3583 }
3584
3585 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003586 * Performs button-related actions during a touch down event.
3587 *
3588 * @param event The event.
3589 * @return True if the down was consumed.
3590 *
3591 * @hide
3592 */
3593 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3594 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3595 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3596 return true;
3597 }
3598 }
3599 return false;
3600 }
3601
3602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 * Bring up the context menu for this view.
3604 *
3605 * @return Whether a context menu was displayed.
3606 */
3607 public boolean showContextMenu() {
3608 return getParent().showContextMenuForChild(this);
3609 }
3610
3611 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003612 * Bring up the context menu for this view, referring to the item under the specified point.
3613 *
3614 * @param x The referenced x coordinate.
3615 * @param y The referenced y coordinate.
3616 * @param metaState The keyboard modifiers that were pressed.
3617 * @return Whether a context menu was displayed.
3618 *
3619 * @hide
3620 */
3621 public boolean showContextMenu(float x, float y, int metaState) {
3622 return showContextMenu();
3623 }
3624
3625 /**
Adam Powell6e346362010-07-23 10:18:23 -07003626 * Start an action mode.
3627 *
3628 * @param callback Callback that will control the lifecycle of the action mode
3629 * @return The new action mode if it is started, null otherwise
3630 *
3631 * @see ActionMode
3632 */
3633 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003634 ViewParent parent = getParent();
3635 if (parent == null) return null;
3636 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003637 }
3638
3639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 * Register a callback to be invoked when a key is pressed in this view.
3641 * @param l the key listener to attach to this view
3642 */
3643 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003644 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 }
3646
3647 /**
3648 * Register a callback to be invoked when a touch event is sent to this view.
3649 * @param l the touch listener to attach to this view
3650 */
3651 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003652 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 }
3654
3655 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003656 * Register a callback to be invoked when a generic motion event is sent to this view.
3657 * @param l the generic motion listener to attach to this view
3658 */
3659 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003660 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003661 }
3662
3663 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003664 * Register a callback to be invoked when a hover event is sent to this view.
3665 * @param l the hover listener to attach to this view
3666 */
3667 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003668 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003669 }
3670
3671 /**
Joe Malin32736f02011-01-19 16:14:20 -08003672 * Register a drag event listener callback object for this View. The parameter is
3673 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3674 * View, the system calls the
3675 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3676 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003677 */
3678 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003679 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003680 }
3681
3682 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003683 * Give this view focus. This will cause
3684 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 *
3686 * Note: this does not check whether this {@link View} should get focus, it just
3687 * gives it focus no matter what. It should only be called internally by framework
3688 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3689 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003690 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3691 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 * focus moved when requestFocus() is called. It may not always
3693 * apply, in which case use the default View.FOCUS_DOWN.
3694 * @param previouslyFocusedRect The rectangle of the view that had focus
3695 * prior in this View's coordinate system.
3696 */
3697 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3698 if (DBG) {
3699 System.out.println(this + " requestFocus()");
3700 }
3701
3702 if ((mPrivateFlags & FOCUSED) == 0) {
3703 mPrivateFlags |= FOCUSED;
3704
3705 if (mParent != null) {
3706 mParent.requestChildFocus(this, this);
3707 }
3708
3709 onFocusChanged(true, direction, previouslyFocusedRect);
3710 refreshDrawableState();
3711 }
3712 }
3713
3714 /**
3715 * Request that a rectangle of this view be visible on the screen,
3716 * scrolling if necessary just enough.
3717 *
3718 * <p>A View should call this if it maintains some notion of which part
3719 * of its content is interesting. For example, a text editing view
3720 * should call this when its cursor moves.
3721 *
3722 * @param rectangle The rectangle.
3723 * @return Whether any parent scrolled.
3724 */
3725 public boolean requestRectangleOnScreen(Rect rectangle) {
3726 return requestRectangleOnScreen(rectangle, false);
3727 }
3728
3729 /**
3730 * Request that a rectangle of this view be visible on the screen,
3731 * scrolling if necessary just enough.
3732 *
3733 * <p>A View should call this if it maintains some notion of which part
3734 * of its content is interesting. For example, a text editing view
3735 * should call this when its cursor moves.
3736 *
3737 * <p>When <code>immediate</code> is set to true, scrolling will not be
3738 * animated.
3739 *
3740 * @param rectangle The rectangle.
3741 * @param immediate True to forbid animated scrolling, false otherwise
3742 * @return Whether any parent scrolled.
3743 */
3744 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3745 View child = this;
3746 ViewParent parent = mParent;
3747 boolean scrolled = false;
3748 while (parent != null) {
3749 scrolled |= parent.requestChildRectangleOnScreen(child,
3750 rectangle, immediate);
3751
3752 // offset rect so next call has the rectangle in the
3753 // coordinate system of its direct child.
3754 rectangle.offset(child.getLeft(), child.getTop());
3755 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3756
3757 if (!(parent instanceof View)) {
3758 break;
3759 }
Romain Guy8506ab42009-06-11 17:35:47 -07003760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 child = (View) parent;
3762 parent = child.getParent();
3763 }
3764 return scrolled;
3765 }
3766
3767 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003768 * Called when this view wants to give up focus. If focus is cleared
3769 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3770 * <p>
3771 * <strong>Note:</strong> When a View clears focus the framework is trying
3772 * to give focus to the first focusable View from the top. Hence, if this
3773 * View is the first from the top that can take focus, then its focus will
3774 * not be cleared nor will the focus change callback be invoked.
3775 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 */
3777 public void clearFocus() {
3778 if (DBG) {
3779 System.out.println(this + " clearFocus()");
3780 }
3781
3782 if ((mPrivateFlags & FOCUSED) != 0) {
3783 mPrivateFlags &= ~FOCUSED;
3784
3785 if (mParent != null) {
3786 mParent.clearChildFocus(this);
3787 }
3788
3789 onFocusChanged(false, 0, null);
3790 refreshDrawableState();
3791 }
3792 }
3793
3794 /**
3795 * Called to clear the focus of a view that is about to be removed.
3796 * Doesn't call clearChildFocus, which prevents this view from taking
3797 * focus again before it has been removed from the parent
3798 */
3799 void clearFocusForRemoval() {
3800 if ((mPrivateFlags & FOCUSED) != 0) {
3801 mPrivateFlags &= ~FOCUSED;
3802
3803 onFocusChanged(false, 0, null);
3804 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003805
3806 // The view cleared focus and invoked the callbacks, so now is the
3807 // time to give focus to the the first focusable from the top to
3808 // ensure that the gain focus is announced after clear focus.
3809 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 }
3811 }
3812
3813 /**
3814 * Called internally by the view system when a new view is getting focus.
3815 * This is what clears the old focus.
3816 */
3817 void unFocus() {
3818 if (DBG) {
3819 System.out.println(this + " unFocus()");
3820 }
3821
3822 if ((mPrivateFlags & FOCUSED) != 0) {
3823 mPrivateFlags &= ~FOCUSED;
3824
3825 onFocusChanged(false, 0, null);
3826 refreshDrawableState();
3827 }
3828 }
3829
3830 /**
3831 * Returns true if this view has focus iteself, or is the ancestor of the
3832 * view that has focus.
3833 *
3834 * @return True if this view has or contains focus, false otherwise.
3835 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003836 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 public boolean hasFocus() {
3838 return (mPrivateFlags & FOCUSED) != 0;
3839 }
3840
3841 /**
3842 * Returns true if this view is focusable or if it contains a reachable View
3843 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3844 * is a View whose parents do not block descendants focus.
3845 *
3846 * Only {@link #VISIBLE} views are considered focusable.
3847 *
3848 * @return True if the view is focusable or if the view contains a focusable
3849 * View, false otherwise.
3850 *
3851 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3852 */
3853 public boolean hasFocusable() {
3854 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3855 }
3856
3857 /**
3858 * Called by the view system when the focus state of this view changes.
3859 * When the focus change event is caused by directional navigation, direction
3860 * and previouslyFocusedRect provide insight into where the focus is coming from.
3861 * When overriding, be sure to call up through to the super class so that
3862 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 * @param gainFocus True if the View has focus; false otherwise.
3865 * @param direction The direction focus has moved when requestFocus()
3866 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003867 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3868 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3869 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3871 * system, of the previously focused view. If applicable, this will be
3872 * passed in as finer grained information about where the focus is coming
3873 * from (in addition to direction). Will be <code>null</code> otherwise.
3874 */
3875 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003876 if (gainFocus) {
3877 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3878 }
3879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 InputMethodManager imm = InputMethodManager.peekInstance();
3881 if (!gainFocus) {
3882 if (isPressed()) {
3883 setPressed(false);
3884 }
3885 if (imm != null && mAttachInfo != null
3886 && mAttachInfo.mHasWindowFocus) {
3887 imm.focusOut(this);
3888 }
Romain Guya2431d02009-04-30 16:30:00 -07003889 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 } else if (imm != null && mAttachInfo != null
3891 && mAttachInfo.mHasWindowFocus) {
3892 imm.focusIn(this);
3893 }
Romain Guy8506ab42009-06-11 17:35:47 -07003894
Romain Guy0fd89bf2011-01-26 15:41:30 -08003895 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003896 ListenerInfo li = mListenerInfo;
3897 if (li != null && li.mOnFocusChangeListener != null) {
3898 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 }
Joe Malin32736f02011-01-19 16:14:20 -08003900
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003901 if (mAttachInfo != null) {
3902 mAttachInfo.mKeyDispatchState.reset(this);
3903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 }
3905
3906 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003907 * Sends an accessibility event of the given type. If accessiiblity is
3908 * not enabled this method has no effect. The default implementation calls
3909 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3910 * to populate information about the event source (this View), then calls
3911 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3912 * populate the text content of the event source including its descendants,
3913 * and last calls
3914 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3915 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003916 * <p>
3917 * If an {@link AccessibilityDelegate} has been specified via calling
3918 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3919 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3920 * responsible for handling this call.
3921 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003922 *
Scott Mainb303d832011-10-12 16:45:18 -07003923 * @param eventType The type of the event to send, as defined by several types from
3924 * {@link android.view.accessibility.AccessibilityEvent}, such as
3925 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3926 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003927 *
3928 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3929 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3930 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003931 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003932 */
3933 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003934 if (mAccessibilityDelegate != null) {
3935 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3936 } else {
3937 sendAccessibilityEventInternal(eventType);
3938 }
3939 }
3940
3941 /**
3942 * @see #sendAccessibilityEvent(int)
3943 *
3944 * Note: Called from the default {@link AccessibilityDelegate}.
3945 */
3946 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003947 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3948 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3949 }
3950 }
3951
3952 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003953 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3954 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003955 * perform a check whether accessibility is enabled.
3956 * <p>
3957 * If an {@link AccessibilityDelegate} has been specified via calling
3958 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3959 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3960 * is responsible for handling this call.
3961 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003962 *
3963 * @param event The event to send.
3964 *
3965 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003966 */
3967 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003968 if (mAccessibilityDelegate != null) {
3969 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3970 } else {
3971 sendAccessibilityEventUncheckedInternal(event);
3972 }
3973 }
3974
3975 /**
3976 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3977 *
3978 * Note: Called from the default {@link AccessibilityDelegate}.
3979 */
3980 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003981 if (!isShown()) {
3982 return;
3983 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003984 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003985 // Only a subset of accessibility events populates text content.
3986 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3987 dispatchPopulateAccessibilityEvent(event);
3988 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003989 // In the beginning we called #isShown(), so we know that getParent() is not null.
3990 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003991 }
3992
3993 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003994 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3995 * to its children for adding their text content to the event. Note that the
3996 * event text is populated in a separate dispatch path since we add to the
3997 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003998 * A typical implementation will call
3999 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4000 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4001 * on each child. Override this method if custom population of the event text
4002 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004003 * <p>
4004 * If an {@link AccessibilityDelegate} has been specified via calling
4005 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4006 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4007 * is responsible for handling this call.
4008 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004009 * <p>
4010 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4011 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4012 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004013 *
4014 * @param event The event.
4015 *
4016 * @return True if the event population was completed.
4017 */
4018 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004019 if (mAccessibilityDelegate != null) {
4020 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4021 } else {
4022 return dispatchPopulateAccessibilityEventInternal(event);
4023 }
4024 }
4025
4026 /**
4027 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4028 *
4029 * Note: Called from the default {@link AccessibilityDelegate}.
4030 */
4031 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004032 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004033 return false;
4034 }
4035
4036 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004037 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004038 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004039 * text content. While this method is free to modify event
4040 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004041 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4042 * <p>
4043 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004044 * to the text added by the super implementation:
4045 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004046 * super.onPopulateAccessibilityEvent(event);
4047 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4048 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4049 * mCurrentDate.getTimeInMillis(), flags);
4050 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004051 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004052 * <p>
4053 * If an {@link AccessibilityDelegate} has been specified via calling
4054 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4055 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4056 * is responsible for handling this call.
4057 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004058 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4059 * information to the event, in case the default implementation has basic information to add.
4060 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004061 *
4062 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004063 *
4064 * @see #sendAccessibilityEvent(int)
4065 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004066 */
4067 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004068 if (mAccessibilityDelegate != null) {
4069 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4070 } else {
4071 onPopulateAccessibilityEventInternal(event);
4072 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004073 }
4074
4075 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004076 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4077 *
4078 * Note: Called from the default {@link AccessibilityDelegate}.
4079 */
4080 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4081
4082 }
4083
4084 /**
4085 * Initializes an {@link AccessibilityEvent} with information about
4086 * this View which is the event source. In other words, the source of
4087 * an accessibility event is the view whose state change triggered firing
4088 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004089 * <p>
4090 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004091 * to properties set by the super implementation:
4092 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4093 * super.onInitializeAccessibilityEvent(event);
4094 * event.setPassword(true);
4095 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004096 * <p>
4097 * If an {@link AccessibilityDelegate} has been specified via calling
4098 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4099 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4100 * is responsible for handling this call.
4101 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004102 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4103 * information to the event, in case the default implementation has basic information to add.
4104 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004105 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004106 *
4107 * @see #sendAccessibilityEvent(int)
4108 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4109 */
4110 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004111 if (mAccessibilityDelegate != null) {
4112 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4113 } else {
4114 onInitializeAccessibilityEventInternal(event);
4115 }
4116 }
4117
4118 /**
4119 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4120 *
4121 * Note: Called from the default {@link AccessibilityDelegate}.
4122 */
4123 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004124 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004125 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004126 event.setPackageName(getContext().getPackageName());
4127 event.setEnabled(isEnabled());
4128 event.setContentDescription(mContentDescription);
4129
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004130 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4131 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4132 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4133 FOCUSABLES_ALL);
4134 event.setItemCount(focusablesTempList.size());
4135 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4136 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004137 }
4138 }
4139
4140 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004141 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4142 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4143 * This method is responsible for obtaining an accessibility node info from a
4144 * pool of reusable instances and calling
4145 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4146 * initialize the former.
4147 * <p>
4148 * Note: The client is responsible for recycling the obtained instance by calling
4149 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4150 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004151 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004152 * @return A populated {@link AccessibilityNodeInfo}.
4153 *
4154 * @see AccessibilityNodeInfo
4155 */
4156 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004157 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4158 if (provider != null) {
4159 return provider.createAccessibilityNodeInfo(View.NO_ID);
4160 } else {
4161 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4162 onInitializeAccessibilityNodeInfo(info);
4163 return info;
4164 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004165 }
4166
4167 /**
4168 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4169 * The base implementation sets:
4170 * <ul>
4171 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004172 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4173 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004174 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4175 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4176 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4177 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4178 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4179 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4180 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4181 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4182 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4183 * </ul>
4184 * <p>
4185 * Subclasses should override this method, call the super implementation,
4186 * and set additional attributes.
4187 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004188 * <p>
4189 * If an {@link AccessibilityDelegate} has been specified via calling
4190 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4191 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4192 * is responsible for handling this call.
4193 * </p>
4194 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004195 * @param info The instance to initialize.
4196 */
4197 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004198 if (mAccessibilityDelegate != null) {
4199 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4200 } else {
4201 onInitializeAccessibilityNodeInfoInternal(info);
4202 }
4203 }
4204
4205 /**
4206 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4207 *
4208 * Note: Called from the default {@link AccessibilityDelegate}.
4209 */
4210 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004211 Rect bounds = mAttachInfo.mTmpInvalRect;
4212 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004213 info.setBoundsInParent(bounds);
4214
4215 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4216 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004217 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004218 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4219 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004220
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004221 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4222 ViewParent parent = getParent();
4223 if (parent instanceof View) {
4224 View parentView = (View) parent;
4225 info.setParent(parentView);
4226 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004227 }
4228
4229 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004230 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004231 info.setContentDescription(getContentDescription());
4232
4233 info.setEnabled(isEnabled());
4234 info.setClickable(isClickable());
4235 info.setFocusable(isFocusable());
4236 info.setFocused(isFocused());
4237 info.setSelected(isSelected());
4238 info.setLongClickable(isLongClickable());
4239
4240 // TODO: These make sense only if we are in an AdapterView but all
4241 // views can be selected. Maybe from accessiiblity perspective
4242 // we should report as selectable view in an AdapterView.
4243 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4244 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4245
4246 if (isFocusable()) {
4247 if (isFocused()) {
4248 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4249 } else {
4250 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4251 }
4252 }
4253 }
4254
4255 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004256 * Sets a delegate for implementing accessibility support via compositon as
4257 * opposed to inheritance. The delegate's primary use is for implementing
4258 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4259 *
4260 * @param delegate The delegate instance.
4261 *
4262 * @see AccessibilityDelegate
4263 */
4264 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4265 mAccessibilityDelegate = delegate;
4266 }
4267
4268 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004269 * Gets the provider for managing a virtual view hierarchy rooted at this View
4270 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4271 * that explore the window content.
4272 * <p>
4273 * If this method returns an instance, this instance is responsible for managing
4274 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4275 * View including the one representing the View itself. Similarly the returned
4276 * instance is responsible for performing accessibility actions on any virtual
4277 * view or the root view itself.
4278 * </p>
4279 * <p>
4280 * If an {@link AccessibilityDelegate} has been specified via calling
4281 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4282 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4283 * is responsible for handling this call.
4284 * </p>
4285 *
4286 * @return The provider.
4287 *
4288 * @see AccessibilityNodeProvider
4289 */
4290 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4291 if (mAccessibilityDelegate != null) {
4292 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4293 } else {
4294 return null;
4295 }
4296 }
4297
4298 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004299 * Gets the unique identifier of this view on the screen for accessibility purposes.
4300 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4301 *
4302 * @return The view accessibility id.
4303 *
4304 * @hide
4305 */
4306 public int getAccessibilityViewId() {
4307 if (mAccessibilityViewId == NO_ID) {
4308 mAccessibilityViewId = sNextAccessibilityViewId++;
4309 }
4310 return mAccessibilityViewId;
4311 }
4312
4313 /**
4314 * Gets the unique identifier of the window in which this View reseides.
4315 *
4316 * @return The window accessibility id.
4317 *
4318 * @hide
4319 */
4320 public int getAccessibilityWindowId() {
4321 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4322 }
4323
4324 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004325 * Gets the {@link View} description. It briefly describes the view and is
4326 * primarily used for accessibility support. Set this property to enable
4327 * better accessibility support for your application. This is especially
4328 * true for views that do not have textual representation (For example,
4329 * ImageButton).
4330 *
4331 * @return The content descriptiopn.
4332 *
4333 * @attr ref android.R.styleable#View_contentDescription
4334 */
4335 public CharSequence getContentDescription() {
4336 return mContentDescription;
4337 }
4338
4339 /**
4340 * Sets the {@link View} description. It briefly describes the view and is
4341 * primarily used for accessibility support. Set this property to enable
4342 * better accessibility support for your application. This is especially
4343 * true for views that do not have textual representation (For example,
4344 * ImageButton).
4345 *
4346 * @param contentDescription The content description.
4347 *
4348 * @attr ref android.R.styleable#View_contentDescription
4349 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004350 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004351 public void setContentDescription(CharSequence contentDescription) {
4352 mContentDescription = contentDescription;
4353 }
4354
4355 /**
Romain Guya2431d02009-04-30 16:30:00 -07004356 * Invoked whenever this view loses focus, either by losing window focus or by losing
4357 * focus within its window. This method can be used to clear any state tied to the
4358 * focus. For instance, if a button is held pressed with the trackball and the window
4359 * loses focus, this method can be used to cancel the press.
4360 *
4361 * Subclasses of View overriding this method should always call super.onFocusLost().
4362 *
4363 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004364 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004365 *
4366 * @hide pending API council approval
4367 */
4368 protected void onFocusLost() {
4369 resetPressedState();
4370 }
4371
4372 private void resetPressedState() {
4373 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4374 return;
4375 }
4376
4377 if (isPressed()) {
4378 setPressed(false);
4379
4380 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004381 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004382 }
4383 }
4384 }
4385
4386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 * Returns true if this view has focus
4388 *
4389 * @return True if this view has focus, false otherwise.
4390 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004391 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 public boolean isFocused() {
4393 return (mPrivateFlags & FOCUSED) != 0;
4394 }
4395
4396 /**
4397 * Find the view in the hierarchy rooted at this view that currently has
4398 * focus.
4399 *
4400 * @return The view that currently has focus, or null if no focused view can
4401 * be found.
4402 */
4403 public View findFocus() {
4404 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4405 }
4406
4407 /**
4408 * Change whether this view is one of the set of scrollable containers in
4409 * its window. This will be used to determine whether the window can
4410 * resize or must pan when a soft input area is open -- scrollable
4411 * containers allow the window to use resize mode since the container
4412 * will appropriately shrink.
4413 */
4414 public void setScrollContainer(boolean isScrollContainer) {
4415 if (isScrollContainer) {
4416 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4417 mAttachInfo.mScrollContainers.add(this);
4418 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4419 }
4420 mPrivateFlags |= SCROLL_CONTAINER;
4421 } else {
4422 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4423 mAttachInfo.mScrollContainers.remove(this);
4424 }
4425 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4426 }
4427 }
4428
4429 /**
4430 * Returns the quality of the drawing cache.
4431 *
4432 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4433 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4434 *
4435 * @see #setDrawingCacheQuality(int)
4436 * @see #setDrawingCacheEnabled(boolean)
4437 * @see #isDrawingCacheEnabled()
4438 *
4439 * @attr ref android.R.styleable#View_drawingCacheQuality
4440 */
4441 public int getDrawingCacheQuality() {
4442 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4443 }
4444
4445 /**
4446 * Set the drawing cache quality of this view. This value is used only when the
4447 * drawing cache is enabled
4448 *
4449 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4450 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4451 *
4452 * @see #getDrawingCacheQuality()
4453 * @see #setDrawingCacheEnabled(boolean)
4454 * @see #isDrawingCacheEnabled()
4455 *
4456 * @attr ref android.R.styleable#View_drawingCacheQuality
4457 */
4458 public void setDrawingCacheQuality(int quality) {
4459 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4460 }
4461
4462 /**
4463 * Returns whether the screen should remain on, corresponding to the current
4464 * value of {@link #KEEP_SCREEN_ON}.
4465 *
4466 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4467 *
4468 * @see #setKeepScreenOn(boolean)
4469 *
4470 * @attr ref android.R.styleable#View_keepScreenOn
4471 */
4472 public boolean getKeepScreenOn() {
4473 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4474 }
4475
4476 /**
4477 * Controls whether the screen should remain on, modifying the
4478 * value of {@link #KEEP_SCREEN_ON}.
4479 *
4480 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4481 *
4482 * @see #getKeepScreenOn()
4483 *
4484 * @attr ref android.R.styleable#View_keepScreenOn
4485 */
4486 public void setKeepScreenOn(boolean keepScreenOn) {
4487 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4488 }
4489
4490 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004491 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4492 * @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 -08004493 *
4494 * @attr ref android.R.styleable#View_nextFocusLeft
4495 */
4496 public int getNextFocusLeftId() {
4497 return mNextFocusLeftId;
4498 }
4499
4500 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004501 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4502 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4503 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 *
4505 * @attr ref android.R.styleable#View_nextFocusLeft
4506 */
4507 public void setNextFocusLeftId(int nextFocusLeftId) {
4508 mNextFocusLeftId = nextFocusLeftId;
4509 }
4510
4511 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004512 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4513 * @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 -08004514 *
4515 * @attr ref android.R.styleable#View_nextFocusRight
4516 */
4517 public int getNextFocusRightId() {
4518 return mNextFocusRightId;
4519 }
4520
4521 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004522 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4523 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4524 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 *
4526 * @attr ref android.R.styleable#View_nextFocusRight
4527 */
4528 public void setNextFocusRightId(int nextFocusRightId) {
4529 mNextFocusRightId = nextFocusRightId;
4530 }
4531
4532 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004533 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4534 * @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 -08004535 *
4536 * @attr ref android.R.styleable#View_nextFocusUp
4537 */
4538 public int getNextFocusUpId() {
4539 return mNextFocusUpId;
4540 }
4541
4542 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004543 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4544 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4545 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 *
4547 * @attr ref android.R.styleable#View_nextFocusUp
4548 */
4549 public void setNextFocusUpId(int nextFocusUpId) {
4550 mNextFocusUpId = nextFocusUpId;
4551 }
4552
4553 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004554 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4555 * @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 -08004556 *
4557 * @attr ref android.R.styleable#View_nextFocusDown
4558 */
4559 public int getNextFocusDownId() {
4560 return mNextFocusDownId;
4561 }
4562
4563 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004564 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4565 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4566 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 *
4568 * @attr ref android.R.styleable#View_nextFocusDown
4569 */
4570 public void setNextFocusDownId(int nextFocusDownId) {
4571 mNextFocusDownId = nextFocusDownId;
4572 }
4573
4574 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004575 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4576 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4577 *
4578 * @attr ref android.R.styleable#View_nextFocusForward
4579 */
4580 public int getNextFocusForwardId() {
4581 return mNextFocusForwardId;
4582 }
4583
4584 /**
4585 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4586 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4587 * decide automatically.
4588 *
4589 * @attr ref android.R.styleable#View_nextFocusForward
4590 */
4591 public void setNextFocusForwardId(int nextFocusForwardId) {
4592 mNextFocusForwardId = nextFocusForwardId;
4593 }
4594
4595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 * Returns the visibility of this view and all of its ancestors
4597 *
4598 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4599 */
4600 public boolean isShown() {
4601 View current = this;
4602 //noinspection ConstantConditions
4603 do {
4604 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4605 return false;
4606 }
4607 ViewParent parent = current.mParent;
4608 if (parent == null) {
4609 return false; // We are not attached to the view root
4610 }
4611 if (!(parent instanceof View)) {
4612 return true;
4613 }
4614 current = (View) parent;
4615 } while (current != null);
4616
4617 return false;
4618 }
4619
4620 /**
4621 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4622 * is set
4623 *
4624 * @param insets Insets for system windows
4625 *
4626 * @return True if this view applied the insets, false otherwise
4627 */
4628 protected boolean fitSystemWindows(Rect insets) {
4629 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4630 mPaddingLeft = insets.left;
4631 mPaddingTop = insets.top;
4632 mPaddingRight = insets.right;
4633 mPaddingBottom = insets.bottom;
4634 requestLayout();
4635 return true;
4636 }
4637 return false;
4638 }
4639
4640 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004641 * Set whether or not this view should account for system screen decorations
4642 * such as the status bar and inset its content. This allows this view to be
4643 * positioned in absolute screen coordinates and remain visible to the user.
4644 *
4645 * <p>This should only be used by top-level window decor views.
4646 *
4647 * @param fitSystemWindows true to inset content for system screen decorations, false for
4648 * default behavior.
4649 *
4650 * @attr ref android.R.styleable#View_fitsSystemWindows
4651 */
4652 public void setFitsSystemWindows(boolean fitSystemWindows) {
4653 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4654 }
4655
4656 /**
4657 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4658 * will account for system screen decorations such as the status bar and inset its
4659 * content. This allows the view to be positioned in absolute screen coordinates
4660 * and remain visible to the user.
4661 *
4662 * @return true if this view will adjust its content bounds for system screen decorations.
4663 *
4664 * @attr ref android.R.styleable#View_fitsSystemWindows
4665 */
4666 public boolean fitsSystemWindows() {
4667 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4668 }
4669
4670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 * Returns the visibility status for this view.
4672 *
4673 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4674 * @attr ref android.R.styleable#View_visibility
4675 */
4676 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004677 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4678 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4679 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 })
4681 public int getVisibility() {
4682 return mViewFlags & VISIBILITY_MASK;
4683 }
4684
4685 /**
4686 * Set the enabled state of this view.
4687 *
4688 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4689 * @attr ref android.R.styleable#View_visibility
4690 */
4691 @RemotableViewMethod
4692 public void setVisibility(int visibility) {
4693 setFlags(visibility, VISIBILITY_MASK);
4694 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4695 }
4696
4697 /**
4698 * Returns the enabled status for this view. The interpretation of the
4699 * enabled state varies by subclass.
4700 *
4701 * @return True if this view is enabled, false otherwise.
4702 */
4703 @ViewDebug.ExportedProperty
4704 public boolean isEnabled() {
4705 return (mViewFlags & ENABLED_MASK) == ENABLED;
4706 }
4707
4708 /**
4709 * Set the enabled state of this view. The interpretation of the enabled
4710 * state varies by subclass.
4711 *
4712 * @param enabled True if this view is enabled, false otherwise.
4713 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004714 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004716 if (enabled == isEnabled()) return;
4717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4719
4720 /*
4721 * The View most likely has to change its appearance, so refresh
4722 * the drawable state.
4723 */
4724 refreshDrawableState();
4725
4726 // Invalidate too, since the default behavior for views is to be
4727 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004728 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 }
4730
4731 /**
4732 * Set whether this view can receive the focus.
4733 *
4734 * Setting this to false will also ensure that this view is not focusable
4735 * in touch mode.
4736 *
4737 * @param focusable If true, this view can receive the focus.
4738 *
4739 * @see #setFocusableInTouchMode(boolean)
4740 * @attr ref android.R.styleable#View_focusable
4741 */
4742 public void setFocusable(boolean focusable) {
4743 if (!focusable) {
4744 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4745 }
4746 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4747 }
4748
4749 /**
4750 * Set whether this view can receive focus while in touch mode.
4751 *
4752 * Setting this to true will also ensure that this view is focusable.
4753 *
4754 * @param focusableInTouchMode If true, this view can receive the focus while
4755 * in touch mode.
4756 *
4757 * @see #setFocusable(boolean)
4758 * @attr ref android.R.styleable#View_focusableInTouchMode
4759 */
4760 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4761 // Focusable in touch mode should always be set before the focusable flag
4762 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4763 // which, in touch mode, will not successfully request focus on this view
4764 // because the focusable in touch mode flag is not set
4765 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4766 if (focusableInTouchMode) {
4767 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4768 }
4769 }
4770
4771 /**
4772 * Set whether this view should have sound effects enabled for events such as
4773 * clicking and touching.
4774 *
4775 * <p>You may wish to disable sound effects for a view if you already play sounds,
4776 * for instance, a dial key that plays dtmf tones.
4777 *
4778 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4779 * @see #isSoundEffectsEnabled()
4780 * @see #playSoundEffect(int)
4781 * @attr ref android.R.styleable#View_soundEffectsEnabled
4782 */
4783 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4784 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4785 }
4786
4787 /**
4788 * @return whether this view should have sound effects enabled for events such as
4789 * clicking and touching.
4790 *
4791 * @see #setSoundEffectsEnabled(boolean)
4792 * @see #playSoundEffect(int)
4793 * @attr ref android.R.styleable#View_soundEffectsEnabled
4794 */
4795 @ViewDebug.ExportedProperty
4796 public boolean isSoundEffectsEnabled() {
4797 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4798 }
4799
4800 /**
4801 * Set whether this view should have haptic feedback for events such as
4802 * long presses.
4803 *
4804 * <p>You may wish to disable haptic feedback if your view already controls
4805 * its own haptic feedback.
4806 *
4807 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4808 * @see #isHapticFeedbackEnabled()
4809 * @see #performHapticFeedback(int)
4810 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4811 */
4812 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4813 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4814 }
4815
4816 /**
4817 * @return whether this view should have haptic feedback enabled for events
4818 * long presses.
4819 *
4820 * @see #setHapticFeedbackEnabled(boolean)
4821 * @see #performHapticFeedback(int)
4822 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4823 */
4824 @ViewDebug.ExportedProperty
4825 public boolean isHapticFeedbackEnabled() {
4826 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4827 }
4828
4829 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004830 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004831 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004832 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4833 * {@link #LAYOUT_DIRECTION_RTL},
4834 * {@link #LAYOUT_DIRECTION_INHERIT} or
4835 * {@link #LAYOUT_DIRECTION_LOCALE}.
4836 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004837 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004838 * @hide
4839 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004840 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004841 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4842 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4843 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4844 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004845 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004846 public int getLayoutDirection() {
4847 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004848 }
4849
4850 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004851 * Set the layout direction for this view. This will propagate a reset of layout direction
4852 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004853 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004854 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4855 * {@link #LAYOUT_DIRECTION_RTL},
4856 * {@link #LAYOUT_DIRECTION_INHERIT} or
4857 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004858 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004859 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004860 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004861 * @hide
4862 */
4863 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004864 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004865 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004866 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004867 // Setting the flag will also request a layout.
4868 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4869 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004870 }
4871
4872 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004873 * Returns the resolved layout direction for this view.
4874 *
4875 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4876 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4877 *
4878 * @hide
4879 */
4880 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4881 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4882 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4883 })
4884 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004885 resolveLayoutDirectionIfNeeded();
4886 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004887 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4888 }
4889
4890 /**
4891 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4892 * layout attribute and/or the inherited value from the parent.</p>
4893 *
4894 * @return true if the layout is right-to-left.
4895 *
4896 * @hide
4897 */
4898 @ViewDebug.ExportedProperty(category = "layout")
4899 public boolean isLayoutRtl() {
4900 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4901 }
4902
4903 /**
Adam Powell539ee872012-02-03 19:00:49 -08004904 * Indicates whether the view is currently tracking transient state that the
4905 * app should not need to concern itself with saving and restoring, but that
4906 * the framework should take special note to preserve when possible.
4907 *
4908 * @return true if the view has transient state
4909 *
4910 * @hide
4911 */
4912 @ViewDebug.ExportedProperty(category = "layout")
4913 public boolean hasTransientState() {
4914 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4915 }
4916
4917 /**
4918 * Set whether this view is currently tracking transient state that the
4919 * framework should attempt to preserve when possible.
4920 *
4921 * @param hasTransientState true if this view has transient state
4922 *
4923 * @hide
4924 */
4925 public void setHasTransientState(boolean hasTransientState) {
4926 if (hasTransientState() == hasTransientState) return;
4927
4928 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4929 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4930 if (mParent != null) {
4931 try {
4932 mParent.childHasTransientStateChanged(this, hasTransientState);
4933 } catch (AbstractMethodError e) {
4934 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
4935 " does not fully implement ViewParent", e);
4936 }
4937 }
4938 }
4939
4940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 * If this view doesn't do any drawing on its own, set this flag to
4942 * allow further optimizations. By default, this flag is not set on
4943 * View, but could be set on some View subclasses such as ViewGroup.
4944 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004945 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4946 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 *
4948 * @param willNotDraw whether or not this View draw on its own
4949 */
4950 public void setWillNotDraw(boolean willNotDraw) {
4951 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4952 }
4953
4954 /**
4955 * Returns whether or not this View draws on its own.
4956 *
4957 * @return true if this view has nothing to draw, false otherwise
4958 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004959 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004960 public boolean willNotDraw() {
4961 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4962 }
4963
4964 /**
4965 * When a View's drawing cache is enabled, drawing is redirected to an
4966 * offscreen bitmap. Some views, like an ImageView, must be able to
4967 * bypass this mechanism if they already draw a single bitmap, to avoid
4968 * unnecessary usage of the memory.
4969 *
4970 * @param willNotCacheDrawing true if this view does not cache its
4971 * drawing, false otherwise
4972 */
4973 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4974 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4975 }
4976
4977 /**
4978 * Returns whether or not this View can cache its drawing or not.
4979 *
4980 * @return true if this view does not cache its drawing, false otherwise
4981 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004982 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 public boolean willNotCacheDrawing() {
4984 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4985 }
4986
4987 /**
4988 * Indicates whether this view reacts to click events or not.
4989 *
4990 * @return true if the view is clickable, false otherwise
4991 *
4992 * @see #setClickable(boolean)
4993 * @attr ref android.R.styleable#View_clickable
4994 */
4995 @ViewDebug.ExportedProperty
4996 public boolean isClickable() {
4997 return (mViewFlags & CLICKABLE) == CLICKABLE;
4998 }
4999
5000 /**
5001 * Enables or disables click events for this view. When a view
5002 * is clickable it will change its state to "pressed" on every click.
5003 * Subclasses should set the view clickable to visually react to
5004 * user's clicks.
5005 *
5006 * @param clickable true to make the view clickable, false otherwise
5007 *
5008 * @see #isClickable()
5009 * @attr ref android.R.styleable#View_clickable
5010 */
5011 public void setClickable(boolean clickable) {
5012 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5013 }
5014
5015 /**
5016 * Indicates whether this view reacts to long click events or not.
5017 *
5018 * @return true if the view is long clickable, false otherwise
5019 *
5020 * @see #setLongClickable(boolean)
5021 * @attr ref android.R.styleable#View_longClickable
5022 */
5023 public boolean isLongClickable() {
5024 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5025 }
5026
5027 /**
5028 * Enables or disables long click events for this view. When a view is long
5029 * clickable it reacts to the user holding down the button for a longer
5030 * duration than a tap. This event can either launch the listener or a
5031 * context menu.
5032 *
5033 * @param longClickable true to make the view long clickable, false otherwise
5034 * @see #isLongClickable()
5035 * @attr ref android.R.styleable#View_longClickable
5036 */
5037 public void setLongClickable(boolean longClickable) {
5038 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5039 }
5040
5041 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005042 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 *
5044 * @see #isClickable()
5045 * @see #setClickable(boolean)
5046 *
5047 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5048 * the View's internal state from a previously set "pressed" state.
5049 */
5050 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005051 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005053 if (pressed) {
5054 mPrivateFlags |= PRESSED;
5055 } else {
5056 mPrivateFlags &= ~PRESSED;
5057 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005058
5059 if (needsRefresh) {
5060 refreshDrawableState();
5061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 dispatchSetPressed(pressed);
5063 }
5064
5065 /**
5066 * Dispatch setPressed to all of this View's children.
5067 *
5068 * @see #setPressed(boolean)
5069 *
5070 * @param pressed The new pressed state
5071 */
5072 protected void dispatchSetPressed(boolean pressed) {
5073 }
5074
5075 /**
5076 * Indicates whether the view is currently in pressed state. Unless
5077 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5078 * the pressed state.
5079 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005080 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 * @see #isClickable()
5082 * @see #setClickable(boolean)
5083 *
5084 * @return true if the view is currently pressed, false otherwise
5085 */
5086 public boolean isPressed() {
5087 return (mPrivateFlags & PRESSED) == PRESSED;
5088 }
5089
5090 /**
5091 * Indicates whether this view will save its state (that is,
5092 * whether its {@link #onSaveInstanceState} method will be called).
5093 *
5094 * @return Returns true if the view state saving is enabled, else false.
5095 *
5096 * @see #setSaveEnabled(boolean)
5097 * @attr ref android.R.styleable#View_saveEnabled
5098 */
5099 public boolean isSaveEnabled() {
5100 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5101 }
5102
5103 /**
5104 * Controls whether the saving of this view's state is
5105 * enabled (that is, whether its {@link #onSaveInstanceState} method
5106 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005107 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 * for its state to be saved. This flag can only disable the
5109 * saving of this view; any child views may still have their state saved.
5110 *
5111 * @param enabled Set to false to <em>disable</em> state saving, or true
5112 * (the default) to allow it.
5113 *
5114 * @see #isSaveEnabled()
5115 * @see #setId(int)
5116 * @see #onSaveInstanceState()
5117 * @attr ref android.R.styleable#View_saveEnabled
5118 */
5119 public void setSaveEnabled(boolean enabled) {
5120 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5121 }
5122
Jeff Brown85a31762010-09-01 17:01:00 -07005123 /**
5124 * Gets whether the framework should discard touches when the view's
5125 * window is obscured by another visible window.
5126 * Refer to the {@link View} security documentation for more details.
5127 *
5128 * @return True if touch filtering is enabled.
5129 *
5130 * @see #setFilterTouchesWhenObscured(boolean)
5131 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5132 */
5133 @ViewDebug.ExportedProperty
5134 public boolean getFilterTouchesWhenObscured() {
5135 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5136 }
5137
5138 /**
5139 * Sets whether the framework should discard touches when the view's
5140 * window is obscured by another visible window.
5141 * Refer to the {@link View} security documentation for more details.
5142 *
5143 * @param enabled True if touch filtering should be enabled.
5144 *
5145 * @see #getFilterTouchesWhenObscured
5146 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5147 */
5148 public void setFilterTouchesWhenObscured(boolean enabled) {
5149 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5150 FILTER_TOUCHES_WHEN_OBSCURED);
5151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152
5153 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005154 * Indicates whether the entire hierarchy under this view will save its
5155 * state when a state saving traversal occurs from its parent. The default
5156 * is true; if false, these views will not be saved unless
5157 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5158 *
5159 * @return Returns true if the view state saving from parent is enabled, else false.
5160 *
5161 * @see #setSaveFromParentEnabled(boolean)
5162 */
5163 public boolean isSaveFromParentEnabled() {
5164 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5165 }
5166
5167 /**
5168 * Controls whether the entire hierarchy under this view will save its
5169 * state when a state saving traversal occurs from its parent. The default
5170 * is true; if false, these views will not be saved unless
5171 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5172 *
5173 * @param enabled Set to false to <em>disable</em> state saving, or true
5174 * (the default) to allow it.
5175 *
5176 * @see #isSaveFromParentEnabled()
5177 * @see #setId(int)
5178 * @see #onSaveInstanceState()
5179 */
5180 public void setSaveFromParentEnabled(boolean enabled) {
5181 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5182 }
5183
5184
5185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 * Returns whether this View is able to take focus.
5187 *
5188 * @return True if this view can take focus, or false otherwise.
5189 * @attr ref android.R.styleable#View_focusable
5190 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005191 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 public final boolean isFocusable() {
5193 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5194 }
5195
5196 /**
5197 * When a view is focusable, it may not want to take focus when in touch mode.
5198 * For example, a button would like focus when the user is navigating via a D-pad
5199 * so that the user can click on it, but once the user starts touching the screen,
5200 * the button shouldn't take focus
5201 * @return Whether the view is focusable in touch mode.
5202 * @attr ref android.R.styleable#View_focusableInTouchMode
5203 */
5204 @ViewDebug.ExportedProperty
5205 public final boolean isFocusableInTouchMode() {
5206 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5207 }
5208
5209 /**
5210 * Find the nearest view in the specified direction that can take focus.
5211 * This does not actually give focus to that view.
5212 *
5213 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5214 *
5215 * @return The nearest focusable in the specified direction, or null if none
5216 * can be found.
5217 */
5218 public View focusSearch(int direction) {
5219 if (mParent != null) {
5220 return mParent.focusSearch(this, direction);
5221 } else {
5222 return null;
5223 }
5224 }
5225
5226 /**
5227 * This method is the last chance for the focused view and its ancestors to
5228 * respond to an arrow key. This is called when the focused view did not
5229 * consume the key internally, nor could the view system find a new view in
5230 * the requested direction to give focus to.
5231 *
5232 * @param focused The currently focused view.
5233 * @param direction The direction focus wants to move. One of FOCUS_UP,
5234 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5235 * @return True if the this view consumed this unhandled move.
5236 */
5237 public boolean dispatchUnhandledMove(View focused, int direction) {
5238 return false;
5239 }
5240
5241 /**
5242 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005243 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005245 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5246 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 * @return The user specified next view, or null if there is none.
5248 */
5249 View findUserSetNextFocus(View root, int direction) {
5250 switch (direction) {
5251 case FOCUS_LEFT:
5252 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005253 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 case FOCUS_RIGHT:
5255 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005256 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 case FOCUS_UP:
5258 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005259 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 case FOCUS_DOWN:
5261 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005262 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005263 case FOCUS_FORWARD:
5264 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005265 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005266 case FOCUS_BACKWARD: {
5267 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005268 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005269 @Override
5270 public boolean apply(View t) {
5271 return t.mNextFocusForwardId == id;
5272 }
5273 });
5274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 }
5276 return null;
5277 }
5278
Jeff Brown4dfbec22011-08-15 14:55:37 -07005279 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5280 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5281 @Override
5282 public boolean apply(View t) {
5283 return t.mID == childViewId;
5284 }
5285 });
5286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 if (result == null) {
5288 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5289 + "by user for id " + childViewId);
5290 }
5291 return result;
5292 }
5293
5294 /**
5295 * Find and return all focusable views that are descendants of this view,
5296 * possibly including this view if it is focusable itself.
5297 *
5298 * @param direction The direction of the focus
5299 * @return A list of focusable views
5300 */
5301 public ArrayList<View> getFocusables(int direction) {
5302 ArrayList<View> result = new ArrayList<View>(24);
5303 addFocusables(result, direction);
5304 return result;
5305 }
5306
5307 /**
5308 * Add any focusable views that are descendants of this view (possibly
5309 * including this view if it is focusable itself) to views. If we are in touch mode,
5310 * only add views that are also focusable in touch mode.
5311 *
5312 * @param views Focusable views found so far
5313 * @param direction The direction of the focus
5314 */
5315 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005316 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318
svetoslavganov75986cf2009-05-14 22:28:01 -07005319 /**
5320 * Adds any focusable views that are descendants of this view (possibly
5321 * including this view if it is focusable itself) to views. This method
5322 * adds all focusable views regardless if we are in touch mode or
5323 * only views focusable in touch mode if we are in touch mode depending on
5324 * the focusable mode paramater.
5325 *
5326 * @param views Focusable views found so far or null if all we are interested is
5327 * the number of focusables.
5328 * @param direction The direction of the focus.
5329 * @param focusableMode The type of focusables to be added.
5330 *
5331 * @see #FOCUSABLES_ALL
5332 * @see #FOCUSABLES_TOUCH_MODE
5333 */
5334 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5335 if (!isFocusable()) {
5336 return;
5337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338
svetoslavganov75986cf2009-05-14 22:28:01 -07005339 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5340 isInTouchMode() && !isFocusableInTouchMode()) {
5341 return;
5342 }
5343
5344 if (views != null) {
5345 views.add(this);
5346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 }
5348
5349 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005350 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005351 * The search is performed by either the text that the View renders or the content
5352 * description that describes the view for accessibility purposes and the view does
5353 * not render or both. Clients can specify how the search is to be performed via
5354 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5355 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005356 *
5357 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005358 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005359 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005360 * @see #FIND_VIEWS_WITH_TEXT
5361 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5362 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005363 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005364 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005365 if (getAccessibilityNodeProvider() != null) {
5366 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5367 outViews.add(this);
5368 }
5369 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5370 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005371 String searchedLowerCase = searched.toString().toLowerCase();
5372 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5373 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5374 outViews.add(this);
5375 }
5376 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005377 }
5378
5379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 * Find and return all touchable views that are descendants of this view,
5381 * possibly including this view if it is touchable itself.
5382 *
5383 * @return A list of touchable views
5384 */
5385 public ArrayList<View> getTouchables() {
5386 ArrayList<View> result = new ArrayList<View>();
5387 addTouchables(result);
5388 return result;
5389 }
5390
5391 /**
5392 * Add any touchable views that are descendants of this view (possibly
5393 * including this view if it is touchable itself) to views.
5394 *
5395 * @param views Touchable views found so far
5396 */
5397 public void addTouchables(ArrayList<View> views) {
5398 final int viewFlags = mViewFlags;
5399
5400 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5401 && (viewFlags & ENABLED_MASK) == ENABLED) {
5402 views.add(this);
5403 }
5404 }
5405
5406 /**
5407 * Call this to try to give focus to a specific view or to one of its
5408 * descendants.
5409 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005410 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5411 * false), or if it is focusable and it is not focusable in touch mode
5412 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005414 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 * have focus, and you want your parent to look for the next one.
5416 *
5417 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5418 * {@link #FOCUS_DOWN} and <code>null</code>.
5419 *
5420 * @return Whether this view or one of its descendants actually took focus.
5421 */
5422 public final boolean requestFocus() {
5423 return requestFocus(View.FOCUS_DOWN);
5424 }
5425
5426
5427 /**
5428 * Call this to try to give focus to a specific view or to one of its
5429 * descendants and give it a hint about what direction focus is heading.
5430 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005431 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5432 * false), or if it is focusable and it is not focusable in touch mode
5433 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005435 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005436 * have focus, and you want your parent to look for the next one.
5437 *
5438 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5439 * <code>null</code> set for the previously focused rectangle.
5440 *
5441 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5442 * @return Whether this view or one of its descendants actually took focus.
5443 */
5444 public final boolean requestFocus(int direction) {
5445 return requestFocus(direction, null);
5446 }
5447
5448 /**
5449 * Call this to try to give focus to a specific view or to one of its descendants
5450 * and give it hints about the direction and a specific rectangle that the focus
5451 * is coming from. The rectangle can help give larger views a finer grained hint
5452 * about where focus is coming from, and therefore, where to show selection, or
5453 * forward focus change internally.
5454 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005455 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5456 * false), or if it is focusable and it is not focusable in touch mode
5457 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 *
5459 * A View will not take focus if it is not visible.
5460 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005461 * A View will not take focus if one of its parents has
5462 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5463 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005465 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 * have focus, and you want your parent to look for the next one.
5467 *
5468 * You may wish to override this method if your custom {@link View} has an internal
5469 * {@link View} that it wishes to forward the request to.
5470 *
5471 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5472 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5473 * to give a finer grained hint about where focus is coming from. May be null
5474 * if there is no hint.
5475 * @return Whether this view or one of its descendants actually took focus.
5476 */
5477 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5478 // need to be focusable
5479 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5480 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5481 return false;
5482 }
5483
5484 // need to be focusable in touch mode if in touch mode
5485 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005486 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5487 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 }
5489
5490 // need to not have any parents blocking us
5491 if (hasAncestorThatBlocksDescendantFocus()) {
5492 return false;
5493 }
5494
5495 handleFocusGainInternal(direction, previouslyFocusedRect);
5496 return true;
5497 }
5498
5499 /**
5500 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5501 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5502 * touch mode to request focus when they are touched.
5503 *
5504 * @return Whether this view or one of its descendants actually took focus.
5505 *
5506 * @see #isInTouchMode()
5507 *
5508 */
5509 public final boolean requestFocusFromTouch() {
5510 // Leave touch mode if we need to
5511 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005512 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005513 if (viewRoot != null) {
5514 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 }
5516 }
5517 return requestFocus(View.FOCUS_DOWN);
5518 }
5519
5520 /**
5521 * @return Whether any ancestor of this view blocks descendant focus.
5522 */
5523 private boolean hasAncestorThatBlocksDescendantFocus() {
5524 ViewParent ancestor = mParent;
5525 while (ancestor instanceof ViewGroup) {
5526 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5527 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5528 return true;
5529 } else {
5530 ancestor = vgAncestor.getParent();
5531 }
5532 }
5533 return false;
5534 }
5535
5536 /**
Romain Guya440b002010-02-24 15:57:54 -08005537 * @hide
5538 */
5539 public void dispatchStartTemporaryDetach() {
5540 onStartTemporaryDetach();
5541 }
5542
5543 /**
5544 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5546 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005547 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 */
5549 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005550 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005551 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005552 }
5553
5554 /**
5555 * @hide
5556 */
5557 public void dispatchFinishTemporaryDetach() {
5558 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 }
Romain Guy8506ab42009-06-11 17:35:47 -07005560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 /**
5562 * Called after {@link #onStartTemporaryDetach} when the container is done
5563 * changing the view.
5564 */
5565 public void onFinishTemporaryDetach() {
5566 }
Romain Guy8506ab42009-06-11 17:35:47 -07005567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005569 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5570 * for this view's window. Returns null if the view is not currently attached
5571 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005572 * just use the standard high-level event callbacks like
5573 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005574 */
5575 public KeyEvent.DispatcherState getKeyDispatcherState() {
5576 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5577 }
Joe Malin32736f02011-01-19 16:14:20 -08005578
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 * Dispatch a key event before it is processed by any input method
5581 * associated with the view hierarchy. This can be used to intercept
5582 * key events in special situations before the IME consumes them; a
5583 * typical example would be handling the BACK key to update the application's
5584 * UI instead of allowing the IME to see it and close itself.
5585 *
5586 * @param event The key event to be dispatched.
5587 * @return True if the event was handled, false otherwise.
5588 */
5589 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5590 return onKeyPreIme(event.getKeyCode(), event);
5591 }
5592
5593 /**
5594 * Dispatch a key event to the next view on the focus path. This path runs
5595 * from the top of the view tree down to the currently focused view. If this
5596 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5597 * the next node down the focus path. This method also fires any key
5598 * listeners.
5599 *
5600 * @param event The key event to be dispatched.
5601 * @return True if the event was handled, false otherwise.
5602 */
5603 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005604 if (mInputEventConsistencyVerifier != null) {
5605 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607
Jeff Brown21bc5c92011-02-28 18:27:14 -08005608 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005609 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005610 ListenerInfo li = mListenerInfo;
5611 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5612 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 return true;
5614 }
5615
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005616 if (event.dispatch(this, mAttachInfo != null
5617 ? mAttachInfo.mKeyDispatchState : null, this)) {
5618 return true;
5619 }
5620
5621 if (mInputEventConsistencyVerifier != null) {
5622 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5623 }
5624 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 }
5626
5627 /**
5628 * Dispatches a key shortcut event.
5629 *
5630 * @param event The key event to be dispatched.
5631 * @return True if the event was handled by the view, false otherwise.
5632 */
5633 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5634 return onKeyShortcut(event.getKeyCode(), event);
5635 }
5636
5637 /**
5638 * Pass the touch screen motion event down to the target view, or this
5639 * view if it is the target.
5640 *
5641 * @param event The motion event to be dispatched.
5642 * @return True if the event was handled by the view, false otherwise.
5643 */
5644 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005645 if (mInputEventConsistencyVerifier != null) {
5646 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5647 }
5648
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005649 if (onFilterTouchEventForSecurity(event)) {
5650 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005651 ListenerInfo li = mListenerInfo;
5652 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5653 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005654 return true;
5655 }
5656
5657 if (onTouchEvent(event)) {
5658 return true;
5659 }
Jeff Brown85a31762010-09-01 17:01:00 -07005660 }
5661
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005662 if (mInputEventConsistencyVerifier != null) {
5663 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005665 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 }
5667
5668 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005669 * Filter the touch event to apply security policies.
5670 *
5671 * @param event The motion event to be filtered.
5672 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005673 *
Jeff Brown85a31762010-09-01 17:01:00 -07005674 * @see #getFilterTouchesWhenObscured
5675 */
5676 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005677 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005678 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5679 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5680 // Window is obscured, drop this touch.
5681 return false;
5682 }
5683 return true;
5684 }
5685
5686 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 * Pass a trackball motion event down to the focused view.
5688 *
5689 * @param event The motion event to be dispatched.
5690 * @return True if the event was handled by the view, false otherwise.
5691 */
5692 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005693 if (mInputEventConsistencyVerifier != null) {
5694 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5695 }
5696
Romain Guy02ccac62011-06-24 13:20:23 -07005697 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005698 }
5699
5700 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005701 * Dispatch a generic motion event.
5702 * <p>
5703 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5704 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005705 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005706 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005707 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005708 *
5709 * @param event The motion event to be dispatched.
5710 * @return True if the event was handled by the view, false otherwise.
5711 */
5712 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005713 if (mInputEventConsistencyVerifier != null) {
5714 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5715 }
5716
Jeff Browna032cc02011-03-07 16:56:21 -08005717 final int source = event.getSource();
5718 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5719 final int action = event.getAction();
5720 if (action == MotionEvent.ACTION_HOVER_ENTER
5721 || action == MotionEvent.ACTION_HOVER_MOVE
5722 || action == MotionEvent.ACTION_HOVER_EXIT) {
5723 if (dispatchHoverEvent(event)) {
5724 return true;
5725 }
5726 } else if (dispatchGenericPointerEvent(event)) {
5727 return true;
5728 }
5729 } else if (dispatchGenericFocusedEvent(event)) {
5730 return true;
5731 }
5732
Jeff Brown10b62902011-06-20 16:40:37 -07005733 if (dispatchGenericMotionEventInternal(event)) {
5734 return true;
5735 }
5736
5737 if (mInputEventConsistencyVerifier != null) {
5738 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5739 }
5740 return false;
5741 }
5742
5743 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005744 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005745 ListenerInfo li = mListenerInfo;
5746 if (li != null && li.mOnGenericMotionListener != null
5747 && (mViewFlags & ENABLED_MASK) == ENABLED
5748 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005749 return true;
5750 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005751
5752 if (onGenericMotionEvent(event)) {
5753 return true;
5754 }
5755
5756 if (mInputEventConsistencyVerifier != null) {
5757 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5758 }
5759 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005760 }
5761
5762 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005763 * Dispatch a hover event.
5764 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005765 * Do not call this method directly.
5766 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005767 * </p>
5768 *
5769 * @param event The motion event to be dispatched.
5770 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005771 */
5772 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005773 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005774 ListenerInfo li = mListenerInfo;
5775 if (li != null && li.mOnHoverListener != null
5776 && (mViewFlags & ENABLED_MASK) == ENABLED
5777 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005778 return true;
5779 }
5780
Jeff Browna032cc02011-03-07 16:56:21 -08005781 return onHoverEvent(event);
5782 }
5783
5784 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005785 * Returns true if the view has a child to which it has recently sent
5786 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5787 * it does not have a hovered child, then it must be the innermost hovered view.
5788 * @hide
5789 */
5790 protected boolean hasHoveredChild() {
5791 return false;
5792 }
5793
5794 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005795 * Dispatch a generic motion event to the view under the first pointer.
5796 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005797 * Do not call this method directly.
5798 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005799 * </p>
5800 *
5801 * @param event The motion event to be dispatched.
5802 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005803 */
5804 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5805 return false;
5806 }
5807
5808 /**
5809 * Dispatch a generic motion event to the currently focused view.
5810 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005811 * Do not call this method directly.
5812 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005813 * </p>
5814 *
5815 * @param event The motion event to be dispatched.
5816 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005817 */
5818 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5819 return false;
5820 }
5821
5822 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005823 * Dispatch a pointer event.
5824 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005825 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5826 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5827 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005828 * and should not be expected to handle other pointing device features.
5829 * </p>
5830 *
5831 * @param event The motion event to be dispatched.
5832 * @return True if the event was handled by the view, false otherwise.
5833 * @hide
5834 */
5835 public final boolean dispatchPointerEvent(MotionEvent event) {
5836 if (event.isTouchEvent()) {
5837 return dispatchTouchEvent(event);
5838 } else {
5839 return dispatchGenericMotionEvent(event);
5840 }
5841 }
5842
5843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 * Called when the window containing this view gains or loses window focus.
5845 * ViewGroups should override to route to their children.
5846 *
5847 * @param hasFocus True if the window containing this view now has focus,
5848 * false otherwise.
5849 */
5850 public void dispatchWindowFocusChanged(boolean hasFocus) {
5851 onWindowFocusChanged(hasFocus);
5852 }
5853
5854 /**
5855 * Called when the window containing this view gains or loses focus. Note
5856 * that this is separate from view focus: to receive key events, both
5857 * your view and its window must have focus. If a window is displayed
5858 * on top of yours that takes input focus, then your own window will lose
5859 * focus but the view focus will remain unchanged.
5860 *
5861 * @param hasWindowFocus True if the window containing this view now has
5862 * focus, false otherwise.
5863 */
5864 public void onWindowFocusChanged(boolean hasWindowFocus) {
5865 InputMethodManager imm = InputMethodManager.peekInstance();
5866 if (!hasWindowFocus) {
5867 if (isPressed()) {
5868 setPressed(false);
5869 }
5870 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5871 imm.focusOut(this);
5872 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005873 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005874 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005875 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5877 imm.focusIn(this);
5878 }
5879 refreshDrawableState();
5880 }
5881
5882 /**
5883 * Returns true if this view is in a window that currently has window focus.
5884 * Note that this is not the same as the view itself having focus.
5885 *
5886 * @return True if this view is in a window that currently has window focus.
5887 */
5888 public boolean hasWindowFocus() {
5889 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5890 }
5891
5892 /**
Adam Powell326d8082009-12-09 15:10:07 -08005893 * Dispatch a view visibility change down the view hierarchy.
5894 * ViewGroups should override to route to their children.
5895 * @param changedView The view whose visibility changed. Could be 'this' or
5896 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005897 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5898 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005899 */
5900 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5901 onVisibilityChanged(changedView, visibility);
5902 }
5903
5904 /**
5905 * Called when the visibility of the view or an ancestor of the view is changed.
5906 * @param changedView The view whose visibility changed. Could be 'this' or
5907 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005908 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5909 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005910 */
5911 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005912 if (visibility == VISIBLE) {
5913 if (mAttachInfo != null) {
5914 initialAwakenScrollBars();
5915 } else {
5916 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5917 }
5918 }
Adam Powell326d8082009-12-09 15:10:07 -08005919 }
5920
5921 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005922 * Dispatch a hint about whether this view is displayed. For instance, when
5923 * a View moves out of the screen, it might receives a display hint indicating
5924 * the view is not displayed. Applications should not <em>rely</em> on this hint
5925 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005926 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005927 * @param hint A hint about whether or not this view is displayed:
5928 * {@link #VISIBLE} or {@link #INVISIBLE}.
5929 */
5930 public void dispatchDisplayHint(int hint) {
5931 onDisplayHint(hint);
5932 }
5933
5934 /**
5935 * Gives this view a hint about whether is displayed or not. For instance, when
5936 * a View moves out of the screen, it might receives a display hint indicating
5937 * the view is not displayed. Applications should not <em>rely</em> on this hint
5938 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005939 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005940 * @param hint A hint about whether or not this view is displayed:
5941 * {@link #VISIBLE} or {@link #INVISIBLE}.
5942 */
5943 protected void onDisplayHint(int hint) {
5944 }
5945
5946 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005947 * Dispatch a window visibility change down the view hierarchy.
5948 * ViewGroups should override to route to their children.
5949 *
5950 * @param visibility The new visibility of the window.
5951 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005952 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 */
5954 public void dispatchWindowVisibilityChanged(int visibility) {
5955 onWindowVisibilityChanged(visibility);
5956 }
5957
5958 /**
5959 * Called when the window containing has change its visibility
5960 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5961 * that this tells you whether or not your window is being made visible
5962 * to the window manager; this does <em>not</em> tell you whether or not
5963 * your window is obscured by other windows on the screen, even if it
5964 * is itself visible.
5965 *
5966 * @param visibility The new visibility of the window.
5967 */
5968 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005969 if (visibility == VISIBLE) {
5970 initialAwakenScrollBars();
5971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 }
5973
5974 /**
5975 * Returns the current visibility of the window this view is attached to
5976 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5977 *
5978 * @return Returns the current visibility of the view's window.
5979 */
5980 public int getWindowVisibility() {
5981 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5982 }
5983
5984 /**
5985 * Retrieve the overall visible display size in which the window this view is
5986 * attached to has been positioned in. This takes into account screen
5987 * decorations above the window, for both cases where the window itself
5988 * is being position inside of them or the window is being placed under
5989 * then and covered insets are used for the window to position its content
5990 * inside. In effect, this tells you the available area where content can
5991 * be placed and remain visible to users.
5992 *
5993 * <p>This function requires an IPC back to the window manager to retrieve
5994 * the requested information, so should not be used in performance critical
5995 * code like drawing.
5996 *
5997 * @param outRect Filled in with the visible display frame. If the view
5998 * is not attached to a window, this is simply the raw display size.
5999 */
6000 public void getWindowVisibleDisplayFrame(Rect outRect) {
6001 if (mAttachInfo != null) {
6002 try {
6003 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6004 } catch (RemoteException e) {
6005 return;
6006 }
6007 // XXX This is really broken, and probably all needs to be done
6008 // in the window manager, and we need to know more about whether
6009 // we want the area behind or in front of the IME.
6010 final Rect insets = mAttachInfo.mVisibleInsets;
6011 outRect.left += insets.left;
6012 outRect.top += insets.top;
6013 outRect.right -= insets.right;
6014 outRect.bottom -= insets.bottom;
6015 return;
6016 }
6017 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006018 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 }
6020
6021 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006022 * Dispatch a notification about a resource configuration change down
6023 * the view hierarchy.
6024 * ViewGroups should override to route to their children.
6025 *
6026 * @param newConfig The new resource configuration.
6027 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006028 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006029 */
6030 public void dispatchConfigurationChanged(Configuration newConfig) {
6031 onConfigurationChanged(newConfig);
6032 }
6033
6034 /**
6035 * Called when the current configuration of the resources being used
6036 * by the application have changed. You can use this to decide when
6037 * to reload resources that can changed based on orientation and other
6038 * configuration characterstics. You only need to use this if you are
6039 * not relying on the normal {@link android.app.Activity} mechanism of
6040 * recreating the activity instance upon a configuration change.
6041 *
6042 * @param newConfig The new resource configuration.
6043 */
6044 protected void onConfigurationChanged(Configuration newConfig) {
6045 }
6046
6047 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 * Private function to aggregate all per-view attributes in to the view
6049 * root.
6050 */
6051 void dispatchCollectViewAttributes(int visibility) {
6052 performCollectViewAttributes(visibility);
6053 }
6054
6055 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006056 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006057 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6058 mAttachInfo.mKeepScreenOn = true;
6059 }
6060 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006061 ListenerInfo li = mListenerInfo;
6062 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006063 mAttachInfo.mHasSystemUiListeners = true;
6064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 }
6066 }
6067
6068 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006069 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006071 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6072 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 ai.mRecomputeGlobalAttributes = true;
6074 }
6075 }
6076 }
6077
6078 /**
6079 * Returns whether the device is currently in touch mode. Touch mode is entered
6080 * once the user begins interacting with the device by touch, and affects various
6081 * things like whether focus is always visible to the user.
6082 *
6083 * @return Whether the device is in touch mode.
6084 */
6085 @ViewDebug.ExportedProperty
6086 public boolean isInTouchMode() {
6087 if (mAttachInfo != null) {
6088 return mAttachInfo.mInTouchMode;
6089 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006090 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 }
6092 }
6093
6094 /**
6095 * Returns the context the view is running in, through which it can
6096 * access the current theme, resources, etc.
6097 *
6098 * @return The view's Context.
6099 */
6100 @ViewDebug.CapturedViewProperty
6101 public final Context getContext() {
6102 return mContext;
6103 }
6104
6105 /**
6106 * Handle a key event before it is processed by any input method
6107 * associated with the view hierarchy. This can be used to intercept
6108 * key events in special situations before the IME consumes them; a
6109 * typical example would be handling the BACK key to update the application's
6110 * UI instead of allowing the IME to see it and close itself.
6111 *
6112 * @param keyCode The value in event.getKeyCode().
6113 * @param event Description of the key event.
6114 * @return If you handled the event, return true. If you want to allow the
6115 * event to be handled by the next receiver, return false.
6116 */
6117 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6118 return false;
6119 }
6120
6121 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006122 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6123 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6125 * is released, if the view is enabled and clickable.
6126 *
6127 * @param keyCode A key code that represents the button pressed, from
6128 * {@link android.view.KeyEvent}.
6129 * @param event The KeyEvent object that defines the button action.
6130 */
6131 public boolean onKeyDown(int keyCode, KeyEvent event) {
6132 boolean result = false;
6133
6134 switch (keyCode) {
6135 case KeyEvent.KEYCODE_DPAD_CENTER:
6136 case KeyEvent.KEYCODE_ENTER: {
6137 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6138 return true;
6139 }
6140 // Long clickable items don't necessarily have to be clickable
6141 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6142 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6143 (event.getRepeatCount() == 0)) {
6144 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006145 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 return true;
6147 }
6148 break;
6149 }
6150 }
6151 return result;
6152 }
6153
6154 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006155 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6156 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6157 * the event).
6158 */
6159 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6160 return false;
6161 }
6162
6163 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006164 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6165 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6167 * {@link KeyEvent#KEYCODE_ENTER} is released.
6168 *
6169 * @param keyCode A key code that represents the button pressed, from
6170 * {@link android.view.KeyEvent}.
6171 * @param event The KeyEvent object that defines the button action.
6172 */
6173 public boolean onKeyUp(int keyCode, KeyEvent event) {
6174 boolean result = false;
6175
6176 switch (keyCode) {
6177 case KeyEvent.KEYCODE_DPAD_CENTER:
6178 case KeyEvent.KEYCODE_ENTER: {
6179 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6180 return true;
6181 }
6182 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6183 setPressed(false);
6184
6185 if (!mHasPerformedLongPress) {
6186 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006187 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188
6189 result = performClick();
6190 }
6191 }
6192 break;
6193 }
6194 }
6195 return result;
6196 }
6197
6198 /**
6199 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6200 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6201 * the event).
6202 *
6203 * @param keyCode A key code that represents the button pressed, from
6204 * {@link android.view.KeyEvent}.
6205 * @param repeatCount The number of times the action was made.
6206 * @param event The KeyEvent object that defines the button action.
6207 */
6208 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6209 return false;
6210 }
6211
6212 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006213 * Called on the focused view when a key shortcut event is not handled.
6214 * Override this method to implement local key shortcuts for the View.
6215 * Key shortcuts can also be implemented by setting the
6216 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 *
6218 * @param keyCode The value in event.getKeyCode().
6219 * @param event Description of the key event.
6220 * @return If you handled the event, return true. If you want to allow the
6221 * event to be handled by the next receiver, return false.
6222 */
6223 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6224 return false;
6225 }
6226
6227 /**
6228 * Check whether the called view is a text editor, in which case it
6229 * would make sense to automatically display a soft input window for
6230 * it. Subclasses should override this if they implement
6231 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006232 * a call on that method would return a non-null InputConnection, and
6233 * they are really a first-class editor that the user would normally
6234 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006235 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006236 * <p>The default implementation always returns false. This does
6237 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6238 * will not be called or the user can not otherwise perform edits on your
6239 * view; it is just a hint to the system that this is not the primary
6240 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006242 * @return Returns true if this view is a text editor, else false.
6243 */
6244 public boolean onCheckIsTextEditor() {
6245 return false;
6246 }
Romain Guy8506ab42009-06-11 17:35:47 -07006247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 /**
6249 * Create a new InputConnection for an InputMethod to interact
6250 * with the view. The default implementation returns null, since it doesn't
6251 * support input methods. You can override this to implement such support.
6252 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 * <p>When implementing this, you probably also want to implement
6255 * {@link #onCheckIsTextEditor()} to indicate you will return a
6256 * non-null InputConnection.
6257 *
6258 * @param outAttrs Fill in with attribute information about the connection.
6259 */
6260 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6261 return null;
6262 }
6263
6264 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006265 * Called by the {@link android.view.inputmethod.InputMethodManager}
6266 * when a view who is not the current
6267 * input connection target is trying to make a call on the manager. The
6268 * default implementation returns false; you can override this to return
6269 * true for certain views if you are performing InputConnection proxying
6270 * to them.
6271 * @param view The View that is making the InputMethodManager call.
6272 * @return Return true to allow the call, false to reject.
6273 */
6274 public boolean checkInputConnectionProxy(View view) {
6275 return false;
6276 }
Romain Guy8506ab42009-06-11 17:35:47 -07006277
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 * Show the context menu for this view. It is not safe to hold on to the
6280 * menu after returning from this method.
6281 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006282 * You should normally not overload this method. Overload
6283 * {@link #onCreateContextMenu(ContextMenu)} or define an
6284 * {@link OnCreateContextMenuListener} to add items to the context menu.
6285 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 * @param menu The context menu to populate
6287 */
6288 public void createContextMenu(ContextMenu menu) {
6289 ContextMenuInfo menuInfo = getContextMenuInfo();
6290
6291 // Sets the current menu info so all items added to menu will have
6292 // my extra info set.
6293 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6294
6295 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006296 ListenerInfo li = mListenerInfo;
6297 if (li != null && li.mOnCreateContextMenuListener != null) {
6298 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 }
6300
6301 // Clear the extra information so subsequent items that aren't mine don't
6302 // have my extra info.
6303 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6304
6305 if (mParent != null) {
6306 mParent.createContextMenu(menu);
6307 }
6308 }
6309
6310 /**
6311 * Views should implement this if they have extra information to associate
6312 * with the context menu. The return result is supplied as a parameter to
6313 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6314 * callback.
6315 *
6316 * @return Extra information about the item for which the context menu
6317 * should be shown. This information will vary across different
6318 * subclasses of View.
6319 */
6320 protected ContextMenuInfo getContextMenuInfo() {
6321 return null;
6322 }
6323
6324 /**
6325 * Views should implement this if the view itself is going to add items to
6326 * the context menu.
6327 *
6328 * @param menu the context menu to populate
6329 */
6330 protected void onCreateContextMenu(ContextMenu menu) {
6331 }
6332
6333 /**
6334 * Implement this method to handle trackball motion events. The
6335 * <em>relative</em> movement of the trackball since the last event
6336 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6337 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6338 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6339 * they will often be fractional values, representing the more fine-grained
6340 * movement information available from a trackball).
6341 *
6342 * @param event The motion event.
6343 * @return True if the event was handled, false otherwise.
6344 */
6345 public boolean onTrackballEvent(MotionEvent event) {
6346 return false;
6347 }
6348
6349 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006350 * Implement this method to handle generic motion events.
6351 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006352 * Generic motion events describe joystick movements, mouse hovers, track pad
6353 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006354 * {@link MotionEvent#getSource() source} of the motion event specifies
6355 * the class of input that was received. Implementations of this method
6356 * must examine the bits in the source before processing the event.
6357 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006358 * </p><p>
6359 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6360 * are delivered to the view under the pointer. All other generic motion events are
6361 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006362 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006363 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006364 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006365 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6366 * // process the joystick movement...
6367 * return true;
6368 * }
6369 * }
6370 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6371 * switch (event.getAction()) {
6372 * case MotionEvent.ACTION_HOVER_MOVE:
6373 * // process the mouse hover movement...
6374 * return true;
6375 * case MotionEvent.ACTION_SCROLL:
6376 * // process the scroll wheel movement...
6377 * return true;
6378 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006379 * }
6380 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006381 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006382 *
6383 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006384 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006385 */
6386 public boolean onGenericMotionEvent(MotionEvent event) {
6387 return false;
6388 }
6389
6390 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006391 * Implement this method to handle hover events.
6392 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006393 * This method is called whenever a pointer is hovering into, over, or out of the
6394 * bounds of a view and the view is not currently being touched.
6395 * Hover events are represented as pointer events with action
6396 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6397 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6398 * </p>
6399 * <ul>
6400 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6401 * when the pointer enters the bounds of the view.</li>
6402 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6403 * when the pointer has already entered the bounds of the view and has moved.</li>
6404 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6405 * when the pointer has exited the bounds of the view or when the pointer is
6406 * about to go down due to a button click, tap, or similar user action that
6407 * causes the view to be touched.</li>
6408 * </ul>
6409 * <p>
6410 * The view should implement this method to return true to indicate that it is
6411 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006412 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006413 * The default implementation calls {@link #setHovered} to update the hovered state
6414 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006415 * is enabled and is clickable. The default implementation also sends hover
6416 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006417 * </p>
6418 *
6419 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006420 * @return True if the view handled the hover event.
6421 *
6422 * @see #isHovered
6423 * @see #setHovered
6424 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006425 */
6426 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006427 // The root view may receive hover (or touch) events that are outside the bounds of
6428 // the window. This code ensures that we only send accessibility events for
6429 // hovers that are actually within the bounds of the root view.
6430 final int action = event.getAction();
6431 if (!mSendingHoverAccessibilityEvents) {
6432 if ((action == MotionEvent.ACTION_HOVER_ENTER
6433 || action == MotionEvent.ACTION_HOVER_MOVE)
6434 && !hasHoveredChild()
6435 && pointInView(event.getX(), event.getY())) {
6436 mSendingHoverAccessibilityEvents = true;
6437 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6438 }
6439 } else {
6440 if (action == MotionEvent.ACTION_HOVER_EXIT
6441 || (action == MotionEvent.ACTION_HOVER_MOVE
6442 && !pointInView(event.getX(), event.getY()))) {
6443 mSendingHoverAccessibilityEvents = false;
6444 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6445 }
Jeff Browna1b24182011-07-28 13:38:24 -07006446 }
6447
Jeff Brown87b7f802011-06-21 18:35:45 -07006448 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006449 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006450 case MotionEvent.ACTION_HOVER_ENTER:
6451 setHovered(true);
6452 break;
6453 case MotionEvent.ACTION_HOVER_EXIT:
6454 setHovered(false);
6455 break;
6456 }
Jeff Browna1b24182011-07-28 13:38:24 -07006457
6458 // Dispatch the event to onGenericMotionEvent before returning true.
6459 // This is to provide compatibility with existing applications that
6460 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6461 // break because of the new default handling for hoverable views
6462 // in onHoverEvent.
6463 // Note that onGenericMotionEvent will be called by default when
6464 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6465 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006466 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006467 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006468 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006469 }
6470
6471 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006472 * Returns true if the view should handle {@link #onHoverEvent}
6473 * by calling {@link #setHovered} to change its hovered state.
6474 *
6475 * @return True if the view is hoverable.
6476 */
6477 private boolean isHoverable() {
6478 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006479 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006480 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6481 return false;
6482 }
6483
6484 return (viewFlags & CLICKABLE) == CLICKABLE
6485 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6486 }
6487
6488 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006489 * Returns true if the view is currently hovered.
6490 *
6491 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006492 *
6493 * @see #setHovered
6494 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006495 */
Jeff Brown10b62902011-06-20 16:40:37 -07006496 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006497 public boolean isHovered() {
6498 return (mPrivateFlags & HOVERED) != 0;
6499 }
6500
6501 /**
6502 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006503 * <p>
6504 * Calling this method also changes the drawable state of the view. This
6505 * enables the view to react to hover by using different drawable resources
6506 * to change its appearance.
6507 * </p><p>
6508 * The {@link #onHoverChanged} method is called when the hovered state changes.
6509 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006510 *
6511 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006512 *
6513 * @see #isHovered
6514 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006515 */
6516 public void setHovered(boolean hovered) {
6517 if (hovered) {
6518 if ((mPrivateFlags & HOVERED) == 0) {
6519 mPrivateFlags |= HOVERED;
6520 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006521 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006522 }
6523 } else {
6524 if ((mPrivateFlags & HOVERED) != 0) {
6525 mPrivateFlags &= ~HOVERED;
6526 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006527 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006528 }
6529 }
6530 }
6531
6532 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006533 * Implement this method to handle hover state changes.
6534 * <p>
6535 * This method is called whenever the hover state changes as a result of a
6536 * call to {@link #setHovered}.
6537 * </p>
6538 *
6539 * @param hovered The current hover state, as returned by {@link #isHovered}.
6540 *
6541 * @see #isHovered
6542 * @see #setHovered
6543 */
6544 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006545 }
6546
6547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 * Implement this method to handle touch screen motion events.
6549 *
6550 * @param event The motion event.
6551 * @return True if the event was handled, false otherwise.
6552 */
6553 public boolean onTouchEvent(MotionEvent event) {
6554 final int viewFlags = mViewFlags;
6555
6556 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006557 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006558 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 // A disabled view that is clickable still consumes the touch
6561 // events, it just doesn't respond to them.
6562 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6563 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6564 }
6565
6566 if (mTouchDelegate != null) {
6567 if (mTouchDelegate.onTouchEvent(event)) {
6568 return true;
6569 }
6570 }
6571
6572 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6573 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6574 switch (event.getAction()) {
6575 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006576 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6577 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 // take focus if we don't have it already and we should in
6579 // touch mode.
6580 boolean focusTaken = false;
6581 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6582 focusTaken = requestFocus();
6583 }
6584
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006585 if (prepressed) {
6586 // The button is being released before we actually
6587 // showed it as pressed. Make it show the pressed
6588 // state now (before scheduling the click) to ensure
6589 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006590 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006591 }
Joe Malin32736f02011-01-19 16:14:20 -08006592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006593 if (!mHasPerformedLongPress) {
6594 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006595 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006596
6597 // Only perform take click actions if we were in the pressed state
6598 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006599 // Use a Runnable and post this rather than calling
6600 // performClick directly. This lets other visual state
6601 // of the view update before click actions start.
6602 if (mPerformClick == null) {
6603 mPerformClick = new PerformClick();
6604 }
6605 if (!post(mPerformClick)) {
6606 performClick();
6607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 }
6609 }
6610
6611 if (mUnsetPressedState == null) {
6612 mUnsetPressedState = new UnsetPressedState();
6613 }
6614
Adam Powelle14579b2009-12-16 18:39:52 -08006615 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006616 postDelayed(mUnsetPressedState,
6617 ViewConfiguration.getPressedStateDuration());
6618 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 // If the post failed, unpress right now
6620 mUnsetPressedState.run();
6621 }
Adam Powelle14579b2009-12-16 18:39:52 -08006622 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 }
6624 break;
6625
6626 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006627 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006628
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006629 if (performButtonActionOnTouchDown(event)) {
6630 break;
6631 }
6632
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006633 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006634 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006635
6636 // For views inside a scrolling container, delay the pressed feedback for
6637 // a short period in case this is a scroll.
6638 if (isInScrollingContainer) {
6639 mPrivateFlags |= PREPRESSED;
6640 if (mPendingCheckForTap == null) {
6641 mPendingCheckForTap = new CheckForTap();
6642 }
6643 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6644 } else {
6645 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006646 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006647 checkForLongClick(0);
6648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 break;
6650
6651 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006652 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006653 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006654 break;
6655
6656 case MotionEvent.ACTION_MOVE:
6657 final int x = (int) event.getX();
6658 final int y = (int) event.getY();
6659
6660 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006661 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006663 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006665 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006666 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006667
Adam Powell4d6f0662012-02-21 15:11:11 -08006668 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 }
6671 break;
6672 }
6673 return true;
6674 }
6675
6676 return false;
6677 }
6678
6679 /**
Adam Powell10298662011-08-14 18:26:30 -07006680 * @hide
6681 */
6682 public boolean isInScrollingContainer() {
6683 ViewParent p = getParent();
6684 while (p != null && p instanceof ViewGroup) {
6685 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6686 return true;
6687 }
6688 p = p.getParent();
6689 }
6690 return false;
6691 }
6692
6693 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006694 * Remove the longpress detection timer.
6695 */
6696 private void removeLongPressCallback() {
6697 if (mPendingCheckForLongPress != null) {
6698 removeCallbacks(mPendingCheckForLongPress);
6699 }
6700 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006701
6702 /**
6703 * Remove the pending click action
6704 */
6705 private void removePerformClickCallback() {
6706 if (mPerformClick != null) {
6707 removeCallbacks(mPerformClick);
6708 }
6709 }
6710
Adam Powelle14579b2009-12-16 18:39:52 -08006711 /**
Romain Guya440b002010-02-24 15:57:54 -08006712 * Remove the prepress detection timer.
6713 */
6714 private void removeUnsetPressCallback() {
6715 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6716 setPressed(false);
6717 removeCallbacks(mUnsetPressedState);
6718 }
6719 }
6720
6721 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006722 * Remove the tap detection timer.
6723 */
6724 private void removeTapCallback() {
6725 if (mPendingCheckForTap != null) {
6726 mPrivateFlags &= ~PREPRESSED;
6727 removeCallbacks(mPendingCheckForTap);
6728 }
6729 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006730
6731 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006732 * Cancels a pending long press. Your subclass can use this if you
6733 * want the context menu to come up if the user presses and holds
6734 * at the same place, but you don't want it to come up if they press
6735 * and then move around enough to cause scrolling.
6736 */
6737 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006738 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006739
6740 /*
6741 * The prepressed state handled by the tap callback is a display
6742 * construct, but the tap callback will post a long press callback
6743 * less its own timeout. Remove it here.
6744 */
6745 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 }
6747
6748 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006749 * Remove the pending callback for sending a
6750 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6751 */
6752 private void removeSendViewScrolledAccessibilityEventCallback() {
6753 if (mSendViewScrolledAccessibilityEvent != null) {
6754 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6755 }
6756 }
6757
6758 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006759 * Sets the TouchDelegate for this View.
6760 */
6761 public void setTouchDelegate(TouchDelegate delegate) {
6762 mTouchDelegate = delegate;
6763 }
6764
6765 /**
6766 * Gets the TouchDelegate for this View.
6767 */
6768 public TouchDelegate getTouchDelegate() {
6769 return mTouchDelegate;
6770 }
6771
6772 /**
6773 * Set flags controlling behavior of this view.
6774 *
6775 * @param flags Constant indicating the value which should be set
6776 * @param mask Constant indicating the bit range that should be changed
6777 */
6778 void setFlags(int flags, int mask) {
6779 int old = mViewFlags;
6780 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6781
6782 int changed = mViewFlags ^ old;
6783 if (changed == 0) {
6784 return;
6785 }
6786 int privateFlags = mPrivateFlags;
6787
6788 /* Check if the FOCUSABLE bit has changed */
6789 if (((changed & FOCUSABLE_MASK) != 0) &&
6790 ((privateFlags & HAS_BOUNDS) !=0)) {
6791 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6792 && ((privateFlags & FOCUSED) != 0)) {
6793 /* Give up focus if we are no longer focusable */
6794 clearFocus();
6795 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6796 && ((privateFlags & FOCUSED) == 0)) {
6797 /*
6798 * Tell the view system that we are now available to take focus
6799 * if no one else already has it.
6800 */
6801 if (mParent != null) mParent.focusableViewAvailable(this);
6802 }
6803 }
6804
6805 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6806 if ((changed & VISIBILITY_MASK) != 0) {
6807 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006808 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006809 * it was not visible. Marking it drawn ensures that the invalidation will
6810 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006812 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006813 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814
6815 needGlobalAttributesUpdate(true);
6816
6817 // a view becoming visible is worth notifying the parent
6818 // about in case nothing has focus. even if this specific view
6819 // isn't focusable, it may contain something that is, so let
6820 // the root view try to give this focus if nothing else does.
6821 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6822 mParent.focusableViewAvailable(this);
6823 }
6824 }
6825 }
6826
6827 /* Check if the GONE bit has changed */
6828 if ((changed & GONE) != 0) {
6829 needGlobalAttributesUpdate(false);
6830 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831
Romain Guyecd80ee2009-12-03 17:13:02 -08006832 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6833 if (hasFocus()) clearFocus();
6834 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006835 if (mParent instanceof View) {
6836 // GONE views noop invalidation, so invalidate the parent
6837 ((View) mParent).invalidate(true);
6838 }
6839 // Mark the view drawn to ensure that it gets invalidated properly the next
6840 // time it is visible and gets invalidated
6841 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 }
6843 if (mAttachInfo != null) {
6844 mAttachInfo.mViewVisibilityChanged = true;
6845 }
6846 }
6847
6848 /* Check if the VISIBLE bit has changed */
6849 if ((changed & INVISIBLE) != 0) {
6850 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006851 /*
6852 * If this view is becoming invisible, set the DRAWN flag so that
6853 * the next invalidate() will not be skipped.
6854 */
6855 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006856
6857 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6858 // root view becoming invisible shouldn't clear focus
6859 if (getRootView() != this) {
6860 clearFocus();
6861 }
6862 }
6863 if (mAttachInfo != null) {
6864 mAttachInfo.mViewVisibilityChanged = true;
6865 }
6866 }
6867
Adam Powell326d8082009-12-09 15:10:07 -08006868 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006869 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006870 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6871 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006872 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006873 } else if (mParent != null) {
6874 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006875 }
Adam Powell326d8082009-12-09 15:10:07 -08006876 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6877 }
6878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6880 destroyDrawingCache();
6881 }
6882
6883 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6884 destroyDrawingCache();
6885 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006886 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006887 }
6888
6889 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6890 destroyDrawingCache();
6891 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6892 }
6893
6894 if ((changed & DRAW_MASK) != 0) {
6895 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6896 if (mBGDrawable != null) {
6897 mPrivateFlags &= ~SKIP_DRAW;
6898 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6899 } else {
6900 mPrivateFlags |= SKIP_DRAW;
6901 }
6902 } else {
6903 mPrivateFlags &= ~SKIP_DRAW;
6904 }
6905 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006906 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 }
6908
6909 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006910 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911 mParent.recomputeViewAttributes(this);
6912 }
6913 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006914
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006915 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006916 requestLayout();
6917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 }
6919
6920 /**
6921 * Change the view's z order in the tree, so it's on top of other sibling
6922 * views
6923 */
6924 public void bringToFront() {
6925 if (mParent != null) {
6926 mParent.bringChildToFront(this);
6927 }
6928 }
6929
6930 /**
6931 * This is called in response to an internal scroll in this view (i.e., the
6932 * view scrolled its own contents). This is typically as a result of
6933 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6934 * called.
6935 *
6936 * @param l Current horizontal scroll origin.
6937 * @param t Current vertical scroll origin.
6938 * @param oldl Previous horizontal scroll origin.
6939 * @param oldt Previous vertical scroll origin.
6940 */
6941 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006942 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6943 postSendViewScrolledAccessibilityEventCallback();
6944 }
6945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 mBackgroundSizeChanged = true;
6947
6948 final AttachInfo ai = mAttachInfo;
6949 if (ai != null) {
6950 ai.mViewScrollChanged = true;
6951 }
6952 }
6953
6954 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006955 * Interface definition for a callback to be invoked when the layout bounds of a view
6956 * changes due to layout processing.
6957 */
6958 public interface OnLayoutChangeListener {
6959 /**
6960 * Called when the focus state of a view has changed.
6961 *
6962 * @param v The view whose state has changed.
6963 * @param left The new value of the view's left property.
6964 * @param top The new value of the view's top property.
6965 * @param right The new value of the view's right property.
6966 * @param bottom The new value of the view's bottom property.
6967 * @param oldLeft The previous value of the view's left property.
6968 * @param oldTop The previous value of the view's top property.
6969 * @param oldRight The previous value of the view's right property.
6970 * @param oldBottom The previous value of the view's bottom property.
6971 */
6972 void onLayoutChange(View v, int left, int top, int right, int bottom,
6973 int oldLeft, int oldTop, int oldRight, int oldBottom);
6974 }
6975
6976 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 * This is called during layout when the size of this view has changed. If
6978 * you were just added to the view hierarchy, you're called with the old
6979 * values of 0.
6980 *
6981 * @param w Current width of this view.
6982 * @param h Current height of this view.
6983 * @param oldw Old width of this view.
6984 * @param oldh Old height of this view.
6985 */
6986 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6987 }
6988
6989 /**
6990 * Called by draw to draw the child views. This may be overridden
6991 * by derived classes to gain control just before its children are drawn
6992 * (but after its own view has been drawn).
6993 * @param canvas the canvas on which to draw the view
6994 */
6995 protected void dispatchDraw(Canvas canvas) {
6996 }
6997
6998 /**
6999 * Gets the parent of this view. Note that the parent is a
7000 * ViewParent and not necessarily a View.
7001 *
7002 * @return Parent of this view.
7003 */
7004 public final ViewParent getParent() {
7005 return mParent;
7006 }
7007
7008 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007009 * Set the horizontal scrolled position of your view. This will cause a call to
7010 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7011 * invalidated.
7012 * @param value the x position to scroll to
7013 */
7014 public void setScrollX(int value) {
7015 scrollTo(value, mScrollY);
7016 }
7017
7018 /**
7019 * Set the vertical scrolled position of your view. This will cause a call to
7020 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7021 * invalidated.
7022 * @param value the y position to scroll to
7023 */
7024 public void setScrollY(int value) {
7025 scrollTo(mScrollX, value);
7026 }
7027
7028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 * Return the scrolled left position of this view. This is the left edge of
7030 * the displayed part of your view. You do not need to draw any pixels
7031 * farther left, since those are outside of the frame of your view on
7032 * screen.
7033 *
7034 * @return The left edge of the displayed part of your view, in pixels.
7035 */
7036 public final int getScrollX() {
7037 return mScrollX;
7038 }
7039
7040 /**
7041 * Return the scrolled top position of this view. This is the top edge of
7042 * the displayed part of your view. You do not need to draw any pixels above
7043 * it, since those are outside of the frame of your view on screen.
7044 *
7045 * @return The top edge of the displayed part of your view, in pixels.
7046 */
7047 public final int getScrollY() {
7048 return mScrollY;
7049 }
7050
7051 /**
7052 * Return the width of the your view.
7053 *
7054 * @return The width of your view, in pixels.
7055 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007056 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007057 public final int getWidth() {
7058 return mRight - mLeft;
7059 }
7060
7061 /**
7062 * Return the height of your view.
7063 *
7064 * @return The height of your view, in pixels.
7065 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007066 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 public final int getHeight() {
7068 return mBottom - mTop;
7069 }
7070
7071 /**
7072 * Return the visible drawing bounds of your view. Fills in the output
7073 * rectangle with the values from getScrollX(), getScrollY(),
7074 * getWidth(), and getHeight().
7075 *
7076 * @param outRect The (scrolled) drawing bounds of the view.
7077 */
7078 public void getDrawingRect(Rect outRect) {
7079 outRect.left = mScrollX;
7080 outRect.top = mScrollY;
7081 outRect.right = mScrollX + (mRight - mLeft);
7082 outRect.bottom = mScrollY + (mBottom - mTop);
7083 }
7084
7085 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007086 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7087 * raw width component (that is the result is masked by
7088 * {@link #MEASURED_SIZE_MASK}).
7089 *
7090 * @return The raw measured width of this view.
7091 */
7092 public final int getMeasuredWidth() {
7093 return mMeasuredWidth & MEASURED_SIZE_MASK;
7094 }
7095
7096 /**
7097 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007098 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007099 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 * This should be used during measurement and layout calculations only. Use
7101 * {@link #getWidth()} to see how wide a view is after layout.
7102 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007103 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007105 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 return mMeasuredWidth;
7107 }
7108
7109 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007110 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7111 * raw width component (that is the result is masked by
7112 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007114 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 */
7116 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007117 return mMeasuredHeight & MEASURED_SIZE_MASK;
7118 }
7119
7120 /**
7121 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007122 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007123 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7124 * This should be used during measurement and layout calculations only. Use
7125 * {@link #getHeight()} to see how wide a view is after layout.
7126 *
7127 * @return The measured width of this view as a bit mask.
7128 */
7129 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007130 return mMeasuredHeight;
7131 }
7132
7133 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007134 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7135 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7136 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7137 * and the height component is at the shifted bits
7138 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7139 */
7140 public final int getMeasuredState() {
7141 return (mMeasuredWidth&MEASURED_STATE_MASK)
7142 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7143 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7144 }
7145
7146 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007147 * The transform matrix of this view, which is calculated based on the current
7148 * roation, scale, and pivot properties.
7149 *
7150 * @see #getRotation()
7151 * @see #getScaleX()
7152 * @see #getScaleY()
7153 * @see #getPivotX()
7154 * @see #getPivotY()
7155 * @return The current transform matrix for the view
7156 */
7157 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007158 if (mTransformationInfo != null) {
7159 updateMatrix();
7160 return mTransformationInfo.mMatrix;
7161 }
7162 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007163 }
7164
7165 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007166 * Utility function to determine if the value is far enough away from zero to be
7167 * considered non-zero.
7168 * @param value A floating point value to check for zero-ness
7169 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7170 */
7171 private static boolean nonzero(float value) {
7172 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7173 }
7174
7175 /**
Jeff Brown86671742010-09-30 20:00:15 -07007176 * Returns true if the transform matrix is the identity matrix.
7177 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007178 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007179 * @return True if the transform matrix is the identity matrix, false otherwise.
7180 */
Jeff Brown86671742010-09-30 20:00:15 -07007181 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007182 if (mTransformationInfo != null) {
7183 updateMatrix();
7184 return mTransformationInfo.mMatrixIsIdentity;
7185 }
7186 return true;
7187 }
7188
7189 void ensureTransformationInfo() {
7190 if (mTransformationInfo == null) {
7191 mTransformationInfo = new TransformationInfo();
7192 }
Jeff Brown86671742010-09-30 20:00:15 -07007193 }
7194
7195 /**
7196 * Recomputes the transform matrix if necessary.
7197 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007198 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007199 final TransformationInfo info = mTransformationInfo;
7200 if (info == null) {
7201 return;
7202 }
7203 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007204 // transform-related properties have changed since the last time someone
7205 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007206
7207 // Figure out if we need to update the pivot point
7208 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007209 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7210 info.mPrevWidth = mRight - mLeft;
7211 info.mPrevHeight = mBottom - mTop;
7212 info.mPivotX = info.mPrevWidth / 2f;
7213 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007214 }
7215 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007216 info.mMatrix.reset();
7217 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7218 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7219 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7220 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007221 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007222 if (info.mCamera == null) {
7223 info.mCamera = new Camera();
7224 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007225 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007226 info.mCamera.save();
7227 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7228 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7229 info.mCamera.getMatrix(info.matrix3D);
7230 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7231 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7232 info.mPivotY + info.mTranslationY);
7233 info.mMatrix.postConcat(info.matrix3D);
7234 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007235 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007236 info.mMatrixDirty = false;
7237 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7238 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007239 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007240 }
7241
7242 /**
7243 * Utility method to retrieve the inverse of the current mMatrix property.
7244 * We cache the matrix to avoid recalculating it when transform properties
7245 * have not changed.
7246 *
7247 * @return The inverse of the current matrix of this view.
7248 */
Jeff Brown86671742010-09-30 20:00:15 -07007249 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007250 final TransformationInfo info = mTransformationInfo;
7251 if (info != null) {
7252 updateMatrix();
7253 if (info.mInverseMatrixDirty) {
7254 if (info.mInverseMatrix == null) {
7255 info.mInverseMatrix = new Matrix();
7256 }
7257 info.mMatrix.invert(info.mInverseMatrix);
7258 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007259 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007260 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007261 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007262 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007263 }
7264
7265 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007266 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7267 * views are drawn) from the camera to this view. The camera's distance
7268 * affects 3D transformations, for instance rotations around the X and Y
7269 * axis. If the rotationX or rotationY properties are changed and this view is
7270 * large (more than half the size of the screen), it is recommended to always
7271 * use a camera distance that's greater than the height (X axis rotation) or
7272 * the width (Y axis rotation) of this view.</p>
7273 *
7274 * <p>The distance of the camera from the view plane can have an affect on the
7275 * perspective distortion of the view when it is rotated around the x or y axis.
7276 * For example, a large distance will result in a large viewing angle, and there
7277 * will not be much perspective distortion of the view as it rotates. A short
7278 * distance may cause much more perspective distortion upon rotation, and can
7279 * also result in some drawing artifacts if the rotated view ends up partially
7280 * behind the camera (which is why the recommendation is to use a distance at
7281 * least as far as the size of the view, if the view is to be rotated.)</p>
7282 *
7283 * <p>The distance is expressed in "depth pixels." The default distance depends
7284 * on the screen density. For instance, on a medium density display, the
7285 * default distance is 1280. On a high density display, the default distance
7286 * is 1920.</p>
7287 *
7288 * <p>If you want to specify a distance that leads to visually consistent
7289 * results across various densities, use the following formula:</p>
7290 * <pre>
7291 * float scale = context.getResources().getDisplayMetrics().density;
7292 * view.setCameraDistance(distance * scale);
7293 * </pre>
7294 *
7295 * <p>The density scale factor of a high density display is 1.5,
7296 * and 1920 = 1280 * 1.5.</p>
7297 *
7298 * @param distance The distance in "depth pixels", if negative the opposite
7299 * value is used
7300 *
7301 * @see #setRotationX(float)
7302 * @see #setRotationY(float)
7303 */
7304 public void setCameraDistance(float distance) {
7305 invalidateParentCaches();
7306 invalidate(false);
7307
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007308 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007309 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007310 final TransformationInfo info = mTransformationInfo;
7311 if (info.mCamera == null) {
7312 info.mCamera = new Camera();
7313 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007314 }
7315
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007316 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7317 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007318
7319 invalidate(false);
7320 }
7321
7322 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007323 * The degrees that the view is rotated around the pivot point.
7324 *
Romain Guya5364ee2011-02-24 14:46:04 -08007325 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007326 * @see #getPivotX()
7327 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007328 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007329 * @return The degrees of rotation.
7330 */
Chet Haasea5531132012-02-02 13:41:44 -08007331 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007332 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007333 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007334 }
7335
7336 /**
Chet Haase897247b2010-09-09 14:54:47 -07007337 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7338 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007339 *
7340 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007341 *
7342 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007343 * @see #getPivotX()
7344 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007345 * @see #setRotationX(float)
7346 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007347 *
7348 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007349 */
7350 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007351 ensureTransformationInfo();
7352 final TransformationInfo info = mTransformationInfo;
7353 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007354 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007355 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007356 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007357 info.mRotation = rotation;
7358 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007359 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007360 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007361 }
7362 }
7363
7364 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007365 * The degrees that the view is rotated around the vertical axis through the pivot point.
7366 *
7367 * @see #getPivotX()
7368 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007369 * @see #setRotationY(float)
7370 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007371 * @return The degrees of Y rotation.
7372 */
Chet Haasea5531132012-02-02 13:41:44 -08007373 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007374 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007375 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007376 }
7377
7378 /**
Chet Haase897247b2010-09-09 14:54:47 -07007379 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7380 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7381 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007382 *
7383 * When rotating large views, it is recommended to adjust the camera distance
7384 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007385 *
7386 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007387 *
7388 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007389 * @see #getPivotX()
7390 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007391 * @see #setRotation(float)
7392 * @see #setRotationX(float)
7393 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007394 *
7395 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007396 */
7397 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007398 ensureTransformationInfo();
7399 final TransformationInfo info = mTransformationInfo;
7400 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007401 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007402 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007403 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007404 info.mRotationY = rotationY;
7405 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007406 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007407 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007408 }
7409 }
7410
7411 /**
7412 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7413 *
7414 * @see #getPivotX()
7415 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007416 * @see #setRotationX(float)
7417 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007418 * @return The degrees of X rotation.
7419 */
Chet Haasea5531132012-02-02 13:41:44 -08007420 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007421 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007422 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007423 }
7424
7425 /**
Chet Haase897247b2010-09-09 14:54:47 -07007426 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7427 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7428 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007429 *
7430 * When rotating large views, it is recommended to adjust the camera distance
7431 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007432 *
7433 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007434 *
7435 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007436 * @see #getPivotX()
7437 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007438 * @see #setRotation(float)
7439 * @see #setRotationY(float)
7440 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007441 *
7442 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007443 */
7444 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007445 ensureTransformationInfo();
7446 final TransformationInfo info = mTransformationInfo;
7447 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007448 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007449 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007450 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007451 info.mRotationX = rotationX;
7452 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007453 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007454 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007455 }
7456 }
7457
7458 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007459 * The amount that the view is scaled in x around the pivot point, as a proportion of
7460 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7461 *
Joe Onorato93162322010-09-16 15:42:01 -04007462 * <p>By default, this is 1.0f.
7463 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007464 * @see #getPivotX()
7465 * @see #getPivotY()
7466 * @return The scaling factor.
7467 */
Chet Haasea5531132012-02-02 13:41:44 -08007468 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007469 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007470 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007471 }
7472
7473 /**
7474 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7475 * the view's unscaled width. A value of 1 means that no scaling is applied.
7476 *
7477 * @param scaleX The scaling factor.
7478 * @see #getPivotX()
7479 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007480 *
7481 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007482 */
7483 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007484 ensureTransformationInfo();
7485 final TransformationInfo info = mTransformationInfo;
7486 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007487 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007488 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007489 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007490 info.mScaleX = scaleX;
7491 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007492 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007493 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007494 }
7495 }
7496
7497 /**
7498 * The amount that the view is scaled in y around the pivot point, as a proportion of
7499 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7500 *
Joe Onorato93162322010-09-16 15:42:01 -04007501 * <p>By default, this is 1.0f.
7502 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007503 * @see #getPivotX()
7504 * @see #getPivotY()
7505 * @return The scaling factor.
7506 */
Chet Haasea5531132012-02-02 13:41:44 -08007507 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007508 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007509 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007510 }
7511
7512 /**
7513 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7514 * the view's unscaled width. A value of 1 means that no scaling is applied.
7515 *
7516 * @param scaleY The scaling factor.
7517 * @see #getPivotX()
7518 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007519 *
7520 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007521 */
7522 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007523 ensureTransformationInfo();
7524 final TransformationInfo info = mTransformationInfo;
7525 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007526 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007527 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007528 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007529 info.mScaleY = scaleY;
7530 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007531 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007532 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007533 }
7534 }
7535
7536 /**
7537 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7538 * and {@link #setScaleX(float) scaled}.
7539 *
7540 * @see #getRotation()
7541 * @see #getScaleX()
7542 * @see #getScaleY()
7543 * @see #getPivotY()
7544 * @return The x location of the pivot point.
7545 */
Chet Haasea5531132012-02-02 13:41:44 -08007546 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007547 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007548 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007549 }
7550
7551 /**
7552 * Sets the x location of the point around which the view is
7553 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007554 * By default, the pivot point is centered on the object.
7555 * Setting this property disables this behavior and causes the view to use only the
7556 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007557 *
7558 * @param pivotX The x location of the pivot point.
7559 * @see #getRotation()
7560 * @see #getScaleX()
7561 * @see #getScaleY()
7562 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007563 *
7564 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007565 */
7566 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007567 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007568 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007569 final TransformationInfo info = mTransformationInfo;
7570 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007571 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007572 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007573 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007574 info.mPivotX = pivotX;
7575 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007576 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007577 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007578 }
7579 }
7580
7581 /**
7582 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7583 * and {@link #setScaleY(float) scaled}.
7584 *
7585 * @see #getRotation()
7586 * @see #getScaleX()
7587 * @see #getScaleY()
7588 * @see #getPivotY()
7589 * @return The y location of the pivot point.
7590 */
Chet Haasea5531132012-02-02 13:41:44 -08007591 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007592 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007593 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007594 }
7595
7596 /**
7597 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007598 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7599 * Setting this property disables this behavior and causes the view to use only the
7600 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007601 *
7602 * @param pivotY The y location of the pivot point.
7603 * @see #getRotation()
7604 * @see #getScaleX()
7605 * @see #getScaleY()
7606 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007607 *
7608 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007609 */
7610 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007611 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007612 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007613 final TransformationInfo info = mTransformationInfo;
7614 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007615 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007616 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007617 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007618 info.mPivotY = pivotY;
7619 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007620 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007621 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007622 }
7623 }
7624
7625 /**
7626 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7627 * completely transparent and 1 means the view is completely opaque.
7628 *
Joe Onorato93162322010-09-16 15:42:01 -04007629 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007630 * @return The opacity of the view.
7631 */
Chet Haasea5531132012-02-02 13:41:44 -08007632 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007633 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007634 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007635 }
7636
7637 /**
Romain Guy171c5922011-01-06 10:04:23 -08007638 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7639 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007640 *
Romain Guy171c5922011-01-06 10:04:23 -08007641 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7642 * responsible for applying the opacity itself. Otherwise, calling this method is
7643 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007644 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007645 *
Chet Haasea5531132012-02-02 13:41:44 -08007646 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7647 * performance implications. It is generally best to use the alpha property sparingly and
7648 * transiently, as in the case of fading animations.</p>
7649 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007650 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007651 *
Joe Malin32736f02011-01-19 16:14:20 -08007652 * @see #setLayerType(int, android.graphics.Paint)
7653 *
Chet Haase73066682010-11-29 15:55:32 -08007654 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007655 */
7656 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007657 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007658 if (mTransformationInfo.mAlpha != alpha) {
7659 mTransformationInfo.mAlpha = alpha;
7660 invalidateParentCaches();
7661 if (onSetAlpha((int) (alpha * 255))) {
7662 mPrivateFlags |= ALPHA_SET;
7663 // subclass is handling alpha - don't optimize rendering cache invalidation
7664 invalidate(true);
7665 } else {
7666 mPrivateFlags &= ~ALPHA_SET;
7667 invalidate(false);
7668 }
Chet Haaseed032702010-10-01 14:05:54 -07007669 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007670 }
7671
7672 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007673 * Faster version of setAlpha() which performs the same steps except there are
7674 * no calls to invalidate(). The caller of this function should perform proper invalidation
7675 * on the parent and this object. The return value indicates whether the subclass handles
7676 * alpha (the return value for onSetAlpha()).
7677 *
7678 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007679 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7680 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007681 */
7682 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007683 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007684 if (mTransformationInfo.mAlpha != alpha) {
7685 mTransformationInfo.mAlpha = alpha;
7686 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7687 if (subclassHandlesAlpha) {
7688 mPrivateFlags |= ALPHA_SET;
7689 return true;
7690 } else {
7691 mPrivateFlags &= ~ALPHA_SET;
7692 }
Chet Haasea00f3862011-02-22 06:34:40 -08007693 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007694 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007695 }
7696
7697 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 * Top position of this view relative to its parent.
7699 *
7700 * @return The top of this view, in pixels.
7701 */
7702 @ViewDebug.CapturedViewProperty
7703 public final int getTop() {
7704 return mTop;
7705 }
7706
7707 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007708 * Sets the top position of this view relative to its parent. This method is meant to be called
7709 * by the layout system and should not generally be called otherwise, because the property
7710 * may be changed at any time by the layout.
7711 *
7712 * @param top The top of this view, in pixels.
7713 */
7714 public final void setTop(int top) {
7715 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007716 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007717 final boolean matrixIsIdentity = mTransformationInfo == null
7718 || mTransformationInfo.mMatrixIsIdentity;
7719 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007720 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007721 int minTop;
7722 int yLoc;
7723 if (top < mTop) {
7724 minTop = top;
7725 yLoc = top - mTop;
7726 } else {
7727 minTop = mTop;
7728 yLoc = 0;
7729 }
Chet Haasee9140a72011-02-16 16:23:29 -08007730 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007731 }
7732 } else {
7733 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007734 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007735 }
7736
Chet Haaseed032702010-10-01 14:05:54 -07007737 int width = mRight - mLeft;
7738 int oldHeight = mBottom - mTop;
7739
Chet Haase21cd1382010-09-01 17:42:29 -07007740 mTop = top;
7741
Chet Haaseed032702010-10-01 14:05:54 -07007742 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7743
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007744 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007745 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7746 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007747 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007748 }
Chet Haase21cd1382010-09-01 17:42:29 -07007749 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007750 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007751 }
Chet Haase55dbb652010-12-21 20:15:08 -08007752 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007753 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007754 }
7755 }
7756
7757 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 * Bottom position of this view relative to its parent.
7759 *
7760 * @return The bottom of this view, in pixels.
7761 */
7762 @ViewDebug.CapturedViewProperty
7763 public final int getBottom() {
7764 return mBottom;
7765 }
7766
7767 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007768 * True if this view has changed since the last time being drawn.
7769 *
7770 * @return The dirty state of this view.
7771 */
7772 public boolean isDirty() {
7773 return (mPrivateFlags & DIRTY_MASK) != 0;
7774 }
7775
7776 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007777 * Sets the bottom position of this view relative to its parent. This method is meant to be
7778 * called by the layout system and should not generally be called otherwise, because the
7779 * property may be changed at any time by the layout.
7780 *
7781 * @param bottom The bottom of this view, in pixels.
7782 */
7783 public final void setBottom(int bottom) {
7784 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007785 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007786 final boolean matrixIsIdentity = mTransformationInfo == null
7787 || mTransformationInfo.mMatrixIsIdentity;
7788 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007789 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007790 int maxBottom;
7791 if (bottom < mBottom) {
7792 maxBottom = mBottom;
7793 } else {
7794 maxBottom = bottom;
7795 }
Chet Haasee9140a72011-02-16 16:23:29 -08007796 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007797 }
7798 } else {
7799 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007800 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007801 }
7802
Chet Haaseed032702010-10-01 14:05:54 -07007803 int width = mRight - mLeft;
7804 int oldHeight = mBottom - mTop;
7805
Chet Haase21cd1382010-09-01 17:42:29 -07007806 mBottom = bottom;
7807
Chet Haaseed032702010-10-01 14:05:54 -07007808 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7809
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007810 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007811 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7812 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007813 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007814 }
Chet Haase21cd1382010-09-01 17:42:29 -07007815 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007816 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007817 }
Chet Haase55dbb652010-12-21 20:15:08 -08007818 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007819 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007820 }
7821 }
7822
7823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 * Left position of this view relative to its parent.
7825 *
7826 * @return The left edge of this view, in pixels.
7827 */
7828 @ViewDebug.CapturedViewProperty
7829 public final int getLeft() {
7830 return mLeft;
7831 }
7832
7833 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007834 * Sets the left position of this view relative to its parent. This method is meant to be called
7835 * by the layout system and should not generally be called otherwise, because the property
7836 * may be changed at any time by the layout.
7837 *
7838 * @param left The bottom of this view, in pixels.
7839 */
7840 public final void setLeft(int left) {
7841 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007842 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007843 final boolean matrixIsIdentity = mTransformationInfo == null
7844 || mTransformationInfo.mMatrixIsIdentity;
7845 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007846 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007847 int minLeft;
7848 int xLoc;
7849 if (left < mLeft) {
7850 minLeft = left;
7851 xLoc = left - mLeft;
7852 } else {
7853 minLeft = mLeft;
7854 xLoc = 0;
7855 }
Chet Haasee9140a72011-02-16 16:23:29 -08007856 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007857 }
7858 } else {
7859 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007860 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007861 }
7862
Chet Haaseed032702010-10-01 14:05:54 -07007863 int oldWidth = mRight - mLeft;
7864 int height = mBottom - mTop;
7865
Chet Haase21cd1382010-09-01 17:42:29 -07007866 mLeft = left;
7867
Chet Haaseed032702010-10-01 14:05:54 -07007868 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7869
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007870 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007871 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7872 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007873 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007874 }
Chet Haase21cd1382010-09-01 17:42:29 -07007875 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007876 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007877 }
Chet Haase55dbb652010-12-21 20:15:08 -08007878 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007879 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007880 }
7881 }
7882
7883 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 * Right position of this view relative to its parent.
7885 *
7886 * @return The right edge of this view, in pixels.
7887 */
7888 @ViewDebug.CapturedViewProperty
7889 public final int getRight() {
7890 return mRight;
7891 }
7892
7893 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007894 * Sets the right position of this view relative to its parent. This method is meant to be called
7895 * by the layout system and should not generally be called otherwise, because the property
7896 * may be changed at any time by the layout.
7897 *
7898 * @param right The bottom of this view, in pixels.
7899 */
7900 public final void setRight(int right) {
7901 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007902 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007903 final boolean matrixIsIdentity = mTransformationInfo == null
7904 || mTransformationInfo.mMatrixIsIdentity;
7905 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007906 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007907 int maxRight;
7908 if (right < mRight) {
7909 maxRight = mRight;
7910 } else {
7911 maxRight = right;
7912 }
Chet Haasee9140a72011-02-16 16:23:29 -08007913 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007914 }
7915 } else {
7916 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007917 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007918 }
7919
Chet Haaseed032702010-10-01 14:05:54 -07007920 int oldWidth = mRight - mLeft;
7921 int height = mBottom - mTop;
7922
Chet Haase21cd1382010-09-01 17:42:29 -07007923 mRight = right;
7924
Chet Haaseed032702010-10-01 14:05:54 -07007925 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7926
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007927 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007928 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7929 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007930 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007931 }
Chet Haase21cd1382010-09-01 17:42:29 -07007932 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007933 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007934 }
Chet Haase55dbb652010-12-21 20:15:08 -08007935 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007936 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007937 }
7938 }
7939
7940 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007941 * The visual x position of this view, in pixels. This is equivalent to the
7942 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007943 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007944 *
Chet Haasedf030d22010-07-30 17:22:38 -07007945 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007946 */
Chet Haasea5531132012-02-02 13:41:44 -08007947 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007948 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007949 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007950 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007951
Chet Haasedf030d22010-07-30 17:22:38 -07007952 /**
7953 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7954 * {@link #setTranslationX(float) translationX} property to be the difference between
7955 * the x value passed in and the current {@link #getLeft() left} property.
7956 *
7957 * @param x The visual x position of this view, in pixels.
7958 */
7959 public void setX(float x) {
7960 setTranslationX(x - mLeft);
7961 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007962
Chet Haasedf030d22010-07-30 17:22:38 -07007963 /**
7964 * The visual y position of this view, in pixels. This is equivalent to the
7965 * {@link #setTranslationY(float) translationY} property plus the current
7966 * {@link #getTop() top} property.
7967 *
7968 * @return The visual y position of this view, in pixels.
7969 */
Chet Haasea5531132012-02-02 13:41:44 -08007970 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007971 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007972 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007973 }
7974
7975 /**
7976 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7977 * {@link #setTranslationY(float) translationY} property to be the difference between
7978 * the y value passed in and the current {@link #getTop() top} property.
7979 *
7980 * @param y The visual y position of this view, in pixels.
7981 */
7982 public void setY(float y) {
7983 setTranslationY(y - mTop);
7984 }
7985
7986
7987 /**
7988 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7989 * This position is post-layout, in addition to wherever the object's
7990 * layout placed it.
7991 *
7992 * @return The horizontal position of this view relative to its left position, in pixels.
7993 */
Chet Haasea5531132012-02-02 13:41:44 -08007994 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007995 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007996 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007997 }
7998
7999 /**
8000 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8001 * This effectively positions the object post-layout, in addition to wherever the object's
8002 * layout placed it.
8003 *
8004 * @param translationX The horizontal position of this view relative to its left position,
8005 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008006 *
8007 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008008 */
8009 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008010 ensureTransformationInfo();
8011 final TransformationInfo info = mTransformationInfo;
8012 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008013 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008014 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008015 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008016 info.mTranslationX = translationX;
8017 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008018 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008019 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008020 }
8021 }
8022
8023 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008024 * The horizontal location of this view relative to its {@link #getTop() top} position.
8025 * This position is post-layout, in addition to wherever the object's
8026 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008027 *
Chet Haasedf030d22010-07-30 17:22:38 -07008028 * @return The vertical position of this view relative to its top position,
8029 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008030 */
Chet Haasea5531132012-02-02 13:41:44 -08008031 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008032 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008033 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008034 }
8035
8036 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008037 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8038 * This effectively positions the object post-layout, in addition to wherever the object's
8039 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008040 *
Chet Haasedf030d22010-07-30 17:22:38 -07008041 * @param translationY The vertical position of this view relative to its top position,
8042 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008043 *
8044 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008045 */
Chet Haasedf030d22010-07-30 17:22:38 -07008046 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008047 ensureTransformationInfo();
8048 final TransformationInfo info = mTransformationInfo;
8049 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008050 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008051 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008052 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008053 info.mTranslationY = translationY;
8054 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008055 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008056 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07008057 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008058 }
8059
8060 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008061 * Hit rectangle in parent's coordinates
8062 *
8063 * @param outRect The hit rectangle of the view.
8064 */
8065 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008066 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008067 final TransformationInfo info = mTransformationInfo;
8068 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008069 outRect.set(mLeft, mTop, mRight, mBottom);
8070 } else {
8071 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008072 tmpRect.set(-info.mPivotX, -info.mPivotY,
8073 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8074 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008075 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8076 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008077 }
8078 }
8079
8080 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008081 * Determines whether the given point, in local coordinates is inside the view.
8082 */
8083 /*package*/ final boolean pointInView(float localX, float localY) {
8084 return localX >= 0 && localX < (mRight - mLeft)
8085 && localY >= 0 && localY < (mBottom - mTop);
8086 }
8087
8088 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008089 * Utility method to determine whether the given point, in local coordinates,
8090 * is inside the view, where the area of the view is expanded by the slop factor.
8091 * This method is called while processing touch-move events to determine if the event
8092 * is still within the view.
8093 */
8094 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008095 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008096 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008097 }
8098
8099 /**
8100 * When a view has focus and the user navigates away from it, the next view is searched for
8101 * starting from the rectangle filled in by this method.
8102 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008103 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8104 * of the view. However, if your view maintains some idea of internal selection,
8105 * such as a cursor, or a selected row or column, you should override this method and
8106 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008107 *
8108 * @param r The rectangle to fill in, in this view's coordinates.
8109 */
8110 public void getFocusedRect(Rect r) {
8111 getDrawingRect(r);
8112 }
8113
8114 /**
8115 * If some part of this view is not clipped by any of its parents, then
8116 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008117 * coordinates (without taking possible View rotations into account), offset
8118 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8119 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 *
8121 * @param r If true is returned, r holds the global coordinates of the
8122 * visible portion of this view.
8123 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8124 * between this view and its root. globalOffet may be null.
8125 * @return true if r is non-empty (i.e. part of the view is visible at the
8126 * root level.
8127 */
8128 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8129 int width = mRight - mLeft;
8130 int height = mBottom - mTop;
8131 if (width > 0 && height > 0) {
8132 r.set(0, 0, width, height);
8133 if (globalOffset != null) {
8134 globalOffset.set(-mScrollX, -mScrollY);
8135 }
8136 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8137 }
8138 return false;
8139 }
8140
8141 public final boolean getGlobalVisibleRect(Rect r) {
8142 return getGlobalVisibleRect(r, null);
8143 }
8144
8145 public final boolean getLocalVisibleRect(Rect r) {
8146 Point offset = new Point();
8147 if (getGlobalVisibleRect(r, offset)) {
8148 r.offset(-offset.x, -offset.y); // make r local
8149 return true;
8150 }
8151 return false;
8152 }
8153
8154 /**
8155 * Offset this view's vertical location by the specified number of pixels.
8156 *
8157 * @param offset the number of pixels to offset the view by
8158 */
8159 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008160 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008161 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008162 final boolean matrixIsIdentity = mTransformationInfo == null
8163 || mTransformationInfo.mMatrixIsIdentity;
8164 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008165 final ViewParent p = mParent;
8166 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008167 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008168 int minTop;
8169 int maxBottom;
8170 int yLoc;
8171 if (offset < 0) {
8172 minTop = mTop + offset;
8173 maxBottom = mBottom;
8174 yLoc = offset;
8175 } else {
8176 minTop = mTop;
8177 maxBottom = mBottom + offset;
8178 yLoc = 0;
8179 }
8180 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8181 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008182 }
8183 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008184 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008185 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008186
Chet Haasec3aa3612010-06-17 08:50:37 -07008187 mTop += offset;
8188 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008189
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008190 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008191 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008192 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008193 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008194 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008196 }
8197
8198 /**
8199 * Offset this view's horizontal location by the specified amount of pixels.
8200 *
8201 * @param offset the numer of pixels to offset the view by
8202 */
8203 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008204 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008205 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008206 final boolean matrixIsIdentity = mTransformationInfo == null
8207 || mTransformationInfo.mMatrixIsIdentity;
8208 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008209 final ViewParent p = mParent;
8210 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008211 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008212 int minLeft;
8213 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008214 if (offset < 0) {
8215 minLeft = mLeft + offset;
8216 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008217 } else {
8218 minLeft = mLeft;
8219 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008220 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008221 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008222 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008223 }
8224 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008225 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008226 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008227
Chet Haasec3aa3612010-06-17 08:50:37 -07008228 mLeft += offset;
8229 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008230
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008231 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008232 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008233 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008234 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008235 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008237 }
8238
8239 /**
8240 * Get the LayoutParams associated with this view. All views should have
8241 * layout parameters. These supply parameters to the <i>parent</i> of this
8242 * view specifying how it should be arranged. There are many subclasses of
8243 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8244 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008245 *
8246 * This method may return null if this View is not attached to a parent
8247 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8248 * was not invoked successfully. When a View is attached to a parent
8249 * ViewGroup, this method must not return null.
8250 *
8251 * @return The LayoutParams associated with this view, or null if no
8252 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008254 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 public ViewGroup.LayoutParams getLayoutParams() {
8256 return mLayoutParams;
8257 }
8258
8259 /**
8260 * Set the layout parameters associated with this view. These supply
8261 * parameters to the <i>parent</i> of this view specifying how it should be
8262 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8263 * correspond to the different subclasses of ViewGroup that are responsible
8264 * for arranging their children.
8265 *
Romain Guy01c174b2011-02-22 11:51:06 -08008266 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 */
8268 public void setLayoutParams(ViewGroup.LayoutParams params) {
8269 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008270 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008271 }
8272 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008273 if (mParent instanceof ViewGroup) {
8274 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008276 requestLayout();
8277 }
8278
8279 /**
8280 * Set the scrolled position of your view. This will cause a call to
8281 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8282 * invalidated.
8283 * @param x the x position to scroll to
8284 * @param y the y position to scroll to
8285 */
8286 public void scrollTo(int x, int y) {
8287 if (mScrollX != x || mScrollY != y) {
8288 int oldX = mScrollX;
8289 int oldY = mScrollY;
8290 mScrollX = x;
8291 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008292 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008294 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008295 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 }
8298 }
8299
8300 /**
8301 * Move the scrolled position of your view. This will cause a call to
8302 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8303 * invalidated.
8304 * @param x the amount of pixels to scroll by horizontally
8305 * @param y the amount of pixels to scroll by vertically
8306 */
8307 public void scrollBy(int x, int y) {
8308 scrollTo(mScrollX + x, mScrollY + y);
8309 }
8310
8311 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008312 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8313 * animation to fade the scrollbars out after a default delay. If a subclass
8314 * provides animated scrolling, the start delay should equal the duration
8315 * of the scrolling animation.</p>
8316 *
8317 * <p>The animation starts only if at least one of the scrollbars is
8318 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8319 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8320 * this method returns true, and false otherwise. If the animation is
8321 * started, this method calls {@link #invalidate()}; in that case the
8322 * caller should not call {@link #invalidate()}.</p>
8323 *
8324 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008325 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008326 *
8327 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8328 * and {@link #scrollTo(int, int)}.</p>
8329 *
8330 * @return true if the animation is played, false otherwise
8331 *
8332 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008333 * @see #scrollBy(int, int)
8334 * @see #scrollTo(int, int)
8335 * @see #isHorizontalScrollBarEnabled()
8336 * @see #isVerticalScrollBarEnabled()
8337 * @see #setHorizontalScrollBarEnabled(boolean)
8338 * @see #setVerticalScrollBarEnabled(boolean)
8339 */
8340 protected boolean awakenScrollBars() {
8341 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008342 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008343 }
8344
8345 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008346 * Trigger the scrollbars to draw.
8347 * This method differs from awakenScrollBars() only in its default duration.
8348 * initialAwakenScrollBars() will show the scroll bars for longer than
8349 * usual to give the user more of a chance to notice them.
8350 *
8351 * @return true if the animation is played, false otherwise.
8352 */
8353 private boolean initialAwakenScrollBars() {
8354 return mScrollCache != null &&
8355 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8356 }
8357
8358 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008359 * <p>
8360 * Trigger the scrollbars to draw. When invoked this method starts an
8361 * animation to fade the scrollbars out after a fixed delay. If a subclass
8362 * provides animated scrolling, the start delay should equal the duration of
8363 * the scrolling animation.
8364 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008365 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008366 * <p>
8367 * The animation starts only if at least one of the scrollbars is enabled,
8368 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8369 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8370 * this method returns true, and false otherwise. If the animation is
8371 * started, this method calls {@link #invalidate()}; in that case the caller
8372 * should not call {@link #invalidate()}.
8373 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008374 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008375 * <p>
8376 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008377 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008378 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008379 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008380 * @param startDelay the delay, in milliseconds, after which the animation
8381 * should start; when the delay is 0, the animation starts
8382 * immediately
8383 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008384 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008385 * @see #scrollBy(int, int)
8386 * @see #scrollTo(int, int)
8387 * @see #isHorizontalScrollBarEnabled()
8388 * @see #isVerticalScrollBarEnabled()
8389 * @see #setHorizontalScrollBarEnabled(boolean)
8390 * @see #setVerticalScrollBarEnabled(boolean)
8391 */
8392 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008393 return awakenScrollBars(startDelay, true);
8394 }
Joe Malin32736f02011-01-19 16:14:20 -08008395
Mike Cleron290947b2009-09-29 18:34:32 -07008396 /**
8397 * <p>
8398 * Trigger the scrollbars to draw. When invoked this method starts an
8399 * animation to fade the scrollbars out after a fixed delay. If a subclass
8400 * provides animated scrolling, the start delay should equal the duration of
8401 * the scrolling animation.
8402 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008403 *
Mike Cleron290947b2009-09-29 18:34:32 -07008404 * <p>
8405 * The animation starts only if at least one of the scrollbars is enabled,
8406 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8407 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8408 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008409 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008410 * is set to true; in that case the caller
8411 * should not call {@link #invalidate()}.
8412 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008413 *
Mike Cleron290947b2009-09-29 18:34:32 -07008414 * <p>
8415 * This method should be invoked everytime a subclass directly updates the
8416 * scroll parameters.
8417 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008418 *
Mike Cleron290947b2009-09-29 18:34:32 -07008419 * @param startDelay the delay, in milliseconds, after which the animation
8420 * should start; when the delay is 0, the animation starts
8421 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008422 *
Mike Cleron290947b2009-09-29 18:34:32 -07008423 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008424 *
Mike Cleron290947b2009-09-29 18:34:32 -07008425 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008426 *
Mike Cleron290947b2009-09-29 18:34:32 -07008427 * @see #scrollBy(int, int)
8428 * @see #scrollTo(int, int)
8429 * @see #isHorizontalScrollBarEnabled()
8430 * @see #isVerticalScrollBarEnabled()
8431 * @see #setHorizontalScrollBarEnabled(boolean)
8432 * @see #setVerticalScrollBarEnabled(boolean)
8433 */
8434 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008435 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008436
Mike Cleronf116bf82009-09-27 19:14:12 -07008437 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8438 return false;
8439 }
8440
8441 if (scrollCache.scrollBar == null) {
8442 scrollCache.scrollBar = new ScrollBarDrawable();
8443 }
8444
8445 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8446
Mike Cleron290947b2009-09-29 18:34:32 -07008447 if (invalidate) {
8448 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008449 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008450 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008451
8452 if (scrollCache.state == ScrollabilityCache.OFF) {
8453 // FIXME: this is copied from WindowManagerService.
8454 // We should get this value from the system when it
8455 // is possible to do so.
8456 final int KEY_REPEAT_FIRST_DELAY = 750;
8457 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8458 }
8459
8460 // Tell mScrollCache when we should start fading. This may
8461 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008462 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008463 scrollCache.fadeStartTime = fadeStartTime;
8464 scrollCache.state = ScrollabilityCache.ON;
8465
8466 // Schedule our fader to run, unscheduling any old ones first
8467 if (mAttachInfo != null) {
8468 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8469 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8470 }
8471
8472 return true;
8473 }
8474
8475 return false;
8476 }
8477
8478 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008479 * Do not invalidate views which are not visible and which are not running an animation. They
8480 * will not get drawn and they should not set dirty flags as if they will be drawn
8481 */
8482 private boolean skipInvalidate() {
8483 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8484 (!(mParent instanceof ViewGroup) ||
8485 !((ViewGroup) mParent).isViewTransitioning(this));
8486 }
8487 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008488 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008489 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8490 * in the future. This must be called from a UI thread. To call from a non-UI
8491 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 *
8493 * WARNING: This method is destructive to dirty.
8494 * @param dirty the rectangle representing the bounds of the dirty region
8495 */
8496 public void invalidate(Rect dirty) {
8497 if (ViewDebug.TRACE_HIERARCHY) {
8498 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8499 }
8500
Chet Haaseaceafe62011-08-26 15:44:33 -07008501 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008502 return;
8503 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008504 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008505 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8506 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008507 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008508 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008509 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008510 final ViewParent p = mParent;
8511 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008512 //noinspection PointlessBooleanExpression,ConstantConditions
8513 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8514 if (p != null && ai != null && ai.mHardwareAccelerated) {
8515 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008516 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008517 p.invalidateChild(this, null);
8518 return;
8519 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 if (p != null && ai != null) {
8522 final int scrollX = mScrollX;
8523 final int scrollY = mScrollY;
8524 final Rect r = ai.mTmpInvalRect;
8525 r.set(dirty.left - scrollX, dirty.top - scrollY,
8526 dirty.right - scrollX, dirty.bottom - scrollY);
8527 mParent.invalidateChild(this, r);
8528 }
8529 }
8530 }
8531
8532 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008533 * 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 -08008534 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008535 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8536 * will be called at some point in the future. This must be called from
8537 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008538 * @param l the left position of the dirty region
8539 * @param t the top position of the dirty region
8540 * @param r the right position of the dirty region
8541 * @param b the bottom position of the dirty region
8542 */
8543 public void invalidate(int l, int t, int r, int b) {
8544 if (ViewDebug.TRACE_HIERARCHY) {
8545 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8546 }
8547
Chet Haaseaceafe62011-08-26 15:44:33 -07008548 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008549 return;
8550 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008551 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008552 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8553 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008554 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008555 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008556 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 final ViewParent p = mParent;
8558 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008559 //noinspection PointlessBooleanExpression,ConstantConditions
8560 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8561 if (p != null && ai != null && ai.mHardwareAccelerated) {
8562 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008563 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008564 p.invalidateChild(this, null);
8565 return;
8566 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008568 if (p != null && ai != null && l < r && t < b) {
8569 final int scrollX = mScrollX;
8570 final int scrollY = mScrollY;
8571 final Rect tmpr = ai.mTmpInvalRect;
8572 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8573 p.invalidateChild(this, tmpr);
8574 }
8575 }
8576 }
8577
8578 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008579 * Invalidate the whole view. If the view is visible,
8580 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8581 * the future. This must be called from a UI thread. To call from a non-UI thread,
8582 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008583 */
8584 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008585 invalidate(true);
8586 }
Joe Malin32736f02011-01-19 16:14:20 -08008587
Chet Haaseed032702010-10-01 14:05:54 -07008588 /**
8589 * This is where the invalidate() work actually happens. A full invalidate()
8590 * causes the drawing cache to be invalidated, but this function can be called with
8591 * invalidateCache set to false to skip that invalidation step for cases that do not
8592 * need it (for example, a component that remains at the same dimensions with the same
8593 * content).
8594 *
8595 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8596 * well. This is usually true for a full invalidate, but may be set to false if the
8597 * View's contents or dimensions have not changed.
8598 */
Romain Guy849d0a32011-02-01 17:20:48 -08008599 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008600 if (ViewDebug.TRACE_HIERARCHY) {
8601 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8602 }
8603
Chet Haaseaceafe62011-08-26 15:44:33 -07008604 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008605 return;
8606 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008607 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008608 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008609 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8610 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008611 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008612 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008613 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008614 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008615 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008617 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008618 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008619 //noinspection PointlessBooleanExpression,ConstantConditions
8620 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8621 if (p != null && ai != null && ai.mHardwareAccelerated) {
8622 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008623 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008624 p.invalidateChild(this, null);
8625 return;
8626 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008627 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008629 if (p != null && ai != null) {
8630 final Rect r = ai.mTmpInvalRect;
8631 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8632 // Don't call invalidate -- we don't want to internally scroll
8633 // our own bounds
8634 p.invalidateChild(this, r);
8635 }
8636 }
8637 }
8638
8639 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008640 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008641 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8642 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008643 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8644 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008645 *
8646 * @hide
8647 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008648 protected void invalidateParentCaches() {
8649 if (mParent instanceof View) {
8650 ((View) mParent).mPrivateFlags |= INVALIDATED;
8651 }
8652 }
Joe Malin32736f02011-01-19 16:14:20 -08008653
Romain Guy0fd89bf2011-01-26 15:41:30 -08008654 /**
8655 * Used to indicate that the parent of this view should be invalidated. This functionality
8656 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8657 * which is necessary when various parent-managed properties of the view change, such as
8658 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8659 * an invalidation event to the parent.
8660 *
8661 * @hide
8662 */
8663 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008664 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008665 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008666 }
8667 }
8668
8669 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008670 * Indicates whether this View is opaque. An opaque View guarantees that it will
8671 * draw all the pixels overlapping its bounds using a fully opaque color.
8672 *
8673 * Subclasses of View should override this method whenever possible to indicate
8674 * whether an instance is opaque. Opaque Views are treated in a special way by
8675 * the View hierarchy, possibly allowing it to perform optimizations during
8676 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008677 *
Romain Guy24443ea2009-05-11 11:56:30 -07008678 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008679 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008680 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008681 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008682 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008683 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8684 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008685 }
8686
Adam Powell20232d02010-12-08 21:08:53 -08008687 /**
8688 * @hide
8689 */
8690 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008691 // Opaque if:
8692 // - Has a background
8693 // - Background is opaque
8694 // - Doesn't have scrollbars or scrollbars are inside overlay
8695
8696 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8697 mPrivateFlags |= OPAQUE_BACKGROUND;
8698 } else {
8699 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8700 }
8701
8702 final int flags = mViewFlags;
8703 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8704 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8705 mPrivateFlags |= OPAQUE_SCROLLBARS;
8706 } else {
8707 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8708 }
8709 }
8710
8711 /**
8712 * @hide
8713 */
8714 protected boolean hasOpaqueScrollbars() {
8715 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008716 }
8717
8718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008719 * @return A handler associated with the thread running the View. This
8720 * handler can be used to pump events in the UI events queue.
8721 */
8722 public Handler getHandler() {
8723 if (mAttachInfo != null) {
8724 return mAttachInfo.mHandler;
8725 }
8726 return null;
8727 }
8728
8729 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008730 * Gets the view root associated with the View.
8731 * @return The view root, or null if none.
8732 * @hide
8733 */
8734 public ViewRootImpl getViewRootImpl() {
8735 if (mAttachInfo != null) {
8736 return mAttachInfo.mViewRootImpl;
8737 }
8738 return null;
8739 }
8740
8741 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008742 * <p>Causes the Runnable to be added to the message queue.
8743 * The runnable will be run on the user interface thread.</p>
8744 *
8745 * <p>This method can be invoked from outside of the UI thread
8746 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008747 *
8748 * @param action The Runnable that will be executed.
8749 *
8750 * @return Returns true if the Runnable was successfully placed in to the
8751 * message queue. Returns false on failure, usually because the
8752 * looper processing the message queue is exiting.
8753 */
8754 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008755 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008756 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008757 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008758 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008759 // Assume that post will succeed later
8760 ViewRootImpl.getRunQueue().post(action);
8761 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 }
8763
8764 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008765 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008767 * The runnable will be run on the user interface thread.</p>
8768 *
8769 * <p>This method can be invoked from outside of the UI thread
8770 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008771 *
8772 * @param action The Runnable that will be executed.
8773 * @param delayMillis The delay (in milliseconds) until the Runnable
8774 * will be executed.
8775 *
8776 * @return true if the Runnable was successfully placed in to the
8777 * message queue. Returns false on failure, usually because the
8778 * looper processing the message queue is exiting. Note that a
8779 * result of true does not mean the Runnable will be processed --
8780 * if the looper is quit before the delivery time of the message
8781 * occurs then the message will be dropped.
8782 */
8783 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008784 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008785 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008786 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008787 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008788 // Assume that post will succeed later
8789 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8790 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008791 }
8792
8793 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008794 * <p>Removes the specified Runnable from the message queue.</p>
8795 *
8796 * <p>This method can be invoked from outside of the UI thread
8797 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008798 *
8799 * @param action The Runnable to remove from the message handling queue
8800 *
8801 * @return true if this view could ask the Handler to remove the Runnable,
8802 * false otherwise. When the returned value is true, the Runnable
8803 * may or may not have been actually removed from the message queue
8804 * (for instance, if the Runnable was not in the queue already.)
8805 */
8806 public boolean removeCallbacks(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008807 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008808 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008809 attachInfo.mHandler.removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 } else {
8811 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008812 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 return true;
8815 }
8816
8817 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008818 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8819 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 *
Romain Guye63a4f32011-08-11 11:33:31 -07008821 * <p>This method can be invoked from outside of the UI thread
8822 * only when this View is attached to a window.</p>
8823 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008824 * @see #invalidate()
8825 */
8826 public void postInvalidate() {
8827 postInvalidateDelayed(0);
8828 }
8829
8830 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008831 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8832 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8833 *
8834 * <p>This method can be invoked from outside of the UI thread
8835 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 *
8837 * @param left The left coordinate of the rectangle to invalidate.
8838 * @param top The top coordinate of the rectangle to invalidate.
8839 * @param right The right coordinate of the rectangle to invalidate.
8840 * @param bottom The bottom coordinate of the rectangle to invalidate.
8841 *
8842 * @see #invalidate(int, int, int, int)
8843 * @see #invalidate(Rect)
8844 */
8845 public void postInvalidate(int left, int top, int right, int bottom) {
8846 postInvalidateDelayed(0, left, top, right, bottom);
8847 }
8848
8849 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008850 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8851 * loop. Waits for the specified amount of time.</p>
8852 *
8853 * <p>This method can be invoked from outside of the UI thread
8854 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008855 *
8856 * @param delayMilliseconds the duration in milliseconds to delay the
8857 * invalidation by
8858 */
8859 public void postInvalidateDelayed(long delayMilliseconds) {
8860 // We try only with the AttachInfo because there's no point in invalidating
8861 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008862 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008863 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008864 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 }
8866 }
8867
8868 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008869 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8870 * through the event loop. Waits for the specified amount of time.</p>
8871 *
8872 * <p>This method can be invoked from outside of the UI thread
8873 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008874 *
8875 * @param delayMilliseconds the duration in milliseconds to delay the
8876 * invalidation by
8877 * @param left The left coordinate of the rectangle to invalidate.
8878 * @param top The top coordinate of the rectangle to invalidate.
8879 * @param right The right coordinate of the rectangle to invalidate.
8880 * @param bottom The bottom coordinate of the rectangle to invalidate.
8881 */
8882 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8883 int right, int bottom) {
8884
8885 // We try only with the AttachInfo because there's no point in invalidating
8886 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008887 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008888 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8890 info.target = this;
8891 info.left = left;
8892 info.top = top;
8893 info.right = right;
8894 info.bottom = bottom;
8895
Jeff Browna175a5b2012-02-15 19:18:31 -08008896 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008897 }
8898 }
8899
8900 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008901 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8902 * This event is sent at most once every
8903 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8904 */
8905 private void postSendViewScrolledAccessibilityEventCallback() {
8906 if (mSendViewScrolledAccessibilityEvent == null) {
8907 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8908 }
8909 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8910 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8911 postDelayed(mSendViewScrolledAccessibilityEvent,
8912 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8913 }
8914 }
8915
8916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008917 * Called by a parent to request that a child update its values for mScrollX
8918 * and mScrollY if necessary. This will typically be done if the child is
8919 * animating a scroll using a {@link android.widget.Scroller Scroller}
8920 * object.
8921 */
8922 public void computeScroll() {
8923 }
8924
8925 /**
8926 * <p>Indicate whether the horizontal edges are faded when the view is
8927 * scrolled horizontally.</p>
8928 *
8929 * @return true if the horizontal edges should are faded on scroll, false
8930 * otherwise
8931 *
8932 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008933 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 */
8935 public boolean isHorizontalFadingEdgeEnabled() {
8936 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8937 }
8938
8939 /**
8940 * <p>Define whether the horizontal edges should be faded when this view
8941 * is scrolled horizontally.</p>
8942 *
8943 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8944 * be faded when the view is scrolled
8945 * horizontally
8946 *
8947 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008948 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008949 */
8950 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8951 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8952 if (horizontalFadingEdgeEnabled) {
8953 initScrollCache();
8954 }
8955
8956 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8957 }
8958 }
8959
8960 /**
8961 * <p>Indicate whether the vertical edges are faded when the view is
8962 * scrolled horizontally.</p>
8963 *
8964 * @return true if the vertical edges should are faded on scroll, false
8965 * otherwise
8966 *
8967 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008968 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 */
8970 public boolean isVerticalFadingEdgeEnabled() {
8971 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8972 }
8973
8974 /**
8975 * <p>Define whether the vertical edges should be faded when this view
8976 * is scrolled vertically.</p>
8977 *
8978 * @param verticalFadingEdgeEnabled true if the vertical edges should
8979 * be faded when the view is scrolled
8980 * vertically
8981 *
8982 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008983 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008984 */
8985 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8986 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8987 if (verticalFadingEdgeEnabled) {
8988 initScrollCache();
8989 }
8990
8991 mViewFlags ^= FADING_EDGE_VERTICAL;
8992 }
8993 }
8994
8995 /**
8996 * Returns the strength, or intensity, of the top faded edge. The strength is
8997 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8998 * returns 0.0 or 1.0 but no value in between.
8999 *
9000 * Subclasses should override this method to provide a smoother fade transition
9001 * when scrolling occurs.
9002 *
9003 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9004 */
9005 protected float getTopFadingEdgeStrength() {
9006 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9007 }
9008
9009 /**
9010 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9011 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9012 * returns 0.0 or 1.0 but no value in between.
9013 *
9014 * Subclasses should override this method to provide a smoother fade transition
9015 * when scrolling occurs.
9016 *
9017 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9018 */
9019 protected float getBottomFadingEdgeStrength() {
9020 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9021 computeVerticalScrollRange() ? 1.0f : 0.0f;
9022 }
9023
9024 /**
9025 * Returns the strength, or intensity, of the left faded edge. The strength is
9026 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9027 * returns 0.0 or 1.0 but no value in between.
9028 *
9029 * Subclasses should override this method to provide a smoother fade transition
9030 * when scrolling occurs.
9031 *
9032 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9033 */
9034 protected float getLeftFadingEdgeStrength() {
9035 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9036 }
9037
9038 /**
9039 * Returns the strength, or intensity, of the right faded edge. The strength is
9040 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9041 * returns 0.0 or 1.0 but no value in between.
9042 *
9043 * Subclasses should override this method to provide a smoother fade transition
9044 * when scrolling occurs.
9045 *
9046 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9047 */
9048 protected float getRightFadingEdgeStrength() {
9049 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9050 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9051 }
9052
9053 /**
9054 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9055 * scrollbar is not drawn by default.</p>
9056 *
9057 * @return true if the horizontal scrollbar should be painted, false
9058 * otherwise
9059 *
9060 * @see #setHorizontalScrollBarEnabled(boolean)
9061 */
9062 public boolean isHorizontalScrollBarEnabled() {
9063 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9064 }
9065
9066 /**
9067 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9068 * scrollbar is not drawn by default.</p>
9069 *
9070 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9071 * be painted
9072 *
9073 * @see #isHorizontalScrollBarEnabled()
9074 */
9075 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9076 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9077 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009078 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009079 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009080 }
9081 }
9082
9083 /**
9084 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9085 * scrollbar is not drawn by default.</p>
9086 *
9087 * @return true if the vertical scrollbar should be painted, false
9088 * otherwise
9089 *
9090 * @see #setVerticalScrollBarEnabled(boolean)
9091 */
9092 public boolean isVerticalScrollBarEnabled() {
9093 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9094 }
9095
9096 /**
9097 * <p>Define whether the vertical scrollbar should be drawn or not. The
9098 * scrollbar is not drawn by default.</p>
9099 *
9100 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9101 * be painted
9102 *
9103 * @see #isVerticalScrollBarEnabled()
9104 */
9105 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9106 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9107 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009108 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009109 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009110 }
9111 }
9112
Adam Powell20232d02010-12-08 21:08:53 -08009113 /**
9114 * @hide
9115 */
9116 protected void recomputePadding() {
9117 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009118 }
Joe Malin32736f02011-01-19 16:14:20 -08009119
Mike Cleronfe81d382009-09-28 14:22:16 -07009120 /**
9121 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009122 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009123 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009124 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009125 */
9126 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9127 initScrollCache();
9128 final ScrollabilityCache scrollabilityCache = mScrollCache;
9129 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009130 if (fadeScrollbars) {
9131 scrollabilityCache.state = ScrollabilityCache.OFF;
9132 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009133 scrollabilityCache.state = ScrollabilityCache.ON;
9134 }
9135 }
Joe Malin32736f02011-01-19 16:14:20 -08009136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009137 /**
Joe Malin32736f02011-01-19 16:14:20 -08009138 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009139 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009140 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009141 * @return true if scrollbar fading is enabled
9142 */
9143 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009144 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009145 }
Joe Malin32736f02011-01-19 16:14:20 -08009146
Mike Cleron52f0a642009-09-28 18:21:37 -07009147 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009148 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9149 * inset. When inset, they add to the padding of the view. And the scrollbars
9150 * can be drawn inside the padding area or on the edge of the view. For example,
9151 * if a view has a background drawable and you want to draw the scrollbars
9152 * inside the padding specified by the drawable, you can use
9153 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9154 * appear at the edge of the view, ignoring the padding, then you can use
9155 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9156 * @param style the style of the scrollbars. Should be one of
9157 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9158 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9159 * @see #SCROLLBARS_INSIDE_OVERLAY
9160 * @see #SCROLLBARS_INSIDE_INSET
9161 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9162 * @see #SCROLLBARS_OUTSIDE_INSET
9163 */
9164 public void setScrollBarStyle(int style) {
9165 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9166 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009167 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009168 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009169 }
9170 }
9171
9172 /**
9173 * <p>Returns the current scrollbar style.</p>
9174 * @return the current scrollbar style
9175 * @see #SCROLLBARS_INSIDE_OVERLAY
9176 * @see #SCROLLBARS_INSIDE_INSET
9177 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9178 * @see #SCROLLBARS_OUTSIDE_INSET
9179 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009180 @ViewDebug.ExportedProperty(mapping = {
9181 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9182 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9183 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9184 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9185 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009186 public int getScrollBarStyle() {
9187 return mViewFlags & SCROLLBARS_STYLE_MASK;
9188 }
9189
9190 /**
9191 * <p>Compute the horizontal range that the horizontal scrollbar
9192 * represents.</p>
9193 *
9194 * <p>The range is expressed in arbitrary units that must be the same as the
9195 * units used by {@link #computeHorizontalScrollExtent()} and
9196 * {@link #computeHorizontalScrollOffset()}.</p>
9197 *
9198 * <p>The default range is the drawing width of this view.</p>
9199 *
9200 * @return the total horizontal range represented by the horizontal
9201 * scrollbar
9202 *
9203 * @see #computeHorizontalScrollExtent()
9204 * @see #computeHorizontalScrollOffset()
9205 * @see android.widget.ScrollBarDrawable
9206 */
9207 protected int computeHorizontalScrollRange() {
9208 return getWidth();
9209 }
9210
9211 /**
9212 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9213 * within the horizontal range. This value is used to compute the position
9214 * of the thumb within the scrollbar's track.</p>
9215 *
9216 * <p>The range is expressed in arbitrary units that must be the same as the
9217 * units used by {@link #computeHorizontalScrollRange()} and
9218 * {@link #computeHorizontalScrollExtent()}.</p>
9219 *
9220 * <p>The default offset is the scroll offset of this view.</p>
9221 *
9222 * @return the horizontal offset of the scrollbar's thumb
9223 *
9224 * @see #computeHorizontalScrollRange()
9225 * @see #computeHorizontalScrollExtent()
9226 * @see android.widget.ScrollBarDrawable
9227 */
9228 protected int computeHorizontalScrollOffset() {
9229 return mScrollX;
9230 }
9231
9232 /**
9233 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9234 * within the horizontal range. This value is used to compute the length
9235 * of the thumb within the scrollbar's track.</p>
9236 *
9237 * <p>The range is expressed in arbitrary units that must be the same as the
9238 * units used by {@link #computeHorizontalScrollRange()} and
9239 * {@link #computeHorizontalScrollOffset()}.</p>
9240 *
9241 * <p>The default extent is the drawing width of this view.</p>
9242 *
9243 * @return the horizontal extent of the scrollbar's thumb
9244 *
9245 * @see #computeHorizontalScrollRange()
9246 * @see #computeHorizontalScrollOffset()
9247 * @see android.widget.ScrollBarDrawable
9248 */
9249 protected int computeHorizontalScrollExtent() {
9250 return getWidth();
9251 }
9252
9253 /**
9254 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9255 *
9256 * <p>The range is expressed in arbitrary units that must be the same as the
9257 * units used by {@link #computeVerticalScrollExtent()} and
9258 * {@link #computeVerticalScrollOffset()}.</p>
9259 *
9260 * @return the total vertical range represented by the vertical scrollbar
9261 *
9262 * <p>The default range is the drawing height of this view.</p>
9263 *
9264 * @see #computeVerticalScrollExtent()
9265 * @see #computeVerticalScrollOffset()
9266 * @see android.widget.ScrollBarDrawable
9267 */
9268 protected int computeVerticalScrollRange() {
9269 return getHeight();
9270 }
9271
9272 /**
9273 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9274 * within the horizontal range. This value is used to compute the position
9275 * of the thumb within the scrollbar's track.</p>
9276 *
9277 * <p>The range is expressed in arbitrary units that must be the same as the
9278 * units used by {@link #computeVerticalScrollRange()} and
9279 * {@link #computeVerticalScrollExtent()}.</p>
9280 *
9281 * <p>The default offset is the scroll offset of this view.</p>
9282 *
9283 * @return the vertical offset of the scrollbar's thumb
9284 *
9285 * @see #computeVerticalScrollRange()
9286 * @see #computeVerticalScrollExtent()
9287 * @see android.widget.ScrollBarDrawable
9288 */
9289 protected int computeVerticalScrollOffset() {
9290 return mScrollY;
9291 }
9292
9293 /**
9294 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9295 * within the vertical range. This value is used to compute the length
9296 * of the thumb within the scrollbar's track.</p>
9297 *
9298 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009299 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 * {@link #computeVerticalScrollOffset()}.</p>
9301 *
9302 * <p>The default extent is the drawing height of this view.</p>
9303 *
9304 * @return the vertical extent of the scrollbar's thumb
9305 *
9306 * @see #computeVerticalScrollRange()
9307 * @see #computeVerticalScrollOffset()
9308 * @see android.widget.ScrollBarDrawable
9309 */
9310 protected int computeVerticalScrollExtent() {
9311 return getHeight();
9312 }
9313
9314 /**
Adam Powell69159442011-06-13 17:53:06 -07009315 * Check if this view can be scrolled horizontally in a certain direction.
9316 *
9317 * @param direction Negative to check scrolling left, positive to check scrolling right.
9318 * @return true if this view can be scrolled in the specified direction, false otherwise.
9319 */
9320 public boolean canScrollHorizontally(int direction) {
9321 final int offset = computeHorizontalScrollOffset();
9322 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9323 if (range == 0) return false;
9324 if (direction < 0) {
9325 return offset > 0;
9326 } else {
9327 return offset < range - 1;
9328 }
9329 }
9330
9331 /**
9332 * Check if this view can be scrolled vertically in a certain direction.
9333 *
9334 * @param direction Negative to check scrolling up, positive to check scrolling down.
9335 * @return true if this view can be scrolled in the specified direction, false otherwise.
9336 */
9337 public boolean canScrollVertically(int direction) {
9338 final int offset = computeVerticalScrollOffset();
9339 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9340 if (range == 0) return false;
9341 if (direction < 0) {
9342 return offset > 0;
9343 } else {
9344 return offset < range - 1;
9345 }
9346 }
9347
9348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009349 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9350 * scrollbars are painted only if they have been awakened first.</p>
9351 *
9352 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009353 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009354 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009355 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009356 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009357 // scrollbars are drawn only when the animation is running
9358 final ScrollabilityCache cache = mScrollCache;
9359 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009360
Mike Cleronf116bf82009-09-27 19:14:12 -07009361 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009362
Mike Cleronf116bf82009-09-27 19:14:12 -07009363 if (state == ScrollabilityCache.OFF) {
9364 return;
9365 }
Joe Malin32736f02011-01-19 16:14:20 -08009366
Mike Cleronf116bf82009-09-27 19:14:12 -07009367 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009368
Mike Cleronf116bf82009-09-27 19:14:12 -07009369 if (state == ScrollabilityCache.FADING) {
9370 // We're fading -- get our fade interpolation
9371 if (cache.interpolatorValues == null) {
9372 cache.interpolatorValues = new float[1];
9373 }
Joe Malin32736f02011-01-19 16:14:20 -08009374
Mike Cleronf116bf82009-09-27 19:14:12 -07009375 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009376
Mike Cleronf116bf82009-09-27 19:14:12 -07009377 // Stops the animation if we're done
9378 if (cache.scrollBarInterpolator.timeToValues(values) ==
9379 Interpolator.Result.FREEZE_END) {
9380 cache.state = ScrollabilityCache.OFF;
9381 } else {
9382 cache.scrollBar.setAlpha(Math.round(values[0]));
9383 }
Joe Malin32736f02011-01-19 16:14:20 -08009384
9385 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009386 // drawing. We only want this when we're fading so that
9387 // we prevent excessive redraws
9388 invalidate = true;
9389 } else {
9390 // We're just on -- but we may have been fading before so
9391 // reset alpha
9392 cache.scrollBar.setAlpha(255);
9393 }
9394
Joe Malin32736f02011-01-19 16:14:20 -08009395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009396 final int viewFlags = mViewFlags;
9397
9398 final boolean drawHorizontalScrollBar =
9399 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9400 final boolean drawVerticalScrollBar =
9401 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9402 && !isVerticalScrollBarHidden();
9403
9404 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9405 final int width = mRight - mLeft;
9406 final int height = mBottom - mTop;
9407
9408 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009409
Mike Reede8853fc2009-09-04 14:01:48 -04009410 final int scrollX = mScrollX;
9411 final int scrollY = mScrollY;
9412 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9413
Mike Cleronf116bf82009-09-27 19:14:12 -07009414 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009416 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009417 int size = scrollBar.getSize(false);
9418 if (size <= 0) {
9419 size = cache.scrollBarSize;
9420 }
9421
Mike Cleronf116bf82009-09-27 19:14:12 -07009422 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009423 computeHorizontalScrollOffset(),
9424 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009425 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009426 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009427 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009428 left = scrollX + (mPaddingLeft & inside);
9429 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9430 bottom = top + size;
9431 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9432 if (invalidate) {
9433 invalidate(left, top, right, bottom);
9434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009435 }
9436
9437 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009438 int size = scrollBar.getSize(true);
9439 if (size <= 0) {
9440 size = cache.scrollBarSize;
9441 }
9442
Mike Reede8853fc2009-09-04 14:01:48 -04009443 scrollBar.setParameters(computeVerticalScrollRange(),
9444 computeVerticalScrollOffset(),
9445 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009446 switch (mVerticalScrollbarPosition) {
9447 default:
9448 case SCROLLBAR_POSITION_DEFAULT:
9449 case SCROLLBAR_POSITION_RIGHT:
9450 left = scrollX + width - size - (mUserPaddingRight & inside);
9451 break;
9452 case SCROLLBAR_POSITION_LEFT:
9453 left = scrollX + (mUserPaddingLeft & inside);
9454 break;
9455 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009456 top = scrollY + (mPaddingTop & inside);
9457 right = left + size;
9458 bottom = scrollY + height - (mUserPaddingBottom & inside);
9459 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9460 if (invalidate) {
9461 invalidate(left, top, right, bottom);
9462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 }
9464 }
9465 }
9466 }
Romain Guy8506ab42009-06-11 17:35:47 -07009467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009468 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009469 * 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 -08009470 * FastScroller is visible.
9471 * @return whether to temporarily hide the vertical scrollbar
9472 * @hide
9473 */
9474 protected boolean isVerticalScrollBarHidden() {
9475 return false;
9476 }
9477
9478 /**
9479 * <p>Draw the horizontal scrollbar if
9480 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9481 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482 * @param canvas the canvas on which to draw the scrollbar
9483 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009484 *
9485 * @see #isHorizontalScrollBarEnabled()
9486 * @see #computeHorizontalScrollRange()
9487 * @see #computeHorizontalScrollExtent()
9488 * @see #computeHorizontalScrollOffset()
9489 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009490 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009491 */
Romain Guy8fb95422010-08-17 18:38:51 -07009492 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9493 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009494 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009495 scrollBar.draw(canvas);
9496 }
Mike Reede8853fc2009-09-04 14:01:48 -04009497
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9500 * returns true.</p>
9501 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009502 * @param canvas the canvas on which to draw the scrollbar
9503 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009504 *
9505 * @see #isVerticalScrollBarEnabled()
9506 * @see #computeVerticalScrollRange()
9507 * @see #computeVerticalScrollExtent()
9508 * @see #computeVerticalScrollOffset()
9509 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009510 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009511 */
Romain Guy8fb95422010-08-17 18:38:51 -07009512 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9513 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009514 scrollBar.setBounds(l, t, r, b);
9515 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009516 }
9517
9518 /**
9519 * Implement this to do your drawing.
9520 *
9521 * @param canvas the canvas on which the background will be drawn
9522 */
9523 protected void onDraw(Canvas canvas) {
9524 }
9525
9526 /*
9527 * Caller is responsible for calling requestLayout if necessary.
9528 * (This allows addViewInLayout to not request a new layout.)
9529 */
9530 void assignParent(ViewParent parent) {
9531 if (mParent == null) {
9532 mParent = parent;
9533 } else if (parent == null) {
9534 mParent = null;
9535 } else {
9536 throw new RuntimeException("view " + this + " being added, but"
9537 + " it already has a parent");
9538 }
9539 }
9540
9541 /**
9542 * This is called when the view is attached to a window. At this point it
9543 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009544 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9545 * however it may be called any time before the first onDraw -- including
9546 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009547 *
9548 * @see #onDetachedFromWindow()
9549 */
9550 protected void onAttachedToWindow() {
9551 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9552 mParent.requestTransparentRegion(this);
9553 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009554 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9555 initialAwakenScrollBars();
9556 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9557 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009558 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009559 // Order is important here: LayoutDirection MUST be resolved before Padding
9560 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009561 resolveLayoutDirectionIfNeeded();
9562 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009563 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009564 if (isFocused()) {
9565 InputMethodManager imm = InputMethodManager.peekInstance();
9566 imm.focusIn(this);
9567 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009568 }
Cibu Johny86666632010-02-22 13:01:02 -08009569
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009570 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009571 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9572 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009573 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009574 private void resolveLayoutDirectionIfNeeded() {
9575 // Do not resolve if it is not needed
9576 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9577
9578 // Clear any previous layout direction resolution
9579 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9580
9581 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009582 switch (getLayoutDirection()) {
9583 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009584 // We cannot do the resolution if there is no parent
9585 if (mParent == null) return;
9586
Cibu Johny86666632010-02-22 13:01:02 -08009587 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009588 if (mParent instanceof ViewGroup) {
9589 ViewGroup viewGroup = ((ViewGroup) mParent);
9590
9591 // Check if the parent view group can resolve
9592 if (! viewGroup.canResolveLayoutDirection()) {
9593 return;
9594 }
9595 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9596 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9597 }
Cibu Johny86666632010-02-22 13:01:02 -08009598 }
9599 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009600 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009601 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009602 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009603 case LAYOUT_DIRECTION_LOCALE:
9604 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009605 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009606 }
9607 break;
9608 default:
9609 // Nothing to do, LTR by default
9610 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009611
9612 // Set to resolved
9613 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9614 }
9615
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009616 /**
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009617 * Force padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009618 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009619 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009620 // If the user specified the absolute padding (either with android:padding or
9621 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9622 // use the default padding or the padding from the background drawable
9623 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009624 int resolvedLayoutDirection = getResolvedLayoutDirection();
9625 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009626 case LAYOUT_DIRECTION_RTL:
9627 // Start user padding override Right user padding. Otherwise, if Right user
9628 // padding is not defined, use the default Right padding. If Right user padding
9629 // is defined, just use it.
9630 if (mUserPaddingStart >= 0) {
9631 mUserPaddingRight = mUserPaddingStart;
9632 } else if (mUserPaddingRight < 0) {
9633 mUserPaddingRight = mPaddingRight;
9634 }
9635 if (mUserPaddingEnd >= 0) {
9636 mUserPaddingLeft = mUserPaddingEnd;
9637 } else if (mUserPaddingLeft < 0) {
9638 mUserPaddingLeft = mPaddingLeft;
9639 }
9640 break;
9641 case LAYOUT_DIRECTION_LTR:
9642 default:
9643 // Start user padding override Left user padding. Otherwise, if Left user
9644 // padding is not defined, use the default left padding. If Left user padding
9645 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009646 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009647 mUserPaddingLeft = mUserPaddingStart;
9648 } else if (mUserPaddingLeft < 0) {
9649 mUserPaddingLeft = mPaddingLeft;
9650 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009651 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009652 mUserPaddingRight = mUserPaddingEnd;
9653 } else if (mUserPaddingRight < 0) {
9654 mUserPaddingRight = mPaddingRight;
9655 }
9656 }
9657
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009658 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9659
9660 recomputePadding();
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009661 onResolvePadding(resolvedLayoutDirection);
9662 }
9663
9664 /**
9665 * Resolve padding depending on the layout direction. Subclasses that care about
9666 * padding resolution should override this method. The default implementation does
9667 * nothing.
9668 *
9669 * @param layoutDirection the direction of the layout
9670 *
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009671 */
9672 public void onResolvePadding(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009673 }
9674
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009675 /**
9676 * Return true if layout direction resolution can be done
9677 *
9678 * @hide
9679 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009680 protected boolean canResolveLayoutDirection() {
9681 switch (getLayoutDirection()) {
9682 case LAYOUT_DIRECTION_INHERIT:
9683 return (mParent != null);
9684 default:
9685 return true;
9686 }
9687 }
9688
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009689 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009690 * Reset the resolved layout direction.
9691 *
9692 * Subclasses need to override this method to clear cached information that depends on the
9693 * resolved layout direction, or to inform child views that inherit their layout direction.
9694 * Overrides must also call the superclass implementation at the start of their implementation.
9695 *
9696 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009697 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009698 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009699 // Reset the layout direction resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009700 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009701 // Reset also the text direction
9702 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009703 }
9704
9705 /**
9706 * Check if a Locale is corresponding to a RTL script.
9707 *
9708 * @param locale Locale to check
9709 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009710 *
9711 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009712 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009713 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009714 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9715 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 }
9717
9718 /**
9719 * This is called when the view is detached from a window. At this point it
9720 * no longer has a surface for drawing.
9721 *
9722 * @see #onAttachedToWindow()
9723 */
9724 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009725 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009726
Romain Guya440b002010-02-24 15:57:54 -08009727 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009728 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009729 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009730 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009733
Romain Guy6d7475d2011-07-27 16:28:21 -07009734 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009735
Chet Haasedaf98e92011-01-10 14:10:36 -08009736 if (mDisplayList != null) {
9737 mDisplayList.invalidate();
9738 }
9739
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009740 if (mAttachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009741 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009742 }
9743
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009744 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009745
9746 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009747 }
9748
9749 /**
9750 * @return The number of times this view has been attached to a window
9751 */
9752 protected int getWindowAttachCount() {
9753 return mWindowAttachCount;
9754 }
9755
9756 /**
9757 * Retrieve a unique token identifying the window this view is attached to.
9758 * @return Return the window's token for use in
9759 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9760 */
9761 public IBinder getWindowToken() {
9762 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9763 }
9764
9765 /**
9766 * Retrieve a unique token identifying the top-level "real" window of
9767 * the window that this view is attached to. That is, this is like
9768 * {@link #getWindowToken}, except if the window this view in is a panel
9769 * window (attached to another containing window), then the token of
9770 * the containing window is returned instead.
9771 *
9772 * @return Returns the associated window token, either
9773 * {@link #getWindowToken()} or the containing window's token.
9774 */
9775 public IBinder getApplicationWindowToken() {
9776 AttachInfo ai = mAttachInfo;
9777 if (ai != null) {
9778 IBinder appWindowToken = ai.mPanelParentWindowToken;
9779 if (appWindowToken == null) {
9780 appWindowToken = ai.mWindowToken;
9781 }
9782 return appWindowToken;
9783 }
9784 return null;
9785 }
9786
9787 /**
9788 * Retrieve private session object this view hierarchy is using to
9789 * communicate with the window manager.
9790 * @return the session object to communicate with the window manager
9791 */
9792 /*package*/ IWindowSession getWindowSession() {
9793 return mAttachInfo != null ? mAttachInfo.mSession : null;
9794 }
9795
9796 /**
9797 * @param info the {@link android.view.View.AttachInfo} to associated with
9798 * this view
9799 */
9800 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9801 //System.out.println("Attached! " + this);
9802 mAttachInfo = info;
9803 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009804 // We will need to evaluate the drawable state at least once.
9805 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 if (mFloatingTreeObserver != null) {
9807 info.mTreeObserver.merge(mFloatingTreeObserver);
9808 mFloatingTreeObserver = null;
9809 }
9810 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9811 mAttachInfo.mScrollContainers.add(this);
9812 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9813 }
9814 performCollectViewAttributes(visibility);
9815 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009816
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009817 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009818 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009819 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009820 if (listeners != null && listeners.size() > 0) {
9821 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9822 // perform the dispatching. The iterator is a safe guard against listeners that
9823 // could mutate the list by calling the various add/remove methods. This prevents
9824 // the array from being modified while we iterate it.
9825 for (OnAttachStateChangeListener listener : listeners) {
9826 listener.onViewAttachedToWindow(this);
9827 }
9828 }
9829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 int vis = info.mWindowVisibility;
9831 if (vis != GONE) {
9832 onWindowVisibilityChanged(vis);
9833 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009834 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9835 // If nobody has evaluated the drawable state yet, then do it now.
9836 refreshDrawableState();
9837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009838 }
9839
9840 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009841 AttachInfo info = mAttachInfo;
9842 if (info != null) {
9843 int vis = info.mWindowVisibility;
9844 if (vis != GONE) {
9845 onWindowVisibilityChanged(GONE);
9846 }
9847 }
9848
9849 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009850
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009851 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009852 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009853 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009854 if (listeners != null && listeners.size() > 0) {
9855 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9856 // perform the dispatching. The iterator is a safe guard against listeners that
9857 // could mutate the list by calling the various add/remove methods. This prevents
9858 // the array from being modified while we iterate it.
9859 for (OnAttachStateChangeListener listener : listeners) {
9860 listener.onViewDetachedFromWindow(this);
9861 }
9862 }
9863
Romain Guy01d5edc2011-01-28 11:28:53 -08009864 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009865 mAttachInfo.mScrollContainers.remove(this);
9866 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9867 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 mAttachInfo = null;
9870 }
9871
9872 /**
9873 * Store this view hierarchy's frozen state into the given container.
9874 *
9875 * @param container The SparseArray in which to save the view's state.
9876 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009877 * @see #restoreHierarchyState(android.util.SparseArray)
9878 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9879 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 */
9881 public void saveHierarchyState(SparseArray<Parcelable> container) {
9882 dispatchSaveInstanceState(container);
9883 }
9884
9885 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009886 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9887 * this view and its children. May be overridden to modify how freezing happens to a
9888 * 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 -08009889 *
9890 * @param container The SparseArray in which to save the view's state.
9891 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009892 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9893 * @see #saveHierarchyState(android.util.SparseArray)
9894 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009895 */
9896 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9897 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9898 mPrivateFlags &= ~SAVE_STATE_CALLED;
9899 Parcelable state = onSaveInstanceState();
9900 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9901 throw new IllegalStateException(
9902 "Derived class did not call super.onSaveInstanceState()");
9903 }
9904 if (state != null) {
9905 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9906 // + ": " + state);
9907 container.put(mID, state);
9908 }
9909 }
9910 }
9911
9912 /**
9913 * Hook allowing a view to generate a representation of its internal state
9914 * that can later be used to create a new instance with that same state.
9915 * This state should only contain information that is not persistent or can
9916 * not be reconstructed later. For example, you will never store your
9917 * current position on screen because that will be computed again when a
9918 * new instance of the view is placed in its view hierarchy.
9919 * <p>
9920 * Some examples of things you may store here: the current cursor position
9921 * in a text view (but usually not the text itself since that is stored in a
9922 * content provider or other persistent storage), the currently selected
9923 * item in a list view.
9924 *
9925 * @return Returns a Parcelable object containing the view's current dynamic
9926 * state, or null if there is nothing interesting to save. The
9927 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009928 * @see #onRestoreInstanceState(android.os.Parcelable)
9929 * @see #saveHierarchyState(android.util.SparseArray)
9930 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 * @see #setSaveEnabled(boolean)
9932 */
9933 protected Parcelable onSaveInstanceState() {
9934 mPrivateFlags |= SAVE_STATE_CALLED;
9935 return BaseSavedState.EMPTY_STATE;
9936 }
9937
9938 /**
9939 * Restore this view hierarchy's frozen state from the given container.
9940 *
9941 * @param container The SparseArray which holds previously frozen states.
9942 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009943 * @see #saveHierarchyState(android.util.SparseArray)
9944 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9945 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946 */
9947 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9948 dispatchRestoreInstanceState(container);
9949 }
9950
9951 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009952 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9953 * state for this view and its children. May be overridden to modify how restoring
9954 * happens to a view's children; for example, some views may want to not store state
9955 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 *
9957 * @param container The SparseArray which holds previously saved state.
9958 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009959 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9960 * @see #restoreHierarchyState(android.util.SparseArray)
9961 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009962 */
9963 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9964 if (mID != NO_ID) {
9965 Parcelable state = container.get(mID);
9966 if (state != null) {
9967 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9968 // + ": " + state);
9969 mPrivateFlags &= ~SAVE_STATE_CALLED;
9970 onRestoreInstanceState(state);
9971 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9972 throw new IllegalStateException(
9973 "Derived class did not call super.onRestoreInstanceState()");
9974 }
9975 }
9976 }
9977 }
9978
9979 /**
9980 * Hook allowing a view to re-apply a representation of its internal state that had previously
9981 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9982 * null state.
9983 *
9984 * @param state The frozen state that had previously been returned by
9985 * {@link #onSaveInstanceState}.
9986 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009987 * @see #onSaveInstanceState()
9988 * @see #restoreHierarchyState(android.util.SparseArray)
9989 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009990 */
9991 protected void onRestoreInstanceState(Parcelable state) {
9992 mPrivateFlags |= SAVE_STATE_CALLED;
9993 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009994 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9995 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009996 + "when two views of different type have the same id in the same hierarchy. "
9997 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009998 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 }
10000 }
10001
10002 /**
10003 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10004 *
10005 * @return the drawing start time in milliseconds
10006 */
10007 public long getDrawingTime() {
10008 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10009 }
10010
10011 /**
10012 * <p>Enables or disables the duplication of the parent's state into this view. When
10013 * duplication is enabled, this view gets its drawable state from its parent rather
10014 * than from its own internal properties.</p>
10015 *
10016 * <p>Note: in the current implementation, setting this property to true after the
10017 * view was added to a ViewGroup might have no effect at all. This property should
10018 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10019 *
10020 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10021 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010022 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010023 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10024 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010025 *
10026 * @param enabled True to enable duplication of the parent's drawable state, false
10027 * to disable it.
10028 *
10029 * @see #getDrawableState()
10030 * @see #isDuplicateParentStateEnabled()
10031 */
10032 public void setDuplicateParentStateEnabled(boolean enabled) {
10033 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10034 }
10035
10036 /**
10037 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10038 *
10039 * @return True if this view's drawable state is duplicated from the parent,
10040 * false otherwise
10041 *
10042 * @see #getDrawableState()
10043 * @see #setDuplicateParentStateEnabled(boolean)
10044 */
10045 public boolean isDuplicateParentStateEnabled() {
10046 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10047 }
10048
10049 /**
Romain Guy171c5922011-01-06 10:04:23 -080010050 * <p>Specifies the type of layer backing this view. The layer can be
10051 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10052 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010053 *
Romain Guy171c5922011-01-06 10:04:23 -080010054 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10055 * instance that controls how the layer is composed on screen. The following
10056 * properties of the paint are taken into account when composing the layer:</p>
10057 * <ul>
10058 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10059 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10060 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10061 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010062 *
Romain Guy171c5922011-01-06 10:04:23 -080010063 * <p>If this view has an alpha value set to < 1.0 by calling
10064 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10065 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10066 * equivalent to setting a hardware layer on this view and providing a paint with
10067 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010068 *
Romain Guy171c5922011-01-06 10:04:23 -080010069 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10070 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10071 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010072 *
Romain Guy171c5922011-01-06 10:04:23 -080010073 * @param layerType The ype of layer to use with this view, must be one of
10074 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10075 * {@link #LAYER_TYPE_HARDWARE}
10076 * @param paint The paint used to compose the layer. This argument is optional
10077 * and can be null. It is ignored when the layer type is
10078 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010079 *
10080 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010081 * @see #LAYER_TYPE_NONE
10082 * @see #LAYER_TYPE_SOFTWARE
10083 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010084 * @see #setAlpha(float)
10085 *
Romain Guy171c5922011-01-06 10:04:23 -080010086 * @attr ref android.R.styleable#View_layerType
10087 */
10088 public void setLayerType(int layerType, Paint paint) {
10089 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010090 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010091 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10092 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010093
Romain Guyd6cd5722011-01-17 14:42:41 -080010094 if (layerType == mLayerType) {
10095 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10096 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010097 invalidateParentCaches();
10098 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010099 }
10100 return;
10101 }
Romain Guy171c5922011-01-06 10:04:23 -080010102
10103 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010104 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010105 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010106 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010107 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010108 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010109 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010110 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010111 default:
10112 break;
Romain Guy171c5922011-01-06 10:04:23 -080010113 }
10114
10115 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010116 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10117 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10118 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010119
Romain Guy0fd89bf2011-01-26 15:41:30 -080010120 invalidateParentCaches();
10121 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010122 }
10123
10124 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010125 * Indicates whether this view has a static layer. A view with layer type
10126 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10127 * dynamic.
10128 */
10129 boolean hasStaticLayer() {
10130 return mLayerType == LAYER_TYPE_NONE;
10131 }
10132
10133 /**
Romain Guy171c5922011-01-06 10:04:23 -080010134 * Indicates what type of layer is currently associated with this view. By default
10135 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10136 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10137 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010138 *
Romain Guy171c5922011-01-06 10:04:23 -080010139 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10140 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010141 *
10142 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010143 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010144 * @see #LAYER_TYPE_NONE
10145 * @see #LAYER_TYPE_SOFTWARE
10146 * @see #LAYER_TYPE_HARDWARE
10147 */
10148 public int getLayerType() {
10149 return mLayerType;
10150 }
Joe Malin32736f02011-01-19 16:14:20 -080010151
Romain Guy6c319ca2011-01-11 14:29:25 -080010152 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010153 * Forces this view's layer to be created and this view to be rendered
10154 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10155 * invoking this method will have no effect.
10156 *
10157 * This method can for instance be used to render a view into its layer before
10158 * starting an animation. If this view is complex, rendering into the layer
10159 * before starting the animation will avoid skipping frames.
10160 *
10161 * @throws IllegalStateException If this view is not attached to a window
10162 *
10163 * @see #setLayerType(int, android.graphics.Paint)
10164 */
10165 public void buildLayer() {
10166 if (mLayerType == LAYER_TYPE_NONE) return;
10167
10168 if (mAttachInfo == null) {
10169 throw new IllegalStateException("This view must be attached to a window first");
10170 }
10171
10172 switch (mLayerType) {
10173 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010174 if (mAttachInfo.mHardwareRenderer != null &&
10175 mAttachInfo.mHardwareRenderer.isEnabled() &&
10176 mAttachInfo.mHardwareRenderer.validate()) {
10177 getHardwareLayer();
10178 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010179 break;
10180 case LAYER_TYPE_SOFTWARE:
10181 buildDrawingCache(true);
10182 break;
10183 }
10184 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010185
10186 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10187 void flushLayer() {
10188 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10189 mHardwareLayer.flush();
10190 }
10191 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010192
10193 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010194 * <p>Returns a hardware layer that can be used to draw this view again
10195 * without executing its draw method.</p>
10196 *
10197 * @return A HardwareLayer ready to render, or null if an error occurred.
10198 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010199 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010200 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10201 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010202 return null;
10203 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010204
10205 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010206
10207 final int width = mRight - mLeft;
10208 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010209
Romain Guy6c319ca2011-01-11 14:29:25 -080010210 if (width == 0 || height == 0) {
10211 return null;
10212 }
10213
10214 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10215 if (mHardwareLayer == null) {
10216 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10217 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010218 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010219 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10220 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010221 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010222 }
10223
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010224 // The layer is not valid if the underlying GPU resources cannot be allocated
10225 if (!mHardwareLayer.isValid()) {
10226 return null;
10227 }
10228
Romain Guy59a12ca2011-06-09 17:48:21 -070010229 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010230 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010231
10232 // Make sure all the GPU resources have been properly allocated
10233 if (canvas == null) {
10234 mHardwareLayer.end(currentCanvas);
10235 return null;
10236 }
10237
Romain Guy5e7f7662011-01-24 22:35:56 -080010238 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010239 try {
10240 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010241 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010242 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010243
Chet Haase88172fe2011-03-07 17:36:33 -080010244 final int restoreCount = canvas.save();
10245
Romain Guy6c319ca2011-01-11 14:29:25 -080010246 computeScroll();
10247 canvas.translate(-mScrollX, -mScrollY);
10248
Romain Guy6c319ca2011-01-11 14:29:25 -080010249 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010250
Romain Guy6c319ca2011-01-11 14:29:25 -080010251 // Fast path for layouts with no backgrounds
10252 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10253 mPrivateFlags &= ~DIRTY_MASK;
10254 dispatchDraw(canvas);
10255 } else {
10256 draw(canvas);
10257 }
Joe Malin32736f02011-01-19 16:14:20 -080010258
Chet Haase88172fe2011-03-07 17:36:33 -080010259 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010260 } finally {
10261 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010262 mHardwareLayer.end(currentCanvas);
10263 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010264 }
10265 }
10266
10267 return mHardwareLayer;
10268 }
Romain Guy171c5922011-01-06 10:04:23 -080010269
Romain Guy589b0bb2011-10-10 13:57:47 -070010270 /**
10271 * Destroys this View's hardware layer if possible.
10272 *
10273 * @return True if the layer was destroyed, false otherwise.
10274 *
10275 * @see #setLayerType(int, android.graphics.Paint)
10276 * @see #LAYER_TYPE_HARDWARE
10277 */
Romain Guy65b345f2011-07-27 18:51:50 -070010278 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010279 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010280 AttachInfo info = mAttachInfo;
10281 if (info != null && info.mHardwareRenderer != null &&
10282 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10283 mHardwareLayer.destroy();
10284 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010285
Romain Guy9c4b79a2011-11-10 19:23:58 -080010286 invalidate(true);
10287 invalidateParentCaches();
10288 }
Romain Guy65b345f2011-07-27 18:51:50 -070010289 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010290 }
Romain Guy65b345f2011-07-27 18:51:50 -070010291 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010292 }
10293
Romain Guy171c5922011-01-06 10:04:23 -080010294 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010295 * Destroys all hardware rendering resources. This method is invoked
10296 * when the system needs to reclaim resources. Upon execution of this
10297 * method, you should free any OpenGL resources created by the view.
10298 *
10299 * Note: you <strong>must</strong> call
10300 * <code>super.destroyHardwareResources()</code> when overriding
10301 * this method.
10302 *
10303 * @hide
10304 */
10305 protected void destroyHardwareResources() {
10306 destroyLayer();
10307 }
10308
10309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10311 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10312 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10313 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10314 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10315 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010316 *
Romain Guy171c5922011-01-06 10:04:23 -080010317 * <p>Enabling the drawing cache is similar to
10318 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010319 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10320 * drawing cache has no effect on rendering because the system uses a different mechanism
10321 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10322 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10323 * for information on how to enable software and hardware layers.</p>
10324 *
10325 * <p>This API can be used to manually generate
10326 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10327 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 *
10329 * @param enabled true to enable the drawing cache, false otherwise
10330 *
10331 * @see #isDrawingCacheEnabled()
10332 * @see #getDrawingCache()
10333 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010334 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 */
10336 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010337 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010338 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10339 }
10340
10341 /**
10342 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10343 *
10344 * @return true if the drawing cache is enabled
10345 *
10346 * @see #setDrawingCacheEnabled(boolean)
10347 * @see #getDrawingCache()
10348 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010349 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 public boolean isDrawingCacheEnabled() {
10351 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10352 }
10353
10354 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010355 * Debugging utility which recursively outputs the dirty state of a view and its
10356 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010357 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010358 * @hide
10359 */
Romain Guy676b1732011-02-14 14:45:33 -080010360 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010361 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10362 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10363 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10364 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10365 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10366 if (clear) {
10367 mPrivateFlags &= clearMask;
10368 }
10369 if (this instanceof ViewGroup) {
10370 ViewGroup parent = (ViewGroup) this;
10371 final int count = parent.getChildCount();
10372 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010373 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010374 child.outputDirtyFlags(indent + " ", clear, clearMask);
10375 }
10376 }
10377 }
10378
10379 /**
10380 * This method is used by ViewGroup to cause its children to restore or recreate their
10381 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10382 * to recreate its own display list, which would happen if it went through the normal
10383 * draw/dispatchDraw mechanisms.
10384 *
10385 * @hide
10386 */
10387 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010388
10389 /**
10390 * A view that is not attached or hardware accelerated cannot create a display list.
10391 * This method checks these conditions and returns the appropriate result.
10392 *
10393 * @return true if view has the ability to create a display list, false otherwise.
10394 *
10395 * @hide
10396 */
10397 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010398 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010399 }
Joe Malin32736f02011-01-19 16:14:20 -080010400
Chet Haasedaf98e92011-01-10 14:10:36 -080010401 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010402 * @return The HardwareRenderer associated with that view or null if hardware rendering
10403 * is not supported or this this has not been attached to a window.
10404 *
10405 * @hide
10406 */
10407 public HardwareRenderer getHardwareRenderer() {
10408 if (mAttachInfo != null) {
10409 return mAttachInfo.mHardwareRenderer;
10410 }
10411 return null;
10412 }
10413
10414 /**
Romain Guyb051e892010-09-28 19:09:36 -070010415 * <p>Returns a display list that can be used to draw this view again
10416 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010417 *
Romain Guyb051e892010-09-28 19:09:36 -070010418 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010419 *
10420 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010421 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010422 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010423 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010424 return null;
10425 }
10426
Chet Haasedaf98e92011-01-10 14:10:36 -080010427 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10428 mDisplayList == null || !mDisplayList.isValid() ||
10429 mRecreateDisplayList)) {
10430 // Don't need to recreate the display list, just need to tell our
10431 // children to restore/recreate theirs
10432 if (mDisplayList != null && mDisplayList.isValid() &&
10433 !mRecreateDisplayList) {
10434 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10435 mPrivateFlags &= ~DIRTY_MASK;
10436 dispatchGetDisplayList();
10437
10438 return mDisplayList;
10439 }
10440
10441 // If we got here, we're recreating it. Mark it as such to ensure that
10442 // we copy in child display lists into ours in drawChild()
10443 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010444 if (mDisplayList == null) {
Romain Guy13631f32012-01-30 17:41:55 -080010445 final String name = getClass().getSimpleName();
10446 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
Chet Haasedaf98e92011-01-10 14:10:36 -080010447 // If we're creating a new display list, make sure our parent gets invalidated
10448 // since they will need to recreate their display list to account for this
10449 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010450 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010451 }
Romain Guyb051e892010-09-28 19:09:36 -070010452
10453 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010454 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010455 try {
10456 int width = mRight - mLeft;
10457 int height = mBottom - mTop;
10458
10459 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010460 // The dirty rect should always be null for a display list
10461 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010462
Chet Haasedaf98e92011-01-10 14:10:36 -080010463 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010464
10465 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010466 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010467 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010468 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010469
Romain Guyb051e892010-09-28 19:09:36 -070010470 // Fast path for layouts with no backgrounds
10471 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010472 dispatchDraw(canvas);
10473 } else {
10474 draw(canvas);
10475 }
Romain Guyb051e892010-09-28 19:09:36 -070010476 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010477 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010478 canvas.onPostDraw();
10479
10480 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010481 }
Chet Haase77785f92011-01-25 23:22:09 -080010482 } else {
10483 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10484 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010485 }
10486
10487 return mDisplayList;
10488 }
10489
10490 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010491 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010492 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010493 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010494 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010495 * @see #getDrawingCache(boolean)
10496 */
10497 public Bitmap getDrawingCache() {
10498 return getDrawingCache(false);
10499 }
10500
10501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10503 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10504 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10505 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10506 * request the drawing cache by calling this method and draw it on screen if the
10507 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010508 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010509 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10510 * this method will create a bitmap of the same size as this view. Because this bitmap
10511 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10512 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10513 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10514 * size than the view. This implies that your application must be able to handle this
10515 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010516 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010517 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10518 * the current density of the screen when the application is in compatibility
10519 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010521 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010522 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523 * @see #setDrawingCacheEnabled(boolean)
10524 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010525 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 * @see #destroyDrawingCache()
10527 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010528 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10530 return null;
10531 }
10532 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010533 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 }
Romain Guy02890fd2010-08-06 17:58:44 -070010535 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 }
10537
10538 /**
10539 * <p>Frees the resources used by the drawing cache. If you call
10540 * {@link #buildDrawingCache()} manually without calling
10541 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10542 * should cleanup the cache with this method afterwards.</p>
10543 *
10544 * @see #setDrawingCacheEnabled(boolean)
10545 * @see #buildDrawingCache()
10546 * @see #getDrawingCache()
10547 */
10548 public void destroyDrawingCache() {
10549 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010550 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 mDrawingCache = null;
10552 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010553 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010554 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010555 mUnscaledDrawingCache = null;
10556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 }
10558
10559 /**
10560 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010561 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 * view will always be drawn on top of a solid color.
10563 *
10564 * @param color The background color to use for the drawing cache's bitmap
10565 *
10566 * @see #setDrawingCacheEnabled(boolean)
10567 * @see #buildDrawingCache()
10568 * @see #getDrawingCache()
10569 */
10570 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010571 if (color != mDrawingCacheBackgroundColor) {
10572 mDrawingCacheBackgroundColor = color;
10573 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 }
10576
10577 /**
10578 * @see #setDrawingCacheBackgroundColor(int)
10579 *
10580 * @return The background color to used for the drawing cache's bitmap
10581 */
10582 public int getDrawingCacheBackgroundColor() {
10583 return mDrawingCacheBackgroundColor;
10584 }
10585
10586 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010587 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010588 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010589 * @see #buildDrawingCache(boolean)
10590 */
10591 public void buildDrawingCache() {
10592 buildDrawingCache(false);
10593 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010594
Romain Guyfbd8f692009-06-26 14:51:58 -070010595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010596 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10597 *
10598 * <p>If you call {@link #buildDrawingCache()} manually without calling
10599 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10600 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010601 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010602 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10603 * this method will create a bitmap of the same size as this view. Because this bitmap
10604 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10605 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10606 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10607 * size than the view. This implies that your application must be able to handle this
10608 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010609 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010610 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10611 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010612 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010613 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010614 *
10615 * @see #getDrawingCache()
10616 * @see #destroyDrawingCache()
10617 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010618 public void buildDrawingCache(boolean autoScale) {
10619 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010620 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010621 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622
10623 if (ViewDebug.TRACE_HIERARCHY) {
10624 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626
Romain Guy8506ab42009-06-11 17:35:47 -070010627 int width = mRight - mLeft;
10628 int height = mBottom - mTop;
10629
10630 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010631 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010632
Romain Guye1123222009-06-29 14:24:56 -070010633 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010634 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10635 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637
10638 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010639 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010640 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010641
10642 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010643 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010644 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010645 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10646 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010647 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 return;
10649 }
10650
10651 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010652 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653
10654 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 Bitmap.Config quality;
10656 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010657 // Never pick ARGB_4444 because it looks awful
10658 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10660 case DRAWING_CACHE_QUALITY_AUTO:
10661 quality = Bitmap.Config.ARGB_8888;
10662 break;
10663 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010664 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 break;
10666 case DRAWING_CACHE_QUALITY_HIGH:
10667 quality = Bitmap.Config.ARGB_8888;
10668 break;
10669 default:
10670 quality = Bitmap.Config.ARGB_8888;
10671 break;
10672 }
10673 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010674 // Optimization for translucent windows
10675 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010676 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010677 }
10678
10679 // Try to cleanup memory
10680 if (bitmap != null) bitmap.recycle();
10681
10682 try {
10683 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010684 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010685 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010686 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010687 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010688 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010689 }
Adam Powell26153a32010-11-08 15:22:27 -080010690 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 } catch (OutOfMemoryError e) {
10692 // If there is not enough memory to create the bitmap cache, just
10693 // ignore the issue as bitmap caches are not required to draw the
10694 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010695 if (autoScale) {
10696 mDrawingCache = null;
10697 } else {
10698 mUnscaledDrawingCache = null;
10699 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010700 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010701 return;
10702 }
10703
10704 clear = drawingCacheBackgroundColor != 0;
10705 }
10706
10707 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 if (attachInfo != null) {
10709 canvas = attachInfo.mCanvas;
10710 if (canvas == null) {
10711 canvas = new Canvas();
10712 }
10713 canvas.setBitmap(bitmap);
10714 // Temporarily clobber the cached Canvas in case one of our children
10715 // is also using a drawing cache. Without this, the children would
10716 // steal the canvas by attaching their own bitmap to it and bad, bad
10717 // thing would happen (invisible views, corrupted drawings, etc.)
10718 attachInfo.mCanvas = null;
10719 } else {
10720 // This case should hopefully never or seldom happen
10721 canvas = new Canvas(bitmap);
10722 }
10723
10724 if (clear) {
10725 bitmap.eraseColor(drawingCacheBackgroundColor);
10726 }
10727
10728 computeScroll();
10729 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010730
Romain Guye1123222009-06-29 14:24:56 -070010731 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010732 final float scale = attachInfo.mApplicationScale;
10733 canvas.scale(scale, scale);
10734 }
Joe Malin32736f02011-01-19 16:14:20 -080010735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010736 canvas.translate(-mScrollX, -mScrollY);
10737
Romain Guy5bcdff42009-05-14 21:27:18 -070010738 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010739 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10740 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010741 mPrivateFlags |= DRAWING_CACHE_VALID;
10742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010743
10744 // Fast path for layouts with no backgrounds
10745 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10746 if (ViewDebug.TRACE_HIERARCHY) {
10747 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10748 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010749 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 dispatchDraw(canvas);
10751 } else {
10752 draw(canvas);
10753 }
10754
10755 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010756 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757
10758 if (attachInfo != null) {
10759 // Restore the cached Canvas for our siblings
10760 attachInfo.mCanvas = canvas;
10761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 }
10763 }
10764
10765 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010766 * Create a snapshot of the view into a bitmap. We should probably make
10767 * some form of this public, but should think about the API.
10768 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010769 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010770 int width = mRight - mLeft;
10771 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010772
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010773 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010774 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010775 width = (int) ((width * scale) + 0.5f);
10776 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010777
Romain Guy8c11e312009-09-14 15:15:30 -070010778 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010779 if (bitmap == null) {
10780 throw new OutOfMemoryError();
10781 }
10782
Romain Guyc529d8d2011-09-06 15:01:39 -070010783 Resources resources = getResources();
10784 if (resources != null) {
10785 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10786 }
Joe Malin32736f02011-01-19 16:14:20 -080010787
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010788 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010789 if (attachInfo != null) {
10790 canvas = attachInfo.mCanvas;
10791 if (canvas == null) {
10792 canvas = new Canvas();
10793 }
10794 canvas.setBitmap(bitmap);
10795 // Temporarily clobber the cached Canvas in case one of our children
10796 // is also using a drawing cache. Without this, the children would
10797 // steal the canvas by attaching their own bitmap to it and bad, bad
10798 // things would happen (invisible views, corrupted drawings, etc.)
10799 attachInfo.mCanvas = null;
10800 } else {
10801 // This case should hopefully never or seldom happen
10802 canvas = new Canvas(bitmap);
10803 }
10804
Romain Guy5bcdff42009-05-14 21:27:18 -070010805 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010806 bitmap.eraseColor(backgroundColor);
10807 }
10808
10809 computeScroll();
10810 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010811 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010812 canvas.translate(-mScrollX, -mScrollY);
10813
Romain Guy5bcdff42009-05-14 21:27:18 -070010814 // Temporarily remove the dirty mask
10815 int flags = mPrivateFlags;
10816 mPrivateFlags &= ~DIRTY_MASK;
10817
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010818 // Fast path for layouts with no backgrounds
10819 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10820 dispatchDraw(canvas);
10821 } else {
10822 draw(canvas);
10823 }
10824
Romain Guy5bcdff42009-05-14 21:27:18 -070010825 mPrivateFlags = flags;
10826
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010827 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010828 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010829
10830 if (attachInfo != null) {
10831 // Restore the cached Canvas for our siblings
10832 attachInfo.mCanvas = canvas;
10833 }
Romain Guy8506ab42009-06-11 17:35:47 -070010834
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010835 return bitmap;
10836 }
10837
10838 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 * Indicates whether this View is currently in edit mode. A View is usually
10840 * in edit mode when displayed within a developer tool. For instance, if
10841 * this View is being drawn by a visual user interface builder, this method
10842 * should return true.
10843 *
10844 * Subclasses should check the return value of this method to provide
10845 * different behaviors if their normal behavior might interfere with the
10846 * host environment. For instance: the class spawns a thread in its
10847 * constructor, the drawing code relies on device-specific features, etc.
10848 *
10849 * This method is usually checked in the drawing code of custom widgets.
10850 *
10851 * @return True if this View is in edit mode, false otherwise.
10852 */
10853 public boolean isInEditMode() {
10854 return false;
10855 }
10856
10857 /**
10858 * If the View draws content inside its padding and enables fading edges,
10859 * it needs to support padding offsets. Padding offsets are added to the
10860 * fading edges to extend the length of the fade so that it covers pixels
10861 * drawn inside the padding.
10862 *
10863 * Subclasses of this class should override this method if they need
10864 * to draw content inside the padding.
10865 *
10866 * @return True if padding offset must be applied, false otherwise.
10867 *
10868 * @see #getLeftPaddingOffset()
10869 * @see #getRightPaddingOffset()
10870 * @see #getTopPaddingOffset()
10871 * @see #getBottomPaddingOffset()
10872 *
10873 * @since CURRENT
10874 */
10875 protected boolean isPaddingOffsetRequired() {
10876 return false;
10877 }
10878
10879 /**
10880 * Amount by which to extend the left fading region. Called only when
10881 * {@link #isPaddingOffsetRequired()} returns true.
10882 *
10883 * @return The left padding offset in pixels.
10884 *
10885 * @see #isPaddingOffsetRequired()
10886 *
10887 * @since CURRENT
10888 */
10889 protected int getLeftPaddingOffset() {
10890 return 0;
10891 }
10892
10893 /**
10894 * Amount by which to extend the right fading region. Called only when
10895 * {@link #isPaddingOffsetRequired()} returns true.
10896 *
10897 * @return The right padding offset in pixels.
10898 *
10899 * @see #isPaddingOffsetRequired()
10900 *
10901 * @since CURRENT
10902 */
10903 protected int getRightPaddingOffset() {
10904 return 0;
10905 }
10906
10907 /**
10908 * Amount by which to extend the top fading region. Called only when
10909 * {@link #isPaddingOffsetRequired()} returns true.
10910 *
10911 * @return The top padding offset in pixels.
10912 *
10913 * @see #isPaddingOffsetRequired()
10914 *
10915 * @since CURRENT
10916 */
10917 protected int getTopPaddingOffset() {
10918 return 0;
10919 }
10920
10921 /**
10922 * Amount by which to extend the bottom fading region. Called only when
10923 * {@link #isPaddingOffsetRequired()} returns true.
10924 *
10925 * @return The bottom padding offset in pixels.
10926 *
10927 * @see #isPaddingOffsetRequired()
10928 *
10929 * @since CURRENT
10930 */
10931 protected int getBottomPaddingOffset() {
10932 return 0;
10933 }
10934
10935 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010936 * @hide
10937 * @param offsetRequired
10938 */
10939 protected int getFadeTop(boolean offsetRequired) {
10940 int top = mPaddingTop;
10941 if (offsetRequired) top += getTopPaddingOffset();
10942 return top;
10943 }
10944
10945 /**
10946 * @hide
10947 * @param offsetRequired
10948 */
10949 protected int getFadeHeight(boolean offsetRequired) {
10950 int padding = mPaddingTop;
10951 if (offsetRequired) padding += getTopPaddingOffset();
10952 return mBottom - mTop - mPaddingBottom - padding;
10953 }
10954
10955 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010956 * <p>Indicates whether this view is attached to an hardware accelerated
10957 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010958 *
Romain Guy2bffd262010-09-12 17:40:02 -070010959 * <p>Even if this method returns true, it does not mean that every call
10960 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10961 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10962 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10963 * window is hardware accelerated,
10964 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10965 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010966 *
Romain Guy2bffd262010-09-12 17:40:02 -070010967 * @return True if the view is attached to a window and the window is
10968 * hardware accelerated; false in any other case.
10969 */
10970 public boolean isHardwareAccelerated() {
10971 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10972 }
Joe Malin32736f02011-01-19 16:14:20 -080010973
Romain Guy2bffd262010-09-12 17:40:02 -070010974 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080010975 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
10976 * case of an active Animation being run on the view.
10977 */
10978 private boolean drawAnimation(ViewGroup parent, long drawingTime,
10979 Animation a, boolean scalingRequired) {
10980 Transformation invalidationTransform;
10981 final int flags = parent.mGroupFlags;
10982 final boolean initialized = a.isInitialized();
10983 if (!initialized) {
10984 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
10985 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
10986 onAnimationStart();
10987 }
10988
10989 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
10990 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
10991 if (parent.mInvalidationTransformation == null) {
10992 parent.mInvalidationTransformation = new Transformation();
10993 }
10994 invalidationTransform = parent.mInvalidationTransformation;
10995 a.getTransformation(drawingTime, invalidationTransform, 1f);
10996 } else {
10997 invalidationTransform = parent.mChildTransformation;
10998 }
10999 if (more) {
11000 if (!a.willChangeBounds()) {
11001 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11002 parent.FLAG_OPTIMIZE_INVALIDATE) {
11003 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11004 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11005 // The child need to draw an animation, potentially offscreen, so
11006 // make sure we do not cancel invalidate requests
11007 parent.mPrivateFlags |= DRAW_ANIMATION;
11008 parent.invalidate(mLeft, mTop, mRight, mBottom);
11009 }
11010 } else {
11011 if (parent.mInvalidateRegion == null) {
11012 parent.mInvalidateRegion = new RectF();
11013 }
11014 final RectF region = parent.mInvalidateRegion;
11015 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11016 invalidationTransform);
11017
11018 // The child need to draw an animation, potentially offscreen, so
11019 // make sure we do not cancel invalidate requests
11020 parent.mPrivateFlags |= DRAW_ANIMATION;
11021
11022 final int left = mLeft + (int) region.left;
11023 final int top = mTop + (int) region.top;
11024 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11025 top + (int) (region.height() + .5f));
11026 }
11027 }
11028 return more;
11029 }
11030
11031 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011032 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11033 * This draw() method is an implementation detail and is not intended to be overridden or
11034 * to be called from anywhere else other than ViewGroup.drawChild().
11035 */
11036 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
11037 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011038 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011039 final int flags = parent.mGroupFlags;
11040
11041 if ((flags & parent.FLAG_CLEAR_TRANSFORMATION) == parent.FLAG_CLEAR_TRANSFORMATION) {
11042 parent.mChildTransformation.clear();
11043 parent.mGroupFlags &= ~parent.FLAG_CLEAR_TRANSFORMATION;
11044 }
11045
11046 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011047 boolean concatMatrix = false;
11048
11049 boolean scalingRequired = false;
11050 boolean caching;
11051 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11052
11053 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
11054 if ((flags & parent.FLAG_CHILDREN_DRAWN_WITH_CACHE) == parent.FLAG_CHILDREN_DRAWN_WITH_CACHE ||
11055 (flags & parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) == parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) {
11056 caching = true;
11057 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11058 } else {
11059 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11060 }
11061
Chet Haasebcca79a2012-02-14 08:45:14 -080011062 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011063 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011064 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011065 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011066 transformToApply = parent.mChildTransformation;
Chet Haase64a48c12012-02-13 16:33:29 -080011067 } else if ((flags & parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
11068 parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011069 final boolean hasTransform =
11070 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011071 if (hasTransform) {
11072 final int transformType = parent.mChildTransformation.getTransformationType();
11073 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11074 parent.mChildTransformation : null;
11075 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11076 }
11077 }
11078
11079 concatMatrix |= !childHasIdentityMatrix;
11080
11081 // Sets the flag as early as possible to allow draw() implementations
11082 // to call invalidate() successfully when doing animations
11083 mPrivateFlags |= DRAWN;
11084
Chet Haasebcca79a2012-02-14 08:45:14 -080011085 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011086 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11087 return more;
11088 }
11089
11090 if (hardwareAccelerated) {
11091 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11092 // retain the flag's value temporarily in the mRecreateDisplayList flag
11093 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11094 mPrivateFlags &= ~INVALIDATED;
11095 }
11096
11097 computeScroll();
11098
11099 final int sx = mScrollX;
11100 final int sy = mScrollY;
11101
11102 DisplayList displayList = null;
11103 Bitmap cache = null;
11104 boolean hasDisplayList = false;
11105 if (caching) {
11106 if (!hardwareAccelerated) {
11107 if (layerType != LAYER_TYPE_NONE) {
11108 layerType = LAYER_TYPE_SOFTWARE;
11109 buildDrawingCache(true);
11110 }
11111 cache = getDrawingCache(true);
11112 } else {
11113 switch (layerType) {
11114 case LAYER_TYPE_SOFTWARE:
11115 buildDrawingCache(true);
11116 cache = getDrawingCache(true);
11117 break;
11118 case LAYER_TYPE_NONE:
11119 // Delay getting the display list until animation-driven alpha values are
11120 // set up and possibly passed on to the view
11121 hasDisplayList = canHaveDisplayList();
11122 break;
11123 }
11124 }
11125 }
11126
11127 final boolean hasNoCache = cache == null || hasDisplayList;
11128 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11129 layerType != LAYER_TYPE_HARDWARE;
11130
11131 final int restoreTo = canvas.save();
11132 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011133 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011134 } else {
Chet Haasebcca79a2012-02-14 08:45:14 -080011135 canvas.translate(mLeft, mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011136 if (scalingRequired) {
11137 // mAttachInfo cannot be null, otherwise scalingRequired == false
11138 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11139 canvas.scale(scale, scale);
11140 }
11141 }
11142
11143 float alpha = getAlpha();
11144 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11145 if (transformToApply != null || !childHasIdentityMatrix) {
11146 int transX = 0;
11147 int transY = 0;
11148
11149 if (offsetForScroll) {
11150 transX = -sx;
11151 transY = -sy;
11152 }
11153
11154 if (transformToApply != null) {
11155 if (concatMatrix) {
11156 // Undo the scroll translation, apply the transformation matrix,
11157 // then redo the scroll translate to get the correct result.
11158 canvas.translate(-transX, -transY);
11159 canvas.concat(transformToApply.getMatrix());
11160 canvas.translate(transX, transY);
11161 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11162 }
11163
11164 float transformAlpha = transformToApply.getAlpha();
11165 if (transformAlpha < 1.0f) {
11166 alpha *= transformToApply.getAlpha();
11167 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11168 }
11169 }
11170
11171 if (!childHasIdentityMatrix) {
11172 canvas.translate(-transX, -transY);
11173 canvas.concat(getMatrix());
11174 canvas.translate(transX, transY);
11175 }
11176 }
11177
11178 if (alpha < 1.0f) {
11179 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11180 if (hasNoCache) {
11181 final int multipliedAlpha = (int) (255 * alpha);
11182 if (!onSetAlpha(multipliedAlpha)) {
11183 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11184 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN ||
11185 layerType != LAYER_TYPE_NONE) {
11186 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11187 }
11188 if (layerType == LAYER_TYPE_NONE) {
11189 final int scrollX = hasDisplayList ? 0 : sx;
11190 final int scrollY = hasDisplayList ? 0 : sy;
Chet Haasebcca79a2012-02-14 08:45:14 -080011191 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11192 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011193 }
11194 } else {
11195 // Alpha is handled by the child directly, clobber the layer's alpha
11196 mPrivateFlags |= ALPHA_SET;
11197 }
11198 }
11199 }
11200 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11201 onSetAlpha(255);
11202 mPrivateFlags &= ~ALPHA_SET;
11203 }
11204
11205 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN) {
11206 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011207 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011208 } else {
11209 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011210 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011211 } else {
11212 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11213 }
11214 }
11215 }
11216
11217 if (hasDisplayList) {
11218 displayList = getDisplayList();
11219 if (!displayList.isValid()) {
11220 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11221 // to getDisplayList(), the display list will be marked invalid and we should not
11222 // try to use it again.
11223 displayList = null;
11224 hasDisplayList = false;
11225 }
11226 }
11227
11228 if (hasNoCache) {
11229 boolean layerRendered = false;
11230 if (layerType == LAYER_TYPE_HARDWARE) {
11231 final HardwareLayer layer = getHardwareLayer();
11232 if (layer != null && layer.isValid()) {
11233 mLayerPaint.setAlpha((int) (alpha * 255));
11234 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11235 layerRendered = true;
11236 } else {
11237 final int scrollX = hasDisplayList ? 0 : sx;
11238 final int scrollY = hasDisplayList ? 0 : sy;
11239 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011240 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011241 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11242 }
11243 }
11244
11245 if (!layerRendered) {
11246 if (!hasDisplayList) {
11247 // Fast path for layouts with no backgrounds
11248 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11249 if (ViewDebug.TRACE_HIERARCHY) {
11250 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11251 }
11252 mPrivateFlags &= ~DIRTY_MASK;
11253 dispatchDraw(canvas);
11254 } else {
11255 draw(canvas);
11256 }
11257 } else {
11258 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011259 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011260 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011261 }
11262 }
11263 } else if (cache != null) {
11264 mPrivateFlags &= ~DIRTY_MASK;
11265 Paint cachePaint;
11266
11267 if (layerType == LAYER_TYPE_NONE) {
11268 cachePaint = parent.mCachePaint;
11269 if (cachePaint == null) {
11270 cachePaint = new Paint();
11271 cachePaint.setDither(false);
11272 parent.mCachePaint = cachePaint;
11273 }
11274 if (alpha < 1.0f) {
11275 cachePaint.setAlpha((int) (alpha * 255));
11276 parent.mGroupFlags |= parent.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haasebcca79a2012-02-14 08:45:14 -080011277 } else if ((flags & parent.FLAG_ALPHA_LOWER_THAN_ONE) ==
11278 parent.FLAG_ALPHA_LOWER_THAN_ONE) {
Chet Haase64a48c12012-02-13 16:33:29 -080011279 cachePaint.setAlpha(255);
11280 parent.mGroupFlags &= ~parent.FLAG_ALPHA_LOWER_THAN_ONE;
11281 }
11282 } else {
11283 cachePaint = mLayerPaint;
11284 cachePaint.setAlpha((int) (alpha * 255));
11285 }
11286 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11287 }
11288
11289 canvas.restoreToCount(restoreTo);
11290
11291 if (a != null && !more) {
11292 if (!hardwareAccelerated && !a.getFillAfter()) {
11293 onSetAlpha(255);
11294 }
11295 parent.finishAnimatingView(this, a);
11296 }
11297
11298 if (more && hardwareAccelerated) {
11299 // invalidation is the trigger to recreate display lists, so if we're using
11300 // display lists to render, force an invalidate to allow the animation to
11301 // continue drawing another frame
11302 parent.invalidate(true);
11303 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11304 // alpha animations should cause the child to recreate its display list
11305 invalidate(true);
11306 }
11307 }
11308
11309 mRecreateDisplayList = false;
11310
11311 return more;
11312 }
11313
11314 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011315 * Manually render this view (and all of its children) to the given Canvas.
11316 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011317 * called. When implementing a view, implement
11318 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11319 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011320 *
11321 * @param canvas The Canvas to which the View is rendered.
11322 */
11323 public void draw(Canvas canvas) {
11324 if (ViewDebug.TRACE_HIERARCHY) {
11325 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11326 }
11327
Romain Guy5bcdff42009-05-14 21:27:18 -070011328 final int privateFlags = mPrivateFlags;
11329 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11330 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11331 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011333 /*
11334 * Draw traversal performs several drawing steps which must be executed
11335 * in the appropriate order:
11336 *
11337 * 1. Draw the background
11338 * 2. If necessary, save the canvas' layers to prepare for fading
11339 * 3. Draw view's content
11340 * 4. Draw children
11341 * 5. If necessary, draw the fading edges and restore layers
11342 * 6. Draw decorations (scrollbars for instance)
11343 */
11344
11345 // Step 1, draw the background, if needed
11346 int saveCount;
11347
Romain Guy24443ea2009-05-11 11:56:30 -070011348 if (!dirtyOpaque) {
11349 final Drawable background = mBGDrawable;
11350 if (background != null) {
11351 final int scrollX = mScrollX;
11352 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011353
Romain Guy24443ea2009-05-11 11:56:30 -070011354 if (mBackgroundSizeChanged) {
11355 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11356 mBackgroundSizeChanged = false;
11357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011358
Romain Guy24443ea2009-05-11 11:56:30 -070011359 if ((scrollX | scrollY) == 0) {
11360 background.draw(canvas);
11361 } else {
11362 canvas.translate(scrollX, scrollY);
11363 background.draw(canvas);
11364 canvas.translate(-scrollX, -scrollY);
11365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011366 }
11367 }
11368
11369 // skip step 2 & 5 if possible (common case)
11370 final int viewFlags = mViewFlags;
11371 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11372 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11373 if (!verticalEdges && !horizontalEdges) {
11374 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011375 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376
11377 // Step 4, draw the children
11378 dispatchDraw(canvas);
11379
11380 // Step 6, draw decorations (scrollbars)
11381 onDrawScrollBars(canvas);
11382
11383 // we're done...
11384 return;
11385 }
11386
11387 /*
11388 * Here we do the full fledged routine...
11389 * (this is an uncommon case where speed matters less,
11390 * this is why we repeat some of the tests that have been
11391 * done above)
11392 */
11393
11394 boolean drawTop = false;
11395 boolean drawBottom = false;
11396 boolean drawLeft = false;
11397 boolean drawRight = false;
11398
11399 float topFadeStrength = 0.0f;
11400 float bottomFadeStrength = 0.0f;
11401 float leftFadeStrength = 0.0f;
11402 float rightFadeStrength = 0.0f;
11403
11404 // Step 2, save the canvas' layers
11405 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011406
11407 final boolean offsetRequired = isPaddingOffsetRequired();
11408 if (offsetRequired) {
11409 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011410 }
11411
11412 int left = mScrollX + paddingLeft;
11413 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011414 int top = mScrollY + getFadeTop(offsetRequired);
11415 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011416
11417 if (offsetRequired) {
11418 right += getRightPaddingOffset();
11419 bottom += getBottomPaddingOffset();
11420 }
11421
11422 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011423 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11424 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011425
11426 // clip the fade length if top and bottom fades overlap
11427 // overlapping fades produce odd-looking artifacts
11428 if (verticalEdges && (top + length > bottom - length)) {
11429 length = (bottom - top) / 2;
11430 }
11431
11432 // also clip horizontal fades if necessary
11433 if (horizontalEdges && (left + length > right - length)) {
11434 length = (right - left) / 2;
11435 }
11436
11437 if (verticalEdges) {
11438 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011439 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011440 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011441 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011442 }
11443
11444 if (horizontalEdges) {
11445 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011446 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011447 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011448 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011449 }
11450
11451 saveCount = canvas.getSaveCount();
11452
11453 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011454 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11456
11457 if (drawTop) {
11458 canvas.saveLayer(left, top, right, top + length, null, flags);
11459 }
11460
11461 if (drawBottom) {
11462 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11463 }
11464
11465 if (drawLeft) {
11466 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11467 }
11468
11469 if (drawRight) {
11470 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11471 }
11472 } else {
11473 scrollabilityCache.setFadeColor(solidColor);
11474 }
11475
11476 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011477 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011478
11479 // Step 4, draw the children
11480 dispatchDraw(canvas);
11481
11482 // Step 5, draw the fade effect and restore layers
11483 final Paint p = scrollabilityCache.paint;
11484 final Matrix matrix = scrollabilityCache.matrix;
11485 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011486
11487 if (drawTop) {
11488 matrix.setScale(1, fadeHeight * topFadeStrength);
11489 matrix.postTranslate(left, top);
11490 fade.setLocalMatrix(matrix);
11491 canvas.drawRect(left, top, right, top + length, p);
11492 }
11493
11494 if (drawBottom) {
11495 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11496 matrix.postRotate(180);
11497 matrix.postTranslate(left, bottom);
11498 fade.setLocalMatrix(matrix);
11499 canvas.drawRect(left, bottom - length, right, bottom, p);
11500 }
11501
11502 if (drawLeft) {
11503 matrix.setScale(1, fadeHeight * leftFadeStrength);
11504 matrix.postRotate(-90);
11505 matrix.postTranslate(left, top);
11506 fade.setLocalMatrix(matrix);
11507 canvas.drawRect(left, top, left + length, bottom, p);
11508 }
11509
11510 if (drawRight) {
11511 matrix.setScale(1, fadeHeight * rightFadeStrength);
11512 matrix.postRotate(90);
11513 matrix.postTranslate(right, top);
11514 fade.setLocalMatrix(matrix);
11515 canvas.drawRect(right - length, top, right, bottom, p);
11516 }
11517
11518 canvas.restoreToCount(saveCount);
11519
11520 // Step 6, draw decorations (scrollbars)
11521 onDrawScrollBars(canvas);
11522 }
11523
11524 /**
11525 * Override this if your view is known to always be drawn on top of a solid color background,
11526 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11527 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11528 * should be set to 0xFF.
11529 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011530 * @see #setVerticalFadingEdgeEnabled(boolean)
11531 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011532 *
11533 * @return The known solid color background for this view, or 0 if the color may vary
11534 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011535 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011536 public int getSolidColor() {
11537 return 0;
11538 }
11539
11540 /**
11541 * Build a human readable string representation of the specified view flags.
11542 *
11543 * @param flags the view flags to convert to a string
11544 * @return a String representing the supplied flags
11545 */
11546 private static String printFlags(int flags) {
11547 String output = "";
11548 int numFlags = 0;
11549 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11550 output += "TAKES_FOCUS";
11551 numFlags++;
11552 }
11553
11554 switch (flags & VISIBILITY_MASK) {
11555 case INVISIBLE:
11556 if (numFlags > 0) {
11557 output += " ";
11558 }
11559 output += "INVISIBLE";
11560 // USELESS HERE numFlags++;
11561 break;
11562 case GONE:
11563 if (numFlags > 0) {
11564 output += " ";
11565 }
11566 output += "GONE";
11567 // USELESS HERE numFlags++;
11568 break;
11569 default:
11570 break;
11571 }
11572 return output;
11573 }
11574
11575 /**
11576 * Build a human readable string representation of the specified private
11577 * view flags.
11578 *
11579 * @param privateFlags the private view flags to convert to a string
11580 * @return a String representing the supplied flags
11581 */
11582 private static String printPrivateFlags(int privateFlags) {
11583 String output = "";
11584 int numFlags = 0;
11585
11586 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11587 output += "WANTS_FOCUS";
11588 numFlags++;
11589 }
11590
11591 if ((privateFlags & FOCUSED) == FOCUSED) {
11592 if (numFlags > 0) {
11593 output += " ";
11594 }
11595 output += "FOCUSED";
11596 numFlags++;
11597 }
11598
11599 if ((privateFlags & SELECTED) == SELECTED) {
11600 if (numFlags > 0) {
11601 output += " ";
11602 }
11603 output += "SELECTED";
11604 numFlags++;
11605 }
11606
11607 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11608 if (numFlags > 0) {
11609 output += " ";
11610 }
11611 output += "IS_ROOT_NAMESPACE";
11612 numFlags++;
11613 }
11614
11615 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11616 if (numFlags > 0) {
11617 output += " ";
11618 }
11619 output += "HAS_BOUNDS";
11620 numFlags++;
11621 }
11622
11623 if ((privateFlags & DRAWN) == DRAWN) {
11624 if (numFlags > 0) {
11625 output += " ";
11626 }
11627 output += "DRAWN";
11628 // USELESS HERE numFlags++;
11629 }
11630 return output;
11631 }
11632
11633 /**
11634 * <p>Indicates whether or not this view's layout will be requested during
11635 * the next hierarchy layout pass.</p>
11636 *
11637 * @return true if the layout will be forced during next layout pass
11638 */
11639 public boolean isLayoutRequested() {
11640 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11641 }
11642
11643 /**
11644 * Assign a size and position to a view and all of its
11645 * descendants
11646 *
11647 * <p>This is the second phase of the layout mechanism.
11648 * (The first is measuring). In this phase, each parent calls
11649 * layout on all of its children to position them.
11650 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011651 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011652 *
Chet Haase9c087442011-01-12 16:20:16 -080011653 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011654 * Derived classes with children should override
11655 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011656 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011657 *
11658 * @param l Left position, relative to parent
11659 * @param t Top position, relative to parent
11660 * @param r Right position, relative to parent
11661 * @param b Bottom position, relative to parent
11662 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011663 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011664 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011665 int oldL = mLeft;
11666 int oldT = mTop;
11667 int oldB = mBottom;
11668 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011669 boolean changed = setFrame(l, t, r, b);
11670 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11671 if (ViewDebug.TRACE_HIERARCHY) {
11672 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11673 }
11674
11675 onLayout(changed, l, t, r, b);
11676 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011677
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011678 ListenerInfo li = mListenerInfo;
11679 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011680 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011681 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011682 int numListeners = listenersCopy.size();
11683 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011684 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011685 }
11686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011687 }
11688 mPrivateFlags &= ~FORCE_LAYOUT;
11689 }
11690
11691 /**
11692 * Called from layout when this view should
11693 * assign a size and position to each of its children.
11694 *
11695 * Derived classes with children should override
11696 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011697 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011698 * @param changed This is a new size or position for this view
11699 * @param left Left position, relative to parent
11700 * @param top Top position, relative to parent
11701 * @param right Right position, relative to parent
11702 * @param bottom Bottom position, relative to parent
11703 */
11704 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11705 }
11706
11707 /**
11708 * Assign a size and position to this view.
11709 *
11710 * This is called from layout.
11711 *
11712 * @param left Left position, relative to parent
11713 * @param top Top position, relative to parent
11714 * @param right Right position, relative to parent
11715 * @param bottom Bottom position, relative to parent
11716 * @return true if the new size and position are different than the
11717 * previous ones
11718 * {@hide}
11719 */
11720 protected boolean setFrame(int left, int top, int right, int bottom) {
11721 boolean changed = false;
11722
11723 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011724 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011725 + right + "," + bottom + ")");
11726 }
11727
11728 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11729 changed = true;
11730
11731 // Remember our drawn bit
11732 int drawn = mPrivateFlags & DRAWN;
11733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011734 int oldWidth = mRight - mLeft;
11735 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011736 int newWidth = right - left;
11737 int newHeight = bottom - top;
11738 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11739
11740 // Invalidate our old position
11741 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011742
11743 mLeft = left;
11744 mTop = top;
11745 mRight = right;
11746 mBottom = bottom;
11747
11748 mPrivateFlags |= HAS_BOUNDS;
11749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011750
Chet Haase75755e22011-07-18 17:48:25 -070011751 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011752 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11753 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011754 if (mTransformationInfo != null) {
11755 mTransformationInfo.mMatrixDirty = true;
11756 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011758 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11759 }
11760
11761 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11762 // If we are visible, force the DRAWN bit to on so that
11763 // this invalidate will go through (at least to our parent).
11764 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011765 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011766 // the DRAWN bit.
11767 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011768 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011769 // parent display list may need to be recreated based on a change in the bounds
11770 // of any child
11771 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772 }
11773
11774 // Reset drawn bit to original value (invalidate turns it off)
11775 mPrivateFlags |= drawn;
11776
11777 mBackgroundSizeChanged = true;
11778 }
11779 return changed;
11780 }
11781
11782 /**
11783 * Finalize inflating a view from XML. This is called as the last phase
11784 * of inflation, after all child views have been added.
11785 *
11786 * <p>Even if the subclass overrides onFinishInflate, they should always be
11787 * sure to call the super method, so that we get called.
11788 */
11789 protected void onFinishInflate() {
11790 }
11791
11792 /**
11793 * Returns the resources associated with this view.
11794 *
11795 * @return Resources object.
11796 */
11797 public Resources getResources() {
11798 return mResources;
11799 }
11800
11801 /**
11802 * Invalidates the specified Drawable.
11803 *
11804 * @param drawable the drawable to invalidate
11805 */
11806 public void invalidateDrawable(Drawable drawable) {
11807 if (verifyDrawable(drawable)) {
11808 final Rect dirty = drawable.getBounds();
11809 final int scrollX = mScrollX;
11810 final int scrollY = mScrollY;
11811
11812 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11813 dirty.right + scrollX, dirty.bottom + scrollY);
11814 }
11815 }
11816
11817 /**
11818 * Schedules an action on a drawable to occur at a specified time.
11819 *
11820 * @param who the recipient of the action
11821 * @param what the action to run on the drawable
11822 * @param when the time at which the action must occur. Uses the
11823 * {@link SystemClock#uptimeMillis} timebase.
11824 */
11825 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011826 if (verifyDrawable(who) && what != null) {
11827 if (mAttachInfo != null) {
11828 mAttachInfo.mHandler.postAtTime(what, who, when);
11829 } else {
11830 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011832 }
11833 }
11834
11835 /**
11836 * Cancels a scheduled action on a drawable.
11837 *
11838 * @param who the recipient of the action
11839 * @param what the action to cancel
11840 */
11841 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011842 if (verifyDrawable(who) && what != null) {
11843 if (mAttachInfo != null) {
11844 mAttachInfo.mHandler.removeCallbacks(what, who);
11845 } else {
11846 ViewRootImpl.getRunQueue().removeCallbacks(what);
11847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011848 }
11849 }
11850
11851 /**
11852 * Unschedule any events associated with the given Drawable. This can be
11853 * used when selecting a new Drawable into a view, so that the previous
11854 * one is completely unscheduled.
11855 *
11856 * @param who The Drawable to unschedule.
11857 *
11858 * @see #drawableStateChanged
11859 */
11860 public void unscheduleDrawable(Drawable who) {
11861 if (mAttachInfo != null) {
11862 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11863 }
11864 }
11865
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011866 /**
11867 * Return the layout direction of a given Drawable.
11868 *
11869 * @param who the Drawable to query
11870 *
11871 * @hide
11872 */
11873 public int getResolvedLayoutDirection(Drawable who) {
11874 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011875 }
11876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011877 /**
11878 * If your view subclass is displaying its own Drawable objects, it should
11879 * override this function and return true for any Drawable it is
11880 * displaying. This allows animations for those drawables to be
11881 * scheduled.
11882 *
11883 * <p>Be sure to call through to the super class when overriding this
11884 * function.
11885 *
11886 * @param who The Drawable to verify. Return true if it is one you are
11887 * displaying, else return the result of calling through to the
11888 * super class.
11889 *
11890 * @return boolean If true than the Drawable is being displayed in the
11891 * view; else false and it is not allowed to animate.
11892 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011893 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11894 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011895 */
11896 protected boolean verifyDrawable(Drawable who) {
11897 return who == mBGDrawable;
11898 }
11899
11900 /**
11901 * This function is called whenever the state of the view changes in such
11902 * a way that it impacts the state of drawables being shown.
11903 *
11904 * <p>Be sure to call through to the superclass when overriding this
11905 * function.
11906 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011907 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011908 */
11909 protected void drawableStateChanged() {
11910 Drawable d = mBGDrawable;
11911 if (d != null && d.isStateful()) {
11912 d.setState(getDrawableState());
11913 }
11914 }
11915
11916 /**
11917 * Call this to force a view to update its drawable state. This will cause
11918 * drawableStateChanged to be called on this view. Views that are interested
11919 * in the new state should call getDrawableState.
11920 *
11921 * @see #drawableStateChanged
11922 * @see #getDrawableState
11923 */
11924 public void refreshDrawableState() {
11925 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11926 drawableStateChanged();
11927
11928 ViewParent parent = mParent;
11929 if (parent != null) {
11930 parent.childDrawableStateChanged(this);
11931 }
11932 }
11933
11934 /**
11935 * Return an array of resource IDs of the drawable states representing the
11936 * current state of the view.
11937 *
11938 * @return The current drawable state
11939 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011940 * @see Drawable#setState(int[])
11941 * @see #drawableStateChanged()
11942 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011943 */
11944 public final int[] getDrawableState() {
11945 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11946 return mDrawableState;
11947 } else {
11948 mDrawableState = onCreateDrawableState(0);
11949 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11950 return mDrawableState;
11951 }
11952 }
11953
11954 /**
11955 * Generate the new {@link android.graphics.drawable.Drawable} state for
11956 * this view. This is called by the view
11957 * system when the cached Drawable state is determined to be invalid. To
11958 * retrieve the current state, you should use {@link #getDrawableState}.
11959 *
11960 * @param extraSpace if non-zero, this is the number of extra entries you
11961 * would like in the returned array in which you can place your own
11962 * states.
11963 *
11964 * @return Returns an array holding the current {@link Drawable} state of
11965 * the view.
11966 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011967 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011968 */
11969 protected int[] onCreateDrawableState(int extraSpace) {
11970 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11971 mParent instanceof View) {
11972 return ((View) mParent).onCreateDrawableState(extraSpace);
11973 }
11974
11975 int[] drawableState;
11976
11977 int privateFlags = mPrivateFlags;
11978
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011979 int viewStateIndex = 0;
11980 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11981 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11982 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011983 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011984 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11985 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011986 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11987 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011988 // This is set if HW acceleration is requested, even if the current
11989 // process doesn't allow it. This is just to allow app preview
11990 // windows to better match their app.
11991 viewStateIndex |= VIEW_STATE_ACCELERATED;
11992 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011993 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011994
Christopher Tate3d4bf172011-03-28 16:16:46 -070011995 final int privateFlags2 = mPrivateFlags2;
11996 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11997 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999 drawableState = VIEW_STATE_SETS[viewStateIndex];
12000
12001 //noinspection ConstantIfStatement
12002 if (false) {
12003 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12004 Log.i("View", toString()
12005 + " pressed=" + ((privateFlags & PRESSED) != 0)
12006 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12007 + " fo=" + hasFocus()
12008 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012009 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 + ": " + Arrays.toString(drawableState));
12011 }
12012
12013 if (extraSpace == 0) {
12014 return drawableState;
12015 }
12016
12017 final int[] fullState;
12018 if (drawableState != null) {
12019 fullState = new int[drawableState.length + extraSpace];
12020 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12021 } else {
12022 fullState = new int[extraSpace];
12023 }
12024
12025 return fullState;
12026 }
12027
12028 /**
12029 * Merge your own state values in <var>additionalState</var> into the base
12030 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012031 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012032 *
12033 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012034 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012035 * own additional state values.
12036 *
12037 * @param additionalState The additional state values you would like
12038 * added to <var>baseState</var>; this array is not modified.
12039 *
12040 * @return As a convenience, the <var>baseState</var> array you originally
12041 * passed into the function is returned.
12042 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012043 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012044 */
12045 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12046 final int N = baseState.length;
12047 int i = N - 1;
12048 while (i >= 0 && baseState[i] == 0) {
12049 i--;
12050 }
12051 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12052 return baseState;
12053 }
12054
12055 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012056 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12057 * on all Drawable objects associated with this view.
12058 */
12059 public void jumpDrawablesToCurrentState() {
12060 if (mBGDrawable != null) {
12061 mBGDrawable.jumpToCurrentState();
12062 }
12063 }
12064
12065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012066 * Sets the background color for this view.
12067 * @param color the color of the background
12068 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012069 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012070 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012071 if (mBGDrawable instanceof ColorDrawable) {
12072 ((ColorDrawable) mBGDrawable).setColor(color);
12073 } else {
12074 setBackgroundDrawable(new ColorDrawable(color));
12075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012076 }
12077
12078 /**
12079 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012080 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012081 * @param resid The identifier of the resource.
12082 * @attr ref android.R.styleable#View_background
12083 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012084 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012085 public void setBackgroundResource(int resid) {
12086 if (resid != 0 && resid == mBackgroundResource) {
12087 return;
12088 }
12089
12090 Drawable d= null;
12091 if (resid != 0) {
12092 d = mResources.getDrawable(resid);
12093 }
12094 setBackgroundDrawable(d);
12095
12096 mBackgroundResource = resid;
12097 }
12098
12099 /**
12100 * Set the background to a given Drawable, or remove the background. If the
12101 * background has padding, this View's padding is set to the background's
12102 * padding. However, when a background is removed, this View's padding isn't
12103 * touched. If setting the padding is desired, please use
12104 * {@link #setPadding(int, int, int, int)}.
12105 *
12106 * @param d The Drawable to use as the background, or null to remove the
12107 * background
12108 */
12109 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012110 if (d == mBGDrawable) {
12111 return;
12112 }
12113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012114 boolean requestLayout = false;
12115
12116 mBackgroundResource = 0;
12117
12118 /*
12119 * Regardless of whether we're setting a new background or not, we want
12120 * to clear the previous drawable.
12121 */
12122 if (mBGDrawable != null) {
12123 mBGDrawable.setCallback(null);
12124 unscheduleDrawable(mBGDrawable);
12125 }
12126
12127 if (d != null) {
12128 Rect padding = sThreadLocal.get();
12129 if (padding == null) {
12130 padding = new Rect();
12131 sThreadLocal.set(padding);
12132 }
12133 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012134 switch (d.getResolvedLayoutDirectionSelf()) {
12135 case LAYOUT_DIRECTION_RTL:
12136 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12137 break;
12138 case LAYOUT_DIRECTION_LTR:
12139 default:
12140 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012142 }
12143
12144 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12145 // if it has a different minimum size, we should layout again
12146 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12147 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12148 requestLayout = true;
12149 }
12150
12151 d.setCallback(this);
12152 if (d.isStateful()) {
12153 d.setState(getDrawableState());
12154 }
12155 d.setVisible(getVisibility() == VISIBLE, false);
12156 mBGDrawable = d;
12157
12158 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12159 mPrivateFlags &= ~SKIP_DRAW;
12160 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12161 requestLayout = true;
12162 }
12163 } else {
12164 /* Remove the background */
12165 mBGDrawable = null;
12166
12167 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12168 /*
12169 * This view ONLY drew the background before and we're removing
12170 * the background, so now it won't draw anything
12171 * (hence we SKIP_DRAW)
12172 */
12173 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12174 mPrivateFlags |= SKIP_DRAW;
12175 }
12176
12177 /*
12178 * When the background is set, we try to apply its padding to this
12179 * View. When the background is removed, we don't touch this View's
12180 * padding. This is noted in the Javadocs. Hence, we don't need to
12181 * requestLayout(), the invalidate() below is sufficient.
12182 */
12183
12184 // The old background's minimum size could have affected this
12185 // View's layout, so let's requestLayout
12186 requestLayout = true;
12187 }
12188
Romain Guy8f1344f52009-05-15 16:03:59 -070012189 computeOpaqueFlags();
12190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012191 if (requestLayout) {
12192 requestLayout();
12193 }
12194
12195 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012196 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012197 }
12198
12199 /**
12200 * Gets the background drawable
12201 * @return The drawable used as the background for this view, if any.
12202 */
12203 public Drawable getBackground() {
12204 return mBGDrawable;
12205 }
12206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012207 /**
12208 * Sets the padding. The view may add on the space required to display
12209 * the scrollbars, depending on the style and visibility of the scrollbars.
12210 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12211 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12212 * from the values set in this call.
12213 *
12214 * @attr ref android.R.styleable#View_padding
12215 * @attr ref android.R.styleable#View_paddingBottom
12216 * @attr ref android.R.styleable#View_paddingLeft
12217 * @attr ref android.R.styleable#View_paddingRight
12218 * @attr ref android.R.styleable#View_paddingTop
12219 * @param left the left padding in pixels
12220 * @param top the top padding in pixels
12221 * @param right the right padding in pixels
12222 * @param bottom the bottom padding in pixels
12223 */
12224 public void setPadding(int left, int top, int right, int bottom) {
12225 boolean changed = false;
12226
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012227 mUserPaddingRelative = false;
12228
Adam Powell20232d02010-12-08 21:08:53 -080012229 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012230 mUserPaddingRight = right;
12231 mUserPaddingBottom = bottom;
12232
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012233 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070012234
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012235 // Common case is there are no scroll bars.
12236 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012237 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012238 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012239 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012240 switch (mVerticalScrollbarPosition) {
12241 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012242 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12243 left += offset;
12244 } else {
12245 right += offset;
12246 }
12247 break;
Adam Powell20232d02010-12-08 21:08:53 -080012248 case SCROLLBAR_POSITION_RIGHT:
12249 right += offset;
12250 break;
12251 case SCROLLBAR_POSITION_LEFT:
12252 left += offset;
12253 break;
12254 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012255 }
Adam Powell20232d02010-12-08 21:08:53 -080012256 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012257 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12258 ? 0 : getHorizontalScrollbarHeight();
12259 }
12260 }
Romain Guy8506ab42009-06-11 17:35:47 -070012261
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012262 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012263 changed = true;
12264 mPaddingLeft = left;
12265 }
12266 if (mPaddingTop != top) {
12267 changed = true;
12268 mPaddingTop = top;
12269 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012270 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012271 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012272 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012274 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012275 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012276 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012277 }
12278
12279 if (changed) {
12280 requestLayout();
12281 }
12282 }
12283
12284 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012285 * Sets the relative padding. The view may add on the space required to display
12286 * the scrollbars, depending on the style and visibility of the scrollbars.
12287 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12288 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12289 * from the values set in this call.
12290 *
12291 * @attr ref android.R.styleable#View_padding
12292 * @attr ref android.R.styleable#View_paddingBottom
12293 * @attr ref android.R.styleable#View_paddingStart
12294 * @attr ref android.R.styleable#View_paddingEnd
12295 * @attr ref android.R.styleable#View_paddingTop
12296 * @param start the start padding in pixels
12297 * @param top the top padding in pixels
12298 * @param end the end padding in pixels
12299 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012300 */
12301 public void setPaddingRelative(int start, int top, int end, int bottom) {
12302 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012303
12304 mUserPaddingStart = start;
12305 mUserPaddingEnd = end;
12306
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012307 switch(getResolvedLayoutDirection()) {
12308 case LAYOUT_DIRECTION_RTL:
12309 setPadding(end, top, start, bottom);
12310 break;
12311 case LAYOUT_DIRECTION_LTR:
12312 default:
12313 setPadding(start, top, end, bottom);
12314 }
12315 }
12316
12317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012318 * Returns the top padding of this view.
12319 *
12320 * @return the top padding in pixels
12321 */
12322 public int getPaddingTop() {
12323 return mPaddingTop;
12324 }
12325
12326 /**
12327 * Returns the bottom padding of this view. If there are inset and enabled
12328 * scrollbars, this value may include the space required to display the
12329 * scrollbars as well.
12330 *
12331 * @return the bottom padding in pixels
12332 */
12333 public int getPaddingBottom() {
12334 return mPaddingBottom;
12335 }
12336
12337 /**
12338 * Returns the left padding of this view. If there are inset and enabled
12339 * scrollbars, this value may include the space required to display the
12340 * scrollbars as well.
12341 *
12342 * @return the left padding in pixels
12343 */
12344 public int getPaddingLeft() {
12345 return mPaddingLeft;
12346 }
12347
12348 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012349 * Returns the start padding of this view. If there are inset and enabled
12350 * scrollbars, this value may include the space required to display the
12351 * scrollbars as well.
12352 *
12353 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012354 */
12355 public int getPaddingStart() {
12356 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12357 mPaddingRight : mPaddingLeft;
12358 }
12359
12360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012361 * Returns the right padding of this view. If there are inset and enabled
12362 * scrollbars, this value may include the space required to display the
12363 * scrollbars as well.
12364 *
12365 * @return the right padding in pixels
12366 */
12367 public int getPaddingRight() {
12368 return mPaddingRight;
12369 }
12370
12371 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012372 * Returns the end padding of this view. If there are inset and enabled
12373 * scrollbars, this value may include the space required to display the
12374 * scrollbars as well.
12375 *
12376 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012377 */
12378 public int getPaddingEnd() {
12379 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12380 mPaddingLeft : mPaddingRight;
12381 }
12382
12383 /**
12384 * Return if the padding as been set thru relative values
12385 * {@link #setPaddingRelative(int, int, int, int)} or thru
12386 * @attr ref android.R.styleable#View_paddingStart or
12387 * @attr ref android.R.styleable#View_paddingEnd
12388 *
12389 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012390 */
12391 public boolean isPaddingRelative() {
12392 return mUserPaddingRelative;
12393 }
12394
12395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012396 * Changes the selection state of this view. A view can be selected or not.
12397 * Note that selection is not the same as focus. Views are typically
12398 * selected in the context of an AdapterView like ListView or GridView;
12399 * the selected view is the view that is highlighted.
12400 *
12401 * @param selected true if the view must be selected, false otherwise
12402 */
12403 public void setSelected(boolean selected) {
12404 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12405 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012406 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012407 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012408 refreshDrawableState();
12409 dispatchSetSelected(selected);
12410 }
12411 }
12412
12413 /**
12414 * Dispatch setSelected to all of this View's children.
12415 *
12416 * @see #setSelected(boolean)
12417 *
12418 * @param selected The new selected state
12419 */
12420 protected void dispatchSetSelected(boolean selected) {
12421 }
12422
12423 /**
12424 * Indicates the selection state of this view.
12425 *
12426 * @return true if the view is selected, false otherwise
12427 */
12428 @ViewDebug.ExportedProperty
12429 public boolean isSelected() {
12430 return (mPrivateFlags & SELECTED) != 0;
12431 }
12432
12433 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012434 * Changes the activated state of this view. A view can be activated or not.
12435 * Note that activation is not the same as selection. Selection is
12436 * a transient property, representing the view (hierarchy) the user is
12437 * currently interacting with. Activation is a longer-term state that the
12438 * user can move views in and out of. For example, in a list view with
12439 * single or multiple selection enabled, the views in the current selection
12440 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12441 * here.) The activated state is propagated down to children of the view it
12442 * is set on.
12443 *
12444 * @param activated true if the view must be activated, false otherwise
12445 */
12446 public void setActivated(boolean activated) {
12447 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12448 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012449 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012450 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012451 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012452 }
12453 }
12454
12455 /**
12456 * Dispatch setActivated to all of this View's children.
12457 *
12458 * @see #setActivated(boolean)
12459 *
12460 * @param activated The new activated state
12461 */
12462 protected void dispatchSetActivated(boolean activated) {
12463 }
12464
12465 /**
12466 * Indicates the activation state of this view.
12467 *
12468 * @return true if the view is activated, false otherwise
12469 */
12470 @ViewDebug.ExportedProperty
12471 public boolean isActivated() {
12472 return (mPrivateFlags & ACTIVATED) != 0;
12473 }
12474
12475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012476 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12477 * observer can be used to get notifications when global events, like
12478 * layout, happen.
12479 *
12480 * The returned ViewTreeObserver observer is not guaranteed to remain
12481 * valid for the lifetime of this View. If the caller of this method keeps
12482 * a long-lived reference to ViewTreeObserver, it should always check for
12483 * the return value of {@link ViewTreeObserver#isAlive()}.
12484 *
12485 * @return The ViewTreeObserver for this view's hierarchy.
12486 */
12487 public ViewTreeObserver getViewTreeObserver() {
12488 if (mAttachInfo != null) {
12489 return mAttachInfo.mTreeObserver;
12490 }
12491 if (mFloatingTreeObserver == null) {
12492 mFloatingTreeObserver = new ViewTreeObserver();
12493 }
12494 return mFloatingTreeObserver;
12495 }
12496
12497 /**
12498 * <p>Finds the topmost view in the current view hierarchy.</p>
12499 *
12500 * @return the topmost view containing this view
12501 */
12502 public View getRootView() {
12503 if (mAttachInfo != null) {
12504 final View v = mAttachInfo.mRootView;
12505 if (v != null) {
12506 return v;
12507 }
12508 }
Romain Guy8506ab42009-06-11 17:35:47 -070012509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012510 View parent = this;
12511
12512 while (parent.mParent != null && parent.mParent instanceof View) {
12513 parent = (View) parent.mParent;
12514 }
12515
12516 return parent;
12517 }
12518
12519 /**
12520 * <p>Computes the coordinates of this view on the screen. The argument
12521 * must be an array of two integers. After the method returns, the array
12522 * contains the x and y location in that order.</p>
12523 *
12524 * @param location an array of two integers in which to hold the coordinates
12525 */
12526 public void getLocationOnScreen(int[] location) {
12527 getLocationInWindow(location);
12528
12529 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012530 if (info != null) {
12531 location[0] += info.mWindowLeft;
12532 location[1] += info.mWindowTop;
12533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012534 }
12535
12536 /**
12537 * <p>Computes the coordinates of this view in its window. The argument
12538 * must be an array of two integers. After the method returns, the array
12539 * contains the x and y location in that order.</p>
12540 *
12541 * @param location an array of two integers in which to hold the coordinates
12542 */
12543 public void getLocationInWindow(int[] location) {
12544 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012545 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012546 }
12547
Gilles Debunne6583ce52011-12-06 18:09:02 -080012548 if (mAttachInfo == null) {
12549 // When the view is not attached to a window, this method does not make sense
12550 location[0] = location[1] = 0;
12551 return;
12552 }
12553
Gilles Debunnecea45132011-11-24 02:19:27 +010012554 float[] position = mAttachInfo.mTmpTransformLocation;
12555 position[0] = position[1] = 0.0f;
12556
12557 if (!hasIdentityMatrix()) {
12558 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012560
Gilles Debunnecea45132011-11-24 02:19:27 +010012561 position[0] += mLeft;
12562 position[1] += mTop;
12563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012564 ViewParent viewParent = mParent;
12565 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012566 final View view = (View) viewParent;
12567
12568 position[0] -= view.mScrollX;
12569 position[1] -= view.mScrollY;
12570
12571 if (!view.hasIdentityMatrix()) {
12572 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012573 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012574
12575 position[0] += view.mLeft;
12576 position[1] += view.mTop;
12577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012578 viewParent = view.mParent;
12579 }
Romain Guy8506ab42009-06-11 17:35:47 -070012580
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012581 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012582 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012583 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12584 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012585 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012586
12587 location[0] = (int) (position[0] + 0.5f);
12588 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012589 }
12590
12591 /**
12592 * {@hide}
12593 * @param id the id of the view to be found
12594 * @return the view of the specified id, null if cannot be found
12595 */
12596 protected View findViewTraversal(int id) {
12597 if (id == mID) {
12598 return this;
12599 }
12600 return null;
12601 }
12602
12603 /**
12604 * {@hide}
12605 * @param tag the tag of the view to be found
12606 * @return the view of specified tag, null if cannot be found
12607 */
12608 protected View findViewWithTagTraversal(Object tag) {
12609 if (tag != null && tag.equals(mTag)) {
12610 return this;
12611 }
12612 return null;
12613 }
12614
12615 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012616 * {@hide}
12617 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012618 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012619 * @return The first view that matches the predicate or null.
12620 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012621 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012622 if (predicate.apply(this)) {
12623 return this;
12624 }
12625 return null;
12626 }
12627
12628 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012629 * Look for a child view with the given id. If this view has the given
12630 * id, return this view.
12631 *
12632 * @param id The id to search for.
12633 * @return The view that has the given id in the hierarchy or null
12634 */
12635 public final View findViewById(int id) {
12636 if (id < 0) {
12637 return null;
12638 }
12639 return findViewTraversal(id);
12640 }
12641
12642 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012643 * Finds a view by its unuque and stable accessibility id.
12644 *
12645 * @param accessibilityId The searched accessibility id.
12646 * @return The found view.
12647 */
12648 final View findViewByAccessibilityId(int accessibilityId) {
12649 if (accessibilityId < 0) {
12650 return null;
12651 }
12652 return findViewByAccessibilityIdTraversal(accessibilityId);
12653 }
12654
12655 /**
12656 * Performs the traversal to find a view by its unuque and stable accessibility id.
12657 *
12658 * <strong>Note:</strong>This method does not stop at the root namespace
12659 * boundary since the user can touch the screen at an arbitrary location
12660 * potentially crossing the root namespace bounday which will send an
12661 * accessibility event to accessibility services and they should be able
12662 * to obtain the event source. Also accessibility ids are guaranteed to be
12663 * unique in the window.
12664 *
12665 * @param accessibilityId The accessibility id.
12666 * @return The found view.
12667 */
12668 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12669 if (getAccessibilityViewId() == accessibilityId) {
12670 return this;
12671 }
12672 return null;
12673 }
12674
12675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012676 * Look for a child view with the given tag. If this view has the given
12677 * tag, return this view.
12678 *
12679 * @param tag The tag to search for, using "tag.equals(getTag())".
12680 * @return The View that has the given tag in the hierarchy or null
12681 */
12682 public final View findViewWithTag(Object tag) {
12683 if (tag == null) {
12684 return null;
12685 }
12686 return findViewWithTagTraversal(tag);
12687 }
12688
12689 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012690 * {@hide}
12691 * Look for a child view that matches the specified predicate.
12692 * If this view matches the predicate, return this view.
12693 *
12694 * @param predicate The predicate to evaluate.
12695 * @return The first view that matches the predicate or null.
12696 */
12697 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012698 return findViewByPredicateTraversal(predicate, null);
12699 }
12700
12701 /**
12702 * {@hide}
12703 * Look for a child view that matches the specified predicate,
12704 * starting with the specified view and its descendents and then
12705 * recusively searching the ancestors and siblings of that view
12706 * until this view is reached.
12707 *
12708 * This method is useful in cases where the predicate does not match
12709 * a single unique view (perhaps multiple views use the same id)
12710 * and we are trying to find the view that is "closest" in scope to the
12711 * starting view.
12712 *
12713 * @param start The view to start from.
12714 * @param predicate The predicate to evaluate.
12715 * @return The first view that matches the predicate or null.
12716 */
12717 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12718 View childToSkip = null;
12719 for (;;) {
12720 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12721 if (view != null || start == this) {
12722 return view;
12723 }
12724
12725 ViewParent parent = start.getParent();
12726 if (parent == null || !(parent instanceof View)) {
12727 return null;
12728 }
12729
12730 childToSkip = start;
12731 start = (View) parent;
12732 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012733 }
12734
12735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012736 * Sets the identifier for this view. The identifier does not have to be
12737 * unique in this view's hierarchy. The identifier should be a positive
12738 * number.
12739 *
12740 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012741 * @see #getId()
12742 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012743 *
12744 * @param id a number used to identify the view
12745 *
12746 * @attr ref android.R.styleable#View_id
12747 */
12748 public void setId(int id) {
12749 mID = id;
12750 }
12751
12752 /**
12753 * {@hide}
12754 *
12755 * @param isRoot true if the view belongs to the root namespace, false
12756 * otherwise
12757 */
12758 public void setIsRootNamespace(boolean isRoot) {
12759 if (isRoot) {
12760 mPrivateFlags |= IS_ROOT_NAMESPACE;
12761 } else {
12762 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12763 }
12764 }
12765
12766 /**
12767 * {@hide}
12768 *
12769 * @return true if the view belongs to the root namespace, false otherwise
12770 */
12771 public boolean isRootNamespace() {
12772 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12773 }
12774
12775 /**
12776 * Returns this view's identifier.
12777 *
12778 * @return a positive integer used to identify the view or {@link #NO_ID}
12779 * if the view has no ID
12780 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012781 * @see #setId(int)
12782 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012783 * @attr ref android.R.styleable#View_id
12784 */
12785 @ViewDebug.CapturedViewProperty
12786 public int getId() {
12787 return mID;
12788 }
12789
12790 /**
12791 * Returns this view's tag.
12792 *
12793 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012794 *
12795 * @see #setTag(Object)
12796 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012797 */
12798 @ViewDebug.ExportedProperty
12799 public Object getTag() {
12800 return mTag;
12801 }
12802
12803 /**
12804 * Sets the tag associated with this view. A tag can be used to mark
12805 * a view in its hierarchy and does not have to be unique within the
12806 * hierarchy. Tags can also be used to store data within a view without
12807 * resorting to another data structure.
12808 *
12809 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012810 *
12811 * @see #getTag()
12812 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012813 */
12814 public void setTag(final Object tag) {
12815 mTag = tag;
12816 }
12817
12818 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012819 * Returns the tag associated with this view and the specified key.
12820 *
12821 * @param key The key identifying the tag
12822 *
12823 * @return the Object stored in this view as a tag
12824 *
12825 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012826 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012827 */
12828 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012829 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012830 return null;
12831 }
12832
12833 /**
12834 * Sets a tag associated with this view and a key. A tag can be used
12835 * to mark a view in its hierarchy and does not have to be unique within
12836 * the hierarchy. Tags can also be used to store data within a view
12837 * without resorting to another data structure.
12838 *
12839 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012840 * application to ensure it is unique (see the <a
12841 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12842 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012843 * the Android framework or not associated with any package will cause
12844 * an {@link IllegalArgumentException} to be thrown.
12845 *
12846 * @param key The key identifying the tag
12847 * @param tag An Object to tag the view with
12848 *
12849 * @throws IllegalArgumentException If they specified key is not valid
12850 *
12851 * @see #setTag(Object)
12852 * @see #getTag(int)
12853 */
12854 public void setTag(int key, final Object tag) {
12855 // If the package id is 0x00 or 0x01, it's either an undefined package
12856 // or a framework id
12857 if ((key >>> 24) < 2) {
12858 throw new IllegalArgumentException("The key must be an application-specific "
12859 + "resource id.");
12860 }
12861
Adam Powell2b2f6d62011-09-23 11:15:39 -070012862 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012863 }
12864
12865 /**
12866 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12867 * framework id.
12868 *
12869 * @hide
12870 */
12871 public void setTagInternal(int key, Object tag) {
12872 if ((key >>> 24) != 0x1) {
12873 throw new IllegalArgumentException("The key must be a framework-specific "
12874 + "resource id.");
12875 }
12876
Adam Powell2b2f6d62011-09-23 11:15:39 -070012877 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012878 }
12879
Adam Powell2b2f6d62011-09-23 11:15:39 -070012880 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012881 if (mKeyedTags == null) {
12882 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012883 }
12884
Adam Powell7db82ac2011-09-22 19:44:04 -070012885 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012886 }
12887
12888 /**
Romain Guy13922e02009-05-12 17:56:14 -070012889 * @param consistency The type of consistency. See ViewDebug for more information.
12890 *
12891 * @hide
12892 */
12893 protected boolean dispatchConsistencyCheck(int consistency) {
12894 return onConsistencyCheck(consistency);
12895 }
12896
12897 /**
12898 * Method that subclasses should implement to check their consistency. The type of
12899 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012900 *
Romain Guy13922e02009-05-12 17:56:14 -070012901 * @param consistency The type of consistency. See ViewDebug for more information.
12902 *
12903 * @throws IllegalStateException if the view is in an inconsistent state.
12904 *
12905 * @hide
12906 */
12907 protected boolean onConsistencyCheck(int consistency) {
12908 boolean result = true;
12909
12910 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12911 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12912
12913 if (checkLayout) {
12914 if (getParent() == null) {
12915 result = false;
12916 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12917 "View " + this + " does not have a parent.");
12918 }
12919
12920 if (mAttachInfo == null) {
12921 result = false;
12922 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12923 "View " + this + " is not attached to a window.");
12924 }
12925 }
12926
12927 if (checkDrawing) {
12928 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12929 // from their draw() method
12930
12931 if ((mPrivateFlags & DRAWN) != DRAWN &&
12932 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12933 result = false;
12934 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12935 "View " + this + " was invalidated but its drawing cache is valid.");
12936 }
12937 }
12938
12939 return result;
12940 }
12941
12942 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012943 * Prints information about this view in the log output, with the tag
12944 * {@link #VIEW_LOG_TAG}.
12945 *
12946 * @hide
12947 */
12948 public void debug() {
12949 debug(0);
12950 }
12951
12952 /**
12953 * Prints information about this view in the log output, with the tag
12954 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12955 * indentation defined by the <code>depth</code>.
12956 *
12957 * @param depth the indentation level
12958 *
12959 * @hide
12960 */
12961 protected void debug(int depth) {
12962 String output = debugIndent(depth - 1);
12963
12964 output += "+ " + this;
12965 int id = getId();
12966 if (id != -1) {
12967 output += " (id=" + id + ")";
12968 }
12969 Object tag = getTag();
12970 if (tag != null) {
12971 output += " (tag=" + tag + ")";
12972 }
12973 Log.d(VIEW_LOG_TAG, output);
12974
12975 if ((mPrivateFlags & FOCUSED) != 0) {
12976 output = debugIndent(depth) + " FOCUSED";
12977 Log.d(VIEW_LOG_TAG, output);
12978 }
12979
12980 output = debugIndent(depth);
12981 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12982 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12983 + "} ";
12984 Log.d(VIEW_LOG_TAG, output);
12985
12986 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12987 || mPaddingBottom != 0) {
12988 output = debugIndent(depth);
12989 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12990 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12991 Log.d(VIEW_LOG_TAG, output);
12992 }
12993
12994 output = debugIndent(depth);
12995 output += "mMeasureWidth=" + mMeasuredWidth +
12996 " mMeasureHeight=" + mMeasuredHeight;
12997 Log.d(VIEW_LOG_TAG, output);
12998
12999 output = debugIndent(depth);
13000 if (mLayoutParams == null) {
13001 output += "BAD! no layout params";
13002 } else {
13003 output = mLayoutParams.debug(output);
13004 }
13005 Log.d(VIEW_LOG_TAG, output);
13006
13007 output = debugIndent(depth);
13008 output += "flags={";
13009 output += View.printFlags(mViewFlags);
13010 output += "}";
13011 Log.d(VIEW_LOG_TAG, output);
13012
13013 output = debugIndent(depth);
13014 output += "privateFlags={";
13015 output += View.printPrivateFlags(mPrivateFlags);
13016 output += "}";
13017 Log.d(VIEW_LOG_TAG, output);
13018 }
13019
13020 /**
13021 * Creates an string of whitespaces used for indentation.
13022 *
13023 * @param depth the indentation level
13024 * @return a String containing (depth * 2 + 3) * 2 white spaces
13025 *
13026 * @hide
13027 */
13028 protected static String debugIndent(int depth) {
13029 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13030 for (int i = 0; i < (depth * 2) + 3; i++) {
13031 spaces.append(' ').append(' ');
13032 }
13033 return spaces.toString();
13034 }
13035
13036 /**
13037 * <p>Return the offset of the widget's text baseline from the widget's top
13038 * boundary. If this widget does not support baseline alignment, this
13039 * method returns -1. </p>
13040 *
13041 * @return the offset of the baseline within the widget's bounds or -1
13042 * if baseline alignment is not supported
13043 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013044 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013045 public int getBaseline() {
13046 return -1;
13047 }
13048
13049 /**
13050 * Call this when something has changed which has invalidated the
13051 * layout of this view. This will schedule a layout pass of the view
13052 * tree.
13053 */
13054 public void requestLayout() {
13055 if (ViewDebug.TRACE_HIERARCHY) {
13056 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13057 }
13058
13059 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013060 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013061
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013062 if (mParent != null) {
13063 if (mLayoutParams != null) {
13064 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
13065 }
13066 if (!mParent.isLayoutRequested()) {
13067 mParent.requestLayout();
13068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013069 }
13070 }
13071
13072 /**
13073 * Forces this view to be laid out during the next layout pass.
13074 * This method does not call requestLayout() or forceLayout()
13075 * on the parent.
13076 */
13077 public void forceLayout() {
13078 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013079 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013080 }
13081
13082 /**
13083 * <p>
13084 * This is called to find out how big a view should be. The parent
13085 * supplies constraint information in the width and height parameters.
13086 * </p>
13087 *
13088 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013089 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013090 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013091 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013092 * </p>
13093 *
13094 *
13095 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13096 * parent
13097 * @param heightMeasureSpec Vertical space requirements as imposed by the
13098 * parent
13099 *
13100 * @see #onMeasure(int, int)
13101 */
13102 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13103 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13104 widthMeasureSpec != mOldWidthMeasureSpec ||
13105 heightMeasureSpec != mOldHeightMeasureSpec) {
13106
13107 // first clears the measured dimension flag
13108 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13109
13110 if (ViewDebug.TRACE_HIERARCHY) {
13111 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13112 }
13113
13114 // measure ourselves, this should set the measured dimension flag back
13115 onMeasure(widthMeasureSpec, heightMeasureSpec);
13116
13117 // flag not set, setMeasuredDimension() was not invoked, we raise
13118 // an exception to warn the developer
13119 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13120 throw new IllegalStateException("onMeasure() did not set the"
13121 + " measured dimension by calling"
13122 + " setMeasuredDimension()");
13123 }
13124
13125 mPrivateFlags |= LAYOUT_REQUIRED;
13126 }
13127
13128 mOldWidthMeasureSpec = widthMeasureSpec;
13129 mOldHeightMeasureSpec = heightMeasureSpec;
13130 }
13131
13132 /**
13133 * <p>
13134 * Measure the view and its content to determine the measured width and the
13135 * measured height. This method is invoked by {@link #measure(int, int)} and
13136 * should be overriden by subclasses to provide accurate and efficient
13137 * measurement of their contents.
13138 * </p>
13139 *
13140 * <p>
13141 * <strong>CONTRACT:</strong> When overriding this method, you
13142 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13143 * measured width and height of this view. Failure to do so will trigger an
13144 * <code>IllegalStateException</code>, thrown by
13145 * {@link #measure(int, int)}. Calling the superclass'
13146 * {@link #onMeasure(int, int)} is a valid use.
13147 * </p>
13148 *
13149 * <p>
13150 * The base class implementation of measure defaults to the background size,
13151 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13152 * override {@link #onMeasure(int, int)} to provide better measurements of
13153 * their content.
13154 * </p>
13155 *
13156 * <p>
13157 * If this method is overridden, it is the subclass's responsibility to make
13158 * sure the measured height and width are at least the view's minimum height
13159 * and width ({@link #getSuggestedMinimumHeight()} and
13160 * {@link #getSuggestedMinimumWidth()}).
13161 * </p>
13162 *
13163 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13164 * The requirements are encoded with
13165 * {@link android.view.View.MeasureSpec}.
13166 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13167 * The requirements are encoded with
13168 * {@link android.view.View.MeasureSpec}.
13169 *
13170 * @see #getMeasuredWidth()
13171 * @see #getMeasuredHeight()
13172 * @see #setMeasuredDimension(int, int)
13173 * @see #getSuggestedMinimumHeight()
13174 * @see #getSuggestedMinimumWidth()
13175 * @see android.view.View.MeasureSpec#getMode(int)
13176 * @see android.view.View.MeasureSpec#getSize(int)
13177 */
13178 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13179 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13180 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13181 }
13182
13183 /**
13184 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13185 * measured width and measured height. Failing to do so will trigger an
13186 * exception at measurement time.</p>
13187 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013188 * @param measuredWidth The measured width of this view. May be a complex
13189 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13190 * {@link #MEASURED_STATE_TOO_SMALL}.
13191 * @param measuredHeight The measured height of this view. May be a complex
13192 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13193 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013194 */
13195 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13196 mMeasuredWidth = measuredWidth;
13197 mMeasuredHeight = measuredHeight;
13198
13199 mPrivateFlags |= MEASURED_DIMENSION_SET;
13200 }
13201
13202 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013203 * Merge two states as returned by {@link #getMeasuredState()}.
13204 * @param curState The current state as returned from a view or the result
13205 * of combining multiple views.
13206 * @param newState The new view state to combine.
13207 * @return Returns a new integer reflecting the combination of the two
13208 * states.
13209 */
13210 public static int combineMeasuredStates(int curState, int newState) {
13211 return curState | newState;
13212 }
13213
13214 /**
13215 * Version of {@link #resolveSizeAndState(int, int, int)}
13216 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13217 */
13218 public static int resolveSize(int size, int measureSpec) {
13219 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13220 }
13221
13222 /**
13223 * Utility to reconcile a desired size and state, with constraints imposed
13224 * by a MeasureSpec. Will take the desired size, unless a different size
13225 * is imposed by the constraints. The returned value is a compound integer,
13226 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13227 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13228 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013229 *
13230 * @param size How big the view wants to be
13231 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013232 * @return Size information bit mask as defined by
13233 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013234 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013235 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013236 int result = size;
13237 int specMode = MeasureSpec.getMode(measureSpec);
13238 int specSize = MeasureSpec.getSize(measureSpec);
13239 switch (specMode) {
13240 case MeasureSpec.UNSPECIFIED:
13241 result = size;
13242 break;
13243 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013244 if (specSize < size) {
13245 result = specSize | MEASURED_STATE_TOO_SMALL;
13246 } else {
13247 result = size;
13248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013249 break;
13250 case MeasureSpec.EXACTLY:
13251 result = specSize;
13252 break;
13253 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013254 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013255 }
13256
13257 /**
13258 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013259 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013260 * by the MeasureSpec.
13261 *
13262 * @param size Default size for this view
13263 * @param measureSpec Constraints imposed by the parent
13264 * @return The size this view should be.
13265 */
13266 public static int getDefaultSize(int size, int measureSpec) {
13267 int result = size;
13268 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013269 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013270
13271 switch (specMode) {
13272 case MeasureSpec.UNSPECIFIED:
13273 result = size;
13274 break;
13275 case MeasureSpec.AT_MOST:
13276 case MeasureSpec.EXACTLY:
13277 result = specSize;
13278 break;
13279 }
13280 return result;
13281 }
13282
13283 /**
13284 * Returns the suggested minimum height that the view should use. This
13285 * returns the maximum of the view's minimum height
13286 * and the background's minimum height
13287 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13288 * <p>
13289 * When being used in {@link #onMeasure(int, int)}, the caller should still
13290 * ensure the returned height is within the requirements of the parent.
13291 *
13292 * @return The suggested minimum height of the view.
13293 */
13294 protected int getSuggestedMinimumHeight() {
13295 int suggestedMinHeight = mMinHeight;
13296
13297 if (mBGDrawable != null) {
13298 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13299 if (suggestedMinHeight < bgMinHeight) {
13300 suggestedMinHeight = bgMinHeight;
13301 }
13302 }
13303
13304 return suggestedMinHeight;
13305 }
13306
13307 /**
13308 * Returns the suggested minimum width that the view should use. This
13309 * returns the maximum of the view's minimum width)
13310 * and the background's minimum width
13311 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13312 * <p>
13313 * When being used in {@link #onMeasure(int, int)}, the caller should still
13314 * ensure the returned width is within the requirements of the parent.
13315 *
13316 * @return The suggested minimum width of the view.
13317 */
13318 protected int getSuggestedMinimumWidth() {
13319 int suggestedMinWidth = mMinWidth;
13320
13321 if (mBGDrawable != null) {
13322 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13323 if (suggestedMinWidth < bgMinWidth) {
13324 suggestedMinWidth = bgMinWidth;
13325 }
13326 }
13327
13328 return suggestedMinWidth;
13329 }
13330
13331 /**
13332 * Sets the minimum height of the view. It is not guaranteed the view will
13333 * be able to achieve this minimum height (for example, if its parent layout
13334 * constrains it with less available height).
13335 *
13336 * @param minHeight The minimum height the view will try to be.
13337 */
13338 public void setMinimumHeight(int minHeight) {
13339 mMinHeight = minHeight;
13340 }
13341
13342 /**
13343 * Sets the minimum width of the view. It is not guaranteed the view will
13344 * be able to achieve this minimum width (for example, if its parent layout
13345 * constrains it with less available width).
13346 *
13347 * @param minWidth The minimum width the view will try to be.
13348 */
13349 public void setMinimumWidth(int minWidth) {
13350 mMinWidth = minWidth;
13351 }
13352
13353 /**
13354 * Get the animation currently associated with this view.
13355 *
13356 * @return The animation that is currently playing or
13357 * scheduled to play for this view.
13358 */
13359 public Animation getAnimation() {
13360 return mCurrentAnimation;
13361 }
13362
13363 /**
13364 * Start the specified animation now.
13365 *
13366 * @param animation the animation to start now
13367 */
13368 public void startAnimation(Animation animation) {
13369 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13370 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013371 invalidateParentCaches();
13372 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013373 }
13374
13375 /**
13376 * Cancels any animations for this view.
13377 */
13378 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013379 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013380 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013382 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013383 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013384 }
13385
13386 /**
13387 * Sets the next animation to play for this view.
13388 * If you want the animation to play immediately, use
13389 * startAnimation. This method provides allows fine-grained
13390 * control over the start time and invalidation, but you
13391 * must make sure that 1) the animation has a start time set, and
13392 * 2) the view will be invalidated when the animation is supposed to
13393 * start.
13394 *
13395 * @param animation The next animation, or null.
13396 */
13397 public void setAnimation(Animation animation) {
13398 mCurrentAnimation = animation;
13399 if (animation != null) {
13400 animation.reset();
13401 }
13402 }
13403
13404 /**
13405 * Invoked by a parent ViewGroup to notify the start of the animation
13406 * currently associated with this view. If you override this method,
13407 * always call super.onAnimationStart();
13408 *
13409 * @see #setAnimation(android.view.animation.Animation)
13410 * @see #getAnimation()
13411 */
13412 protected void onAnimationStart() {
13413 mPrivateFlags |= ANIMATION_STARTED;
13414 }
13415
13416 /**
13417 * Invoked by a parent ViewGroup to notify the end of the animation
13418 * currently associated with this view. If you override this method,
13419 * always call super.onAnimationEnd();
13420 *
13421 * @see #setAnimation(android.view.animation.Animation)
13422 * @see #getAnimation()
13423 */
13424 protected void onAnimationEnd() {
13425 mPrivateFlags &= ~ANIMATION_STARTED;
13426 }
13427
13428 /**
13429 * Invoked if there is a Transform that involves alpha. Subclass that can
13430 * draw themselves with the specified alpha should return true, and then
13431 * respect that alpha when their onDraw() is called. If this returns false
13432 * then the view may be redirected to draw into an offscreen buffer to
13433 * fulfill the request, which will look fine, but may be slower than if the
13434 * subclass handles it internally. The default implementation returns false.
13435 *
13436 * @param alpha The alpha (0..255) to apply to the view's drawing
13437 * @return true if the view can draw with the specified alpha.
13438 */
13439 protected boolean onSetAlpha(int alpha) {
13440 return false;
13441 }
13442
13443 /**
13444 * This is used by the RootView to perform an optimization when
13445 * the view hierarchy contains one or several SurfaceView.
13446 * SurfaceView is always considered transparent, but its children are not,
13447 * therefore all View objects remove themselves from the global transparent
13448 * region (passed as a parameter to this function).
13449 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013450 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013451 *
13452 * @return Returns true if the effective visibility of the view at this
13453 * point is opaque, regardless of the transparent region; returns false
13454 * if it is possible for underlying windows to be seen behind the view.
13455 *
13456 * {@hide}
13457 */
13458 public boolean gatherTransparentRegion(Region region) {
13459 final AttachInfo attachInfo = mAttachInfo;
13460 if (region != null && attachInfo != null) {
13461 final int pflags = mPrivateFlags;
13462 if ((pflags & SKIP_DRAW) == 0) {
13463 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13464 // remove it from the transparent region.
13465 final int[] location = attachInfo.mTransparentLocation;
13466 getLocationInWindow(location);
13467 region.op(location[0], location[1], location[0] + mRight - mLeft,
13468 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13469 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13470 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13471 // exists, so we remove the background drawable's non-transparent
13472 // parts from this transparent region.
13473 applyDrawableToTransparentRegion(mBGDrawable, region);
13474 }
13475 }
13476 return true;
13477 }
13478
13479 /**
13480 * Play a sound effect for this view.
13481 *
13482 * <p>The framework will play sound effects for some built in actions, such as
13483 * clicking, but you may wish to play these effects in your widget,
13484 * for instance, for internal navigation.
13485 *
13486 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13487 * {@link #isSoundEffectsEnabled()} is true.
13488 *
13489 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13490 */
13491 public void playSoundEffect(int soundConstant) {
13492 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13493 return;
13494 }
13495 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13496 }
13497
13498 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013499 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013500 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013501 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013502 *
13503 * <p>The framework will provide haptic feedback for some built in actions,
13504 * such as long presses, but you may wish to provide feedback for your
13505 * own widget.
13506 *
13507 * <p>The feedback will only be performed if
13508 * {@link #isHapticFeedbackEnabled()} is true.
13509 *
13510 * @param feedbackConstant One of the constants defined in
13511 * {@link HapticFeedbackConstants}
13512 */
13513 public boolean performHapticFeedback(int feedbackConstant) {
13514 return performHapticFeedback(feedbackConstant, 0);
13515 }
13516
13517 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013518 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013519 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013520 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013521 *
13522 * @param feedbackConstant One of the constants defined in
13523 * {@link HapticFeedbackConstants}
13524 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13525 */
13526 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13527 if (mAttachInfo == null) {
13528 return false;
13529 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013530 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013531 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013532 && !isHapticFeedbackEnabled()) {
13533 return false;
13534 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013535 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13536 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013537 }
13538
13539 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013540 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013541 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13542 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013543 */
13544 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013545 if (visibility != mSystemUiVisibility) {
13546 mSystemUiVisibility = visibility;
13547 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13548 mParent.recomputeViewAttributes(this);
13549 }
Joe Onorato664644d2011-01-23 17:53:23 -080013550 }
13551 }
13552
13553 /**
13554 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013555 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13556 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013557 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013558 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013559 return mSystemUiVisibility;
13560 }
13561
Scott Mainec6331b2011-05-24 16:55:56 -070013562 /**
13563 * Set a listener to receive callbacks when the visibility of the system bar changes.
13564 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13565 */
Joe Onorato664644d2011-01-23 17:53:23 -080013566 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013567 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013568 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13569 mParent.recomputeViewAttributes(this);
13570 }
13571 }
13572
13573 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013574 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13575 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013576 */
13577 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013578 ListenerInfo li = mListenerInfo;
13579 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13580 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013581 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013582 }
13583 }
13584
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013585 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13586 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13587 if (val != mSystemUiVisibility) {
13588 setSystemUiVisibility(val);
13589 }
13590 }
13591
Joe Onorato664644d2011-01-23 17:53:23 -080013592 /**
Joe Malin32736f02011-01-19 16:14:20 -080013593 * Creates an image that the system displays during the drag and drop
13594 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13595 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13596 * appearance as the given View. The default also positions the center of the drag shadow
13597 * directly under the touch point. If no View is provided (the constructor with no parameters
13598 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13599 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13600 * default is an invisible drag shadow.
13601 * <p>
13602 * You are not required to use the View you provide to the constructor as the basis of the
13603 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13604 * anything you want as the drag shadow.
13605 * </p>
13606 * <p>
13607 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13608 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13609 * size and position of the drag shadow. It uses this data to construct a
13610 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13611 * so that your application can draw the shadow image in the Canvas.
13612 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013613 *
13614 * <div class="special reference">
13615 * <h3>Developer Guides</h3>
13616 * <p>For a guide to implementing drag and drop features, read the
13617 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13618 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013619 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013620 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013621 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013622
13623 /**
Joe Malin32736f02011-01-19 16:14:20 -080013624 * Constructs a shadow image builder based on a View. By default, the resulting drag
13625 * shadow will have the same appearance and dimensions as the View, with the touch point
13626 * over the center of the View.
13627 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013628 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013629 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013630 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013631 }
13632
Christopher Tate17ed60c2011-01-18 12:50:26 -080013633 /**
13634 * Construct a shadow builder object with no associated View. This
13635 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13636 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13637 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013638 * reference to any View object. If they are not overridden, then the result is an
13639 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013640 */
13641 public DragShadowBuilder() {
13642 mView = new WeakReference<View>(null);
13643 }
13644
13645 /**
13646 * Returns the View object that had been passed to the
13647 * {@link #View.DragShadowBuilder(View)}
13648 * constructor. If that View parameter was {@code null} or if the
13649 * {@link #View.DragShadowBuilder()}
13650 * constructor was used to instantiate the builder object, this method will return
13651 * null.
13652 *
13653 * @return The View object associate with this builder object.
13654 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013655 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013656 final public View getView() {
13657 return mView.get();
13658 }
13659
Christopher Tate2c095f32010-10-04 14:13:40 -070013660 /**
Joe Malin32736f02011-01-19 16:14:20 -080013661 * Provides the metrics for the shadow image. These include the dimensions of
13662 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013663 * be centered under the touch location while dragging.
13664 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013665 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013666 * same as the dimensions of the View itself and centers the shadow under
13667 * the touch point.
13668 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013669 *
Joe Malin32736f02011-01-19 16:14:20 -080013670 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13671 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13672 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13673 * image.
13674 *
13675 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13676 * shadow image that should be underneath the touch point during the drag and drop
13677 * operation. Your application must set {@link android.graphics.Point#x} to the
13678 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013679 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013680 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013681 final View view = mView.get();
13682 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013683 shadowSize.set(view.getWidth(), view.getHeight());
13684 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013685 } else {
13686 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13687 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013688 }
13689
13690 /**
Joe Malin32736f02011-01-19 16:14:20 -080013691 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13692 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013693 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013694 *
Joe Malin32736f02011-01-19 16:14:20 -080013695 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013696 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013697 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013698 final View view = mView.get();
13699 if (view != null) {
13700 view.draw(canvas);
13701 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013702 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013703 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013704 }
13705 }
13706
13707 /**
Joe Malin32736f02011-01-19 16:14:20 -080013708 * Starts a drag and drop operation. When your application calls this method, it passes a
13709 * {@link android.view.View.DragShadowBuilder} object to the system. The
13710 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13711 * to get metrics for the drag shadow, and then calls the object's
13712 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13713 * <p>
13714 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13715 * drag events to all the View objects in your application that are currently visible. It does
13716 * this either by calling the View object's drag listener (an implementation of
13717 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13718 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13719 * Both are passed a {@link android.view.DragEvent} object that has a
13720 * {@link android.view.DragEvent#getAction()} value of
13721 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13722 * </p>
13723 * <p>
13724 * Your application can invoke startDrag() on any attached View object. The View object does not
13725 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13726 * be related to the View the user selected for dragging.
13727 * </p>
13728 * @param data A {@link android.content.ClipData} object pointing to the data to be
13729 * transferred by the drag and drop operation.
13730 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13731 * drag shadow.
13732 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13733 * drop operation. This Object is put into every DragEvent object sent by the system during the
13734 * current drag.
13735 * <p>
13736 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13737 * to the target Views. For example, it can contain flags that differentiate between a
13738 * a copy operation and a move operation.
13739 * </p>
13740 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13741 * so the parameter should be set to 0.
13742 * @return {@code true} if the method completes successfully, or
13743 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13744 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013745 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013746 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013747 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013748 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013749 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013750 }
13751 boolean okay = false;
13752
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013753 Point shadowSize = new Point();
13754 Point shadowTouchPoint = new Point();
13755 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013756
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013757 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13758 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13759 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013760 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013761
Chris Tatea32dcf72010-10-14 12:13:50 -070013762 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013763 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13764 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013765 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013766 Surface surface = new Surface();
13767 try {
13768 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013769 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013770 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013771 + " surface=" + surface);
13772 if (token != null) {
13773 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013774 try {
Chris Tate6b391282010-10-14 15:48:59 -070013775 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013776 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013777 } finally {
13778 surface.unlockCanvasAndPost(canvas);
13779 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013780
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013781 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013782
13783 // Cache the local state object for delivery with DragEvents
13784 root.setLocalDragState(myLocalState);
13785
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013786 // repurpose 'shadowSize' for the last touch point
13787 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013788
Christopher Tatea53146c2010-09-07 11:57:52 -070013789 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013790 shadowSize.x, shadowSize.y,
13791 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013792 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013793
13794 // Off and running! Release our local surface instance; the drag
13795 // shadow surface is now managed by the system process.
13796 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013797 }
13798 } catch (Exception e) {
13799 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13800 surface.destroy();
13801 }
13802
13803 return okay;
13804 }
13805
Christopher Tatea53146c2010-09-07 11:57:52 -070013806 /**
Joe Malin32736f02011-01-19 16:14:20 -080013807 * Handles drag events sent by the system following a call to
13808 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13809 *<p>
13810 * When the system calls this method, it passes a
13811 * {@link android.view.DragEvent} object. A call to
13812 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13813 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13814 * operation.
13815 * @param event The {@link android.view.DragEvent} sent by the system.
13816 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13817 * in DragEvent, indicating the type of drag event represented by this object.
13818 * @return {@code true} if the method was successful, otherwise {@code false}.
13819 * <p>
13820 * The method should return {@code true} in response to an action type of
13821 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13822 * operation.
13823 * </p>
13824 * <p>
13825 * The method should also return {@code true} in response to an action type of
13826 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13827 * {@code false} if it didn't.
13828 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013829 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013830 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013831 return false;
13832 }
13833
13834 /**
Joe Malin32736f02011-01-19 16:14:20 -080013835 * Detects if this View is enabled and has a drag event listener.
13836 * If both are true, then it calls the drag event listener with the
13837 * {@link android.view.DragEvent} it received. If the drag event listener returns
13838 * {@code true}, then dispatchDragEvent() returns {@code true}.
13839 * <p>
13840 * For all other cases, the method calls the
13841 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13842 * method and returns its result.
13843 * </p>
13844 * <p>
13845 * This ensures that a drag event is always consumed, even if the View does not have a drag
13846 * event listener. However, if the View has a listener and the listener returns true, then
13847 * onDragEvent() is not called.
13848 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013849 */
13850 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013851 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013852 ListenerInfo li = mListenerInfo;
13853 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13854 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013855 return true;
13856 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013857 return onDragEvent(event);
13858 }
13859
Christopher Tate3d4bf172011-03-28 16:16:46 -070013860 boolean canAcceptDrag() {
13861 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13862 }
13863
Christopher Tatea53146c2010-09-07 11:57:52 -070013864 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013865 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13866 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013867 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013868 */
13869 public void onCloseSystemDialogs(String reason) {
13870 }
Joe Malin32736f02011-01-19 16:14:20 -080013871
Dianne Hackbornffa42482009-09-23 22:20:11 -070013872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013873 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013874 * update a Region being computed for
13875 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013876 * that any non-transparent parts of the Drawable are removed from the
13877 * given transparent region.
13878 *
13879 * @param dr The Drawable whose transparency is to be applied to the region.
13880 * @param region A Region holding the current transparency information,
13881 * where any parts of the region that are set are considered to be
13882 * transparent. On return, this region will be modified to have the
13883 * transparency information reduced by the corresponding parts of the
13884 * Drawable that are not transparent.
13885 * {@hide}
13886 */
13887 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13888 if (DBG) {
13889 Log.i("View", "Getting transparent region for: " + this);
13890 }
13891 final Region r = dr.getTransparentRegion();
13892 final Rect db = dr.getBounds();
13893 final AttachInfo attachInfo = mAttachInfo;
13894 if (r != null && attachInfo != null) {
13895 final int w = getRight()-getLeft();
13896 final int h = getBottom()-getTop();
13897 if (db.left > 0) {
13898 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13899 r.op(0, 0, db.left, h, Region.Op.UNION);
13900 }
13901 if (db.right < w) {
13902 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13903 r.op(db.right, 0, w, h, Region.Op.UNION);
13904 }
13905 if (db.top > 0) {
13906 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13907 r.op(0, 0, w, db.top, Region.Op.UNION);
13908 }
13909 if (db.bottom < h) {
13910 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13911 r.op(0, db.bottom, w, h, Region.Op.UNION);
13912 }
13913 final int[] location = attachInfo.mTransparentLocation;
13914 getLocationInWindow(location);
13915 r.translate(location[0], location[1]);
13916 region.op(r, Region.Op.INTERSECT);
13917 } else {
13918 region.op(db, Region.Op.DIFFERENCE);
13919 }
13920 }
13921
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013922 private void checkForLongClick(int delayOffset) {
13923 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13924 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013925
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013926 if (mPendingCheckForLongPress == null) {
13927 mPendingCheckForLongPress = new CheckForLongPress();
13928 }
13929 mPendingCheckForLongPress.rememberWindowAttachCount();
13930 postDelayed(mPendingCheckForLongPress,
13931 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013933 }
13934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013935 /**
13936 * Inflate a view from an XML resource. This convenience method wraps the {@link
13937 * LayoutInflater} class, which provides a full range of options for view inflation.
13938 *
13939 * @param context The Context object for your activity or application.
13940 * @param resource The resource ID to inflate
13941 * @param root A view group that will be the parent. Used to properly inflate the
13942 * layout_* parameters.
13943 * @see LayoutInflater
13944 */
13945 public static View inflate(Context context, int resource, ViewGroup root) {
13946 LayoutInflater factory = LayoutInflater.from(context);
13947 return factory.inflate(resource, root);
13948 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013950 /**
Adam Powell637d3372010-08-25 14:37:03 -070013951 * Scroll the view with standard behavior for scrolling beyond the normal
13952 * content boundaries. Views that call this method should override
13953 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13954 * results of an over-scroll operation.
13955 *
13956 * Views can use this method to handle any touch or fling-based scrolling.
13957 *
13958 * @param deltaX Change in X in pixels
13959 * @param deltaY Change in Y in pixels
13960 * @param scrollX Current X scroll value in pixels before applying deltaX
13961 * @param scrollY Current Y scroll value in pixels before applying deltaY
13962 * @param scrollRangeX Maximum content scroll range along the X axis
13963 * @param scrollRangeY Maximum content scroll range along the Y axis
13964 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13965 * along the X axis.
13966 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13967 * along the Y axis.
13968 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13969 * @return true if scrolling was clamped to an over-scroll boundary along either
13970 * axis, false otherwise.
13971 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013972 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013973 protected boolean overScrollBy(int deltaX, int deltaY,
13974 int scrollX, int scrollY,
13975 int scrollRangeX, int scrollRangeY,
13976 int maxOverScrollX, int maxOverScrollY,
13977 boolean isTouchEvent) {
13978 final int overScrollMode = mOverScrollMode;
13979 final boolean canScrollHorizontal =
13980 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13981 final boolean canScrollVertical =
13982 computeVerticalScrollRange() > computeVerticalScrollExtent();
13983 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13984 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13985 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13986 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13987
13988 int newScrollX = scrollX + deltaX;
13989 if (!overScrollHorizontal) {
13990 maxOverScrollX = 0;
13991 }
13992
13993 int newScrollY = scrollY + deltaY;
13994 if (!overScrollVertical) {
13995 maxOverScrollY = 0;
13996 }
13997
13998 // Clamp values if at the limits and record
13999 final int left = -maxOverScrollX;
14000 final int right = maxOverScrollX + scrollRangeX;
14001 final int top = -maxOverScrollY;
14002 final int bottom = maxOverScrollY + scrollRangeY;
14003
14004 boolean clampedX = false;
14005 if (newScrollX > right) {
14006 newScrollX = right;
14007 clampedX = true;
14008 } else if (newScrollX < left) {
14009 newScrollX = left;
14010 clampedX = true;
14011 }
14012
14013 boolean clampedY = false;
14014 if (newScrollY > bottom) {
14015 newScrollY = bottom;
14016 clampedY = true;
14017 } else if (newScrollY < top) {
14018 newScrollY = top;
14019 clampedY = true;
14020 }
14021
14022 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14023
14024 return clampedX || clampedY;
14025 }
14026
14027 /**
14028 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14029 * respond to the results of an over-scroll operation.
14030 *
14031 * @param scrollX New X scroll value in pixels
14032 * @param scrollY New Y scroll value in pixels
14033 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14034 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14035 */
14036 protected void onOverScrolled(int scrollX, int scrollY,
14037 boolean clampedX, boolean clampedY) {
14038 // Intentionally empty.
14039 }
14040
14041 /**
14042 * Returns the over-scroll mode for this view. The result will be
14043 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14044 * (allow over-scrolling only if the view content is larger than the container),
14045 * or {@link #OVER_SCROLL_NEVER}.
14046 *
14047 * @return This view's over-scroll mode.
14048 */
14049 public int getOverScrollMode() {
14050 return mOverScrollMode;
14051 }
14052
14053 /**
14054 * Set the over-scroll mode for this view. Valid over-scroll modes are
14055 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14056 * (allow over-scrolling only if the view content is larger than the container),
14057 * or {@link #OVER_SCROLL_NEVER}.
14058 *
14059 * Setting the over-scroll mode of a view will have an effect only if the
14060 * view is capable of scrolling.
14061 *
14062 * @param overScrollMode The new over-scroll mode for this view.
14063 */
14064 public void setOverScrollMode(int overScrollMode) {
14065 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14066 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14067 overScrollMode != OVER_SCROLL_NEVER) {
14068 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14069 }
14070 mOverScrollMode = overScrollMode;
14071 }
14072
14073 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014074 * Gets a scale factor that determines the distance the view should scroll
14075 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14076 * @return The vertical scroll scale factor.
14077 * @hide
14078 */
14079 protected float getVerticalScrollFactor() {
14080 if (mVerticalScrollFactor == 0) {
14081 TypedValue outValue = new TypedValue();
14082 if (!mContext.getTheme().resolveAttribute(
14083 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14084 throw new IllegalStateException(
14085 "Expected theme to define listPreferredItemHeight.");
14086 }
14087 mVerticalScrollFactor = outValue.getDimension(
14088 mContext.getResources().getDisplayMetrics());
14089 }
14090 return mVerticalScrollFactor;
14091 }
14092
14093 /**
14094 * Gets a scale factor that determines the distance the view should scroll
14095 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14096 * @return The horizontal scroll scale factor.
14097 * @hide
14098 */
14099 protected float getHorizontalScrollFactor() {
14100 // TODO: Should use something else.
14101 return getVerticalScrollFactor();
14102 }
14103
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014104 /**
14105 * Return the value specifying the text direction or policy that was set with
14106 * {@link #setTextDirection(int)}.
14107 *
14108 * @return the defined text direction. It can be one of:
14109 *
14110 * {@link #TEXT_DIRECTION_INHERIT},
14111 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14112 * {@link #TEXT_DIRECTION_ANY_RTL},
14113 * {@link #TEXT_DIRECTION_LTR},
14114 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014115 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014116 */
14117 public int getTextDirection() {
14118 return mTextDirection;
14119 }
14120
14121 /**
14122 * Set the text direction.
14123 *
14124 * @param textDirection the direction to set. Should be one of:
14125 *
14126 * {@link #TEXT_DIRECTION_INHERIT},
14127 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14128 * {@link #TEXT_DIRECTION_ANY_RTL},
14129 * {@link #TEXT_DIRECTION_LTR},
14130 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014131 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014132 */
14133 public void setTextDirection(int textDirection) {
14134 if (textDirection != mTextDirection) {
14135 mTextDirection = textDirection;
14136 resetResolvedTextDirection();
14137 requestLayout();
14138 }
14139 }
14140
14141 /**
14142 * Return the resolved text direction.
14143 *
14144 * @return the resolved text direction. Return one of:
14145 *
Doug Feltcb3791202011-07-07 11:57:48 -070014146 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14147 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014148 * {@link #TEXT_DIRECTION_LTR},
14149 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014150 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014151 */
14152 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014153 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014154 resolveTextDirection();
14155 }
14156 return mResolvedTextDirection;
14157 }
14158
14159 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014160 * Resolve the text direction. Will call {@link View#onResolveTextDirection()} when resolution
14161 * is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014162 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014163 public void resolveTextDirection() {
14164 if (mResolvedTextDirection != TEXT_DIRECTION_INHERIT) {
14165 // Resolution has already been done.
14166 return;
14167 }
Doug Feltcb3791202011-07-07 11:57:48 -070014168 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
14169 mResolvedTextDirection = mTextDirection;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014170 } else if (mParent != null && mParent instanceof ViewGroup) {
Doug Feltcb3791202011-07-07 11:57:48 -070014171 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014172 } else {
14173 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Doug Feltcb3791202011-07-07 11:57:48 -070014174 }
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014175 onResolveTextDirection();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014176 }
14177
14178 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014179 * Called when text direction has been resolved. Subclasses that care about text direction
14180 * resolution should override this method. The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014181 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014182 public void onResolveTextDirection() {
14183 }
14184
14185 /**
14186 * Reset resolved text direction. Text direction can be resolved with a call to
14187 * getResolvedTextDirection(). Will call {@link View#onResetResolvedTextDirection()} when
14188 * reset is done.
14189 */
14190 public void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014191 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014192 onResetResolvedTextDirection();
14193 }
14194
14195 /**
14196 * Called when text direction is reset. Subclasses that care about text direction reset should
14197 * override this method and do a reset of the text direction of their children. The default
14198 * implementation does nothing.
14199 */
14200 public void onResetResolvedTextDirection() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014201 }
14202
Chet Haaseb39f0512011-05-24 14:36:40 -070014203 //
14204 // Properties
14205 //
14206 /**
14207 * A Property wrapper around the <code>alpha</code> functionality handled by the
14208 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14209 */
Chet Haased47f1532011-12-16 11:18:52 -080014210 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014211 @Override
14212 public void setValue(View object, float value) {
14213 object.setAlpha(value);
14214 }
14215
14216 @Override
14217 public Float get(View object) {
14218 return object.getAlpha();
14219 }
14220 };
14221
14222 /**
14223 * A Property wrapper around the <code>translationX</code> functionality handled by the
14224 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14225 */
Chet Haased47f1532011-12-16 11:18:52 -080014226 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014227 @Override
14228 public void setValue(View object, float value) {
14229 object.setTranslationX(value);
14230 }
14231
14232 @Override
14233 public Float get(View object) {
14234 return object.getTranslationX();
14235 }
14236 };
14237
14238 /**
14239 * A Property wrapper around the <code>translationY</code> functionality handled by the
14240 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14241 */
Chet Haased47f1532011-12-16 11:18:52 -080014242 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014243 @Override
14244 public void setValue(View object, float value) {
14245 object.setTranslationY(value);
14246 }
14247
14248 @Override
14249 public Float get(View object) {
14250 return object.getTranslationY();
14251 }
14252 };
14253
14254 /**
14255 * A Property wrapper around the <code>x</code> functionality handled by the
14256 * {@link View#setX(float)} and {@link View#getX()} methods.
14257 */
Chet Haased47f1532011-12-16 11:18:52 -080014258 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014259 @Override
14260 public void setValue(View object, float value) {
14261 object.setX(value);
14262 }
14263
14264 @Override
14265 public Float get(View object) {
14266 return object.getX();
14267 }
14268 };
14269
14270 /**
14271 * A Property wrapper around the <code>y</code> functionality handled by the
14272 * {@link View#setY(float)} and {@link View#getY()} methods.
14273 */
Chet Haased47f1532011-12-16 11:18:52 -080014274 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014275 @Override
14276 public void setValue(View object, float value) {
14277 object.setY(value);
14278 }
14279
14280 @Override
14281 public Float get(View object) {
14282 return object.getY();
14283 }
14284 };
14285
14286 /**
14287 * A Property wrapper around the <code>rotation</code> functionality handled by the
14288 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14289 */
Chet Haased47f1532011-12-16 11:18:52 -080014290 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014291 @Override
14292 public void setValue(View object, float value) {
14293 object.setRotation(value);
14294 }
14295
14296 @Override
14297 public Float get(View object) {
14298 return object.getRotation();
14299 }
14300 };
14301
14302 /**
14303 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14304 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14305 */
Chet Haased47f1532011-12-16 11:18:52 -080014306 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014307 @Override
14308 public void setValue(View object, float value) {
14309 object.setRotationX(value);
14310 }
14311
14312 @Override
14313 public Float get(View object) {
14314 return object.getRotationX();
14315 }
14316 };
14317
14318 /**
14319 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14320 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14321 */
Chet Haased47f1532011-12-16 11:18:52 -080014322 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014323 @Override
14324 public void setValue(View object, float value) {
14325 object.setRotationY(value);
14326 }
14327
14328 @Override
14329 public Float get(View object) {
14330 return object.getRotationY();
14331 }
14332 };
14333
14334 /**
14335 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14336 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14337 */
Chet Haased47f1532011-12-16 11:18:52 -080014338 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014339 @Override
14340 public void setValue(View object, float value) {
14341 object.setScaleX(value);
14342 }
14343
14344 @Override
14345 public Float get(View object) {
14346 return object.getScaleX();
14347 }
14348 };
14349
14350 /**
14351 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14352 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14353 */
Chet Haased47f1532011-12-16 11:18:52 -080014354 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014355 @Override
14356 public void setValue(View object, float value) {
14357 object.setScaleY(value);
14358 }
14359
14360 @Override
14361 public Float get(View object) {
14362 return object.getScaleY();
14363 }
14364 };
14365
Jeff Brown33bbfd22011-02-24 20:55:35 -080014366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014367 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14368 * Each MeasureSpec represents a requirement for either the width or the height.
14369 * A MeasureSpec is comprised of a size and a mode. There are three possible
14370 * modes:
14371 * <dl>
14372 * <dt>UNSPECIFIED</dt>
14373 * <dd>
14374 * The parent has not imposed any constraint on the child. It can be whatever size
14375 * it wants.
14376 * </dd>
14377 *
14378 * <dt>EXACTLY</dt>
14379 * <dd>
14380 * The parent has determined an exact size for the child. The child is going to be
14381 * given those bounds regardless of how big it wants to be.
14382 * </dd>
14383 *
14384 * <dt>AT_MOST</dt>
14385 * <dd>
14386 * The child can be as large as it wants up to the specified size.
14387 * </dd>
14388 * </dl>
14389 *
14390 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14391 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14392 */
14393 public static class MeasureSpec {
14394 private static final int MODE_SHIFT = 30;
14395 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14396
14397 /**
14398 * Measure specification mode: The parent has not imposed any constraint
14399 * on the child. It can be whatever size it wants.
14400 */
14401 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14402
14403 /**
14404 * Measure specification mode: The parent has determined an exact size
14405 * for the child. The child is going to be given those bounds regardless
14406 * of how big it wants to be.
14407 */
14408 public static final int EXACTLY = 1 << MODE_SHIFT;
14409
14410 /**
14411 * Measure specification mode: The child can be as large as it wants up
14412 * to the specified size.
14413 */
14414 public static final int AT_MOST = 2 << MODE_SHIFT;
14415
14416 /**
14417 * Creates a measure specification based on the supplied size and mode.
14418 *
14419 * The mode must always be one of the following:
14420 * <ul>
14421 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14422 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14423 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14424 * </ul>
14425 *
14426 * @param size the size of the measure specification
14427 * @param mode the mode of the measure specification
14428 * @return the measure specification based on size and mode
14429 */
14430 public static int makeMeasureSpec(int size, int mode) {
14431 return size + mode;
14432 }
14433
14434 /**
14435 * Extracts the mode from the supplied measure specification.
14436 *
14437 * @param measureSpec the measure specification to extract the mode from
14438 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14439 * {@link android.view.View.MeasureSpec#AT_MOST} or
14440 * {@link android.view.View.MeasureSpec#EXACTLY}
14441 */
14442 public static int getMode(int measureSpec) {
14443 return (measureSpec & MODE_MASK);
14444 }
14445
14446 /**
14447 * Extracts the size from the supplied measure specification.
14448 *
14449 * @param measureSpec the measure specification to extract the size from
14450 * @return the size in pixels defined in the supplied measure specification
14451 */
14452 public static int getSize(int measureSpec) {
14453 return (measureSpec & ~MODE_MASK);
14454 }
14455
14456 /**
14457 * Returns a String representation of the specified measure
14458 * specification.
14459 *
14460 * @param measureSpec the measure specification to convert to a String
14461 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14462 */
14463 public static String toString(int measureSpec) {
14464 int mode = getMode(measureSpec);
14465 int size = getSize(measureSpec);
14466
14467 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14468
14469 if (mode == UNSPECIFIED)
14470 sb.append("UNSPECIFIED ");
14471 else if (mode == EXACTLY)
14472 sb.append("EXACTLY ");
14473 else if (mode == AT_MOST)
14474 sb.append("AT_MOST ");
14475 else
14476 sb.append(mode).append(" ");
14477
14478 sb.append(size);
14479 return sb.toString();
14480 }
14481 }
14482
14483 class CheckForLongPress implements Runnable {
14484
14485 private int mOriginalWindowAttachCount;
14486
14487 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014488 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014489 && mOriginalWindowAttachCount == mWindowAttachCount) {
14490 if (performLongClick()) {
14491 mHasPerformedLongPress = true;
14492 }
14493 }
14494 }
14495
14496 public void rememberWindowAttachCount() {
14497 mOriginalWindowAttachCount = mWindowAttachCount;
14498 }
14499 }
Joe Malin32736f02011-01-19 16:14:20 -080014500
Adam Powelle14579b2009-12-16 18:39:52 -080014501 private final class CheckForTap implements Runnable {
14502 public void run() {
14503 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014504 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014505 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014506 }
14507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014508
Adam Powella35d7682010-03-12 14:48:13 -080014509 private final class PerformClick implements Runnable {
14510 public void run() {
14511 performClick();
14512 }
14513 }
14514
Dianne Hackborn63042d62011-01-26 18:56:29 -080014515 /** @hide */
14516 public void hackTurnOffWindowResizeAnim(boolean off) {
14517 mAttachInfo.mTurnOffWindowResizeAnim = off;
14518 }
Joe Malin32736f02011-01-19 16:14:20 -080014519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014520 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014521 * This method returns a ViewPropertyAnimator object, which can be used to animate
14522 * specific properties on this View.
14523 *
14524 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14525 */
14526 public ViewPropertyAnimator animate() {
14527 if (mAnimator == null) {
14528 mAnimator = new ViewPropertyAnimator(this);
14529 }
14530 return mAnimator;
14531 }
14532
14533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014534 * Interface definition for a callback to be invoked when a key event is
14535 * dispatched to this view. The callback will be invoked before the key
14536 * event is given to the view.
14537 */
14538 public interface OnKeyListener {
14539 /**
14540 * Called when a key is dispatched to a view. This allows listeners to
14541 * get a chance to respond before the target view.
14542 *
14543 * @param v The view the key has been dispatched to.
14544 * @param keyCode The code for the physical key that was pressed
14545 * @param event The KeyEvent object containing full information about
14546 * the event.
14547 * @return True if the listener has consumed the event, false otherwise.
14548 */
14549 boolean onKey(View v, int keyCode, KeyEvent event);
14550 }
14551
14552 /**
14553 * Interface definition for a callback to be invoked when a touch event is
14554 * dispatched to this view. The callback will be invoked before the touch
14555 * event is given to the view.
14556 */
14557 public interface OnTouchListener {
14558 /**
14559 * Called when a touch event is dispatched to a view. This allows listeners to
14560 * get a chance to respond before the target view.
14561 *
14562 * @param v The view the touch event has been dispatched to.
14563 * @param event The MotionEvent object containing full information about
14564 * the event.
14565 * @return True if the listener has consumed the event, false otherwise.
14566 */
14567 boolean onTouch(View v, MotionEvent event);
14568 }
14569
14570 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014571 * Interface definition for a callback to be invoked when a hover event is
14572 * dispatched to this view. The callback will be invoked before the hover
14573 * event is given to the view.
14574 */
14575 public interface OnHoverListener {
14576 /**
14577 * Called when a hover event is dispatched to a view. This allows listeners to
14578 * get a chance to respond before the target view.
14579 *
14580 * @param v The view the hover event has been dispatched to.
14581 * @param event The MotionEvent object containing full information about
14582 * the event.
14583 * @return True if the listener has consumed the event, false otherwise.
14584 */
14585 boolean onHover(View v, MotionEvent event);
14586 }
14587
14588 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014589 * Interface definition for a callback to be invoked when a generic motion event is
14590 * dispatched to this view. The callback will be invoked before the generic motion
14591 * event is given to the view.
14592 */
14593 public interface OnGenericMotionListener {
14594 /**
14595 * Called when a generic motion event is dispatched to a view. This allows listeners to
14596 * get a chance to respond before the target view.
14597 *
14598 * @param v The view the generic motion event has been dispatched to.
14599 * @param event The MotionEvent object containing full information about
14600 * the event.
14601 * @return True if the listener has consumed the event, false otherwise.
14602 */
14603 boolean onGenericMotion(View v, MotionEvent event);
14604 }
14605
14606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014607 * Interface definition for a callback to be invoked when a view has been clicked and held.
14608 */
14609 public interface OnLongClickListener {
14610 /**
14611 * Called when a view has been clicked and held.
14612 *
14613 * @param v The view that was clicked and held.
14614 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014615 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014616 */
14617 boolean onLongClick(View v);
14618 }
14619
14620 /**
Chris Tate32affef2010-10-18 15:29:21 -070014621 * Interface definition for a callback to be invoked when a drag is being dispatched
14622 * to this view. The callback will be invoked before the hosting view's own
14623 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14624 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014625 *
14626 * <div class="special reference">
14627 * <h3>Developer Guides</h3>
14628 * <p>For a guide to implementing drag and drop features, read the
14629 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14630 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014631 */
14632 public interface OnDragListener {
14633 /**
14634 * Called when a drag event is dispatched to a view. This allows listeners
14635 * to get a chance to override base View behavior.
14636 *
Joe Malin32736f02011-01-19 16:14:20 -080014637 * @param v The View that received the drag event.
14638 * @param event The {@link android.view.DragEvent} object for the drag event.
14639 * @return {@code true} if the drag event was handled successfully, or {@code false}
14640 * if the drag event was not handled. Note that {@code false} will trigger the View
14641 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014642 */
14643 boolean onDrag(View v, DragEvent event);
14644 }
14645
14646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014647 * Interface definition for a callback to be invoked when the focus state of
14648 * a view changed.
14649 */
14650 public interface OnFocusChangeListener {
14651 /**
14652 * Called when the focus state of a view has changed.
14653 *
14654 * @param v The view whose state has changed.
14655 * @param hasFocus The new focus state of v.
14656 */
14657 void onFocusChange(View v, boolean hasFocus);
14658 }
14659
14660 /**
14661 * Interface definition for a callback to be invoked when a view is clicked.
14662 */
14663 public interface OnClickListener {
14664 /**
14665 * Called when a view has been clicked.
14666 *
14667 * @param v The view that was clicked.
14668 */
14669 void onClick(View v);
14670 }
14671
14672 /**
14673 * Interface definition for a callback to be invoked when the context menu
14674 * for this view is being built.
14675 */
14676 public interface OnCreateContextMenuListener {
14677 /**
14678 * Called when the context menu for this view is being built. It is not
14679 * safe to hold onto the menu after this method returns.
14680 *
14681 * @param menu The context menu that is being built
14682 * @param v The view for which the context menu is being built
14683 * @param menuInfo Extra information about the item for which the
14684 * context menu should be shown. This information will vary
14685 * depending on the class of v.
14686 */
14687 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14688 }
14689
Joe Onorato664644d2011-01-23 17:53:23 -080014690 /**
14691 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014692 * visibility. This reports <strong>global</strong> changes to the system UI
14693 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014694 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014695 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014696 */
14697 public interface OnSystemUiVisibilityChangeListener {
14698 /**
14699 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014700 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014701 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014702 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014703 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14704 * <strong>global</strong> state of the UI visibility flags, not what your
14705 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014706 */
14707 public void onSystemUiVisibilityChange(int visibility);
14708 }
14709
Adam Powell4afd62b2011-02-18 15:02:18 -080014710 /**
14711 * Interface definition for a callback to be invoked when this view is attached
14712 * or detached from its window.
14713 */
14714 public interface OnAttachStateChangeListener {
14715 /**
14716 * Called when the view is attached to a window.
14717 * @param v The view that was attached
14718 */
14719 public void onViewAttachedToWindow(View v);
14720 /**
14721 * Called when the view is detached from a window.
14722 * @param v The view that was detached
14723 */
14724 public void onViewDetachedFromWindow(View v);
14725 }
14726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014727 private final class UnsetPressedState implements Runnable {
14728 public void run() {
14729 setPressed(false);
14730 }
14731 }
14732
14733 /**
14734 * Base class for derived classes that want to save and restore their own
14735 * state in {@link android.view.View#onSaveInstanceState()}.
14736 */
14737 public static class BaseSavedState extends AbsSavedState {
14738 /**
14739 * Constructor used when reading from a parcel. Reads the state of the superclass.
14740 *
14741 * @param source
14742 */
14743 public BaseSavedState(Parcel source) {
14744 super(source);
14745 }
14746
14747 /**
14748 * Constructor called by derived classes when creating their SavedState objects
14749 *
14750 * @param superState The state of the superclass of this view
14751 */
14752 public BaseSavedState(Parcelable superState) {
14753 super(superState);
14754 }
14755
14756 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14757 new Parcelable.Creator<BaseSavedState>() {
14758 public BaseSavedState createFromParcel(Parcel in) {
14759 return new BaseSavedState(in);
14760 }
14761
14762 public BaseSavedState[] newArray(int size) {
14763 return new BaseSavedState[size];
14764 }
14765 };
14766 }
14767
14768 /**
14769 * A set of information given to a view when it is attached to its parent
14770 * window.
14771 */
14772 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014773 interface Callbacks {
14774 void playSoundEffect(int effectId);
14775 boolean performHapticFeedback(int effectId, boolean always);
14776 }
14777
14778 /**
14779 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14780 * to a Handler. This class contains the target (View) to invalidate and
14781 * the coordinates of the dirty rectangle.
14782 *
14783 * For performance purposes, this class also implements a pool of up to
14784 * POOL_LIMIT objects that get reused. This reduces memory allocations
14785 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014786 */
Romain Guyd928d682009-03-31 17:52:16 -070014787 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014788 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014789 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14790 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014791 public InvalidateInfo newInstance() {
14792 return new InvalidateInfo();
14793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014794
Romain Guyd928d682009-03-31 17:52:16 -070014795 public void onAcquired(InvalidateInfo element) {
14796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014797
Romain Guyd928d682009-03-31 17:52:16 -070014798 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014799 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014800 }
14801 }, POOL_LIMIT)
14802 );
14803
14804 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014805 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014806
14807 View target;
14808
14809 int left;
14810 int top;
14811 int right;
14812 int bottom;
14813
Romain Guyd928d682009-03-31 17:52:16 -070014814 public void setNextPoolable(InvalidateInfo element) {
14815 mNext = element;
14816 }
14817
14818 public InvalidateInfo getNextPoolable() {
14819 return mNext;
14820 }
14821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014822 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014823 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014824 }
14825
14826 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014827 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014828 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014829
14830 public boolean isPooled() {
14831 return mIsPooled;
14832 }
14833
14834 public void setPooled(boolean isPooled) {
14835 mIsPooled = isPooled;
14836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014837 }
14838
14839 final IWindowSession mSession;
14840
14841 final IWindow mWindow;
14842
14843 final IBinder mWindowToken;
14844
14845 final Callbacks mRootCallbacks;
14846
Romain Guy59a12ca2011-06-09 17:48:21 -070014847 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014849 /**
14850 * The top view of the hierarchy.
14851 */
14852 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014854 IBinder mPanelParentWindowToken;
14855 Surface mSurface;
14856
Romain Guyb051e892010-09-28 19:09:36 -070014857 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014858 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014859 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014861 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014862 * Scale factor used by the compatibility mode
14863 */
14864 float mApplicationScale;
14865
14866 /**
14867 * Indicates whether the application is in compatibility mode
14868 */
14869 boolean mScalingRequired;
14870
14871 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014872 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014873 */
14874 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014875
Dianne Hackborn63042d62011-01-26 18:56:29 -080014876 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014877 * Left position of this view's window
14878 */
14879 int mWindowLeft;
14880
14881 /**
14882 * Top position of this view's window
14883 */
14884 int mWindowTop;
14885
14886 /**
Adam Powell26153a32010-11-08 15:22:27 -080014887 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014888 */
Adam Powell26153a32010-11-08 15:22:27 -080014889 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014890
14891 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014892 * For windows that are full-screen but using insets to layout inside
14893 * of the screen decorations, these are the current insets for the
14894 * content of the window.
14895 */
14896 final Rect mContentInsets = new Rect();
14897
14898 /**
14899 * For windows that are full-screen but using insets to layout inside
14900 * of the screen decorations, these are the current insets for the
14901 * actual visible parts of the window.
14902 */
14903 final Rect mVisibleInsets = new Rect();
14904
14905 /**
14906 * The internal insets given by this window. This value is
14907 * supplied by the client (through
14908 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14909 * be given to the window manager when changed to be used in laying
14910 * out windows behind it.
14911 */
14912 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14913 = new ViewTreeObserver.InternalInsetsInfo();
14914
14915 /**
14916 * All views in the window's hierarchy that serve as scroll containers,
14917 * used to determine if the window can be resized or must be panned
14918 * to adjust for a soft input area.
14919 */
14920 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14921
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014922 final KeyEvent.DispatcherState mKeyDispatchState
14923 = new KeyEvent.DispatcherState();
14924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014925 /**
14926 * Indicates whether the view's window currently has the focus.
14927 */
14928 boolean mHasWindowFocus;
14929
14930 /**
14931 * The current visibility of the window.
14932 */
14933 int mWindowVisibility;
14934
14935 /**
14936 * Indicates the time at which drawing started to occur.
14937 */
14938 long mDrawingTime;
14939
14940 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014941 * Indicates whether or not ignoring the DIRTY_MASK flags.
14942 */
14943 boolean mIgnoreDirtyState;
14944
14945 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014946 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14947 * to avoid clearing that flag prematurely.
14948 */
14949 boolean mSetIgnoreDirtyState = false;
14950
14951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014952 * Indicates whether the view's window is currently in touch mode.
14953 */
14954 boolean mInTouchMode;
14955
14956 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014957 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014958 * the next time it performs a traversal
14959 */
14960 boolean mRecomputeGlobalAttributes;
14961
14962 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014963 * Always report new attributes at next traversal.
14964 */
14965 boolean mForceReportNewAttributes;
14966
14967 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014968 * Set during a traveral if any views want to keep the screen on.
14969 */
14970 boolean mKeepScreenOn;
14971
14972 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014973 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14974 */
14975 int mSystemUiVisibility;
14976
14977 /**
14978 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14979 * attached.
14980 */
14981 boolean mHasSystemUiListeners;
14982
14983 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014984 * Set if the visibility of any views has changed.
14985 */
14986 boolean mViewVisibilityChanged;
14987
14988 /**
14989 * Set to true if a view has been scrolled.
14990 */
14991 boolean mViewScrollChanged;
14992
14993 /**
14994 * Global to the view hierarchy used as a temporary for dealing with
14995 * x/y points in the transparent region computations.
14996 */
14997 final int[] mTransparentLocation = new int[2];
14998
14999 /**
15000 * Global to the view hierarchy used as a temporary for dealing with
15001 * x/y points in the ViewGroup.invalidateChild implementation.
15002 */
15003 final int[] mInvalidateChildLocation = new int[2];
15004
Chet Haasec3aa3612010-06-17 08:50:37 -070015005
15006 /**
15007 * Global to the view hierarchy used as a temporary for dealing with
15008 * x/y location when view is transformed.
15009 */
15010 final float[] mTmpTransformLocation = new float[2];
15011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015012 /**
15013 * The view tree observer used to dispatch global events like
15014 * layout, pre-draw, touch mode change, etc.
15015 */
15016 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15017
15018 /**
15019 * A Canvas used by the view hierarchy to perform bitmap caching.
15020 */
15021 Canvas mCanvas;
15022
15023 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015024 * The view root impl.
15025 */
15026 final ViewRootImpl mViewRootImpl;
15027
15028 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015029 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015030 * handler can be used to pump events in the UI events queue.
15031 */
15032 final Handler mHandler;
15033
15034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015035 * Temporary for use in computing invalidate rectangles while
15036 * calling up the hierarchy.
15037 */
15038 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015039
15040 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015041 * Temporary for use in computing hit areas with transformed views
15042 */
15043 final RectF mTmpTransformRect = new RectF();
15044
15045 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015046 * Temporary list for use in collecting focusable descendents of a view.
15047 */
15048 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015050 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015051 * The id of the window for accessibility purposes.
15052 */
15053 int mAccessibilityWindowId = View.NO_ID;
15054
15055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015056 * Creates a new set of attachment information with the specified
15057 * events handler and thread.
15058 *
15059 * @param handler the events handler the view must use
15060 */
15061 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015062 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015063 mSession = session;
15064 mWindow = window;
15065 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015066 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015067 mHandler = handler;
15068 mRootCallbacks = effectPlayer;
15069 }
15070 }
15071
15072 /**
15073 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15074 * is supported. This avoids keeping too many unused fields in most
15075 * instances of View.</p>
15076 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015077 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015078
Mike Cleronf116bf82009-09-27 19:14:12 -070015079 /**
15080 * Scrollbars are not visible
15081 */
15082 public static final int OFF = 0;
15083
15084 /**
15085 * Scrollbars are visible
15086 */
15087 public static final int ON = 1;
15088
15089 /**
15090 * Scrollbars are fading away
15091 */
15092 public static final int FADING = 2;
15093
15094 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015096 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015097 public int scrollBarDefaultDelayBeforeFade;
15098 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015099
15100 public int scrollBarSize;
15101 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015102 public float[] interpolatorValues;
15103 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015104
15105 public final Paint paint;
15106 public final Matrix matrix;
15107 public Shader shader;
15108
Mike Cleronf116bf82009-09-27 19:14:12 -070015109 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15110
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015111 private static final float[] OPAQUE = { 255 };
15112 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015113
Mike Cleronf116bf82009-09-27 19:14:12 -070015114 /**
15115 * When fading should start. This time moves into the future every time
15116 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15117 */
15118 public long fadeStartTime;
15119
15120
15121 /**
15122 * The current state of the scrollbars: ON, OFF, or FADING
15123 */
15124 public int state = OFF;
15125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015126 private int mLastColor;
15127
Mike Cleronf116bf82009-09-27 19:14:12 -070015128 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015129 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15130 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015131 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15132 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015133
15134 paint = new Paint();
15135 matrix = new Matrix();
15136 // use use a height of 1, and then wack the matrix each time we
15137 // actually use it.
15138 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015140 paint.setShader(shader);
15141 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015142 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015143 }
Romain Guy8506ab42009-06-11 17:35:47 -070015144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015145 public void setFadeColor(int color) {
15146 if (color != 0 && color != mLastColor) {
15147 mLastColor = color;
15148 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015149
Romain Guye55e1a72009-08-27 10:42:26 -070015150 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15151 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015153 paint.setShader(shader);
15154 // Restore the default transfer mode (src_over)
15155 paint.setXfermode(null);
15156 }
15157 }
Joe Malin32736f02011-01-19 16:14:20 -080015158
Mike Cleronf116bf82009-09-27 19:14:12 -070015159 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015160 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015161 if (now >= fadeStartTime) {
15162
15163 // the animation fades the scrollbars out by changing
15164 // the opacity (alpha) from fully opaque to fully
15165 // transparent
15166 int nextFrame = (int) now;
15167 int framesCount = 0;
15168
15169 Interpolator interpolator = scrollBarInterpolator;
15170
15171 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015172 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015173
15174 // End transparent
15175 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015176 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015177
15178 state = FADING;
15179
15180 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015181 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015182 }
15183 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015184 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015185
Svetoslav Ganova0156172011-06-26 17:55:44 -070015186 /**
15187 * Resuable callback for sending
15188 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15189 */
15190 private class SendViewScrolledAccessibilityEvent implements Runnable {
15191 public volatile boolean mIsPending;
15192
15193 public void run() {
15194 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15195 mIsPending = false;
15196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015197 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015198
15199 /**
15200 * <p>
15201 * This class represents a delegate that can be registered in a {@link View}
15202 * to enhance accessibility support via composition rather via inheritance.
15203 * It is specifically targeted to widget developers that extend basic View
15204 * classes i.e. classes in package android.view, that would like their
15205 * applications to be backwards compatible.
15206 * </p>
15207 * <p>
15208 * A scenario in which a developer would like to use an accessibility delegate
15209 * is overriding a method introduced in a later API version then the minimal API
15210 * version supported by the application. For example, the method
15211 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15212 * in API version 4 when the accessibility APIs were first introduced. If a
15213 * developer would like his application to run on API version 4 devices (assuming
15214 * all other APIs used by the application are version 4 or lower) and take advantage
15215 * of this method, instead of overriding the method which would break the application's
15216 * backwards compatibility, he can override the corresponding method in this
15217 * delegate and register the delegate in the target View if the API version of
15218 * the system is high enough i.e. the API version is same or higher to the API
15219 * version that introduced
15220 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15221 * </p>
15222 * <p>
15223 * Here is an example implementation:
15224 * </p>
15225 * <code><pre><p>
15226 * if (Build.VERSION.SDK_INT >= 14) {
15227 * // If the API version is equal of higher than the version in
15228 * // which onInitializeAccessibilityNodeInfo was introduced we
15229 * // register a delegate with a customized implementation.
15230 * View view = findViewById(R.id.view_id);
15231 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15232 * public void onInitializeAccessibilityNodeInfo(View host,
15233 * AccessibilityNodeInfo info) {
15234 * // Let the default implementation populate the info.
15235 * super.onInitializeAccessibilityNodeInfo(host, info);
15236 * // Set some other information.
15237 * info.setEnabled(host.isEnabled());
15238 * }
15239 * });
15240 * }
15241 * </code></pre></p>
15242 * <p>
15243 * This delegate contains methods that correspond to the accessibility methods
15244 * in View. If a delegate has been specified the implementation in View hands
15245 * off handling to the corresponding method in this delegate. The default
15246 * implementation the delegate methods behaves exactly as the corresponding
15247 * method in View for the case of no accessibility delegate been set. Hence,
15248 * to customize the behavior of a View method, clients can override only the
15249 * corresponding delegate method without altering the behavior of the rest
15250 * accessibility related methods of the host view.
15251 * </p>
15252 */
15253 public static class AccessibilityDelegate {
15254
15255 /**
15256 * Sends an accessibility event of the given type. If accessibility is not
15257 * enabled this method has no effect.
15258 * <p>
15259 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15260 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15261 * been set.
15262 * </p>
15263 *
15264 * @param host The View hosting the delegate.
15265 * @param eventType The type of the event to send.
15266 *
15267 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15268 */
15269 public void sendAccessibilityEvent(View host, int eventType) {
15270 host.sendAccessibilityEventInternal(eventType);
15271 }
15272
15273 /**
15274 * Sends an accessibility event. This method behaves exactly as
15275 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15276 * empty {@link AccessibilityEvent} and does not perform a check whether
15277 * accessibility is enabled.
15278 * <p>
15279 * The default implementation behaves as
15280 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15281 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15282 * the case of no accessibility delegate been set.
15283 * </p>
15284 *
15285 * @param host The View hosting the delegate.
15286 * @param event The event to send.
15287 *
15288 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15289 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15290 */
15291 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15292 host.sendAccessibilityEventUncheckedInternal(event);
15293 }
15294
15295 /**
15296 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15297 * to its children for adding their text content to the event.
15298 * <p>
15299 * The default implementation behaves as
15300 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15301 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15302 * the case of no accessibility delegate been set.
15303 * </p>
15304 *
15305 * @param host The View hosting the delegate.
15306 * @param event The event.
15307 * @return True if the event population was completed.
15308 *
15309 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15310 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15311 */
15312 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15313 return host.dispatchPopulateAccessibilityEventInternal(event);
15314 }
15315
15316 /**
15317 * Gives a chance to the host View to populate the accessibility event with its
15318 * text content.
15319 * <p>
15320 * The default implementation behaves as
15321 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15322 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15323 * the case of no accessibility delegate been set.
15324 * </p>
15325 *
15326 * @param host The View hosting the delegate.
15327 * @param event The accessibility event which to populate.
15328 *
15329 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15330 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15331 */
15332 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15333 host.onPopulateAccessibilityEventInternal(event);
15334 }
15335
15336 /**
15337 * Initializes an {@link AccessibilityEvent} with information about the
15338 * the host View which is the event source.
15339 * <p>
15340 * The default implementation behaves as
15341 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15342 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15343 * the case of no accessibility delegate been set.
15344 * </p>
15345 *
15346 * @param host The View hosting the delegate.
15347 * @param event The event to initialize.
15348 *
15349 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15350 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15351 */
15352 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15353 host.onInitializeAccessibilityEventInternal(event);
15354 }
15355
15356 /**
15357 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15358 * <p>
15359 * The default implementation behaves as
15360 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15361 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15362 * the case of no accessibility delegate been set.
15363 * </p>
15364 *
15365 * @param host The View hosting the delegate.
15366 * @param info The instance to initialize.
15367 *
15368 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15369 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15370 */
15371 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15372 host.onInitializeAccessibilityNodeInfoInternal(info);
15373 }
15374
15375 /**
15376 * Called when a child of the host View has requested sending an
15377 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15378 * to augment the event.
15379 * <p>
15380 * The default implementation behaves as
15381 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15382 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15383 * the case of no accessibility delegate been set.
15384 * </p>
15385 *
15386 * @param host The View hosting the delegate.
15387 * @param child The child which requests sending the event.
15388 * @param event The event to be sent.
15389 * @return True if the event should be sent
15390 *
15391 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15392 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15393 */
15394 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15395 AccessibilityEvent event) {
15396 return host.onRequestSendAccessibilityEventInternal(child, event);
15397 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015398
15399 /**
15400 * Gets the provider for managing a virtual view hierarchy rooted at this View
15401 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15402 * that explore the window content.
15403 * <p>
15404 * The default implementation behaves as
15405 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15406 * the case of no accessibility delegate been set.
15407 * </p>
15408 *
15409 * @return The provider.
15410 *
15411 * @see AccessibilityNodeProvider
15412 */
15413 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15414 return null;
15415 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015417}