blob: 5c93a4297f94dbe2310c31a2cc20b639f05d9e67 [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;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.inputmethod.InputConnection;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.widget.ScrollBarDrawable;
72
Romain Guy1ef3fdb2011-09-09 15:30:30 -070073import static android.os.Build.VERSION_CODES.*;
74
Doug Feltcb3791202011-07-07 11:57:48 -070075import com.android.internal.R;
76import com.android.internal.util.Predicate;
77import com.android.internal.view.menu.MenuBuilder;
78
Christopher Tatea0374192010-10-05 13:06:41 -070079import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070080import java.lang.reflect.InvocationTargetException;
81import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
83import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070084import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080085import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * <p>
89 * This class represents the basic building block for user interface components. A View
90 * occupies a rectangular area on the screen and is responsible for drawing and
91 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070092 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
94 * are invisible containers that hold other Views (or other ViewGroups) and define
95 * their layout properties.
96 * </p>
97 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070098 * <div class="special reference">
99 * <h3>Developer Guides</h3>
100 * <p>For information about using this class to develop your application's user interface,
101 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * <a name="Using"></a>
105 * <h3>Using Views</h3>
106 * <p>
107 * All of the views in a window are arranged in a single tree. You can add views
108 * either from code or by specifying a tree of views in one or more XML layout
109 * files. There are many specialized subclasses of views that act as controls or
110 * are capable of displaying text, images, or other content.
111 * </p>
112 * <p>
113 * Once you have created a tree of views, there are typically a few types of
114 * common operations you may wish to perform:
115 * <ul>
116 * <li><strong>Set properties:</strong> for example setting the text of a
117 * {@link android.widget.TextView}. The available properties and the methods
118 * that set them will vary among the different subclasses of views. Note that
119 * properties that are known at build time can be set in the XML layout
120 * files.</li>
121 * <li><strong>Set focus:</strong> The framework will handled moving focus in
122 * response to user input. To force focus to a specific view, call
123 * {@link #requestFocus}.</li>
124 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
125 * that will be notified when something interesting happens to the view. For
126 * example, all views will let you set a listener to be notified when the view
127 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700128 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
129 * Other view subclasses offer more specialized listeners. For example, a Button
130 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * </ul>
134 * </p>
135 * <p><em>
136 * Note: The Android framework is responsible for measuring, laying out and
137 * drawing views. You should not call methods that perform these actions on
138 * views yourself unless you are actually implementing a
139 * {@link android.view.ViewGroup}.
140 * </em></p>
141 *
142 * <a name="Lifecycle"></a>
143 * <h3>Implementing a Custom View</h3>
144 *
145 * <p>
146 * To implement a custom view, you will usually begin by providing overrides for
147 * some of the standard methods that the framework calls on all views. You do
148 * not need to override all of these methods. In fact, you can start by just
149 * overriding {@link #onDraw(android.graphics.Canvas)}.
150 * <table border="2" width="85%" align="center" cellpadding="5">
151 * <thead>
152 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
153 * </thead>
154 *
155 * <tbody>
156 * <tr>
157 * <td rowspan="2">Creation</td>
158 * <td>Constructors</td>
159 * <td>There is a form of the constructor that are called when the view
160 * is created from code and a form that is called when the view is
161 * inflated from a layout file. The second form should parse and apply
162 * any attributes defined in the layout file.
163 * </td>
164 * </tr>
165 * <tr>
166 * <td><code>{@link #onFinishInflate()}</code></td>
167 * <td>Called after a view and all of its children has been inflated
168 * from XML.</td>
169 * </tr>
170 *
171 * <tr>
172 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700173 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * <td>Called to determine the size requirements for this view and all
175 * of its children.
176 * </td>
177 * </tr>
178 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700179 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <td>Called when this view should assign a size and position to all
181 * of its children.
182 * </td>
183 * </tr>
184 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700185 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * <td>Called when the size of this view has changed.
187 * </td>
188 * </tr>
189 *
190 * <tr>
191 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the view should render its content.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when a new key event occurs.
201 * </td>
202 * </tr>
203 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700204 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * <td>Called when a key up event occurs.
206 * </td>
207 * </tr>
208 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700209 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <td>Called when a trackball motion event occurs.
211 * </td>
212 * </tr>
213 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * <td>Called when a touch screen motion event occurs.
216 * </td>
217 * </tr>
218 *
219 * <tr>
220 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when the view gains or loses focus.
223 * </td>
224 * </tr>
225 *
226 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700227 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <td>Called when the window containing the view gains or loses focus.
229 * </td>
230 * </tr>
231 *
232 * <tr>
233 * <td rowspan="3">Attaching</td>
234 * <td><code>{@link #onAttachedToWindow()}</code></td>
235 * <td>Called when the view is attached to a window.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td><code>{@link #onDetachedFromWindow}</code></td>
241 * <td>Called when the view is detached from its window.
242 * </td>
243 * </tr>
244 *
245 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700246 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <td>Called when the visibility of the window containing the view
248 * has changed.
249 * </td>
250 * </tr>
251 * </tbody>
252 *
253 * </table>
254 * </p>
255 *
256 * <a name="IDs"></a>
257 * <h3>IDs</h3>
258 * Views may have an integer id associated with them. These ids are typically
259 * assigned in the layout XML files, and are used to find specific views within
260 * the view tree. A common pattern is to:
261 * <ul>
262 * <li>Define a Button in the layout file and assign it a unique ID.
263 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700264 * &lt;Button
265 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * android:layout_width="wrap_content"
267 * android:layout_height="wrap_content"
268 * android:text="@string/my_button_text"/&gt;
269 * </pre></li>
270 * <li>From the onCreate method of an Activity, find the Button
271 * <pre class="prettyprint">
272 * Button myButton = (Button) findViewById(R.id.my_button);
273 * </pre></li>
274 * </ul>
275 * <p>
276 * View IDs need not be unique throughout the tree, but it is good practice to
277 * ensure that they are at least unique within the part of the tree you are
278 * searching.
279 * </p>
280 *
281 * <a name="Position"></a>
282 * <h3>Position</h3>
283 * <p>
284 * The geometry of a view is that of a rectangle. A view has a location,
285 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
286 * two dimensions, expressed as a width and a height. The unit for location
287 * and dimensions is the pixel.
288 * </p>
289 *
290 * <p>
291 * It is possible to retrieve the location of a view by invoking the methods
292 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
293 * coordinate of the rectangle representing the view. The latter returns the
294 * top, or Y, coordinate of the rectangle representing the view. These methods
295 * both return the location of the view relative to its parent. For instance,
296 * when getLeft() returns 20, that means the view is located 20 pixels to the
297 * right of the left edge of its direct parent.
298 * </p>
299 *
300 * <p>
301 * In addition, several convenience methods are offered to avoid unnecessary
302 * computations, namely {@link #getRight()} and {@link #getBottom()}.
303 * These methods return the coordinates of the right and bottom edges of the
304 * rectangle representing the view. For instance, calling {@link #getRight()}
305 * is similar to the following computation: <code>getLeft() + getWidth()</code>
306 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
307 * </p>
308 *
309 * <a name="SizePaddingMargins"></a>
310 * <h3>Size, padding and margins</h3>
311 * <p>
312 * The size of a view is expressed with a width and a height. A view actually
313 * possess two pairs of width and height values.
314 * </p>
315 *
316 * <p>
317 * The first pair is known as <em>measured width</em> and
318 * <em>measured height</em>. These dimensions define how big a view wants to be
319 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
320 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
321 * and {@link #getMeasuredHeight()}.
322 * </p>
323 *
324 * <p>
325 * The second pair is simply known as <em>width</em> and <em>height</em>, or
326 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
327 * dimensions define the actual size of the view on screen, at drawing time and
328 * after layout. These values may, but do not have to, be different from the
329 * measured width and height. The width and height can be obtained by calling
330 * {@link #getWidth()} and {@link #getHeight()}.
331 * </p>
332 *
333 * <p>
334 * To measure its dimensions, a view takes into account its padding. The padding
335 * is expressed in pixels for the left, top, right and bottom parts of the view.
336 * Padding can be used to offset the content of the view by a specific amount of
337 * pixels. For instance, a left padding of 2 will push the view's content by
338 * 2 pixels to the right of the left edge. Padding can be set using the
339 * {@link #setPadding(int, int, int, int)} method and queried by calling
340 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700341 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 * </p>
343 *
344 * <p>
345 * Even though a view can define a padding, it does not provide any support for
346 * margins. However, view groups provide such a support. Refer to
347 * {@link android.view.ViewGroup} and
348 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
349 * </p>
350 *
351 * <a name="Layout"></a>
352 * <h3>Layout</h3>
353 * <p>
354 * Layout is a two pass process: a measure pass and a layout pass. The measuring
355 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
356 * of the view tree. Each view pushes dimension specifications down the tree
357 * during the recursion. At the end of the measure pass, every view has stored
358 * its measurements. The second pass happens in
359 * {@link #layout(int,int,int,int)} and is also top-down. During
360 * this pass each parent is responsible for positioning all of its children
361 * using the sizes computed in the measure pass.
362 * </p>
363 *
364 * <p>
365 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
366 * {@link #getMeasuredHeight()} values must be set, along with those for all of
367 * that view's descendants. A view's measured width and measured height values
368 * must respect the constraints imposed by the view's parents. This guarantees
369 * that at the end of the measure pass, all parents accept all of their
370 * children's measurements. A parent view may call measure() more than once on
371 * its children. For example, the parent may measure each child once with
372 * unspecified dimensions to find out how big they want to be, then call
373 * measure() on them again with actual numbers if the sum of all the children's
374 * unconstrained sizes is too big or too small.
375 * </p>
376 *
377 * <p>
378 * The measure pass uses two classes to communicate dimensions. The
379 * {@link MeasureSpec} class is used by views to tell their parents how they
380 * want to be measured and positioned. The base LayoutParams class just
381 * describes how big the view wants to be for both width and height. For each
382 * dimension, it can specify one of:
383 * <ul>
384 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800385 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * (minus padding)
387 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
388 * enclose its content (plus padding).
389 * </ul>
390 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
391 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
392 * an X and Y value.
393 * </p>
394 *
395 * <p>
396 * MeasureSpecs are used to push requirements down the tree from parent to
397 * child. A MeasureSpec can be in one of three modes:
398 * <ul>
399 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
400 * of a child view. For example, a LinearLayout may call measure() on its child
401 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
402 * tall the child view wants to be given a width of 240 pixels.
403 * <li>EXACTLY: This is used by the parent to impose an exact size on the
404 * child. The child must use this size, and guarantee that all of its
405 * descendants will fit within this size.
406 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
407 * child. The child must gurantee that it and all of its descendants will fit
408 * within this size.
409 * </ul>
410 * </p>
411 *
412 * <p>
413 * To intiate a layout, call {@link #requestLayout}. This method is typically
414 * called by a view on itself when it believes that is can no longer fit within
415 * its current bounds.
416 * </p>
417 *
418 * <a name="Drawing"></a>
419 * <h3>Drawing</h3>
420 * <p>
421 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700422 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * this means that parents will draw before (i.e., behind) their children, with
424 * siblings drawn in the order they appear in the tree.
425 * If you set a background drawable for a View, then the View will draw it for you
426 * before calling back to its <code>onDraw()</code> method.
427 * </p>
428 *
429 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700430 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * </p>
432 *
433 * <p>
434 * To force a view to draw, call {@link #invalidate()}.
435 * </p>
436 *
437 * <a name="EventHandlingThreading"></a>
438 * <h3>Event Handling and Threading</h3>
439 * <p>
440 * The basic cycle of a view is as follows:
441 * <ol>
442 * <li>An event comes in and is dispatched to the appropriate view. The view
443 * handles the event and notifies any listeners.</li>
444 * <li>If in the course of processing the event, the view's bounds may need
445 * to be changed, the view will call {@link #requestLayout()}.</li>
446 * <li>Similarly, if in the course of processing the event the view's appearance
447 * may need to be changed, the view will call {@link #invalidate()}.</li>
448 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
449 * the framework will take care of measuring, laying out, and drawing the tree
450 * as appropriate.</li>
451 * </ol>
452 * </p>
453 *
454 * <p><em>Note: The entire view tree is single threaded. You must always be on
455 * the UI thread when calling any method on any view.</em>
456 * If you are doing work on other threads and want to update the state of a view
457 * from that thread, you should use a {@link Handler}.
458 * </p>
459 *
460 * <a name="FocusHandling"></a>
461 * <h3>Focus Handling</h3>
462 * <p>
463 * The framework will handle routine focus movement in response to user input.
464 * This includes changing the focus as views are removed or hidden, or as new
465 * views become available. Views indicate their willingness to take focus
466 * through the {@link #isFocusable} method. To change whether a view can take
467 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
468 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
469 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
470 * </p>
471 * <p>
472 * Focus movement is based on an algorithm which finds the nearest neighbor in a
473 * given direction. In rare cases, the default algorithm may not match the
474 * intended behavior of the developer. In these situations, you can provide
475 * explicit overrides by using these XML attributes in the layout file:
476 * <pre>
477 * nextFocusDown
478 * nextFocusLeft
479 * nextFocusRight
480 * nextFocusUp
481 * </pre>
482 * </p>
483 *
484 *
485 * <p>
486 * To get a particular view to take focus, call {@link #requestFocus()}.
487 * </p>
488 *
489 * <a name="TouchMode"></a>
490 * <h3>Touch Mode</h3>
491 * <p>
492 * When a user is navigating a user interface via directional keys such as a D-pad, it is
493 * necessary to give focus to actionable items such as buttons so the user can see
494 * what will take input. If the device has touch capabilities, however, and the user
495 * begins interacting with the interface by touching it, it is no longer necessary to
496 * always highlight, or give focus to, a particular view. This motivates a mode
497 * for interaction named 'touch mode'.
498 * </p>
499 * <p>
500 * For a touch capable device, once the user touches the screen, the device
501 * will enter touch mode. From this point onward, only views for which
502 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
503 * Other views that are touchable, like buttons, will not take focus when touched; they will
504 * only fire the on click listeners.
505 * </p>
506 * <p>
507 * Any time a user hits a directional key, such as a D-pad direction, the view device will
508 * exit touch mode, and find a view to take focus, so that the user may resume interacting
509 * with the user interface without touching the screen again.
510 * </p>
511 * <p>
512 * The touch mode state is maintained across {@link android.app.Activity}s. Call
513 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
514 * </p>
515 *
516 * <a name="Scrolling"></a>
517 * <h3>Scrolling</h3>
518 * <p>
519 * The framework provides basic support for views that wish to internally
520 * scroll their content. This includes keeping track of the X and Y scroll
521 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800522 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700523 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * </p>
525 *
526 * <a name="Tags"></a>
527 * <h3>Tags</h3>
528 * <p>
529 * Unlike IDs, tags are not used to identify views. Tags are essentially an
530 * extra piece of information that can be associated with a view. They are most
531 * often used as a convenience to store data related to views in the views
532 * themselves rather than by putting them in a separate structure.
533 * </p>
534 *
535 * <a name="Animation"></a>
536 * <h3>Animation</h3>
537 * <p>
538 * You can attach an {@link Animation} object to a view using
539 * {@link #setAnimation(Animation)} or
540 * {@link #startAnimation(Animation)}. The animation can alter the scale,
541 * rotation, translation and alpha of a view over time. If the animation is
542 * attached to a view that has children, the animation will affect the entire
543 * subtree rooted by that node. When an animation is started, the framework will
544 * take care of redrawing the appropriate views until the animation completes.
545 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800546 * <p>
547 * Starting with Android 3.0, the preferred way of animating views is to use the
548 * {@link android.animation} package APIs.
549 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 *
Jeff Brown85a31762010-09-01 17:01:00 -0700551 * <a name="Security"></a>
552 * <h3>Security</h3>
553 * <p>
554 * Sometimes it is essential that an application be able to verify that an action
555 * is being performed with the full knowledge and consent of the user, such as
556 * granting a permission request, making a purchase or clicking on an advertisement.
557 * Unfortunately, a malicious application could try to spoof the user into
558 * performing these actions, unaware, by concealing the intended purpose of the view.
559 * As a remedy, the framework offers a touch filtering mechanism that can be used to
560 * improve the security of views that provide access to sensitive functionality.
561 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700562 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800563 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700564 * will discard touches that are received whenever the view's window is obscured by
565 * another visible window. As a result, the view will not receive touches whenever a
566 * toast, dialog or other window appears above the view's window.
567 * </p><p>
568 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700569 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
570 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700571 * </p>
572 *
Romain Guy171c5922011-01-06 10:04:23 -0800573 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700574 * @attr ref android.R.styleable#View_background
575 * @attr ref android.R.styleable#View_clickable
576 * @attr ref android.R.styleable#View_contentDescription
577 * @attr ref android.R.styleable#View_drawingCacheQuality
578 * @attr ref android.R.styleable#View_duplicateParentState
579 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700580 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700581 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700582 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700584 * @attr ref android.R.styleable#View_isScrollContainer
585 * @attr ref android.R.styleable#View_focusable
586 * @attr ref android.R.styleable#View_focusableInTouchMode
587 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
588 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800589 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700590 * @attr ref android.R.styleable#View_longClickable
591 * @attr ref android.R.styleable#View_minHeight
592 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * @attr ref android.R.styleable#View_nextFocusDown
594 * @attr ref android.R.styleable#View_nextFocusLeft
595 * @attr ref android.R.styleable#View_nextFocusRight
596 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700597 * @attr ref android.R.styleable#View_onClick
598 * @attr ref android.R.styleable#View_padding
599 * @attr ref android.R.styleable#View_paddingBottom
600 * @attr ref android.R.styleable#View_paddingLeft
601 * @attr ref android.R.styleable#View_paddingRight
602 * @attr ref android.R.styleable#View_paddingTop
603 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800604 * @attr ref android.R.styleable#View_rotation
605 * @attr ref android.R.styleable#View_rotationX
606 * @attr ref android.R.styleable#View_rotationY
607 * @attr ref android.R.styleable#View_scaleX
608 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 * @attr ref android.R.styleable#View_scrollX
610 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_scrollbarSize
612 * @attr ref android.R.styleable#View_scrollbarStyle
613 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700614 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
615 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
617 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollbarThumbVertical
619 * @attr ref android.R.styleable#View_scrollbarTrackVertical
620 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
621 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700622 * @attr ref android.R.styleable#View_soundEffectsEnabled
623 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800624 * @attr ref android.R.styleable#View_transformPivotX
625 * @attr ref android.R.styleable#View_transformPivotY
626 * @attr ref android.R.styleable#View_translationX
627 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700628 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 *
630 * @see android.view.ViewGroup
631 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700632public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
633 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 private static final boolean DBG = false;
635
636 /**
637 * The logging tag used by this class with android.util.Log.
638 */
639 protected static final String VIEW_LOG_TAG = "View";
640
641 /**
642 * Used to mark a View that has no ID.
643 */
644 public static final int NO_ID = -1;
645
646 /**
647 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
648 * calling setFlags.
649 */
650 private static final int NOT_FOCUSABLE = 0x00000000;
651
652 /**
653 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
654 * setFlags.
655 */
656 private static final int FOCUSABLE = 0x00000001;
657
658 /**
659 * Mask for use with setFlags indicating bits used for focus.
660 */
661 private static final int FOCUSABLE_MASK = 0x00000001;
662
663 /**
664 * This view will adjust its padding to fit sytem windows (e.g. status bar)
665 */
666 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
667
668 /**
Scott Main812634c22011-07-27 13:22:35 -0700669 * This view is visible.
670 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
671 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 */
673 public static final int VISIBLE = 0x00000000;
674
675 /**
676 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700677 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
678 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
680 public static final int INVISIBLE = 0x00000004;
681
682 /**
683 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700684 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
685 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 */
687 public static final int GONE = 0x00000008;
688
689 /**
690 * Mask for use with setFlags indicating bits used for visibility.
691 * {@hide}
692 */
693 static final int VISIBILITY_MASK = 0x0000000C;
694
695 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
696
697 /**
698 * This view is enabled. Intrepretation varies by subclass.
699 * Use with ENABLED_MASK when calling setFlags.
700 * {@hide}
701 */
702 static final int ENABLED = 0x00000000;
703
704 /**
705 * This view is disabled. Intrepretation varies by subclass.
706 * Use with ENABLED_MASK when calling setFlags.
707 * {@hide}
708 */
709 static final int DISABLED = 0x00000020;
710
711 /**
712 * Mask for use with setFlags indicating bits used for indicating whether
713 * this view is enabled
714 * {@hide}
715 */
716 static final int ENABLED_MASK = 0x00000020;
717
718 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700719 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
720 * called and further optimizations will be performed. It is okay to have
721 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 * {@hide}
723 */
724 static final int WILL_NOT_DRAW = 0x00000080;
725
726 /**
727 * Mask for use with setFlags indicating bits used for indicating whether
728 * this view is will draw
729 * {@hide}
730 */
731 static final int DRAW_MASK = 0x00000080;
732
733 /**
734 * <p>This view doesn't show scrollbars.</p>
735 * {@hide}
736 */
737 static final int SCROLLBARS_NONE = 0x00000000;
738
739 /**
740 * <p>This view shows horizontal scrollbars.</p>
741 * {@hide}
742 */
743 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
744
745 /**
746 * <p>This view shows vertical scrollbars.</p>
747 * {@hide}
748 */
749 static final int SCROLLBARS_VERTICAL = 0x00000200;
750
751 /**
752 * <p>Mask for use with setFlags indicating bits used for indicating which
753 * scrollbars are enabled.</p>
754 * {@hide}
755 */
756 static final int SCROLLBARS_MASK = 0x00000300;
757
Jeff Brown85a31762010-09-01 17:01:00 -0700758 /**
759 * Indicates that the view should filter touches when its window is obscured.
760 * Refer to the class comments for more information about this security feature.
761 * {@hide}
762 */
763 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
764
765 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766
767 /**
768 * <p>This view doesn't show fading edges.</p>
769 * {@hide}
770 */
771 static final int FADING_EDGE_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal fading edges.</p>
775 * {@hide}
776 */
777 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
778
779 /**
780 * <p>This view shows vertical fading edges.</p>
781 * {@hide}
782 */
783 static final int FADING_EDGE_VERTICAL = 0x00002000;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * fading edges are enabled.</p>
788 * {@hide}
789 */
790 static final int FADING_EDGE_MASK = 0x00003000;
791
792 /**
793 * <p>Indicates this view can be clicked. When clickable, a View reacts
794 * to clicks by notifying the OnClickListener.<p>
795 * {@hide}
796 */
797 static final int CLICKABLE = 0x00004000;
798
799 /**
800 * <p>Indicates this view is caching its drawing into a bitmap.</p>
801 * {@hide}
802 */
803 static final int DRAWING_CACHE_ENABLED = 0x00008000;
804
805 /**
806 * <p>Indicates that no icicle should be saved for this view.<p>
807 * {@hide}
808 */
809 static final int SAVE_DISABLED = 0x000010000;
810
811 /**
812 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
813 * property.</p>
814 * {@hide}
815 */
816 static final int SAVE_DISABLED_MASK = 0x000010000;
817
818 /**
819 * <p>Indicates that no drawing cache should ever be created for this view.<p>
820 * {@hide}
821 */
822 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
823
824 /**
825 * <p>Indicates this view can take / keep focus when int touch mode.</p>
826 * {@hide}
827 */
828 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
829
830 /**
831 * <p>Enables low quality mode for the drawing cache.</p>
832 */
833 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
834
835 /**
836 * <p>Enables high quality mode for the drawing cache.</p>
837 */
838 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
839
840 /**
841 * <p>Enables automatic quality mode for the drawing cache.</p>
842 */
843 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
844
845 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
846 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
847 };
848
849 /**
850 * <p>Mask for use with setFlags indicating bits used for the cache
851 * quality property.</p>
852 * {@hide}
853 */
854 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
855
856 /**
857 * <p>
858 * Indicates this view can be long clicked. When long clickable, a View
859 * reacts to long clicks by notifying the OnLongClickListener or showing a
860 * context menu.
861 * </p>
862 * {@hide}
863 */
864 static final int LONG_CLICKABLE = 0x00200000;
865
866 /**
867 * <p>Indicates that this view gets its drawable states from its direct parent
868 * and ignores its original internal states.</p>
869 *
870 * @hide
871 */
872 static final int DUPLICATE_PARENT_STATE = 0x00400000;
873
874 /**
875 * The scrollbar style to display the scrollbars inside the content area,
876 * without increasing the padding. The scrollbars will be overlaid with
877 * translucency on the view's content.
878 */
879 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
880
881 /**
882 * The scrollbar style to display the scrollbars inside the padded area,
883 * increasing the padding of the view. The scrollbars will not overlap the
884 * content area of the view.
885 */
886 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
887
888 /**
889 * The scrollbar style to display the scrollbars at the edge of the view,
890 * without increasing the padding. The scrollbars will be overlaid with
891 * translucency.
892 */
893 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
894
895 /**
896 * The scrollbar style to display the scrollbars at the edge of the view,
897 * increasing the padding of the view. The scrollbars will only overlap the
898 * background, if any.
899 */
900 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
901
902 /**
903 * Mask to check if the scrollbar style is overlay or inset.
904 * {@hide}
905 */
906 static final int SCROLLBARS_INSET_MASK = 0x01000000;
907
908 /**
909 * Mask to check if the scrollbar style is inside or outside.
910 * {@hide}
911 */
912 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
913
914 /**
915 * Mask for scrollbar style.
916 * {@hide}
917 */
918 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
919
920 /**
921 * View flag indicating that the screen should remain on while the
922 * window containing this view is visible to the user. This effectively
923 * takes care of automatically setting the WindowManager's
924 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
925 */
926 public static final int KEEP_SCREEN_ON = 0x04000000;
927
928 /**
929 * View flag indicating whether this view should have sound effects enabled
930 * for events such as clicking and touching.
931 */
932 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
933
934 /**
935 * View flag indicating whether this view should have haptic feedback
936 * enabled for events such as long presses.
937 */
938 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
939
940 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700941 * <p>Indicates that the view hierarchy should stop saving state when
942 * it reaches this view. If state saving is initiated immediately at
943 * the view, it will be allowed.
944 * {@hide}
945 */
946 static final int PARENT_SAVE_DISABLED = 0x20000000;
947
948 /**
949 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
950 * {@hide}
951 */
952 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
953
954 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800955 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700956 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800957 * {@hide}
958 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700959 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800960
961 /**
962 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700963 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800964 * {@hide}
965 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700966 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800967
968 /**
969 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700970 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800971 * {@hide}
972 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700973 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800974
975 /**
976 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700977 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800978 * {@hide}
979 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700980 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800981
982 /**
983 * Mask for use with setFlags indicating bits used for horizontalDirection.
984 * {@hide}
985 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700986 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800987
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700988 /*
989 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
990 * flag value.
991 * {@hide}
992 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700993 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
994 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800995
996 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700997 * Default horizontalDirection.
998 * {@hide}
999 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001000 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001001
1002 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001003 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1004 * should add all focusable Views regardless if they are focusable in touch mode.
1005 */
1006 public static final int FOCUSABLES_ALL = 0x00000000;
1007
1008 /**
1009 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1010 * should add only Views focusable in touch mode.
1011 */
1012 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1013
1014 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001015 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 * item.
1017 */
1018 public static final int FOCUS_BACKWARD = 0x00000001;
1019
1020 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001021 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 * item.
1023 */
1024 public static final int FOCUS_FORWARD = 0x00000002;
1025
1026 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001027 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 */
1029 public static final int FOCUS_LEFT = 0x00000011;
1030
1031 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001032 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 */
1034 public static final int FOCUS_UP = 0x00000021;
1035
1036 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001037 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 */
1039 public static final int FOCUS_RIGHT = 0x00000042;
1040
1041 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001042 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 */
1044 public static final int FOCUS_DOWN = 0x00000082;
1045
1046 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001047 * Bits of {@link #getMeasuredWidthAndState()} and
1048 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1049 */
1050 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1051
1052 /**
1053 * Bits of {@link #getMeasuredWidthAndState()} and
1054 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1055 */
1056 public static final int MEASURED_STATE_MASK = 0xff000000;
1057
1058 /**
1059 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1060 * for functions that combine both width and height into a single int,
1061 * such as {@link #getMeasuredState()} and the childState argument of
1062 * {@link #resolveSizeAndState(int, int, int)}.
1063 */
1064 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1065
1066 /**
1067 * Bit of {@link #getMeasuredWidthAndState()} and
1068 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1069 * is smaller that the space the view would like to have.
1070 */
1071 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1072
1073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 * Base View state sets
1075 */
1076 // Singles
1077 /**
1078 * Indicates the view has no states set. States are used with
1079 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1080 * view depending on its state.
1081 *
1082 * @see android.graphics.drawable.Drawable
1083 * @see #getDrawableState()
1084 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001085 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 /**
1087 * Indicates the view is enabled. States are used with
1088 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1089 * view depending on its state.
1090 *
1091 * @see android.graphics.drawable.Drawable
1092 * @see #getDrawableState()
1093 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001094 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 /**
1096 * Indicates the view is focused. States are used with
1097 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1098 * view depending on its state.
1099 *
1100 * @see android.graphics.drawable.Drawable
1101 * @see #getDrawableState()
1102 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001103 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 /**
1105 * Indicates the view is selected. States are used with
1106 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1107 * view depending on its state.
1108 *
1109 * @see android.graphics.drawable.Drawable
1110 * @see #getDrawableState()
1111 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001112 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 /**
1114 * Indicates the view is pressed. States are used with
1115 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1116 * view depending on its state.
1117 *
1118 * @see android.graphics.drawable.Drawable
1119 * @see #getDrawableState()
1120 * @hide
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view's window has focus. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 // Doubles
1133 /**
1134 * Indicates the view is enabled and has the focus.
1135 *
1136 * @see #ENABLED_STATE_SET
1137 * @see #FOCUSED_STATE_SET
1138 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001139 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 /**
1141 * Indicates the view is enabled and selected.
1142 *
1143 * @see #ENABLED_STATE_SET
1144 * @see #SELECTED_STATE_SET
1145 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001146 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 /**
1148 * Indicates the view is enabled and that its window has focus.
1149 *
1150 * @see #ENABLED_STATE_SET
1151 * @see #WINDOW_FOCUSED_STATE_SET
1152 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001153 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 /**
1155 * Indicates the view is focused and selected.
1156 *
1157 * @see #FOCUSED_STATE_SET
1158 * @see #SELECTED_STATE_SET
1159 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001160 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 /**
1162 * Indicates the view has the focus and that its window has the focus.
1163 *
1164 * @see #FOCUSED_STATE_SET
1165 * @see #WINDOW_FOCUSED_STATE_SET
1166 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001167 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 /**
1169 * Indicates the view is selected and that its window has the focus.
1170 *
1171 * @see #SELECTED_STATE_SET
1172 * @see #WINDOW_FOCUSED_STATE_SET
1173 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001174 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 // Triples
1176 /**
1177 * Indicates the view is enabled, focused and selected.
1178 *
1179 * @see #ENABLED_STATE_SET
1180 * @see #FOCUSED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled, focused and its window has the focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #FOCUSED_STATE_SET
1189 * @see #WINDOW_FOCUSED_STATE_SET
1190 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001191 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 /**
1193 * Indicates the view is enabled, selected and its window has the focus.
1194 *
1195 * @see #ENABLED_STATE_SET
1196 * @see #SELECTED_STATE_SET
1197 * @see #WINDOW_FOCUSED_STATE_SET
1198 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001199 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 /**
1201 * Indicates the view is focused, selected and its window has the focus.
1202 *
1203 * @see #FOCUSED_STATE_SET
1204 * @see #SELECTED_STATE_SET
1205 * @see #WINDOW_FOCUSED_STATE_SET
1206 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001207 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 /**
1209 * Indicates the view is enabled, focused, selected and its window
1210 * has the focus.
1211 *
1212 * @see #ENABLED_STATE_SET
1213 * @see #FOCUSED_STATE_SET
1214 * @see #SELECTED_STATE_SET
1215 * @see #WINDOW_FOCUSED_STATE_SET
1216 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001217 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 /**
1219 * Indicates the view is pressed and its window has the focus.
1220 *
1221 * @see #PRESSED_STATE_SET
1222 * @see #WINDOW_FOCUSED_STATE_SET
1223 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001224 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 /**
1226 * Indicates the view is pressed and selected.
1227 *
1228 * @see #PRESSED_STATE_SET
1229 * @see #SELECTED_STATE_SET
1230 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001231 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 /**
1233 * Indicates the view is pressed, selected and its window has the focus.
1234 *
1235 * @see #PRESSED_STATE_SET
1236 * @see #SELECTED_STATE_SET
1237 * @see #WINDOW_FOCUSED_STATE_SET
1238 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001239 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 /**
1241 * Indicates the view is pressed and focused.
1242 *
1243 * @see #PRESSED_STATE_SET
1244 * @see #FOCUSED_STATE_SET
1245 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001246 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 /**
1248 * Indicates the view is pressed, focused and its window has the focus.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #FOCUSED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed, focused and selected.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #SELECTED_STATE_SET
1260 * @see #FOCUSED_STATE_SET
1261 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001262 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 /**
1264 * Indicates the view is pressed, focused, selected and its window has the focus.
1265 *
1266 * @see #PRESSED_STATE_SET
1267 * @see #FOCUSED_STATE_SET
1268 * @see #SELECTED_STATE_SET
1269 * @see #WINDOW_FOCUSED_STATE_SET
1270 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001271 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 /**
1273 * Indicates the view is pressed and enabled.
1274 *
1275 * @see #PRESSED_STATE_SET
1276 * @see #ENABLED_STATE_SET
1277 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001278 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 /**
1280 * Indicates the view is pressed, enabled and its window has the focus.
1281 *
1282 * @see #PRESSED_STATE_SET
1283 * @see #ENABLED_STATE_SET
1284 * @see #WINDOW_FOCUSED_STATE_SET
1285 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001286 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 /**
1288 * Indicates the view is pressed, enabled and selected.
1289 *
1290 * @see #PRESSED_STATE_SET
1291 * @see #ENABLED_STATE_SET
1292 * @see #SELECTED_STATE_SET
1293 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001294 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 /**
1296 * Indicates the view is pressed, enabled, selected and its window has the
1297 * focus.
1298 *
1299 * @see #PRESSED_STATE_SET
1300 * @see #ENABLED_STATE_SET
1301 * @see #SELECTED_STATE_SET
1302 * @see #WINDOW_FOCUSED_STATE_SET
1303 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001304 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 /**
1306 * Indicates the view is pressed, enabled and focused.
1307 *
1308 * @see #PRESSED_STATE_SET
1309 * @see #ENABLED_STATE_SET
1310 * @see #FOCUSED_STATE_SET
1311 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001312 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 /**
1314 * Indicates the view is pressed, enabled, focused and its window has the
1315 * focus.
1316 *
1317 * @see #PRESSED_STATE_SET
1318 * @see #ENABLED_STATE_SET
1319 * @see #FOCUSED_STATE_SET
1320 * @see #WINDOW_FOCUSED_STATE_SET
1321 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001322 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 /**
1324 * Indicates the view is pressed, enabled, focused and selected.
1325 *
1326 * @see #PRESSED_STATE_SET
1327 * @see #ENABLED_STATE_SET
1328 * @see #SELECTED_STATE_SET
1329 * @see #FOCUSED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, focused, selected and its window
1334 * has the focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #FOCUSED_STATE_SET
1340 * @see #WINDOW_FOCUSED_STATE_SET
1341 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343
1344 /**
1345 * The order here is very important to {@link #getDrawableState()}
1346 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001347 private static final int[][] VIEW_STATE_SETS;
1348
Romain Guyb051e892010-09-28 19:09:36 -07001349 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1350 static final int VIEW_STATE_SELECTED = 1 << 1;
1351 static final int VIEW_STATE_FOCUSED = 1 << 2;
1352 static final int VIEW_STATE_ENABLED = 1 << 3;
1353 static final int VIEW_STATE_PRESSED = 1 << 4;
1354 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001355 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001356 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001357 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1358 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359
1360 static final int[] VIEW_STATE_IDS = new int[] {
1361 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1362 R.attr.state_selected, VIEW_STATE_SELECTED,
1363 R.attr.state_focused, VIEW_STATE_FOCUSED,
1364 R.attr.state_enabled, VIEW_STATE_ENABLED,
1365 R.attr.state_pressed, VIEW_STATE_PRESSED,
1366 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001367 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001368 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001369 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1370 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 };
1372
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001373 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001374 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1375 throw new IllegalStateException(
1376 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1377 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001379 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001381 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001383 orderedIds[i * 2] = viewState;
1384 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001385 }
1386 }
1387 }
Romain Guyb051e892010-09-28 19:09:36 -07001388 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1389 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1390 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001391 int numBits = Integer.bitCount(i);
1392 int[] set = new int[numBits];
1393 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001394 for (int j = 0; j < orderedIds.length; j += 2) {
1395 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396 set[pos++] = orderedIds[j];
1397 }
1398 }
1399 VIEW_STATE_SETS[i] = set;
1400 }
1401
1402 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1403 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1404 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1405 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1407 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1408 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1410 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1412 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1414 | VIEW_STATE_FOCUSED];
1415 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1416 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1418 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1420 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1422 | VIEW_STATE_ENABLED];
1423 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1425 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1427 | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1430 | VIEW_STATE_ENABLED];
1431 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1433 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1434
1435 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1436 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1438 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1440 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1442 | VIEW_STATE_PRESSED];
1443 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1445 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1447 | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1450 | VIEW_STATE_PRESSED];
1451 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1453 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1454 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1456 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1458 | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1461 | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1464 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1467 | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1470 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1473 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1476 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1477 | VIEW_STATE_PRESSED];
1478 }
1479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001481 * Accessibility event types that are dispatched for text population.
1482 */
1483 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1484 AccessibilityEvent.TYPE_VIEW_CLICKED
1485 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1486 | AccessibilityEvent.TYPE_VIEW_SELECTED
1487 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1488 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1489 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001490 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001491 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1492 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001493
1494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 * Temporary Rect currently for use in setBackground(). This will probably
1496 * be extended in the future to hold our own class with more than just
1497 * a Rect. :)
1498 */
1499 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001500
1501 /**
1502 * Map used to store views' tags.
1503 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001504 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001507 * The next available accessiiblity id.
1508 */
1509 private static int sNextAccessibilityViewId;
1510
1511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 * The animation currently associated with this view.
1513 * @hide
1514 */
1515 protected Animation mCurrentAnimation = null;
1516
1517 /**
1518 * Width as measured during measure pass.
1519 * {@hide}
1520 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001521 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001522 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523
1524 /**
1525 * Height as measured during measure pass.
1526 * {@hide}
1527 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001528 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001529 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530
1531 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001532 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1533 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1534 * its display list. This flag, used only when hw accelerated, allows us to clear the
1535 * flag while retaining this information until it's needed (at getDisplayList() time and
1536 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1537 *
1538 * {@hide}
1539 */
1540 boolean mRecreateDisplayList = false;
1541
1542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 * The view's identifier.
1544 * {@hide}
1545 *
1546 * @see #setId(int)
1547 * @see #getId()
1548 */
1549 @ViewDebug.ExportedProperty(resolveId = true)
1550 int mID = NO_ID;
1551
1552 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001553 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001554 */
1555 int mAccessibilityViewId = NO_ID;
1556
1557 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 * The view's tag.
1559 * {@hide}
1560 *
1561 * @see #setTag(Object)
1562 * @see #getTag()
1563 */
1564 protected Object mTag;
1565
1566 // for mPrivateFlags:
1567 /** {@hide} */
1568 static final int WANTS_FOCUS = 0x00000001;
1569 /** {@hide} */
1570 static final int FOCUSED = 0x00000002;
1571 /** {@hide} */
1572 static final int SELECTED = 0x00000004;
1573 /** {@hide} */
1574 static final int IS_ROOT_NAMESPACE = 0x00000008;
1575 /** {@hide} */
1576 static final int HAS_BOUNDS = 0x00000010;
1577 /** {@hide} */
1578 static final int DRAWN = 0x00000020;
1579 /**
1580 * When this flag is set, this view is running an animation on behalf of its
1581 * children and should therefore not cancel invalidate requests, even if they
1582 * lie outside of this view's bounds.
1583 *
1584 * {@hide}
1585 */
1586 static final int DRAW_ANIMATION = 0x00000040;
1587 /** {@hide} */
1588 static final int SKIP_DRAW = 0x00000080;
1589 /** {@hide} */
1590 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1591 /** {@hide} */
1592 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1593 /** {@hide} */
1594 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1595 /** {@hide} */
1596 static final int MEASURED_DIMENSION_SET = 0x00000800;
1597 /** {@hide} */
1598 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001599 /** {@hide} */
1600 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601
1602 private static final int PRESSED = 0x00004000;
1603
1604 /** {@hide} */
1605 static final int DRAWING_CACHE_VALID = 0x00008000;
1606 /**
1607 * Flag used to indicate that this view should be drawn once more (and only once
1608 * more) after its animation has completed.
1609 * {@hide}
1610 */
1611 static final int ANIMATION_STARTED = 0x00010000;
1612
1613 private static final int SAVE_STATE_CALLED = 0x00020000;
1614
1615 /**
1616 * Indicates that the View returned true when onSetAlpha() was called and that
1617 * the alpha must be restored.
1618 * {@hide}
1619 */
1620 static final int ALPHA_SET = 0x00040000;
1621
1622 /**
1623 * Set by {@link #setScrollContainer(boolean)}.
1624 */
1625 static final int SCROLL_CONTAINER = 0x00080000;
1626
1627 /**
1628 * Set by {@link #setScrollContainer(boolean)}.
1629 */
1630 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1631
1632 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001633 * View flag indicating whether this view was invalidated (fully or partially.)
1634 *
1635 * @hide
1636 */
1637 static final int DIRTY = 0x00200000;
1638
1639 /**
1640 * View flag indicating whether this view was invalidated by an opaque
1641 * invalidate request.
1642 *
1643 * @hide
1644 */
1645 static final int DIRTY_OPAQUE = 0x00400000;
1646
1647 /**
1648 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1649 *
1650 * @hide
1651 */
1652 static final int DIRTY_MASK = 0x00600000;
1653
1654 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001655 * Indicates whether the background is opaque.
1656 *
1657 * @hide
1658 */
1659 static final int OPAQUE_BACKGROUND = 0x00800000;
1660
1661 /**
1662 * Indicates whether the scrollbars are opaque.
1663 *
1664 * @hide
1665 */
1666 static final int OPAQUE_SCROLLBARS = 0x01000000;
1667
1668 /**
1669 * Indicates whether the view is opaque.
1670 *
1671 * @hide
1672 */
1673 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001674
Adam Powelle14579b2009-12-16 18:39:52 -08001675 /**
1676 * Indicates a prepressed state;
1677 * the short time between ACTION_DOWN and recognizing
1678 * a 'real' press. Prepressed is used to recognize quick taps
1679 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001680 *
Adam Powelle14579b2009-12-16 18:39:52 -08001681 * @hide
1682 */
1683 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001684
Adam Powellc9fbaab2010-02-16 17:16:19 -08001685 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001686 * Indicates whether the view is temporarily detached.
1687 *
1688 * @hide
1689 */
1690 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001691
Adam Powell8568c3a2010-04-19 14:26:11 -07001692 /**
1693 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001694 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001695 * @hide
1696 */
1697 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001698
1699 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001700 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1701 * @hide
1702 */
1703 private static final int HOVERED = 0x10000000;
1704
1705 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001706 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1707 * for transform operations
1708 *
1709 * @hide
1710 */
Adam Powellf37df072010-09-17 16:22:49 -07001711 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001712
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001713 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001714 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001715
Chet Haasefd2b0022010-08-06 13:08:56 -07001716 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001717 * Indicates that this view was specifically invalidated, not just dirtied because some
1718 * child view was invalidated. The flag is used to determine when we need to recreate
1719 * a view's display list (as opposed to just returning a reference to its existing
1720 * display list).
1721 *
1722 * @hide
1723 */
1724 static final int INVALIDATED = 0x80000000;
1725
Christopher Tate3d4bf172011-03-28 16:16:46 -07001726 /* Masks for mPrivateFlags2 */
1727
1728 /**
1729 * Indicates that this view has reported that it can accept the current drag's content.
1730 * Cleared when the drag operation concludes.
1731 * @hide
1732 */
1733 static final int DRAG_CAN_ACCEPT = 0x00000001;
1734
1735 /**
1736 * Indicates that this view is currently directly under the drag location in a
1737 * drag-and-drop operation involving content that it can accept. Cleared when
1738 * the drag exits the view, or when the drag operation concludes.
1739 * @hide
1740 */
1741 static final int DRAG_HOVERED = 0x00000002;
1742
Cibu Johny86666632010-02-22 13:01:02 -08001743 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001744 * Indicates whether the view layout direction has been resolved and drawn to the
1745 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001746 *
1747 * @hide
1748 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001749 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1750
1751 /**
1752 * Indicates whether the view layout direction has been resolved.
1753 *
1754 * @hide
1755 */
1756 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1757
Cibu Johny86666632010-02-22 13:01:02 -08001758
Christopher Tate3d4bf172011-03-28 16:16:46 -07001759 /* End of masks for mPrivateFlags2 */
1760
1761 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1762
Chet Haasedaf98e92011-01-10 14:10:36 -08001763 /**
Adam Powell637d3372010-08-25 14:37:03 -07001764 * Always allow a user to over-scroll this view, provided it is a
1765 * view that can scroll.
1766 *
1767 * @see #getOverScrollMode()
1768 * @see #setOverScrollMode(int)
1769 */
1770 public static final int OVER_SCROLL_ALWAYS = 0;
1771
1772 /**
1773 * Allow a user to over-scroll this view only if the content is large
1774 * enough to meaningfully scroll, provided it is a view that can scroll.
1775 *
1776 * @see #getOverScrollMode()
1777 * @see #setOverScrollMode(int)
1778 */
1779 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1780
1781 /**
1782 * Never allow a user to over-scroll this view.
1783 *
1784 * @see #getOverScrollMode()
1785 * @see #setOverScrollMode(int)
1786 */
1787 public static final int OVER_SCROLL_NEVER = 2;
1788
1789 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001790 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001791 *
Joe Malin32736f02011-01-19 16:14:20 -08001792 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001793 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001794 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001795
1796 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001797 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1798 *
1799 * This is for use in games, book readers, video players, or any other "immersive" application
1800 * where the usual system chrome is deemed too distracting.
1801 *
1802 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001803 *
Joe Malin32736f02011-01-19 16:14:20 -08001804 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001805 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001806 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1807
1808 /**
1809 * View has requested that the system navigation be temporarily hidden.
1810 *
1811 * This is an even less obtrusive state than that called for by
1812 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1813 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1814 * those to disappear. This is useful (in conjunction with the
1815 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1816 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1817 * window flags) for displaying content using every last pixel on the display.
1818 *
1819 * There is a limitation: because navigation controls are so important, the least user
1820 * interaction will cause them to reappear immediately.
1821 *
1822 * @see #setSystemUiVisibility(int)
1823 */
1824 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1825
1826 /**
1827 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1828 */
1829 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1830
1831 /**
1832 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1833 */
1834 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001835
1836 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001837 * @hide
1838 *
1839 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1840 * out of the public fields to keep the undefined bits out of the developer's way.
1841 *
1842 * Flag to make the status bar not expandable. Unless you also
1843 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1844 */
1845 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1846
1847 /**
1848 * @hide
1849 *
1850 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1851 * out of the public fields to keep the undefined bits out of the developer's way.
1852 *
1853 * Flag to hide notification icons and scrolling ticker text.
1854 */
1855 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1856
1857 /**
1858 * @hide
1859 *
1860 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1861 * out of the public fields to keep the undefined bits out of the developer's way.
1862 *
1863 * Flag to disable incoming notification alerts. This will not block
1864 * icons, but it will block sound, vibrating and other visual or aural notifications.
1865 */
1866 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1867
1868 /**
1869 * @hide
1870 *
1871 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1872 * out of the public fields to keep the undefined bits out of the developer's way.
1873 *
1874 * Flag to hide only the scrolling ticker. Note that
1875 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1876 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1877 */
1878 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1879
1880 /**
1881 * @hide
1882 *
1883 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1884 * out of the public fields to keep the undefined bits out of the developer's way.
1885 *
1886 * Flag to hide the center system info area.
1887 */
1888 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1889
1890 /**
1891 * @hide
1892 *
1893 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1894 * out of the public fields to keep the undefined bits out of the developer's way.
1895 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001896 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001897 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1898 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001899 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001900
1901 /**
1902 * @hide
1903 *
1904 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1905 * out of the public fields to keep the undefined bits out of the developer's way.
1906 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001907 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001908 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1909 */
1910 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1911
1912 /**
1913 * @hide
1914 *
1915 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1916 * out of the public fields to keep the undefined bits out of the developer's way.
1917 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001918 * Flag to hide only the clock. You might use this if your activity has
1919 * its own clock making the status bar's clock redundant.
1920 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001921 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1922
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001923 /**
1924 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001925 *
1926 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1927 * out of the public fields to keep the undefined bits out of the developer's way.
1928 *
1929 * Flag to hide only the recent apps button. Don't use this
1930 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1931 */
1932 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1933
1934 /**
1935 * @hide
1936 *
1937 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1938 *
1939 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1940 */
1941 @Deprecated
1942 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1943 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1944
1945 /**
1946 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001947 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001948 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001949
1950 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001951 * These are the system UI flags that can be cleared by events outside
1952 * of an application. Currently this is just the ability to tap on the
1953 * screen while hiding the navigation bar to have it return.
1954 * @hide
1955 */
1956 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1957 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1958
1959 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001960 * Find views that render the specified text.
1961 *
1962 * @see #findViewsWithText(ArrayList, CharSequence, int)
1963 */
1964 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1965
1966 /**
1967 * Find find views that contain the specified content description.
1968 *
1969 * @see #findViewsWithText(ArrayList, CharSequence, int)
1970 */
1971 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1972
1973 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07001974 * Find views that contain {@link AccessibilityNodeProvider}. Such
1975 * a View is a root of virtual view hierarchy and may contain the searched
1976 * text. If this flag is set Views with providers are automatically
1977 * added and it is a responsibility of the client to call the APIs of
1978 * the provider to determine whether the virtual tree rooted at this View
1979 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
1980 * represeting the virtual views with this text.
1981 *
1982 * @see #findViewsWithText(ArrayList, CharSequence, int)
1983 *
1984 * @hide
1985 */
1986 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
1987
1988 /**
Adam Powell637d3372010-08-25 14:37:03 -07001989 * Controls the over-scroll mode for this view.
1990 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1991 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1992 * and {@link #OVER_SCROLL_NEVER}.
1993 */
1994 private int mOverScrollMode;
1995
1996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 * The parent this view is attached to.
1998 * {@hide}
1999 *
2000 * @see #getParent()
2001 */
2002 protected ViewParent mParent;
2003
2004 /**
2005 * {@hide}
2006 */
2007 AttachInfo mAttachInfo;
2008
2009 /**
2010 * {@hide}
2011 */
Romain Guy809a7f62009-05-14 15:44:42 -07002012 @ViewDebug.ExportedProperty(flagMapping = {
2013 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2014 name = "FORCE_LAYOUT"),
2015 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2016 name = "LAYOUT_REQUIRED"),
2017 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002018 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002019 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2020 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2021 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2022 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2023 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002025 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026
2027 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002028 * This view's request for the visibility of the status bar.
2029 * @hide
2030 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002031 @ViewDebug.ExportedProperty(flagMapping = {
2032 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2033 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2034 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2035 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2036 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2037 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2038 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2039 equals = SYSTEM_UI_FLAG_VISIBLE,
2040 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2041 })
Joe Onorato664644d2011-01-23 17:53:23 -08002042 int mSystemUiVisibility;
2043
2044 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 * Count of how many windows this view has been attached to.
2046 */
2047 int mWindowAttachCount;
2048
2049 /**
2050 * The layout parameters associated with this view and used by the parent
2051 * {@link android.view.ViewGroup} to determine how this view should be
2052 * laid out.
2053 * {@hide}
2054 */
2055 protected ViewGroup.LayoutParams mLayoutParams;
2056
2057 /**
2058 * The view flags hold various views states.
2059 * {@hide}
2060 */
2061 @ViewDebug.ExportedProperty
2062 int mViewFlags;
2063
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002064 static class TransformationInfo {
2065 /**
2066 * The transform matrix for the View. This transform is calculated internally
2067 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2068 * is used by default. Do *not* use this variable directly; instead call
2069 * getMatrix(), which will automatically recalculate the matrix if necessary
2070 * to get the correct matrix based on the latest rotation and scale properties.
2071 */
2072 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002073
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002074 /**
2075 * The transform matrix for the View. This transform is calculated internally
2076 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2077 * is used by default. Do *not* use this variable directly; instead call
2078 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2079 * to get the correct matrix based on the latest rotation and scale properties.
2080 */
2081 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002082
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002083 /**
2084 * An internal variable that tracks whether we need to recalculate the
2085 * transform matrix, based on whether the rotation or scaleX/Y properties
2086 * have changed since the matrix was last calculated.
2087 */
2088 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002089
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002090 /**
2091 * An internal variable that tracks whether we need to recalculate the
2092 * transform matrix, based on whether the rotation or scaleX/Y properties
2093 * have changed since the matrix was last calculated.
2094 */
2095 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002096
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002097 /**
2098 * A variable that tracks whether we need to recalculate the
2099 * transform matrix, based on whether the rotation or scaleX/Y properties
2100 * have changed since the matrix was last calculated. This variable
2101 * is only valid after a call to updateMatrix() or to a function that
2102 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2103 */
2104 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002105
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002106 /**
2107 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2108 */
2109 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002110
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002111 /**
2112 * This matrix is used when computing the matrix for 3D rotations.
2113 */
2114 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002115
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002116 /**
2117 * These prev values are used to recalculate a centered pivot point when necessary. The
2118 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2119 * set), so thes values are only used then as well.
2120 */
2121 private int mPrevWidth = -1;
2122 private int mPrevHeight = -1;
2123
2124 /**
2125 * The degrees rotation around the vertical axis through the pivot point.
2126 */
2127 @ViewDebug.ExportedProperty
2128 float mRotationY = 0f;
2129
2130 /**
2131 * The degrees rotation around the horizontal axis through the pivot point.
2132 */
2133 @ViewDebug.ExportedProperty
2134 float mRotationX = 0f;
2135
2136 /**
2137 * The degrees rotation around the pivot point.
2138 */
2139 @ViewDebug.ExportedProperty
2140 float mRotation = 0f;
2141
2142 /**
2143 * The amount of translation of the object away from its left property (post-layout).
2144 */
2145 @ViewDebug.ExportedProperty
2146 float mTranslationX = 0f;
2147
2148 /**
2149 * The amount of translation of the object away from its top property (post-layout).
2150 */
2151 @ViewDebug.ExportedProperty
2152 float mTranslationY = 0f;
2153
2154 /**
2155 * The amount of scale in the x direction around the pivot point. A
2156 * value of 1 means no scaling is applied.
2157 */
2158 @ViewDebug.ExportedProperty
2159 float mScaleX = 1f;
2160
2161 /**
2162 * The amount of scale in the y direction around the pivot point. A
2163 * value of 1 means no scaling is applied.
2164 */
2165 @ViewDebug.ExportedProperty
2166 float mScaleY = 1f;
2167
2168 /**
2169 * The amount of scale in the x direction around the pivot point. A
2170 * value of 1 means no scaling is applied.
2171 */
2172 @ViewDebug.ExportedProperty
2173 float mPivotX = 0f;
2174
2175 /**
2176 * The amount of scale in the y direction around the pivot point. A
2177 * value of 1 means no scaling is applied.
2178 */
2179 @ViewDebug.ExportedProperty
2180 float mPivotY = 0f;
2181
2182 /**
2183 * The opacity of the View. This is a value from 0 to 1, where 0 means
2184 * completely transparent and 1 means completely opaque.
2185 */
2186 @ViewDebug.ExportedProperty
2187 float mAlpha = 1f;
2188 }
2189
2190 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002191
Joe Malin32736f02011-01-19 16:14:20 -08002192 private boolean mLastIsOpaque;
2193
Chet Haasefd2b0022010-08-06 13:08:56 -07002194 /**
2195 * Convenience value to check for float values that are close enough to zero to be considered
2196 * zero.
2197 */
Romain Guy2542d192010-08-18 11:47:12 -07002198 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002199
2200 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 * The distance in pixels from the left edge of this view's parent
2202 * to the left edge of this view.
2203 * {@hide}
2204 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002205 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 protected int mLeft;
2207 /**
2208 * The distance in pixels from the left edge of this view's parent
2209 * to the right edge of this view.
2210 * {@hide}
2211 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002212 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 protected int mRight;
2214 /**
2215 * The distance in pixels from the top edge of this view's parent
2216 * to the top edge of this view.
2217 * {@hide}
2218 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002219 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 protected int mTop;
2221 /**
2222 * The distance in pixels from the top edge of this view's parent
2223 * to the bottom edge of this view.
2224 * {@hide}
2225 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002226 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 protected int mBottom;
2228
2229 /**
2230 * The offset, in pixels, by which the content of this view is scrolled
2231 * horizontally.
2232 * {@hide}
2233 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002234 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 protected int mScrollX;
2236 /**
2237 * The offset, in pixels, by which the content of this view is scrolled
2238 * vertically.
2239 * {@hide}
2240 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002241 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 protected int mScrollY;
2243
2244 /**
2245 * The left padding in pixels, that is the distance in pixels between the
2246 * left edge of this view and the left edge of its content.
2247 * {@hide}
2248 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002249 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 protected int mPaddingLeft;
2251 /**
2252 * The right padding in pixels, that is the distance in pixels between the
2253 * right edge of this view and the right edge of its content.
2254 * {@hide}
2255 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002256 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 protected int mPaddingRight;
2258 /**
2259 * The top padding in pixels, that is the distance in pixels between the
2260 * top edge of this view and the top edge of its content.
2261 * {@hide}
2262 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002263 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 protected int mPaddingTop;
2265 /**
2266 * The bottom padding in pixels, that is the distance in pixels between the
2267 * bottom edge of this view and the bottom edge of its content.
2268 * {@hide}
2269 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002270 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 protected int mPaddingBottom;
2272
2273 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002274 * Briefly describes the view and is primarily used for accessibility support.
2275 */
2276 private CharSequence mContentDescription;
2277
2278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002280 *
2281 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002283 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002284 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285
2286 /**
2287 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002288 *
2289 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002291 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002292 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002294 /**
Adam Powell20232d02010-12-08 21:08:53 -08002295 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002296 *
2297 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002298 */
2299 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002300 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002301
2302 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002303 * Cache if the user padding is relative.
2304 *
2305 */
2306 @ViewDebug.ExportedProperty(category = "padding")
2307 boolean mUserPaddingRelative;
2308
2309 /**
2310 * Cache the paddingStart set by the user to append to the scrollbar's size.
2311 *
2312 */
2313 @ViewDebug.ExportedProperty(category = "padding")
2314 int mUserPaddingStart;
2315
2316 /**
2317 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2318 *
2319 */
2320 @ViewDebug.ExportedProperty(category = "padding")
2321 int mUserPaddingEnd;
2322
2323 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002324 * @hide
2325 */
2326 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2327 /**
2328 * @hide
2329 */
2330 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 private Drawable mBGDrawable;
2333
2334 private int mBackgroundResource;
2335 private boolean mBackgroundSizeChanged;
2336
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002337 static class ListenerInfo {
2338 /**
2339 * Listener used to dispatch focus change events.
2340 * This field should be made private, so it is hidden from the SDK.
2341 * {@hide}
2342 */
2343 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002345 /**
2346 * Listeners for layout change events.
2347 */
2348 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002349
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002350 /**
2351 * Listeners for attach events.
2352 */
2353 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002354
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002355 /**
2356 * Listener used to dispatch click events.
2357 * This field should be made private, so it is hidden from the SDK.
2358 * {@hide}
2359 */
2360 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002362 /**
2363 * Listener used to dispatch long click events.
2364 * This field should be made private, so it is hidden from the SDK.
2365 * {@hide}
2366 */
2367 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002369 /**
2370 * Listener used to build the context menu.
2371 * This field should be made private, so it is hidden from the SDK.
2372 * {@hide}
2373 */
2374 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002376 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002378 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002380 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002381
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002382 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002383
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002384 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002385
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002386 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2387 }
2388
2389 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 /**
2392 * The application environment this view lives in.
2393 * This field should be made private, so it is hidden from the SDK.
2394 * {@hide}
2395 */
2396 protected Context mContext;
2397
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002398 private final Resources mResources;
2399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 private ScrollabilityCache mScrollCache;
2401
2402 private int[] mDrawableState = null;
2403
Romain Guy0211a0a2011-02-14 16:34:59 -08002404 /**
2405 * Set to true when drawing cache is enabled and cannot be created.
2406 *
2407 * @hide
2408 */
2409 public boolean mCachingFailed;
2410
Romain Guy02890fd2010-08-06 17:58:44 -07002411 private Bitmap mDrawingCache;
2412 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002413 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002414 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415
2416 /**
2417 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2418 * the user may specify which view to go to next.
2419 */
2420 private int mNextFocusLeftId = View.NO_ID;
2421
2422 /**
2423 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2424 * the user may specify which view to go to next.
2425 */
2426 private int mNextFocusRightId = View.NO_ID;
2427
2428 /**
2429 * When this view has focus and the next focus is {@link #FOCUS_UP},
2430 * the user may specify which view to go to next.
2431 */
2432 private int mNextFocusUpId = View.NO_ID;
2433
2434 /**
2435 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2436 * the user may specify which view to go to next.
2437 */
2438 private int mNextFocusDownId = View.NO_ID;
2439
Jeff Brown4e6319b2010-12-13 10:36:51 -08002440 /**
2441 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2442 * the user may specify which view to go to next.
2443 */
2444 int mNextFocusForwardId = View.NO_ID;
2445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002447 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002448 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002449 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 private UnsetPressedState mUnsetPressedState;
2452
2453 /**
2454 * Whether the long press's action has been invoked. The tap's action is invoked on the
2455 * up event while a long press is invoked as soon as the long press duration is reached, so
2456 * a long press could be performed before the tap is checked, in which case the tap's action
2457 * should not be invoked.
2458 */
2459 private boolean mHasPerformedLongPress;
2460
2461 /**
2462 * The minimum height of the view. We'll try our best to have the height
2463 * of this view to at least this amount.
2464 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002465 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 private int mMinHeight;
2467
2468 /**
2469 * The minimum width of the view. We'll try our best to have the width
2470 * of this view to at least this amount.
2471 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002472 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 private int mMinWidth;
2474
2475 /**
2476 * The delegate to handle touch events that are physically in this view
2477 * but should be handled by another view.
2478 */
2479 private TouchDelegate mTouchDelegate = null;
2480
2481 /**
2482 * Solid color to use as a background when creating the drawing cache. Enables
2483 * the cache to use 16 bit bitmaps instead of 32 bit.
2484 */
2485 private int mDrawingCacheBackgroundColor = 0;
2486
2487 /**
2488 * Special tree observer used when mAttachInfo is null.
2489 */
2490 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002491
Adam Powelle14579b2009-12-16 18:39:52 -08002492 /**
2493 * Cache the touch slop from the context that created the view.
2494 */
2495 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002498 * Object that handles automatic animation of view properties.
2499 */
2500 private ViewPropertyAnimator mAnimator = null;
2501
2502 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002503 * Flag indicating that a drag can cross window boundaries. When
2504 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2505 * with this flag set, all visible applications will be able to participate
2506 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002507 *
2508 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002509 */
2510 public static final int DRAG_FLAG_GLOBAL = 1;
2511
2512 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002513 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2514 */
2515 private float mVerticalScrollFactor;
2516
2517 /**
Adam Powell20232d02010-12-08 21:08:53 -08002518 * Position of the vertical scroll bar.
2519 */
2520 private int mVerticalScrollbarPosition;
2521
2522 /**
2523 * Position the scroll bar at the default position as determined by the system.
2524 */
2525 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2526
2527 /**
2528 * Position the scroll bar along the left edge.
2529 */
2530 public static final int SCROLLBAR_POSITION_LEFT = 1;
2531
2532 /**
2533 * Position the scroll bar along the right edge.
2534 */
2535 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2536
2537 /**
Romain Guy171c5922011-01-06 10:04:23 -08002538 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002539 *
2540 * @see #getLayerType()
2541 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002542 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002543 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002544 */
2545 public static final int LAYER_TYPE_NONE = 0;
2546
2547 /**
2548 * <p>Indicates that the view has a software layer. A software layer is backed
2549 * by a bitmap and causes the view to be rendered using Android's software
2550 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002551 *
Romain Guy171c5922011-01-06 10:04:23 -08002552 * <p>Software layers have various usages:</p>
2553 * <p>When the application is not using hardware acceleration, a software layer
2554 * is useful to apply a specific color filter and/or blending mode and/or
2555 * translucency to a view and all its children.</p>
2556 * <p>When the application is using hardware acceleration, a software layer
2557 * is useful to render drawing primitives not supported by the hardware
2558 * accelerated pipeline. It can also be used to cache a complex view tree
2559 * into a texture and reduce the complexity of drawing operations. For instance,
2560 * when animating a complex view tree with a translation, a software layer can
2561 * be used to render the view tree only once.</p>
2562 * <p>Software layers should be avoided when the affected view tree updates
2563 * often. Every update will require to re-render the software layer, which can
2564 * potentially be slow (particularly when hardware acceleration is turned on
2565 * since the layer will have to be uploaded into a hardware texture after every
2566 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002567 *
2568 * @see #getLayerType()
2569 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002570 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002571 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002572 */
2573 public static final int LAYER_TYPE_SOFTWARE = 1;
2574
2575 /**
2576 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2577 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2578 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2579 * rendering pipeline, but only if hardware acceleration is turned on for the
2580 * view hierarchy. When hardware acceleration is turned off, hardware layers
2581 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002582 *
Romain Guy171c5922011-01-06 10:04:23 -08002583 * <p>A hardware layer is useful to apply a specific color filter and/or
2584 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002585 * <p>A hardware layer can be used to cache a complex view tree into a
2586 * texture and reduce the complexity of drawing operations. For instance,
2587 * when animating a complex view tree with a translation, a hardware layer can
2588 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002589 * <p>A hardware layer can also be used to increase the rendering quality when
2590 * rotation transformations are applied on a view. It can also be used to
2591 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002592 *
2593 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002594 * @see #setLayerType(int, android.graphics.Paint)
2595 * @see #LAYER_TYPE_NONE
2596 * @see #LAYER_TYPE_SOFTWARE
2597 */
2598 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002599
Romain Guy3aaff3a2011-01-12 14:18:47 -08002600 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2601 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2602 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2603 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2604 })
Romain Guy171c5922011-01-06 10:04:23 -08002605 int mLayerType = LAYER_TYPE_NONE;
2606 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002607 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002608
2609 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002610 * Set to true when the view is sending hover accessibility events because it
2611 * is the innermost hovered view.
2612 */
2613 private boolean mSendingHoverAccessibilityEvents;
2614
2615 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002616 * Delegate for injecting accessiblity functionality.
2617 */
2618 AccessibilityDelegate mAccessibilityDelegate;
2619
2620 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002621 * Text direction is inherited thru {@link ViewGroup}
2622 * @hide
2623 */
2624 public static final int TEXT_DIRECTION_INHERIT = 0;
2625
2626 /**
2627 * Text direction is using "first strong algorithm". The first strong directional character
2628 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002629 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002630 *
2631 * @hide
2632 */
2633 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2634
2635 /**
2636 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2637 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002638 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002639 *
2640 * @hide
2641 */
2642 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2643
2644 /**
2645 * Text direction is forced to LTR.
2646 *
2647 * @hide
2648 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002649 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002650
2651 /**
2652 * Text direction is forced to RTL.
2653 *
2654 * @hide
2655 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002656 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002657
2658 /**
2659 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002660 *
2661 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002662 */
2663 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2664
2665 /**
2666 * The text direction that has been defined by {@link #setTextDirection(int)}.
2667 *
2668 * {@hide}
2669 */
2670 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002671 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2672 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2673 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2674 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2675 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2676 })
Doug Feltcb3791202011-07-07 11:57:48 -07002677 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002678
2679 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002680 * The resolved text direction. This needs resolution if the value is
2681 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2682 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2683 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002684 *
2685 * {@hide}
2686 */
2687 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002688 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2689 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2690 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002691 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2692 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2693 })
Doug Feltcb3791202011-07-07 11:57:48 -07002694 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002695
2696 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002697 * Consistency verifier for debugging purposes.
2698 * @hide
2699 */
2700 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2701 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2702 new InputEventConsistencyVerifier(this, 0) : null;
2703
2704 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 * Simple constructor to use when creating a view from code.
2706 *
2707 * @param context The Context the view is running in, through which it can
2708 * access the current theme, resources, etc.
2709 */
2710 public View(Context context) {
2711 mContext = context;
2712 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002713 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002714 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002715 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002716 mUserPaddingStart = -1;
2717 mUserPaddingEnd = -1;
2718 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 }
2720
2721 /**
2722 * Constructor that is called when inflating a view from XML. This is called
2723 * when a view is being constructed from an XML file, supplying attributes
2724 * that were specified in the XML file. This version uses a default style of
2725 * 0, so the only attribute values applied are those in the Context's Theme
2726 * and the given AttributeSet.
2727 *
2728 * <p>
2729 * The method onFinishInflate() will be called after all children have been
2730 * added.
2731 *
2732 * @param context The Context the view is running in, through which it can
2733 * access the current theme, resources, etc.
2734 * @param attrs The attributes of the XML tag that is inflating the view.
2735 * @see #View(Context, AttributeSet, int)
2736 */
2737 public View(Context context, AttributeSet attrs) {
2738 this(context, attrs, 0);
2739 }
2740
2741 /**
2742 * Perform inflation from XML and apply a class-specific base style. This
2743 * constructor of View allows subclasses to use their own base style when
2744 * they are inflating. For example, a Button class's constructor would call
2745 * this version of the super class constructor and supply
2746 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2747 * the theme's button style to modify all of the base view attributes (in
2748 * particular its background) as well as the Button class's attributes.
2749 *
2750 * @param context The Context the view is running in, through which it can
2751 * access the current theme, resources, etc.
2752 * @param attrs The attributes of the XML tag that is inflating the view.
2753 * @param defStyle The default style to apply to this view. If 0, no style
2754 * will be applied (beyond what is included in the theme). This may
2755 * either be an attribute resource, whose value will be retrieved
2756 * from the current theme, or an explicit style resource.
2757 * @see #View(Context, AttributeSet)
2758 */
2759 public View(Context context, AttributeSet attrs, int defStyle) {
2760 this(context);
2761
2762 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2763 defStyle, 0);
2764
2765 Drawable background = null;
2766
2767 int leftPadding = -1;
2768 int topPadding = -1;
2769 int rightPadding = -1;
2770 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002771 int startPadding = -1;
2772 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773
2774 int padding = -1;
2775
2776 int viewFlagValues = 0;
2777 int viewFlagMasks = 0;
2778
2779 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 int x = 0;
2782 int y = 0;
2783
Chet Haase73066682010-11-29 15:55:32 -08002784 float tx = 0;
2785 float ty = 0;
2786 float rotation = 0;
2787 float rotationX = 0;
2788 float rotationY = 0;
2789 float sx = 1f;
2790 float sy = 1f;
2791 boolean transformSet = false;
2792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2794
Adam Powell637d3372010-08-25 14:37:03 -07002795 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 final int N = a.getIndexCount();
2797 for (int i = 0; i < N; i++) {
2798 int attr = a.getIndex(i);
2799 switch (attr) {
2800 case com.android.internal.R.styleable.View_background:
2801 background = a.getDrawable(attr);
2802 break;
2803 case com.android.internal.R.styleable.View_padding:
2804 padding = a.getDimensionPixelSize(attr, -1);
2805 break;
2806 case com.android.internal.R.styleable.View_paddingLeft:
2807 leftPadding = a.getDimensionPixelSize(attr, -1);
2808 break;
2809 case com.android.internal.R.styleable.View_paddingTop:
2810 topPadding = a.getDimensionPixelSize(attr, -1);
2811 break;
2812 case com.android.internal.R.styleable.View_paddingRight:
2813 rightPadding = a.getDimensionPixelSize(attr, -1);
2814 break;
2815 case com.android.internal.R.styleable.View_paddingBottom:
2816 bottomPadding = a.getDimensionPixelSize(attr, -1);
2817 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002818 case com.android.internal.R.styleable.View_paddingStart:
2819 startPadding = a.getDimensionPixelSize(attr, -1);
2820 break;
2821 case com.android.internal.R.styleable.View_paddingEnd:
2822 endPadding = a.getDimensionPixelSize(attr, -1);
2823 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 case com.android.internal.R.styleable.View_scrollX:
2825 x = a.getDimensionPixelOffset(attr, 0);
2826 break;
2827 case com.android.internal.R.styleable.View_scrollY:
2828 y = a.getDimensionPixelOffset(attr, 0);
2829 break;
Chet Haase73066682010-11-29 15:55:32 -08002830 case com.android.internal.R.styleable.View_alpha:
2831 setAlpha(a.getFloat(attr, 1f));
2832 break;
2833 case com.android.internal.R.styleable.View_transformPivotX:
2834 setPivotX(a.getDimensionPixelOffset(attr, 0));
2835 break;
2836 case com.android.internal.R.styleable.View_transformPivotY:
2837 setPivotY(a.getDimensionPixelOffset(attr, 0));
2838 break;
2839 case com.android.internal.R.styleable.View_translationX:
2840 tx = a.getDimensionPixelOffset(attr, 0);
2841 transformSet = true;
2842 break;
2843 case com.android.internal.R.styleable.View_translationY:
2844 ty = a.getDimensionPixelOffset(attr, 0);
2845 transformSet = true;
2846 break;
2847 case com.android.internal.R.styleable.View_rotation:
2848 rotation = a.getFloat(attr, 0);
2849 transformSet = true;
2850 break;
2851 case com.android.internal.R.styleable.View_rotationX:
2852 rotationX = a.getFloat(attr, 0);
2853 transformSet = true;
2854 break;
2855 case com.android.internal.R.styleable.View_rotationY:
2856 rotationY = a.getFloat(attr, 0);
2857 transformSet = true;
2858 break;
2859 case com.android.internal.R.styleable.View_scaleX:
2860 sx = a.getFloat(attr, 1f);
2861 transformSet = true;
2862 break;
2863 case com.android.internal.R.styleable.View_scaleY:
2864 sy = a.getFloat(attr, 1f);
2865 transformSet = true;
2866 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 case com.android.internal.R.styleable.View_id:
2868 mID = a.getResourceId(attr, NO_ID);
2869 break;
2870 case com.android.internal.R.styleable.View_tag:
2871 mTag = a.getText(attr);
2872 break;
2873 case com.android.internal.R.styleable.View_fitsSystemWindows:
2874 if (a.getBoolean(attr, false)) {
2875 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2876 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2877 }
2878 break;
2879 case com.android.internal.R.styleable.View_focusable:
2880 if (a.getBoolean(attr, false)) {
2881 viewFlagValues |= FOCUSABLE;
2882 viewFlagMasks |= FOCUSABLE_MASK;
2883 }
2884 break;
2885 case com.android.internal.R.styleable.View_focusableInTouchMode:
2886 if (a.getBoolean(attr, false)) {
2887 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2888 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2889 }
2890 break;
2891 case com.android.internal.R.styleable.View_clickable:
2892 if (a.getBoolean(attr, false)) {
2893 viewFlagValues |= CLICKABLE;
2894 viewFlagMasks |= CLICKABLE;
2895 }
2896 break;
2897 case com.android.internal.R.styleable.View_longClickable:
2898 if (a.getBoolean(attr, false)) {
2899 viewFlagValues |= LONG_CLICKABLE;
2900 viewFlagMasks |= LONG_CLICKABLE;
2901 }
2902 break;
2903 case com.android.internal.R.styleable.View_saveEnabled:
2904 if (!a.getBoolean(attr, true)) {
2905 viewFlagValues |= SAVE_DISABLED;
2906 viewFlagMasks |= SAVE_DISABLED_MASK;
2907 }
2908 break;
2909 case com.android.internal.R.styleable.View_duplicateParentState:
2910 if (a.getBoolean(attr, false)) {
2911 viewFlagValues |= DUPLICATE_PARENT_STATE;
2912 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2913 }
2914 break;
2915 case com.android.internal.R.styleable.View_visibility:
2916 final int visibility = a.getInt(attr, 0);
2917 if (visibility != 0) {
2918 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2919 viewFlagMasks |= VISIBILITY_MASK;
2920 }
2921 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002922 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002923 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002924 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002925 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002926 final int layoutDirection = a.getInt(attr, -1);
2927 if (layoutDirection != -1) {
2928 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002929 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002930 // Set to default (LAYOUT_DIRECTION_INHERIT)
2931 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002932 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002933 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002934 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 case com.android.internal.R.styleable.View_drawingCacheQuality:
2936 final int cacheQuality = a.getInt(attr, 0);
2937 if (cacheQuality != 0) {
2938 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2939 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2940 }
2941 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002942 case com.android.internal.R.styleable.View_contentDescription:
2943 mContentDescription = a.getString(attr);
2944 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2946 if (!a.getBoolean(attr, true)) {
2947 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2948 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2949 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002950 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2952 if (!a.getBoolean(attr, true)) {
2953 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2954 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2955 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002956 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 case R.styleable.View_scrollbars:
2958 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2959 if (scrollbars != SCROLLBARS_NONE) {
2960 viewFlagValues |= scrollbars;
2961 viewFlagMasks |= SCROLLBARS_MASK;
2962 initializeScrollbars(a);
2963 }
2964 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002965 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002967 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2968 // Ignore the attribute starting with ICS
2969 break;
2970 }
2971 // With builds < ICS, fall through and apply fading edges
2972 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2974 if (fadingEdge != FADING_EDGE_NONE) {
2975 viewFlagValues |= fadingEdge;
2976 viewFlagMasks |= FADING_EDGE_MASK;
2977 initializeFadingEdge(a);
2978 }
2979 break;
2980 case R.styleable.View_scrollbarStyle:
2981 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2982 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2983 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2984 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2985 }
2986 break;
2987 case R.styleable.View_isScrollContainer:
2988 setScrollContainer = true;
2989 if (a.getBoolean(attr, false)) {
2990 setScrollContainer(true);
2991 }
2992 break;
2993 case com.android.internal.R.styleable.View_keepScreenOn:
2994 if (a.getBoolean(attr, false)) {
2995 viewFlagValues |= KEEP_SCREEN_ON;
2996 viewFlagMasks |= KEEP_SCREEN_ON;
2997 }
2998 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002999 case R.styleable.View_filterTouchesWhenObscured:
3000 if (a.getBoolean(attr, false)) {
3001 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3002 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3003 }
3004 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 case R.styleable.View_nextFocusLeft:
3006 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3007 break;
3008 case R.styleable.View_nextFocusRight:
3009 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3010 break;
3011 case R.styleable.View_nextFocusUp:
3012 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3013 break;
3014 case R.styleable.View_nextFocusDown:
3015 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3016 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003017 case R.styleable.View_nextFocusForward:
3018 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3019 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 case R.styleable.View_minWidth:
3021 mMinWidth = a.getDimensionPixelSize(attr, 0);
3022 break;
3023 case R.styleable.View_minHeight:
3024 mMinHeight = a.getDimensionPixelSize(attr, 0);
3025 break;
Romain Guy9a817362009-05-01 10:57:14 -07003026 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003027 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003028 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003029 + "be used within a restricted context");
3030 }
3031
Romain Guy9a817362009-05-01 10:57:14 -07003032 final String handlerName = a.getString(attr);
3033 if (handlerName != null) {
3034 setOnClickListener(new OnClickListener() {
3035 private Method mHandler;
3036
3037 public void onClick(View v) {
3038 if (mHandler == null) {
3039 try {
3040 mHandler = getContext().getClass().getMethod(handlerName,
3041 View.class);
3042 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003043 int id = getId();
3044 String idText = id == NO_ID ? "" : " with id '"
3045 + getContext().getResources().getResourceEntryName(
3046 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003047 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003048 handlerName + "(View) in the activity "
3049 + getContext().getClass() + " for onClick handler"
3050 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003051 }
3052 }
3053
3054 try {
3055 mHandler.invoke(getContext(), View.this);
3056 } catch (IllegalAccessException e) {
3057 throw new IllegalStateException("Could not execute non "
3058 + "public method of the activity", e);
3059 } catch (InvocationTargetException e) {
3060 throw new IllegalStateException("Could not execute "
3061 + "method of the activity", e);
3062 }
3063 }
3064 });
3065 }
3066 break;
Adam Powell637d3372010-08-25 14:37:03 -07003067 case R.styleable.View_overScrollMode:
3068 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3069 break;
Adam Powell20232d02010-12-08 21:08:53 -08003070 case R.styleable.View_verticalScrollbarPosition:
3071 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3072 break;
Romain Guy171c5922011-01-06 10:04:23 -08003073 case R.styleable.View_layerType:
3074 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3075 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003076 case R.styleable.View_textDirection:
3077 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3078 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 }
3080 }
3081
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003082 a.recycle();
3083
Adam Powell637d3372010-08-25 14:37:03 -07003084 setOverScrollMode(overScrollMode);
3085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 if (background != null) {
3087 setBackgroundDrawable(background);
3088 }
3089
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003090 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3091
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003092 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3093 // layout direction). Those cached values will be used later during padding resolution.
3094 mUserPaddingStart = startPadding;
3095 mUserPaddingEnd = endPadding;
3096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 if (padding >= 0) {
3098 leftPadding = padding;
3099 topPadding = padding;
3100 rightPadding = padding;
3101 bottomPadding = padding;
3102 }
3103
3104 // If the user specified the padding (either with android:padding or
3105 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3106 // use the default padding or the padding from the background drawable
3107 // (stored at this point in mPadding*)
3108 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3109 topPadding >= 0 ? topPadding : mPaddingTop,
3110 rightPadding >= 0 ? rightPadding : mPaddingRight,
3111 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3112
3113 if (viewFlagMasks != 0) {
3114 setFlags(viewFlagValues, viewFlagMasks);
3115 }
3116
3117 // Needs to be called after mViewFlags is set
3118 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3119 recomputePadding();
3120 }
3121
3122 if (x != 0 || y != 0) {
3123 scrollTo(x, y);
3124 }
3125
Chet Haase73066682010-11-29 15:55:32 -08003126 if (transformSet) {
3127 setTranslationX(tx);
3128 setTranslationY(ty);
3129 setRotation(rotation);
3130 setRotationX(rotationX);
3131 setRotationY(rotationY);
3132 setScaleX(sx);
3133 setScaleY(sy);
3134 }
3135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3137 setScrollContainer(true);
3138 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003139
3140 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 }
3142
3143 /**
3144 * Non-public constructor for use in testing
3145 */
3146 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003147 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 }
3149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 /**
3151 * <p>
3152 * Initializes the fading edges from a given set of styled attributes. This
3153 * method should be called by subclasses that need fading edges and when an
3154 * instance of these subclasses is created programmatically rather than
3155 * being inflated from XML. This method is automatically called when the XML
3156 * is inflated.
3157 * </p>
3158 *
3159 * @param a the styled attributes set to initialize the fading edges from
3160 */
3161 protected void initializeFadingEdge(TypedArray a) {
3162 initScrollCache();
3163
3164 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3165 R.styleable.View_fadingEdgeLength,
3166 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3167 }
3168
3169 /**
3170 * Returns the size of the vertical faded edges used to indicate that more
3171 * content in this view is visible.
3172 *
3173 * @return The size in pixels of the vertical faded edge or 0 if vertical
3174 * faded edges are not enabled for this view.
3175 * @attr ref android.R.styleable#View_fadingEdgeLength
3176 */
3177 public int getVerticalFadingEdgeLength() {
3178 if (isVerticalFadingEdgeEnabled()) {
3179 ScrollabilityCache cache = mScrollCache;
3180 if (cache != null) {
3181 return cache.fadingEdgeLength;
3182 }
3183 }
3184 return 0;
3185 }
3186
3187 /**
3188 * Set the size of the faded edge used to indicate that more content in this
3189 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003190 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3191 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3192 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 *
3194 * @param length The size in pixels of the faded edge used to indicate that more
3195 * content in this view is visible.
3196 */
3197 public void setFadingEdgeLength(int length) {
3198 initScrollCache();
3199 mScrollCache.fadingEdgeLength = length;
3200 }
3201
3202 /**
3203 * Returns the size of the horizontal faded edges used to indicate that more
3204 * content in this view is visible.
3205 *
3206 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3207 * faded edges are not enabled for this view.
3208 * @attr ref android.R.styleable#View_fadingEdgeLength
3209 */
3210 public int getHorizontalFadingEdgeLength() {
3211 if (isHorizontalFadingEdgeEnabled()) {
3212 ScrollabilityCache cache = mScrollCache;
3213 if (cache != null) {
3214 return cache.fadingEdgeLength;
3215 }
3216 }
3217 return 0;
3218 }
3219
3220 /**
3221 * Returns the width of the vertical scrollbar.
3222 *
3223 * @return The width in pixels of the vertical scrollbar or 0 if there
3224 * is no vertical scrollbar.
3225 */
3226 public int getVerticalScrollbarWidth() {
3227 ScrollabilityCache cache = mScrollCache;
3228 if (cache != null) {
3229 ScrollBarDrawable scrollBar = cache.scrollBar;
3230 if (scrollBar != null) {
3231 int size = scrollBar.getSize(true);
3232 if (size <= 0) {
3233 size = cache.scrollBarSize;
3234 }
3235 return size;
3236 }
3237 return 0;
3238 }
3239 return 0;
3240 }
3241
3242 /**
3243 * Returns the height of the horizontal scrollbar.
3244 *
3245 * @return The height in pixels of the horizontal scrollbar or 0 if
3246 * there is no horizontal scrollbar.
3247 */
3248 protected int getHorizontalScrollbarHeight() {
3249 ScrollabilityCache cache = mScrollCache;
3250 if (cache != null) {
3251 ScrollBarDrawable scrollBar = cache.scrollBar;
3252 if (scrollBar != null) {
3253 int size = scrollBar.getSize(false);
3254 if (size <= 0) {
3255 size = cache.scrollBarSize;
3256 }
3257 return size;
3258 }
3259 return 0;
3260 }
3261 return 0;
3262 }
3263
3264 /**
3265 * <p>
3266 * Initializes the scrollbars from a given set of styled attributes. This
3267 * method should be called by subclasses that need scrollbars and when an
3268 * instance of these subclasses is created programmatically rather than
3269 * being inflated from XML. This method is automatically called when the XML
3270 * is inflated.
3271 * </p>
3272 *
3273 * @param a the styled attributes set to initialize the scrollbars from
3274 */
3275 protected void initializeScrollbars(TypedArray a) {
3276 initScrollCache();
3277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003279
Mike Cleronf116bf82009-09-27 19:14:12 -07003280 if (scrollabilityCache.scrollBar == null) {
3281 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3282 }
Joe Malin32736f02011-01-19 16:14:20 -08003283
Romain Guy8bda2482010-03-02 11:42:11 -08003284 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285
Mike Cleronf116bf82009-09-27 19:14:12 -07003286 if (!fadeScrollbars) {
3287 scrollabilityCache.state = ScrollabilityCache.ON;
3288 }
3289 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003290
3291
Mike Cleronf116bf82009-09-27 19:14:12 -07003292 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3293 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3294 .getScrollBarFadeDuration());
3295 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3296 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3297 ViewConfiguration.getScrollDefaultDelay());
3298
Joe Malin32736f02011-01-19 16:14:20 -08003299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3301 com.android.internal.R.styleable.View_scrollbarSize,
3302 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3303
3304 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3305 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3306
3307 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3308 if (thumb != null) {
3309 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3310 }
3311
3312 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3313 false);
3314 if (alwaysDraw) {
3315 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3316 }
3317
3318 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3319 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3320
3321 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3322 if (thumb != null) {
3323 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3324 }
3325
3326 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3327 false);
3328 if (alwaysDraw) {
3329 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3330 }
3331
3332 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003333 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 }
3335
3336 /**
3337 * <p>
3338 * Initalizes the scrollability cache if necessary.
3339 * </p>
3340 */
3341 private void initScrollCache() {
3342 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003343 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 }
3345 }
3346
3347 /**
Adam Powell20232d02010-12-08 21:08:53 -08003348 * Set the position of the vertical scroll bar. Should be one of
3349 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3350 * {@link #SCROLLBAR_POSITION_RIGHT}.
3351 *
3352 * @param position Where the vertical scroll bar should be positioned.
3353 */
3354 public void setVerticalScrollbarPosition(int position) {
3355 if (mVerticalScrollbarPosition != position) {
3356 mVerticalScrollbarPosition = position;
3357 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003358 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003359 }
3360 }
3361
3362 /**
3363 * @return The position where the vertical scroll bar will show, if applicable.
3364 * @see #setVerticalScrollbarPosition(int)
3365 */
3366 public int getVerticalScrollbarPosition() {
3367 return mVerticalScrollbarPosition;
3368 }
3369
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003370 ListenerInfo getListenerInfo() {
3371 if (mListenerInfo != null) {
3372 return mListenerInfo;
3373 }
3374 mListenerInfo = new ListenerInfo();
3375 return mListenerInfo;
3376 }
3377
Adam Powell20232d02010-12-08 21:08:53 -08003378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 * Register a callback to be invoked when focus of this view changed.
3380 *
3381 * @param l The callback that will run.
3382 */
3383 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003384 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 }
3386
3387 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003388 * Add a listener that will be called when the bounds of the view change due to
3389 * layout processing.
3390 *
3391 * @param listener The listener that will be called when layout bounds change.
3392 */
3393 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003394 ListenerInfo li = getListenerInfo();
3395 if (li.mOnLayoutChangeListeners == null) {
3396 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003397 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003398 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3399 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003400 }
Chet Haase21cd1382010-09-01 17:42:29 -07003401 }
3402
3403 /**
3404 * Remove a listener for layout changes.
3405 *
3406 * @param listener The listener for layout bounds change.
3407 */
3408 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003409 ListenerInfo li = mListenerInfo;
3410 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003411 return;
3412 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003413 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003414 }
3415
3416 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003417 * Add a listener for attach state changes.
3418 *
3419 * This listener will be called whenever this view is attached or detached
3420 * from a window. Remove the listener using
3421 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3422 *
3423 * @param listener Listener to attach
3424 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3425 */
3426 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003427 ListenerInfo li = getListenerInfo();
3428 if (li.mOnAttachStateChangeListeners == null) {
3429 li.mOnAttachStateChangeListeners
3430 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003431 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003432 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003433 }
3434
3435 /**
3436 * Remove a listener for attach state changes. The listener will receive no further
3437 * notification of window attach/detach events.
3438 *
3439 * @param listener Listener to remove
3440 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3441 */
3442 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003443 ListenerInfo li = mListenerInfo;
3444 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003445 return;
3446 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003447 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003448 }
3449
3450 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 * Returns the focus-change callback registered for this view.
3452 *
3453 * @return The callback, or null if one is not registered.
3454 */
3455 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003456 ListenerInfo li = mListenerInfo;
3457 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 }
3459
3460 /**
3461 * Register a callback to be invoked when this view is clicked. If this view is not
3462 * clickable, it becomes clickable.
3463 *
3464 * @param l The callback that will run
3465 *
3466 * @see #setClickable(boolean)
3467 */
3468 public void setOnClickListener(OnClickListener l) {
3469 if (!isClickable()) {
3470 setClickable(true);
3471 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003472 getListenerInfo().mOnClickListener = l;
3473 }
3474
3475 /**
3476 * Return whether this view has an attached OnClickListener. Returns
3477 * true if there is a listener, false if there is none.
3478 */
3479 public boolean hasOnClickListeners() {
3480 ListenerInfo li = mListenerInfo;
3481 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 }
3483
3484 /**
3485 * Register a callback to be invoked when this view is clicked and held. If this view is not
3486 * long clickable, it becomes long clickable.
3487 *
3488 * @param l The callback that will run
3489 *
3490 * @see #setLongClickable(boolean)
3491 */
3492 public void setOnLongClickListener(OnLongClickListener l) {
3493 if (!isLongClickable()) {
3494 setLongClickable(true);
3495 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003496 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 }
3498
3499 /**
3500 * Register a callback to be invoked when the context menu for this view is
3501 * being built. If this view is not long clickable, it becomes long clickable.
3502 *
3503 * @param l The callback that will run
3504 *
3505 */
3506 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3507 if (!isLongClickable()) {
3508 setLongClickable(true);
3509 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003510 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 }
3512
3513 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003514 * Call this view's OnClickListener, if it is defined. Performs all normal
3515 * actions associated with clicking: reporting accessibility event, playing
3516 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 *
3518 * @return True there was an assigned OnClickListener that was called, false
3519 * otherwise is returned.
3520 */
3521 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003522 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3523
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003524 ListenerInfo li = mListenerInfo;
3525 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003527 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 return true;
3529 }
3530
3531 return false;
3532 }
3533
3534 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003535 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3536 * this only calls the listener, and does not do any associated clicking
3537 * actions like reporting an accessibility event.
3538 *
3539 * @return True there was an assigned OnClickListener that was called, false
3540 * otherwise is returned.
3541 */
3542 public boolean callOnClick() {
3543 ListenerInfo li = mListenerInfo;
3544 if (li != null && li.mOnClickListener != null) {
3545 li.mOnClickListener.onClick(this);
3546 return true;
3547 }
3548 return false;
3549 }
3550
3551 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003552 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3553 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003555 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 */
3557 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003558 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003561 ListenerInfo li = mListenerInfo;
3562 if (li != null && li.mOnLongClickListener != null) {
3563 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 }
3565 if (!handled) {
3566 handled = showContextMenu();
3567 }
3568 if (handled) {
3569 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3570 }
3571 return handled;
3572 }
3573
3574 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003575 * Performs button-related actions during a touch down event.
3576 *
3577 * @param event The event.
3578 * @return True if the down was consumed.
3579 *
3580 * @hide
3581 */
3582 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3583 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3584 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3585 return true;
3586 }
3587 }
3588 return false;
3589 }
3590
3591 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 * Bring up the context menu for this view.
3593 *
3594 * @return Whether a context menu was displayed.
3595 */
3596 public boolean showContextMenu() {
3597 return getParent().showContextMenuForChild(this);
3598 }
3599
3600 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003601 * Bring up the context menu for this view, referring to the item under the specified point.
3602 *
3603 * @param x The referenced x coordinate.
3604 * @param y The referenced y coordinate.
3605 * @param metaState The keyboard modifiers that were pressed.
3606 * @return Whether a context menu was displayed.
3607 *
3608 * @hide
3609 */
3610 public boolean showContextMenu(float x, float y, int metaState) {
3611 return showContextMenu();
3612 }
3613
3614 /**
Adam Powell6e346362010-07-23 10:18:23 -07003615 * Start an action mode.
3616 *
3617 * @param callback Callback that will control the lifecycle of the action mode
3618 * @return The new action mode if it is started, null otherwise
3619 *
3620 * @see ActionMode
3621 */
3622 public ActionMode startActionMode(ActionMode.Callback callback) {
3623 return getParent().startActionModeForChild(this, callback);
3624 }
3625
3626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 * Register a callback to be invoked when a key is pressed in this view.
3628 * @param l the key listener to attach to this view
3629 */
3630 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003631 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 }
3633
3634 /**
3635 * Register a callback to be invoked when a touch event is sent to this view.
3636 * @param l the touch listener to attach to this view
3637 */
3638 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003639 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 }
3641
3642 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003643 * Register a callback to be invoked when a generic motion event is sent to this view.
3644 * @param l the generic motion listener to attach to this view
3645 */
3646 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003647 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003648 }
3649
3650 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003651 * Register a callback to be invoked when a hover event is sent to this view.
3652 * @param l the hover listener to attach to this view
3653 */
3654 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003655 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003656 }
3657
3658 /**
Joe Malin32736f02011-01-19 16:14:20 -08003659 * Register a drag event listener callback object for this View. The parameter is
3660 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3661 * View, the system calls the
3662 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3663 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003664 */
3665 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003666 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003667 }
3668
3669 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003670 * Give this view focus. This will cause
3671 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 *
3673 * Note: this does not check whether this {@link View} should get focus, it just
3674 * gives it focus no matter what. It should only be called internally by framework
3675 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3676 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003677 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3678 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 * focus moved when requestFocus() is called. It may not always
3680 * apply, in which case use the default View.FOCUS_DOWN.
3681 * @param previouslyFocusedRect The rectangle of the view that had focus
3682 * prior in this View's coordinate system.
3683 */
3684 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3685 if (DBG) {
3686 System.out.println(this + " requestFocus()");
3687 }
3688
3689 if ((mPrivateFlags & FOCUSED) == 0) {
3690 mPrivateFlags |= FOCUSED;
3691
3692 if (mParent != null) {
3693 mParent.requestChildFocus(this, this);
3694 }
3695
3696 onFocusChanged(true, direction, previouslyFocusedRect);
3697 refreshDrawableState();
3698 }
3699 }
3700
3701 /**
3702 * Request that a rectangle of this view be visible on the screen,
3703 * scrolling if necessary just enough.
3704 *
3705 * <p>A View should call this if it maintains some notion of which part
3706 * of its content is interesting. For example, a text editing view
3707 * should call this when its cursor moves.
3708 *
3709 * @param rectangle The rectangle.
3710 * @return Whether any parent scrolled.
3711 */
3712 public boolean requestRectangleOnScreen(Rect rectangle) {
3713 return requestRectangleOnScreen(rectangle, false);
3714 }
3715
3716 /**
3717 * Request that a rectangle of this view be visible on the screen,
3718 * scrolling if necessary just enough.
3719 *
3720 * <p>A View should call this if it maintains some notion of which part
3721 * of its content is interesting. For example, a text editing view
3722 * should call this when its cursor moves.
3723 *
3724 * <p>When <code>immediate</code> is set to true, scrolling will not be
3725 * animated.
3726 *
3727 * @param rectangle The rectangle.
3728 * @param immediate True to forbid animated scrolling, false otherwise
3729 * @return Whether any parent scrolled.
3730 */
3731 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3732 View child = this;
3733 ViewParent parent = mParent;
3734 boolean scrolled = false;
3735 while (parent != null) {
3736 scrolled |= parent.requestChildRectangleOnScreen(child,
3737 rectangle, immediate);
3738
3739 // offset rect so next call has the rectangle in the
3740 // coordinate system of its direct child.
3741 rectangle.offset(child.getLeft(), child.getTop());
3742 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3743
3744 if (!(parent instanceof View)) {
3745 break;
3746 }
Romain Guy8506ab42009-06-11 17:35:47 -07003747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 child = (View) parent;
3749 parent = child.getParent();
3750 }
3751 return scrolled;
3752 }
3753
3754 /**
3755 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003756 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 */
3758 public void clearFocus() {
3759 if (DBG) {
3760 System.out.println(this + " clearFocus()");
3761 }
3762
3763 if ((mPrivateFlags & FOCUSED) != 0) {
3764 mPrivateFlags &= ~FOCUSED;
3765
3766 if (mParent != null) {
3767 mParent.clearChildFocus(this);
3768 }
3769
3770 onFocusChanged(false, 0, null);
3771 refreshDrawableState();
3772 }
3773 }
3774
3775 /**
3776 * Called to clear the focus of a view that is about to be removed.
3777 * Doesn't call clearChildFocus, which prevents this view from taking
3778 * focus again before it has been removed from the parent
3779 */
3780 void clearFocusForRemoval() {
3781 if ((mPrivateFlags & FOCUSED) != 0) {
3782 mPrivateFlags &= ~FOCUSED;
3783
3784 onFocusChanged(false, 0, null);
3785 refreshDrawableState();
3786 }
3787 }
3788
3789 /**
3790 * Called internally by the view system when a new view is getting focus.
3791 * This is what clears the old focus.
3792 */
3793 void unFocus() {
3794 if (DBG) {
3795 System.out.println(this + " unFocus()");
3796 }
3797
3798 if ((mPrivateFlags & FOCUSED) != 0) {
3799 mPrivateFlags &= ~FOCUSED;
3800
3801 onFocusChanged(false, 0, null);
3802 refreshDrawableState();
3803 }
3804 }
3805
3806 /**
3807 * Returns true if this view has focus iteself, or is the ancestor of the
3808 * view that has focus.
3809 *
3810 * @return True if this view has or contains focus, false otherwise.
3811 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003812 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 public boolean hasFocus() {
3814 return (mPrivateFlags & FOCUSED) != 0;
3815 }
3816
3817 /**
3818 * Returns true if this view is focusable or if it contains a reachable View
3819 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3820 * is a View whose parents do not block descendants focus.
3821 *
3822 * Only {@link #VISIBLE} views are considered focusable.
3823 *
3824 * @return True if the view is focusable or if the view contains a focusable
3825 * View, false otherwise.
3826 *
3827 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3828 */
3829 public boolean hasFocusable() {
3830 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3831 }
3832
3833 /**
3834 * Called by the view system when the focus state of this view changes.
3835 * When the focus change event is caused by directional navigation, direction
3836 * and previouslyFocusedRect provide insight into where the focus is coming from.
3837 * When overriding, be sure to call up through to the super class so that
3838 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003839 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 * @param gainFocus True if the View has focus; false otherwise.
3841 * @param direction The direction focus has moved when requestFocus()
3842 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003843 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3844 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3845 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3847 * system, of the previously focused view. If applicable, this will be
3848 * passed in as finer grained information about where the focus is coming
3849 * from (in addition to direction). Will be <code>null</code> otherwise.
3850 */
3851 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003852 if (gainFocus) {
3853 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3854 }
3855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 InputMethodManager imm = InputMethodManager.peekInstance();
3857 if (!gainFocus) {
3858 if (isPressed()) {
3859 setPressed(false);
3860 }
3861 if (imm != null && mAttachInfo != null
3862 && mAttachInfo.mHasWindowFocus) {
3863 imm.focusOut(this);
3864 }
Romain Guya2431d02009-04-30 16:30:00 -07003865 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 } else if (imm != null && mAttachInfo != null
3867 && mAttachInfo.mHasWindowFocus) {
3868 imm.focusIn(this);
3869 }
Romain Guy8506ab42009-06-11 17:35:47 -07003870
Romain Guy0fd89bf2011-01-26 15:41:30 -08003871 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003872 ListenerInfo li = mListenerInfo;
3873 if (li != null && li.mOnFocusChangeListener != null) {
3874 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 }
Joe Malin32736f02011-01-19 16:14:20 -08003876
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003877 if (mAttachInfo != null) {
3878 mAttachInfo.mKeyDispatchState.reset(this);
3879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 }
3881
3882 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003883 * Sends an accessibility event of the given type. If accessiiblity is
3884 * not enabled this method has no effect. The default implementation calls
3885 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3886 * to populate information about the event source (this View), then calls
3887 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3888 * populate the text content of the event source including its descendants,
3889 * and last calls
3890 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3891 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003892 * <p>
3893 * If an {@link AccessibilityDelegate} has been specified via calling
3894 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3895 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3896 * responsible for handling this call.
3897 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003898 *
Scott Mainb303d832011-10-12 16:45:18 -07003899 * @param eventType The type of the event to send, as defined by several types from
3900 * {@link android.view.accessibility.AccessibilityEvent}, such as
3901 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3902 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003903 *
3904 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3905 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3906 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003907 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003908 */
3909 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003910 if (mAccessibilityDelegate != null) {
3911 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3912 } else {
3913 sendAccessibilityEventInternal(eventType);
3914 }
3915 }
3916
3917 /**
3918 * @see #sendAccessibilityEvent(int)
3919 *
3920 * Note: Called from the default {@link AccessibilityDelegate}.
3921 */
3922 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003923 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3924 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3925 }
3926 }
3927
3928 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003929 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3930 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003931 * perform a check whether accessibility is enabled.
3932 * <p>
3933 * If an {@link AccessibilityDelegate} has been specified via calling
3934 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3935 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3936 * is responsible for handling this call.
3937 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003938 *
3939 * @param event The event to send.
3940 *
3941 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003942 */
3943 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003944 if (mAccessibilityDelegate != null) {
3945 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3946 } else {
3947 sendAccessibilityEventUncheckedInternal(event);
3948 }
3949 }
3950
3951 /**
3952 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3953 *
3954 * Note: Called from the default {@link AccessibilityDelegate}.
3955 */
3956 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003957 if (!isShown()) {
3958 return;
3959 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003960 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003961 // Only a subset of accessibility events populates text content.
3962 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3963 dispatchPopulateAccessibilityEvent(event);
3964 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003965 // In the beginning we called #isShown(), so we know that getParent() is not null.
3966 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003967 }
3968
3969 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003970 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3971 * to its children for adding their text content to the event. Note that the
3972 * event text is populated in a separate dispatch path since we add to the
3973 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003974 * A typical implementation will call
3975 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3976 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3977 * on each child. Override this method if custom population of the event text
3978 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003979 * <p>
3980 * If an {@link AccessibilityDelegate} has been specified via calling
3981 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3982 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3983 * is responsible for handling this call.
3984 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003985 * <p>
3986 * <em>Note:</em> Accessibility events of certain types are not dispatched for
3987 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
3988 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003989 *
3990 * @param event The event.
3991 *
3992 * @return True if the event population was completed.
3993 */
3994 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003995 if (mAccessibilityDelegate != null) {
3996 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3997 } else {
3998 return dispatchPopulateAccessibilityEventInternal(event);
3999 }
4000 }
4001
4002 /**
4003 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4004 *
4005 * Note: Called from the default {@link AccessibilityDelegate}.
4006 */
4007 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004008 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004009 return false;
4010 }
4011
4012 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004013 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004014 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004015 * text content. While this method is free to modify event
4016 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004017 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4018 * <p>
4019 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004020 * to the text added by the super implementation:
4021 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004022 * super.onPopulateAccessibilityEvent(event);
4023 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4024 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4025 * mCurrentDate.getTimeInMillis(), flags);
4026 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004027 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004028 * <p>
4029 * If an {@link AccessibilityDelegate} has been specified via calling
4030 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4031 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4032 * is responsible for handling this call.
4033 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004034 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4035 * information to the event, in case the default implementation has basic information to add.
4036 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004037 *
4038 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004039 *
4040 * @see #sendAccessibilityEvent(int)
4041 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004042 */
4043 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004044 if (mAccessibilityDelegate != null) {
4045 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4046 } else {
4047 onPopulateAccessibilityEventInternal(event);
4048 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004049 }
4050
4051 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004052 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4053 *
4054 * Note: Called from the default {@link AccessibilityDelegate}.
4055 */
4056 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4057
4058 }
4059
4060 /**
4061 * Initializes an {@link AccessibilityEvent} with information about
4062 * this View which is the event source. In other words, the source of
4063 * an accessibility event is the view whose state change triggered firing
4064 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004065 * <p>
4066 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004067 * to properties set by the super implementation:
4068 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4069 * super.onInitializeAccessibilityEvent(event);
4070 * event.setPassword(true);
4071 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004072 * <p>
4073 * If an {@link AccessibilityDelegate} has been specified via calling
4074 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4075 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4076 * is responsible for handling this call.
4077 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004078 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4079 * information to the event, in case the default implementation has basic information to add.
4080 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004081 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004082 *
4083 * @see #sendAccessibilityEvent(int)
4084 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4085 */
4086 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004087 if (mAccessibilityDelegate != null) {
4088 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4089 } else {
4090 onInitializeAccessibilityEventInternal(event);
4091 }
4092 }
4093
4094 /**
4095 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4096 *
4097 * Note: Called from the default {@link AccessibilityDelegate}.
4098 */
4099 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004100 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07004101 event.setClassName(getClass().getName());
4102 event.setPackageName(getContext().getPackageName());
4103 event.setEnabled(isEnabled());
4104 event.setContentDescription(mContentDescription);
4105
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004106 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4107 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4108 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4109 FOCUSABLES_ALL);
4110 event.setItemCount(focusablesTempList.size());
4111 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4112 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004113 }
4114 }
4115
4116 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004117 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4118 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4119 * This method is responsible for obtaining an accessibility node info from a
4120 * pool of reusable instances and calling
4121 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4122 * initialize the former.
4123 * <p>
4124 * Note: The client is responsible for recycling the obtained instance by calling
4125 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4126 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004127 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004128 * @return A populated {@link AccessibilityNodeInfo}.
4129 *
4130 * @see AccessibilityNodeInfo
4131 */
4132 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004133 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4134 if (provider != null) {
4135 return provider.createAccessibilityNodeInfo(View.NO_ID);
4136 } else {
4137 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4138 onInitializeAccessibilityNodeInfo(info);
4139 return info;
4140 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004141 }
4142
4143 /**
4144 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4145 * The base implementation sets:
4146 * <ul>
4147 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004148 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4149 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004150 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4151 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4152 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4153 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4154 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4155 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4156 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4157 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4158 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4159 * </ul>
4160 * <p>
4161 * Subclasses should override this method, call the super implementation,
4162 * and set additional attributes.
4163 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004164 * <p>
4165 * If an {@link AccessibilityDelegate} has been specified via calling
4166 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4167 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4168 * is responsible for handling this call.
4169 * </p>
4170 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004171 * @param info The instance to initialize.
4172 */
4173 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004174 if (mAccessibilityDelegate != null) {
4175 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4176 } else {
4177 onInitializeAccessibilityNodeInfoInternal(info);
4178 }
4179 }
4180
4181 /**
4182 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4183 *
4184 * Note: Called from the default {@link AccessibilityDelegate}.
4185 */
4186 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004187 Rect bounds = mAttachInfo.mTmpInvalRect;
4188 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004189 info.setBoundsInParent(bounds);
4190
4191 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4192 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004193 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004194 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4195 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004196
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004197 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4198 ViewParent parent = getParent();
4199 if (parent instanceof View) {
4200 View parentView = (View) parent;
4201 info.setParent(parentView);
4202 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004203 }
4204
4205 info.setPackageName(mContext.getPackageName());
4206 info.setClassName(getClass().getName());
4207 info.setContentDescription(getContentDescription());
4208
4209 info.setEnabled(isEnabled());
4210 info.setClickable(isClickable());
4211 info.setFocusable(isFocusable());
4212 info.setFocused(isFocused());
4213 info.setSelected(isSelected());
4214 info.setLongClickable(isLongClickable());
4215
4216 // TODO: These make sense only if we are in an AdapterView but all
4217 // views can be selected. Maybe from accessiiblity perspective
4218 // we should report as selectable view in an AdapterView.
4219 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4220 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4221
4222 if (isFocusable()) {
4223 if (isFocused()) {
4224 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4225 } else {
4226 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4227 }
4228 }
4229 }
4230
4231 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004232 * Sets a delegate for implementing accessibility support via compositon as
4233 * opposed to inheritance. The delegate's primary use is for implementing
4234 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4235 *
4236 * @param delegate The delegate instance.
4237 *
4238 * @see AccessibilityDelegate
4239 */
4240 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4241 mAccessibilityDelegate = delegate;
4242 }
4243
4244 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004245 * Gets the provider for managing a virtual view hierarchy rooted at this View
4246 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4247 * that explore the window content.
4248 * <p>
4249 * If this method returns an instance, this instance is responsible for managing
4250 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4251 * View including the one representing the View itself. Similarly the returned
4252 * instance is responsible for performing accessibility actions on any virtual
4253 * view or the root view itself.
4254 * </p>
4255 * <p>
4256 * If an {@link AccessibilityDelegate} has been specified via calling
4257 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4258 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4259 * is responsible for handling this call.
4260 * </p>
4261 *
4262 * @return The provider.
4263 *
4264 * @see AccessibilityNodeProvider
4265 */
4266 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4267 if (mAccessibilityDelegate != null) {
4268 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4269 } else {
4270 return null;
4271 }
4272 }
4273
4274 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004275 * Gets the unique identifier of this view on the screen for accessibility purposes.
4276 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4277 *
4278 * @return The view accessibility id.
4279 *
4280 * @hide
4281 */
4282 public int getAccessibilityViewId() {
4283 if (mAccessibilityViewId == NO_ID) {
4284 mAccessibilityViewId = sNextAccessibilityViewId++;
4285 }
4286 return mAccessibilityViewId;
4287 }
4288
4289 /**
4290 * Gets the unique identifier of the window in which this View reseides.
4291 *
4292 * @return The window accessibility id.
4293 *
4294 * @hide
4295 */
4296 public int getAccessibilityWindowId() {
4297 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4298 }
4299
4300 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004301 * Gets the {@link View} description. It briefly describes the view and is
4302 * primarily used for accessibility support. Set this property to enable
4303 * better accessibility support for your application. This is especially
4304 * true for views that do not have textual representation (For example,
4305 * ImageButton).
4306 *
4307 * @return The content descriptiopn.
4308 *
4309 * @attr ref android.R.styleable#View_contentDescription
4310 */
4311 public CharSequence getContentDescription() {
4312 return mContentDescription;
4313 }
4314
4315 /**
4316 * Sets the {@link View} description. It briefly describes the view and is
4317 * primarily used for accessibility support. Set this property to enable
4318 * better accessibility support for your application. This is especially
4319 * true for views that do not have textual representation (For example,
4320 * ImageButton).
4321 *
4322 * @param contentDescription The content description.
4323 *
4324 * @attr ref android.R.styleable#View_contentDescription
4325 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004326 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004327 public void setContentDescription(CharSequence contentDescription) {
4328 mContentDescription = contentDescription;
4329 }
4330
4331 /**
Romain Guya2431d02009-04-30 16:30:00 -07004332 * Invoked whenever this view loses focus, either by losing window focus or by losing
4333 * focus within its window. This method can be used to clear any state tied to the
4334 * focus. For instance, if a button is held pressed with the trackball and the window
4335 * loses focus, this method can be used to cancel the press.
4336 *
4337 * Subclasses of View overriding this method should always call super.onFocusLost().
4338 *
4339 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004340 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004341 *
4342 * @hide pending API council approval
4343 */
4344 protected void onFocusLost() {
4345 resetPressedState();
4346 }
4347
4348 private void resetPressedState() {
4349 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4350 return;
4351 }
4352
4353 if (isPressed()) {
4354 setPressed(false);
4355
4356 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004357 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004358 }
4359 }
4360 }
4361
4362 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 * Returns true if this view has focus
4364 *
4365 * @return True if this view has focus, false otherwise.
4366 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004367 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 public boolean isFocused() {
4369 return (mPrivateFlags & FOCUSED) != 0;
4370 }
4371
4372 /**
4373 * Find the view in the hierarchy rooted at this view that currently has
4374 * focus.
4375 *
4376 * @return The view that currently has focus, or null if no focused view can
4377 * be found.
4378 */
4379 public View findFocus() {
4380 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4381 }
4382
4383 /**
4384 * Change whether this view is one of the set of scrollable containers in
4385 * its window. This will be used to determine whether the window can
4386 * resize or must pan when a soft input area is open -- scrollable
4387 * containers allow the window to use resize mode since the container
4388 * will appropriately shrink.
4389 */
4390 public void setScrollContainer(boolean isScrollContainer) {
4391 if (isScrollContainer) {
4392 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4393 mAttachInfo.mScrollContainers.add(this);
4394 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4395 }
4396 mPrivateFlags |= SCROLL_CONTAINER;
4397 } else {
4398 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4399 mAttachInfo.mScrollContainers.remove(this);
4400 }
4401 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4402 }
4403 }
4404
4405 /**
4406 * Returns the quality of the drawing cache.
4407 *
4408 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4409 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4410 *
4411 * @see #setDrawingCacheQuality(int)
4412 * @see #setDrawingCacheEnabled(boolean)
4413 * @see #isDrawingCacheEnabled()
4414 *
4415 * @attr ref android.R.styleable#View_drawingCacheQuality
4416 */
4417 public int getDrawingCacheQuality() {
4418 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4419 }
4420
4421 /**
4422 * Set the drawing cache quality of this view. This value is used only when the
4423 * drawing cache is enabled
4424 *
4425 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4426 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4427 *
4428 * @see #getDrawingCacheQuality()
4429 * @see #setDrawingCacheEnabled(boolean)
4430 * @see #isDrawingCacheEnabled()
4431 *
4432 * @attr ref android.R.styleable#View_drawingCacheQuality
4433 */
4434 public void setDrawingCacheQuality(int quality) {
4435 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4436 }
4437
4438 /**
4439 * Returns whether the screen should remain on, corresponding to the current
4440 * value of {@link #KEEP_SCREEN_ON}.
4441 *
4442 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4443 *
4444 * @see #setKeepScreenOn(boolean)
4445 *
4446 * @attr ref android.R.styleable#View_keepScreenOn
4447 */
4448 public boolean getKeepScreenOn() {
4449 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4450 }
4451
4452 /**
4453 * Controls whether the screen should remain on, modifying the
4454 * value of {@link #KEEP_SCREEN_ON}.
4455 *
4456 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4457 *
4458 * @see #getKeepScreenOn()
4459 *
4460 * @attr ref android.R.styleable#View_keepScreenOn
4461 */
4462 public void setKeepScreenOn(boolean keepScreenOn) {
4463 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4464 }
4465
4466 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004467 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4468 * @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 -08004469 *
4470 * @attr ref android.R.styleable#View_nextFocusLeft
4471 */
4472 public int getNextFocusLeftId() {
4473 return mNextFocusLeftId;
4474 }
4475
4476 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004477 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4478 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4479 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 *
4481 * @attr ref android.R.styleable#View_nextFocusLeft
4482 */
4483 public void setNextFocusLeftId(int nextFocusLeftId) {
4484 mNextFocusLeftId = nextFocusLeftId;
4485 }
4486
4487 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004488 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4489 * @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 -08004490 *
4491 * @attr ref android.R.styleable#View_nextFocusRight
4492 */
4493 public int getNextFocusRightId() {
4494 return mNextFocusRightId;
4495 }
4496
4497 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004498 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4499 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4500 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 *
4502 * @attr ref android.R.styleable#View_nextFocusRight
4503 */
4504 public void setNextFocusRightId(int nextFocusRightId) {
4505 mNextFocusRightId = nextFocusRightId;
4506 }
4507
4508 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004509 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4510 * @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 -08004511 *
4512 * @attr ref android.R.styleable#View_nextFocusUp
4513 */
4514 public int getNextFocusUpId() {
4515 return mNextFocusUpId;
4516 }
4517
4518 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004519 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4520 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4521 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 *
4523 * @attr ref android.R.styleable#View_nextFocusUp
4524 */
4525 public void setNextFocusUpId(int nextFocusUpId) {
4526 mNextFocusUpId = nextFocusUpId;
4527 }
4528
4529 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004530 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4531 * @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 -08004532 *
4533 * @attr ref android.R.styleable#View_nextFocusDown
4534 */
4535 public int getNextFocusDownId() {
4536 return mNextFocusDownId;
4537 }
4538
4539 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004540 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4541 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4542 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 *
4544 * @attr ref android.R.styleable#View_nextFocusDown
4545 */
4546 public void setNextFocusDownId(int nextFocusDownId) {
4547 mNextFocusDownId = nextFocusDownId;
4548 }
4549
4550 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004551 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4552 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4553 *
4554 * @attr ref android.R.styleable#View_nextFocusForward
4555 */
4556 public int getNextFocusForwardId() {
4557 return mNextFocusForwardId;
4558 }
4559
4560 /**
4561 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4562 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4563 * decide automatically.
4564 *
4565 * @attr ref android.R.styleable#View_nextFocusForward
4566 */
4567 public void setNextFocusForwardId(int nextFocusForwardId) {
4568 mNextFocusForwardId = nextFocusForwardId;
4569 }
4570
4571 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004572 * Returns the visibility of this view and all of its ancestors
4573 *
4574 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4575 */
4576 public boolean isShown() {
4577 View current = this;
4578 //noinspection ConstantConditions
4579 do {
4580 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4581 return false;
4582 }
4583 ViewParent parent = current.mParent;
4584 if (parent == null) {
4585 return false; // We are not attached to the view root
4586 }
4587 if (!(parent instanceof View)) {
4588 return true;
4589 }
4590 current = (View) parent;
4591 } while (current != null);
4592
4593 return false;
4594 }
4595
4596 /**
4597 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4598 * is set
4599 *
4600 * @param insets Insets for system windows
4601 *
4602 * @return True if this view applied the insets, false otherwise
4603 */
4604 protected boolean fitSystemWindows(Rect insets) {
4605 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4606 mPaddingLeft = insets.left;
4607 mPaddingTop = insets.top;
4608 mPaddingRight = insets.right;
4609 mPaddingBottom = insets.bottom;
4610 requestLayout();
4611 return true;
4612 }
4613 return false;
4614 }
4615
4616 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004617 * Set whether or not this view should account for system screen decorations
4618 * such as the status bar and inset its content. This allows this view to be
4619 * positioned in absolute screen coordinates and remain visible to the user.
4620 *
4621 * <p>This should only be used by top-level window decor views.
4622 *
4623 * @param fitSystemWindows true to inset content for system screen decorations, false for
4624 * default behavior.
4625 *
4626 * @attr ref android.R.styleable#View_fitsSystemWindows
4627 */
4628 public void setFitsSystemWindows(boolean fitSystemWindows) {
4629 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4630 }
4631
4632 /**
4633 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4634 * will account for system screen decorations such as the status bar and inset its
4635 * content. This allows the view to be positioned in absolute screen coordinates
4636 * and remain visible to the user.
4637 *
4638 * @return true if this view will adjust its content bounds for system screen decorations.
4639 *
4640 * @attr ref android.R.styleable#View_fitsSystemWindows
4641 */
4642 public boolean fitsSystemWindows() {
4643 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4644 }
4645
4646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004647 * Returns the visibility status for this view.
4648 *
4649 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4650 * @attr ref android.R.styleable#View_visibility
4651 */
4652 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004653 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4654 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4655 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004656 })
4657 public int getVisibility() {
4658 return mViewFlags & VISIBILITY_MASK;
4659 }
4660
4661 /**
4662 * Set the enabled state of this view.
4663 *
4664 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4665 * @attr ref android.R.styleable#View_visibility
4666 */
4667 @RemotableViewMethod
4668 public void setVisibility(int visibility) {
4669 setFlags(visibility, VISIBILITY_MASK);
4670 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4671 }
4672
4673 /**
4674 * Returns the enabled status for this view. The interpretation of the
4675 * enabled state varies by subclass.
4676 *
4677 * @return True if this view is enabled, false otherwise.
4678 */
4679 @ViewDebug.ExportedProperty
4680 public boolean isEnabled() {
4681 return (mViewFlags & ENABLED_MASK) == ENABLED;
4682 }
4683
4684 /**
4685 * Set the enabled state of this view. The interpretation of the enabled
4686 * state varies by subclass.
4687 *
4688 * @param enabled True if this view is enabled, false otherwise.
4689 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004690 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004692 if (enabled == isEnabled()) return;
4693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4695
4696 /*
4697 * The View most likely has to change its appearance, so refresh
4698 * the drawable state.
4699 */
4700 refreshDrawableState();
4701
4702 // Invalidate too, since the default behavior for views is to be
4703 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004704 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 }
4706
4707 /**
4708 * Set whether this view can receive the focus.
4709 *
4710 * Setting this to false will also ensure that this view is not focusable
4711 * in touch mode.
4712 *
4713 * @param focusable If true, this view can receive the focus.
4714 *
4715 * @see #setFocusableInTouchMode(boolean)
4716 * @attr ref android.R.styleable#View_focusable
4717 */
4718 public void setFocusable(boolean focusable) {
4719 if (!focusable) {
4720 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4721 }
4722 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4723 }
4724
4725 /**
4726 * Set whether this view can receive focus while in touch mode.
4727 *
4728 * Setting this to true will also ensure that this view is focusable.
4729 *
4730 * @param focusableInTouchMode If true, this view can receive the focus while
4731 * in touch mode.
4732 *
4733 * @see #setFocusable(boolean)
4734 * @attr ref android.R.styleable#View_focusableInTouchMode
4735 */
4736 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4737 // Focusable in touch mode should always be set before the focusable flag
4738 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4739 // which, in touch mode, will not successfully request focus on this view
4740 // because the focusable in touch mode flag is not set
4741 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4742 if (focusableInTouchMode) {
4743 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4744 }
4745 }
4746
4747 /**
4748 * Set whether this view should have sound effects enabled for events such as
4749 * clicking and touching.
4750 *
4751 * <p>You may wish to disable sound effects for a view if you already play sounds,
4752 * for instance, a dial key that plays dtmf tones.
4753 *
4754 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4755 * @see #isSoundEffectsEnabled()
4756 * @see #playSoundEffect(int)
4757 * @attr ref android.R.styleable#View_soundEffectsEnabled
4758 */
4759 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4760 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4761 }
4762
4763 /**
4764 * @return whether this view should have sound effects enabled for events such as
4765 * clicking and touching.
4766 *
4767 * @see #setSoundEffectsEnabled(boolean)
4768 * @see #playSoundEffect(int)
4769 * @attr ref android.R.styleable#View_soundEffectsEnabled
4770 */
4771 @ViewDebug.ExportedProperty
4772 public boolean isSoundEffectsEnabled() {
4773 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4774 }
4775
4776 /**
4777 * Set whether this view should have haptic feedback for events such as
4778 * long presses.
4779 *
4780 * <p>You may wish to disable haptic feedback if your view already controls
4781 * its own haptic feedback.
4782 *
4783 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4784 * @see #isHapticFeedbackEnabled()
4785 * @see #performHapticFeedback(int)
4786 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4787 */
4788 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4789 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4790 }
4791
4792 /**
4793 * @return whether this view should have haptic feedback enabled for events
4794 * long presses.
4795 *
4796 * @see #setHapticFeedbackEnabled(boolean)
4797 * @see #performHapticFeedback(int)
4798 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4799 */
4800 @ViewDebug.ExportedProperty
4801 public boolean isHapticFeedbackEnabled() {
4802 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4803 }
4804
4805 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004806 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004807 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004808 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4809 * {@link #LAYOUT_DIRECTION_RTL},
4810 * {@link #LAYOUT_DIRECTION_INHERIT} or
4811 * {@link #LAYOUT_DIRECTION_LOCALE}.
4812 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004813 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004814 * @hide
4815 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004816 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004817 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4818 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4819 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4820 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004821 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004822 public int getLayoutDirection() {
4823 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004824 }
4825
4826 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004827 * Set the layout direction for this view. This will propagate a reset of layout direction
4828 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004829 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004830 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4831 * {@link #LAYOUT_DIRECTION_RTL},
4832 * {@link #LAYOUT_DIRECTION_INHERIT} or
4833 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004834 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004835 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004836 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004837 * @hide
4838 */
4839 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004840 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004841 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004842 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004843 // Setting the flag will also request a layout.
4844 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4845 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004846 }
4847
4848 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004849 * Returns the resolved layout direction for this view.
4850 *
4851 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4852 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4853 *
4854 * @hide
4855 */
4856 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4857 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4858 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4859 })
4860 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004861 resolveLayoutDirectionIfNeeded();
4862 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004863 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4864 }
4865
4866 /**
4867 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4868 * layout attribute and/or the inherited value from the parent.</p>
4869 *
4870 * @return true if the layout is right-to-left.
4871 *
4872 * @hide
4873 */
4874 @ViewDebug.ExportedProperty(category = "layout")
4875 public boolean isLayoutRtl() {
4876 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4877 }
4878
4879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 * If this view doesn't do any drawing on its own, set this flag to
4881 * allow further optimizations. By default, this flag is not set on
4882 * View, but could be set on some View subclasses such as ViewGroup.
4883 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004884 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4885 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 *
4887 * @param willNotDraw whether or not this View draw on its own
4888 */
4889 public void setWillNotDraw(boolean willNotDraw) {
4890 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4891 }
4892
4893 /**
4894 * Returns whether or not this View draws on its own.
4895 *
4896 * @return true if this view has nothing to draw, false otherwise
4897 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004898 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 public boolean willNotDraw() {
4900 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4901 }
4902
4903 /**
4904 * When a View's drawing cache is enabled, drawing is redirected to an
4905 * offscreen bitmap. Some views, like an ImageView, must be able to
4906 * bypass this mechanism if they already draw a single bitmap, to avoid
4907 * unnecessary usage of the memory.
4908 *
4909 * @param willNotCacheDrawing true if this view does not cache its
4910 * drawing, false otherwise
4911 */
4912 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4913 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4914 }
4915
4916 /**
4917 * Returns whether or not this View can cache its drawing or not.
4918 *
4919 * @return true if this view does not cache its drawing, false otherwise
4920 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004921 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004922 public boolean willNotCacheDrawing() {
4923 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4924 }
4925
4926 /**
4927 * Indicates whether this view reacts to click events or not.
4928 *
4929 * @return true if the view is clickable, false otherwise
4930 *
4931 * @see #setClickable(boolean)
4932 * @attr ref android.R.styleable#View_clickable
4933 */
4934 @ViewDebug.ExportedProperty
4935 public boolean isClickable() {
4936 return (mViewFlags & CLICKABLE) == CLICKABLE;
4937 }
4938
4939 /**
4940 * Enables or disables click events for this view. When a view
4941 * is clickable it will change its state to "pressed" on every click.
4942 * Subclasses should set the view clickable to visually react to
4943 * user's clicks.
4944 *
4945 * @param clickable true to make the view clickable, false otherwise
4946 *
4947 * @see #isClickable()
4948 * @attr ref android.R.styleable#View_clickable
4949 */
4950 public void setClickable(boolean clickable) {
4951 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4952 }
4953
4954 /**
4955 * Indicates whether this view reacts to long click events or not.
4956 *
4957 * @return true if the view is long clickable, false otherwise
4958 *
4959 * @see #setLongClickable(boolean)
4960 * @attr ref android.R.styleable#View_longClickable
4961 */
4962 public boolean isLongClickable() {
4963 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4964 }
4965
4966 /**
4967 * Enables or disables long click events for this view. When a view is long
4968 * clickable it reacts to the user holding down the button for a longer
4969 * duration than a tap. This event can either launch the listener or a
4970 * context menu.
4971 *
4972 * @param longClickable true to make the view long clickable, false otherwise
4973 * @see #isLongClickable()
4974 * @attr ref android.R.styleable#View_longClickable
4975 */
4976 public void setLongClickable(boolean longClickable) {
4977 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4978 }
4979
4980 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004981 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 *
4983 * @see #isClickable()
4984 * @see #setClickable(boolean)
4985 *
4986 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4987 * the View's internal state from a previously set "pressed" state.
4988 */
4989 public void setPressed(boolean pressed) {
4990 if (pressed) {
4991 mPrivateFlags |= PRESSED;
4992 } else {
4993 mPrivateFlags &= ~PRESSED;
4994 }
4995 refreshDrawableState();
4996 dispatchSetPressed(pressed);
4997 }
4998
4999 /**
5000 * Dispatch setPressed to all of this View's children.
5001 *
5002 * @see #setPressed(boolean)
5003 *
5004 * @param pressed The new pressed state
5005 */
5006 protected void dispatchSetPressed(boolean pressed) {
5007 }
5008
5009 /**
5010 * Indicates whether the view is currently in pressed state. Unless
5011 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5012 * the pressed state.
5013 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005014 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 * @see #isClickable()
5016 * @see #setClickable(boolean)
5017 *
5018 * @return true if the view is currently pressed, false otherwise
5019 */
5020 public boolean isPressed() {
5021 return (mPrivateFlags & PRESSED) == PRESSED;
5022 }
5023
5024 /**
5025 * Indicates whether this view will save its state (that is,
5026 * whether its {@link #onSaveInstanceState} method will be called).
5027 *
5028 * @return Returns true if the view state saving is enabled, else false.
5029 *
5030 * @see #setSaveEnabled(boolean)
5031 * @attr ref android.R.styleable#View_saveEnabled
5032 */
5033 public boolean isSaveEnabled() {
5034 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5035 }
5036
5037 /**
5038 * Controls whether the saving of this view's state is
5039 * enabled (that is, whether its {@link #onSaveInstanceState} method
5040 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005041 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 * for its state to be saved. This flag can only disable the
5043 * saving of this view; any child views may still have their state saved.
5044 *
5045 * @param enabled Set to false to <em>disable</em> state saving, or true
5046 * (the default) to allow it.
5047 *
5048 * @see #isSaveEnabled()
5049 * @see #setId(int)
5050 * @see #onSaveInstanceState()
5051 * @attr ref android.R.styleable#View_saveEnabled
5052 */
5053 public void setSaveEnabled(boolean enabled) {
5054 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5055 }
5056
Jeff Brown85a31762010-09-01 17:01:00 -07005057 /**
5058 * Gets whether the framework should discard touches when the view's
5059 * window is obscured by another visible window.
5060 * Refer to the {@link View} security documentation for more details.
5061 *
5062 * @return True if touch filtering is enabled.
5063 *
5064 * @see #setFilterTouchesWhenObscured(boolean)
5065 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5066 */
5067 @ViewDebug.ExportedProperty
5068 public boolean getFilterTouchesWhenObscured() {
5069 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5070 }
5071
5072 /**
5073 * Sets whether the framework should discard touches when the view's
5074 * window is obscured by another visible window.
5075 * Refer to the {@link View} security documentation for more details.
5076 *
5077 * @param enabled True if touch filtering should be enabled.
5078 *
5079 * @see #getFilterTouchesWhenObscured
5080 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5081 */
5082 public void setFilterTouchesWhenObscured(boolean enabled) {
5083 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5084 FILTER_TOUCHES_WHEN_OBSCURED);
5085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086
5087 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005088 * Indicates whether the entire hierarchy under this view will save its
5089 * state when a state saving traversal occurs from its parent. The default
5090 * is true; if false, these views will not be saved unless
5091 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5092 *
5093 * @return Returns true if the view state saving from parent is enabled, else false.
5094 *
5095 * @see #setSaveFromParentEnabled(boolean)
5096 */
5097 public boolean isSaveFromParentEnabled() {
5098 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5099 }
5100
5101 /**
5102 * Controls whether the entire hierarchy under this view will save its
5103 * state when a state saving traversal occurs from its parent. The default
5104 * is true; if false, these views will not be saved unless
5105 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5106 *
5107 * @param enabled Set to false to <em>disable</em> state saving, or true
5108 * (the default) to allow it.
5109 *
5110 * @see #isSaveFromParentEnabled()
5111 * @see #setId(int)
5112 * @see #onSaveInstanceState()
5113 */
5114 public void setSaveFromParentEnabled(boolean enabled) {
5115 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5116 }
5117
5118
5119 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 * Returns whether this View is able to take focus.
5121 *
5122 * @return True if this view can take focus, or false otherwise.
5123 * @attr ref android.R.styleable#View_focusable
5124 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005125 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 public final boolean isFocusable() {
5127 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5128 }
5129
5130 /**
5131 * When a view is focusable, it may not want to take focus when in touch mode.
5132 * For example, a button would like focus when the user is navigating via a D-pad
5133 * so that the user can click on it, but once the user starts touching the screen,
5134 * the button shouldn't take focus
5135 * @return Whether the view is focusable in touch mode.
5136 * @attr ref android.R.styleable#View_focusableInTouchMode
5137 */
5138 @ViewDebug.ExportedProperty
5139 public final boolean isFocusableInTouchMode() {
5140 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5141 }
5142
5143 /**
5144 * Find the nearest view in the specified direction that can take focus.
5145 * This does not actually give focus to that view.
5146 *
5147 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5148 *
5149 * @return The nearest focusable in the specified direction, or null if none
5150 * can be found.
5151 */
5152 public View focusSearch(int direction) {
5153 if (mParent != null) {
5154 return mParent.focusSearch(this, direction);
5155 } else {
5156 return null;
5157 }
5158 }
5159
5160 /**
5161 * This method is the last chance for the focused view and its ancestors to
5162 * respond to an arrow key. This is called when the focused view did not
5163 * consume the key internally, nor could the view system find a new view in
5164 * the requested direction to give focus to.
5165 *
5166 * @param focused The currently focused view.
5167 * @param direction The direction focus wants to move. One of FOCUS_UP,
5168 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5169 * @return True if the this view consumed this unhandled move.
5170 */
5171 public boolean dispatchUnhandledMove(View focused, int direction) {
5172 return false;
5173 }
5174
5175 /**
5176 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005177 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005179 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5180 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 * @return The user specified next view, or null if there is none.
5182 */
5183 View findUserSetNextFocus(View root, int direction) {
5184 switch (direction) {
5185 case FOCUS_LEFT:
5186 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005187 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 case FOCUS_RIGHT:
5189 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005190 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 case FOCUS_UP:
5192 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005193 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 case FOCUS_DOWN:
5195 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005196 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005197 case FOCUS_FORWARD:
5198 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005199 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005200 case FOCUS_BACKWARD: {
5201 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005202 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005203 @Override
5204 public boolean apply(View t) {
5205 return t.mNextFocusForwardId == id;
5206 }
5207 });
5208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 }
5210 return null;
5211 }
5212
Jeff Brown4dfbec22011-08-15 14:55:37 -07005213 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5214 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5215 @Override
5216 public boolean apply(View t) {
5217 return t.mID == childViewId;
5218 }
5219 });
5220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 if (result == null) {
5222 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5223 + "by user for id " + childViewId);
5224 }
5225 return result;
5226 }
5227
5228 /**
5229 * Find and return all focusable views that are descendants of this view,
5230 * possibly including this view if it is focusable itself.
5231 *
5232 * @param direction The direction of the focus
5233 * @return A list of focusable views
5234 */
5235 public ArrayList<View> getFocusables(int direction) {
5236 ArrayList<View> result = new ArrayList<View>(24);
5237 addFocusables(result, direction);
5238 return result;
5239 }
5240
5241 /**
5242 * Add any focusable views that are descendants of this view (possibly
5243 * including this view if it is focusable itself) to views. If we are in touch mode,
5244 * only add views that are also focusable in touch mode.
5245 *
5246 * @param views Focusable views found so far
5247 * @param direction The direction of the focus
5248 */
5249 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005250 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252
svetoslavganov75986cf2009-05-14 22:28:01 -07005253 /**
5254 * Adds any focusable views that are descendants of this view (possibly
5255 * including this view if it is focusable itself) to views. This method
5256 * adds all focusable views regardless if we are in touch mode or
5257 * only views focusable in touch mode if we are in touch mode depending on
5258 * the focusable mode paramater.
5259 *
5260 * @param views Focusable views found so far or null if all we are interested is
5261 * the number of focusables.
5262 * @param direction The direction of the focus.
5263 * @param focusableMode The type of focusables to be added.
5264 *
5265 * @see #FOCUSABLES_ALL
5266 * @see #FOCUSABLES_TOUCH_MODE
5267 */
5268 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5269 if (!isFocusable()) {
5270 return;
5271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272
svetoslavganov75986cf2009-05-14 22:28:01 -07005273 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5274 isInTouchMode() && !isFocusableInTouchMode()) {
5275 return;
5276 }
5277
5278 if (views != null) {
5279 views.add(this);
5280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 }
5282
5283 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005284 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005285 * The search is performed by either the text that the View renders or the content
5286 * description that describes the view for accessibility purposes and the view does
5287 * not render or both. Clients can specify how the search is to be performed via
5288 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5289 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005290 *
5291 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005292 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005293 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005294 * @see #FIND_VIEWS_WITH_TEXT
5295 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5296 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005297 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005298 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005299 if (getAccessibilityNodeProvider() != null) {
5300 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5301 outViews.add(this);
5302 }
5303 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5304 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005305 String searchedLowerCase = searched.toString().toLowerCase();
5306 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5307 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5308 outViews.add(this);
5309 }
5310 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005311 }
5312
5313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 * Find and return all touchable views that are descendants of this view,
5315 * possibly including this view if it is touchable itself.
5316 *
5317 * @return A list of touchable views
5318 */
5319 public ArrayList<View> getTouchables() {
5320 ArrayList<View> result = new ArrayList<View>();
5321 addTouchables(result);
5322 return result;
5323 }
5324
5325 /**
5326 * Add any touchable views that are descendants of this view (possibly
5327 * including this view if it is touchable itself) to views.
5328 *
5329 * @param views Touchable views found so far
5330 */
5331 public void addTouchables(ArrayList<View> views) {
5332 final int viewFlags = mViewFlags;
5333
5334 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5335 && (viewFlags & ENABLED_MASK) == ENABLED) {
5336 views.add(this);
5337 }
5338 }
5339
5340 /**
5341 * Call this to try to give focus to a specific view or to one of its
5342 * descendants.
5343 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005344 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5345 * false), or if it is focusable and it is not focusable in touch mode
5346 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005348 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 * have focus, and you want your parent to look for the next one.
5350 *
5351 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5352 * {@link #FOCUS_DOWN} and <code>null</code>.
5353 *
5354 * @return Whether this view or one of its descendants actually took focus.
5355 */
5356 public final boolean requestFocus() {
5357 return requestFocus(View.FOCUS_DOWN);
5358 }
5359
5360
5361 /**
5362 * Call this to try to give focus to a specific view or to one of its
5363 * descendants and give it a hint about what direction focus is heading.
5364 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005365 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5366 * false), or if it is focusable and it is not focusable in touch mode
5367 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005369 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 * have focus, and you want your parent to look for the next one.
5371 *
5372 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5373 * <code>null</code> set for the previously focused rectangle.
5374 *
5375 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5376 * @return Whether this view or one of its descendants actually took focus.
5377 */
5378 public final boolean requestFocus(int direction) {
5379 return requestFocus(direction, null);
5380 }
5381
5382 /**
5383 * Call this to try to give focus to a specific view or to one of its descendants
5384 * and give it hints about the direction and a specific rectangle that the focus
5385 * is coming from. The rectangle can help give larger views a finer grained hint
5386 * about where focus is coming from, and therefore, where to show selection, or
5387 * forward focus change internally.
5388 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005389 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5390 * false), or if it is focusable and it is not focusable in touch mode
5391 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 *
5393 * A View will not take focus if it is not visible.
5394 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005395 * A View will not take focus if one of its parents has
5396 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5397 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005399 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 * have focus, and you want your parent to look for the next one.
5401 *
5402 * You may wish to override this method if your custom {@link View} has an internal
5403 * {@link View} that it wishes to forward the request to.
5404 *
5405 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5406 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5407 * to give a finer grained hint about where focus is coming from. May be null
5408 * if there is no hint.
5409 * @return Whether this view or one of its descendants actually took focus.
5410 */
5411 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5412 // need to be focusable
5413 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5414 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5415 return false;
5416 }
5417
5418 // need to be focusable in touch mode if in touch mode
5419 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005420 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5421 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 }
5423
5424 // need to not have any parents blocking us
5425 if (hasAncestorThatBlocksDescendantFocus()) {
5426 return false;
5427 }
5428
5429 handleFocusGainInternal(direction, previouslyFocusedRect);
5430 return true;
5431 }
5432
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005433 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005434 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005435 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005436 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005437 }
5438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 /**
5440 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5441 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5442 * touch mode to request focus when they are touched.
5443 *
5444 * @return Whether this view or one of its descendants actually took focus.
5445 *
5446 * @see #isInTouchMode()
5447 *
5448 */
5449 public final boolean requestFocusFromTouch() {
5450 // Leave touch mode if we need to
5451 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005452 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005453 if (viewRoot != null) {
5454 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005455 }
5456 }
5457 return requestFocus(View.FOCUS_DOWN);
5458 }
5459
5460 /**
5461 * @return Whether any ancestor of this view blocks descendant focus.
5462 */
5463 private boolean hasAncestorThatBlocksDescendantFocus() {
5464 ViewParent ancestor = mParent;
5465 while (ancestor instanceof ViewGroup) {
5466 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5467 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5468 return true;
5469 } else {
5470 ancestor = vgAncestor.getParent();
5471 }
5472 }
5473 return false;
5474 }
5475
5476 /**
Romain Guya440b002010-02-24 15:57:54 -08005477 * @hide
5478 */
5479 public void dispatchStartTemporaryDetach() {
5480 onStartTemporaryDetach();
5481 }
5482
5483 /**
5484 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5486 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005487 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 */
5489 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005490 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005491 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005492 }
5493
5494 /**
5495 * @hide
5496 */
5497 public void dispatchFinishTemporaryDetach() {
5498 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 }
Romain Guy8506ab42009-06-11 17:35:47 -07005500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 /**
5502 * Called after {@link #onStartTemporaryDetach} when the container is done
5503 * changing the view.
5504 */
5505 public void onFinishTemporaryDetach() {
5506 }
Romain Guy8506ab42009-06-11 17:35:47 -07005507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005509 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5510 * for this view's window. Returns null if the view is not currently attached
5511 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005512 * just use the standard high-level event callbacks like
5513 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005514 */
5515 public KeyEvent.DispatcherState getKeyDispatcherState() {
5516 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5517 }
Joe Malin32736f02011-01-19 16:14:20 -08005518
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 * Dispatch a key event before it is processed by any input method
5521 * associated with the view hierarchy. This can be used to intercept
5522 * key events in special situations before the IME consumes them; a
5523 * typical example would be handling the BACK key to update the application's
5524 * UI instead of allowing the IME to see it and close itself.
5525 *
5526 * @param event The key event to be dispatched.
5527 * @return True if the event was handled, false otherwise.
5528 */
5529 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5530 return onKeyPreIme(event.getKeyCode(), event);
5531 }
5532
5533 /**
5534 * Dispatch a key event to the next view on the focus path. This path runs
5535 * from the top of the view tree down to the currently focused view. If this
5536 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5537 * the next node down the focus path. This method also fires any key
5538 * listeners.
5539 *
5540 * @param event The key event to be dispatched.
5541 * @return True if the event was handled, false otherwise.
5542 */
5543 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005544 if (mInputEventConsistencyVerifier != null) {
5545 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547
Jeff Brown21bc5c92011-02-28 18:27:14 -08005548 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005549 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005550 ListenerInfo li = mListenerInfo;
5551 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5552 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 return true;
5554 }
5555
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005556 if (event.dispatch(this, mAttachInfo != null
5557 ? mAttachInfo.mKeyDispatchState : null, this)) {
5558 return true;
5559 }
5560
5561 if (mInputEventConsistencyVerifier != null) {
5562 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5563 }
5564 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 }
5566
5567 /**
5568 * Dispatches a key shortcut event.
5569 *
5570 * @param event The key event to be dispatched.
5571 * @return True if the event was handled by the view, false otherwise.
5572 */
5573 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5574 return onKeyShortcut(event.getKeyCode(), event);
5575 }
5576
5577 /**
5578 * Pass the touch screen motion event down to the target view, or this
5579 * view if it is the target.
5580 *
5581 * @param event The motion event to be dispatched.
5582 * @return True if the event was handled by the view, false otherwise.
5583 */
5584 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005585 if (mInputEventConsistencyVerifier != null) {
5586 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5587 }
5588
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005589 if (onFilterTouchEventForSecurity(event)) {
5590 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005591 ListenerInfo li = mListenerInfo;
5592 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5593 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005594 return true;
5595 }
5596
5597 if (onTouchEvent(event)) {
5598 return true;
5599 }
Jeff Brown85a31762010-09-01 17:01:00 -07005600 }
5601
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005602 if (mInputEventConsistencyVerifier != null) {
5603 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005605 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 }
5607
5608 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005609 * Filter the touch event to apply security policies.
5610 *
5611 * @param event The motion event to be filtered.
5612 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005613 *
Jeff Brown85a31762010-09-01 17:01:00 -07005614 * @see #getFilterTouchesWhenObscured
5615 */
5616 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005617 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005618 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5619 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5620 // Window is obscured, drop this touch.
5621 return false;
5622 }
5623 return true;
5624 }
5625
5626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 * Pass a trackball motion event down to the focused view.
5628 *
5629 * @param event The motion event to be dispatched.
5630 * @return True if the event was handled by the view, false otherwise.
5631 */
5632 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005633 if (mInputEventConsistencyVerifier != null) {
5634 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5635 }
5636
Romain Guy02ccac62011-06-24 13:20:23 -07005637 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 }
5639
5640 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005641 * Dispatch a generic motion event.
5642 * <p>
5643 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5644 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005645 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005646 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005647 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005648 *
5649 * @param event The motion event to be dispatched.
5650 * @return True if the event was handled by the view, false otherwise.
5651 */
5652 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005653 if (mInputEventConsistencyVerifier != null) {
5654 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5655 }
5656
Jeff Browna032cc02011-03-07 16:56:21 -08005657 final int source = event.getSource();
5658 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5659 final int action = event.getAction();
5660 if (action == MotionEvent.ACTION_HOVER_ENTER
5661 || action == MotionEvent.ACTION_HOVER_MOVE
5662 || action == MotionEvent.ACTION_HOVER_EXIT) {
5663 if (dispatchHoverEvent(event)) {
5664 return true;
5665 }
5666 } else if (dispatchGenericPointerEvent(event)) {
5667 return true;
5668 }
5669 } else if (dispatchGenericFocusedEvent(event)) {
5670 return true;
5671 }
5672
Jeff Brown10b62902011-06-20 16:40:37 -07005673 if (dispatchGenericMotionEventInternal(event)) {
5674 return true;
5675 }
5676
5677 if (mInputEventConsistencyVerifier != null) {
5678 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5679 }
5680 return false;
5681 }
5682
5683 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005684 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005685 ListenerInfo li = mListenerInfo;
5686 if (li != null && li.mOnGenericMotionListener != null
5687 && (mViewFlags & ENABLED_MASK) == ENABLED
5688 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005689 return true;
5690 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005691
5692 if (onGenericMotionEvent(event)) {
5693 return true;
5694 }
5695
5696 if (mInputEventConsistencyVerifier != null) {
5697 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5698 }
5699 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005700 }
5701
5702 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005703 * Dispatch a hover event.
5704 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005705 * Do not call this method directly.
5706 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005707 * </p>
5708 *
5709 * @param event The motion event to be dispatched.
5710 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005711 */
5712 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005713 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005714 ListenerInfo li = mListenerInfo;
5715 if (li != null && li.mOnHoverListener != null
5716 && (mViewFlags & ENABLED_MASK) == ENABLED
5717 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005718 return true;
5719 }
5720
Jeff Browna032cc02011-03-07 16:56:21 -08005721 return onHoverEvent(event);
5722 }
5723
5724 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005725 * Returns true if the view has a child to which it has recently sent
5726 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5727 * it does not have a hovered child, then it must be the innermost hovered view.
5728 * @hide
5729 */
5730 protected boolean hasHoveredChild() {
5731 return false;
5732 }
5733
5734 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005735 * Dispatch a generic motion event to the view under the first pointer.
5736 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005737 * Do not call this method directly.
5738 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005739 * </p>
5740 *
5741 * @param event The motion event to be dispatched.
5742 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005743 */
5744 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5745 return false;
5746 }
5747
5748 /**
5749 * Dispatch a generic motion event to the currently focused view.
5750 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005751 * Do not call this method directly.
5752 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005753 * </p>
5754 *
5755 * @param event The motion event to be dispatched.
5756 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005757 */
5758 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5759 return false;
5760 }
5761
5762 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005763 * Dispatch a pointer event.
5764 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005765 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5766 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5767 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005768 * and should not be expected to handle other pointing device features.
5769 * </p>
5770 *
5771 * @param event The motion event to be dispatched.
5772 * @return True if the event was handled by the view, false otherwise.
5773 * @hide
5774 */
5775 public final boolean dispatchPointerEvent(MotionEvent event) {
5776 if (event.isTouchEvent()) {
5777 return dispatchTouchEvent(event);
5778 } else {
5779 return dispatchGenericMotionEvent(event);
5780 }
5781 }
5782
5783 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 * Called when the window containing this view gains or loses window focus.
5785 * ViewGroups should override to route to their children.
5786 *
5787 * @param hasFocus True if the window containing this view now has focus,
5788 * false otherwise.
5789 */
5790 public void dispatchWindowFocusChanged(boolean hasFocus) {
5791 onWindowFocusChanged(hasFocus);
5792 }
5793
5794 /**
5795 * Called when the window containing this view gains or loses focus. Note
5796 * that this is separate from view focus: to receive key events, both
5797 * your view and its window must have focus. If a window is displayed
5798 * on top of yours that takes input focus, then your own window will lose
5799 * focus but the view focus will remain unchanged.
5800 *
5801 * @param hasWindowFocus True if the window containing this view now has
5802 * focus, false otherwise.
5803 */
5804 public void onWindowFocusChanged(boolean hasWindowFocus) {
5805 InputMethodManager imm = InputMethodManager.peekInstance();
5806 if (!hasWindowFocus) {
5807 if (isPressed()) {
5808 setPressed(false);
5809 }
5810 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5811 imm.focusOut(this);
5812 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005813 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005814 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005815 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5817 imm.focusIn(this);
5818 }
5819 refreshDrawableState();
5820 }
5821
5822 /**
5823 * Returns true if this view is in a window that currently has window focus.
5824 * Note that this is not the same as the view itself having focus.
5825 *
5826 * @return True if this view is in a window that currently has window focus.
5827 */
5828 public boolean hasWindowFocus() {
5829 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5830 }
5831
5832 /**
Adam Powell326d8082009-12-09 15:10:07 -08005833 * Dispatch a view visibility change down the view hierarchy.
5834 * ViewGroups should override to route to their children.
5835 * @param changedView The view whose visibility changed. Could be 'this' or
5836 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005837 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5838 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005839 */
5840 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5841 onVisibilityChanged(changedView, visibility);
5842 }
5843
5844 /**
5845 * Called when the visibility of the view or an ancestor of the view is changed.
5846 * @param changedView The view whose visibility changed. Could be 'this' or
5847 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005848 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5849 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005850 */
5851 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005852 if (visibility == VISIBLE) {
5853 if (mAttachInfo != null) {
5854 initialAwakenScrollBars();
5855 } else {
5856 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5857 }
5858 }
Adam Powell326d8082009-12-09 15:10:07 -08005859 }
5860
5861 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005862 * Dispatch a hint about whether this view is displayed. For instance, when
5863 * a View moves out of the screen, it might receives a display hint indicating
5864 * the view is not displayed. Applications should not <em>rely</em> on this hint
5865 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005866 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005867 * @param hint A hint about whether or not this view is displayed:
5868 * {@link #VISIBLE} or {@link #INVISIBLE}.
5869 */
5870 public void dispatchDisplayHint(int hint) {
5871 onDisplayHint(hint);
5872 }
5873
5874 /**
5875 * Gives this view a hint about whether is displayed or not. For instance, when
5876 * a View moves out of the screen, it might receives a display hint indicating
5877 * the view is not displayed. Applications should not <em>rely</em> on this hint
5878 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005879 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005880 * @param hint A hint about whether or not this view is displayed:
5881 * {@link #VISIBLE} or {@link #INVISIBLE}.
5882 */
5883 protected void onDisplayHint(int hint) {
5884 }
5885
5886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 * Dispatch a window visibility change down the view hierarchy.
5888 * ViewGroups should override to route to their children.
5889 *
5890 * @param visibility The new visibility of the window.
5891 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005892 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 */
5894 public void dispatchWindowVisibilityChanged(int visibility) {
5895 onWindowVisibilityChanged(visibility);
5896 }
5897
5898 /**
5899 * Called when the window containing has change its visibility
5900 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5901 * that this tells you whether or not your window is being made visible
5902 * to the window manager; this does <em>not</em> tell you whether or not
5903 * your window is obscured by other windows on the screen, even if it
5904 * is itself visible.
5905 *
5906 * @param visibility The new visibility of the window.
5907 */
5908 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005909 if (visibility == VISIBLE) {
5910 initialAwakenScrollBars();
5911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 }
5913
5914 /**
5915 * Returns the current visibility of the window this view is attached to
5916 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5917 *
5918 * @return Returns the current visibility of the view's window.
5919 */
5920 public int getWindowVisibility() {
5921 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5922 }
5923
5924 /**
5925 * Retrieve the overall visible display size in which the window this view is
5926 * attached to has been positioned in. This takes into account screen
5927 * decorations above the window, for both cases where the window itself
5928 * is being position inside of them or the window is being placed under
5929 * then and covered insets are used for the window to position its content
5930 * inside. In effect, this tells you the available area where content can
5931 * be placed and remain visible to users.
5932 *
5933 * <p>This function requires an IPC back to the window manager to retrieve
5934 * the requested information, so should not be used in performance critical
5935 * code like drawing.
5936 *
5937 * @param outRect Filled in with the visible display frame. If the view
5938 * is not attached to a window, this is simply the raw display size.
5939 */
5940 public void getWindowVisibleDisplayFrame(Rect outRect) {
5941 if (mAttachInfo != null) {
5942 try {
5943 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5944 } catch (RemoteException e) {
5945 return;
5946 }
5947 // XXX This is really broken, and probably all needs to be done
5948 // in the window manager, and we need to know more about whether
5949 // we want the area behind or in front of the IME.
5950 final Rect insets = mAttachInfo.mVisibleInsets;
5951 outRect.left += insets.left;
5952 outRect.top += insets.top;
5953 outRect.right -= insets.right;
5954 outRect.bottom -= insets.bottom;
5955 return;
5956 }
5957 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005958 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 }
5960
5961 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005962 * Dispatch a notification about a resource configuration change down
5963 * the view hierarchy.
5964 * ViewGroups should override to route to their children.
5965 *
5966 * @param newConfig The new resource configuration.
5967 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005968 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005969 */
5970 public void dispatchConfigurationChanged(Configuration newConfig) {
5971 onConfigurationChanged(newConfig);
5972 }
5973
5974 /**
5975 * Called when the current configuration of the resources being used
5976 * by the application have changed. You can use this to decide when
5977 * to reload resources that can changed based on orientation and other
5978 * configuration characterstics. You only need to use this if you are
5979 * not relying on the normal {@link android.app.Activity} mechanism of
5980 * recreating the activity instance upon a configuration change.
5981 *
5982 * @param newConfig The new resource configuration.
5983 */
5984 protected void onConfigurationChanged(Configuration newConfig) {
5985 }
5986
5987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 * Private function to aggregate all per-view attributes in to the view
5989 * root.
5990 */
5991 void dispatchCollectViewAttributes(int visibility) {
5992 performCollectViewAttributes(visibility);
5993 }
5994
5995 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005996 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005997 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5998 mAttachInfo.mKeepScreenOn = true;
5999 }
6000 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006001 ListenerInfo li = mListenerInfo;
6002 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006003 mAttachInfo.mHasSystemUiListeners = true;
6004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 }
6006 }
6007
6008 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006009 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006010 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006011 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6012 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 ai.mRecomputeGlobalAttributes = true;
6014 }
6015 }
6016 }
6017
6018 /**
6019 * Returns whether the device is currently in touch mode. Touch mode is entered
6020 * once the user begins interacting with the device by touch, and affects various
6021 * things like whether focus is always visible to the user.
6022 *
6023 * @return Whether the device is in touch mode.
6024 */
6025 @ViewDebug.ExportedProperty
6026 public boolean isInTouchMode() {
6027 if (mAttachInfo != null) {
6028 return mAttachInfo.mInTouchMode;
6029 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006030 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 }
6032 }
6033
6034 /**
6035 * Returns the context the view is running in, through which it can
6036 * access the current theme, resources, etc.
6037 *
6038 * @return The view's Context.
6039 */
6040 @ViewDebug.CapturedViewProperty
6041 public final Context getContext() {
6042 return mContext;
6043 }
6044
6045 /**
6046 * Handle a key event before it is processed by any input method
6047 * associated with the view hierarchy. This can be used to intercept
6048 * key events in special situations before the IME consumes them; a
6049 * typical example would be handling the BACK key to update the application's
6050 * UI instead of allowing the IME to see it and close itself.
6051 *
6052 * @param keyCode The value in event.getKeyCode().
6053 * @param event Description of the key event.
6054 * @return If you handled the event, return true. If you want to allow the
6055 * event to be handled by the next receiver, return false.
6056 */
6057 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6058 return false;
6059 }
6060
6061 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006062 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6063 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006064 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6065 * is released, if the view is enabled and clickable.
6066 *
6067 * @param keyCode A key code that represents the button pressed, from
6068 * {@link android.view.KeyEvent}.
6069 * @param event The KeyEvent object that defines the button action.
6070 */
6071 public boolean onKeyDown(int keyCode, KeyEvent event) {
6072 boolean result = false;
6073
6074 switch (keyCode) {
6075 case KeyEvent.KEYCODE_DPAD_CENTER:
6076 case KeyEvent.KEYCODE_ENTER: {
6077 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6078 return true;
6079 }
6080 // Long clickable items don't necessarily have to be clickable
6081 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6082 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6083 (event.getRepeatCount() == 0)) {
6084 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006085 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 return true;
6087 }
6088 break;
6089 }
6090 }
6091 return result;
6092 }
6093
6094 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006095 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6096 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6097 * the event).
6098 */
6099 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6100 return false;
6101 }
6102
6103 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006104 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6105 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006106 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6107 * {@link KeyEvent#KEYCODE_ENTER} is released.
6108 *
6109 * @param keyCode A key code that represents the button pressed, from
6110 * {@link android.view.KeyEvent}.
6111 * @param event The KeyEvent object that defines the button action.
6112 */
6113 public boolean onKeyUp(int keyCode, KeyEvent event) {
6114 boolean result = false;
6115
6116 switch (keyCode) {
6117 case KeyEvent.KEYCODE_DPAD_CENTER:
6118 case KeyEvent.KEYCODE_ENTER: {
6119 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6120 return true;
6121 }
6122 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6123 setPressed(false);
6124
6125 if (!mHasPerformedLongPress) {
6126 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006127 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006128
6129 result = performClick();
6130 }
6131 }
6132 break;
6133 }
6134 }
6135 return result;
6136 }
6137
6138 /**
6139 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6140 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6141 * the event).
6142 *
6143 * @param keyCode A key code that represents the button pressed, from
6144 * {@link android.view.KeyEvent}.
6145 * @param repeatCount The number of times the action was made.
6146 * @param event The KeyEvent object that defines the button action.
6147 */
6148 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6149 return false;
6150 }
6151
6152 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006153 * Called on the focused view when a key shortcut event is not handled.
6154 * Override this method to implement local key shortcuts for the View.
6155 * Key shortcuts can also be implemented by setting the
6156 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 *
6158 * @param keyCode The value in event.getKeyCode().
6159 * @param event Description of the key event.
6160 * @return If you handled the event, return true. If you want to allow the
6161 * event to be handled by the next receiver, return false.
6162 */
6163 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6164 return false;
6165 }
6166
6167 /**
6168 * Check whether the called view is a text editor, in which case it
6169 * would make sense to automatically display a soft input window for
6170 * it. Subclasses should override this if they implement
6171 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006172 * a call on that method would return a non-null InputConnection, and
6173 * they are really a first-class editor that the user would normally
6174 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006175 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006176 * <p>The default implementation always returns false. This does
6177 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6178 * will not be called or the user can not otherwise perform edits on your
6179 * view; it is just a hint to the system that this is not the primary
6180 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006181 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 * @return Returns true if this view is a text editor, else false.
6183 */
6184 public boolean onCheckIsTextEditor() {
6185 return false;
6186 }
Romain Guy8506ab42009-06-11 17:35:47 -07006187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 /**
6189 * Create a new InputConnection for an InputMethod to interact
6190 * with the view. The default implementation returns null, since it doesn't
6191 * support input methods. You can override this to implement such support.
6192 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006193 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006194 * <p>When implementing this, you probably also want to implement
6195 * {@link #onCheckIsTextEditor()} to indicate you will return a
6196 * non-null InputConnection.
6197 *
6198 * @param outAttrs Fill in with attribute information about the connection.
6199 */
6200 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6201 return null;
6202 }
6203
6204 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006205 * Called by the {@link android.view.inputmethod.InputMethodManager}
6206 * when a view who is not the current
6207 * input connection target is trying to make a call on the manager. The
6208 * default implementation returns false; you can override this to return
6209 * true for certain views if you are performing InputConnection proxying
6210 * to them.
6211 * @param view The View that is making the InputMethodManager call.
6212 * @return Return true to allow the call, false to reject.
6213 */
6214 public boolean checkInputConnectionProxy(View view) {
6215 return false;
6216 }
Romain Guy8506ab42009-06-11 17:35:47 -07006217
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 * Show the context menu for this view. It is not safe to hold on to the
6220 * menu after returning from this method.
6221 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006222 * You should normally not overload this method. Overload
6223 * {@link #onCreateContextMenu(ContextMenu)} or define an
6224 * {@link OnCreateContextMenuListener} to add items to the context menu.
6225 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 * @param menu The context menu to populate
6227 */
6228 public void createContextMenu(ContextMenu menu) {
6229 ContextMenuInfo menuInfo = getContextMenuInfo();
6230
6231 // Sets the current menu info so all items added to menu will have
6232 // my extra info set.
6233 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6234
6235 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006236 ListenerInfo li = mListenerInfo;
6237 if (li != null && li.mOnCreateContextMenuListener != null) {
6238 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 }
6240
6241 // Clear the extra information so subsequent items that aren't mine don't
6242 // have my extra info.
6243 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6244
6245 if (mParent != null) {
6246 mParent.createContextMenu(menu);
6247 }
6248 }
6249
6250 /**
6251 * Views should implement this if they have extra information to associate
6252 * with the context menu. The return result is supplied as a parameter to
6253 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6254 * callback.
6255 *
6256 * @return Extra information about the item for which the context menu
6257 * should be shown. This information will vary across different
6258 * subclasses of View.
6259 */
6260 protected ContextMenuInfo getContextMenuInfo() {
6261 return null;
6262 }
6263
6264 /**
6265 * Views should implement this if the view itself is going to add items to
6266 * the context menu.
6267 *
6268 * @param menu the context menu to populate
6269 */
6270 protected void onCreateContextMenu(ContextMenu menu) {
6271 }
6272
6273 /**
6274 * Implement this method to handle trackball motion events. The
6275 * <em>relative</em> movement of the trackball since the last event
6276 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6277 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6278 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6279 * they will often be fractional values, representing the more fine-grained
6280 * movement information available from a trackball).
6281 *
6282 * @param event The motion event.
6283 * @return True if the event was handled, false otherwise.
6284 */
6285 public boolean onTrackballEvent(MotionEvent event) {
6286 return false;
6287 }
6288
6289 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006290 * Implement this method to handle generic motion events.
6291 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006292 * Generic motion events describe joystick movements, mouse hovers, track pad
6293 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006294 * {@link MotionEvent#getSource() source} of the motion event specifies
6295 * the class of input that was received. Implementations of this method
6296 * must examine the bits in the source before processing the event.
6297 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006298 * </p><p>
6299 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6300 * are delivered to the view under the pointer. All other generic motion events are
6301 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006302 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006303 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006304 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006305 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6306 * // process the joystick movement...
6307 * return true;
6308 * }
6309 * }
6310 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6311 * switch (event.getAction()) {
6312 * case MotionEvent.ACTION_HOVER_MOVE:
6313 * // process the mouse hover movement...
6314 * return true;
6315 * case MotionEvent.ACTION_SCROLL:
6316 * // process the scroll wheel movement...
6317 * return true;
6318 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006319 * }
6320 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006321 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006322 *
6323 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006324 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006325 */
6326 public boolean onGenericMotionEvent(MotionEvent event) {
6327 return false;
6328 }
6329
6330 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006331 * Implement this method to handle hover events.
6332 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006333 * This method is called whenever a pointer is hovering into, over, or out of the
6334 * bounds of a view and the view is not currently being touched.
6335 * Hover events are represented as pointer events with action
6336 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6337 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6338 * </p>
6339 * <ul>
6340 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6341 * when the pointer enters the bounds of the view.</li>
6342 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6343 * when the pointer has already entered the bounds of the view and has moved.</li>
6344 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6345 * when the pointer has exited the bounds of the view or when the pointer is
6346 * about to go down due to a button click, tap, or similar user action that
6347 * causes the view to be touched.</li>
6348 * </ul>
6349 * <p>
6350 * The view should implement this method to return true to indicate that it is
6351 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006352 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006353 * The default implementation calls {@link #setHovered} to update the hovered state
6354 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006355 * is enabled and is clickable. The default implementation also sends hover
6356 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006357 * </p>
6358 *
6359 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006360 * @return True if the view handled the hover event.
6361 *
6362 * @see #isHovered
6363 * @see #setHovered
6364 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006365 */
6366 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006367 // The root view may receive hover (or touch) events that are outside the bounds of
6368 // the window. This code ensures that we only send accessibility events for
6369 // hovers that are actually within the bounds of the root view.
6370 final int action = event.getAction();
6371 if (!mSendingHoverAccessibilityEvents) {
6372 if ((action == MotionEvent.ACTION_HOVER_ENTER
6373 || action == MotionEvent.ACTION_HOVER_MOVE)
6374 && !hasHoveredChild()
6375 && pointInView(event.getX(), event.getY())) {
6376 mSendingHoverAccessibilityEvents = true;
6377 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6378 }
6379 } else {
6380 if (action == MotionEvent.ACTION_HOVER_EXIT
6381 || (action == MotionEvent.ACTION_HOVER_MOVE
6382 && !pointInView(event.getX(), event.getY()))) {
6383 mSendingHoverAccessibilityEvents = false;
6384 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6385 }
Jeff Browna1b24182011-07-28 13:38:24 -07006386 }
6387
Jeff Brown87b7f802011-06-21 18:35:45 -07006388 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006389 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006390 case MotionEvent.ACTION_HOVER_ENTER:
6391 setHovered(true);
6392 break;
6393 case MotionEvent.ACTION_HOVER_EXIT:
6394 setHovered(false);
6395 break;
6396 }
Jeff Browna1b24182011-07-28 13:38:24 -07006397
6398 // Dispatch the event to onGenericMotionEvent before returning true.
6399 // This is to provide compatibility with existing applications that
6400 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6401 // break because of the new default handling for hoverable views
6402 // in onHoverEvent.
6403 // Note that onGenericMotionEvent will be called by default when
6404 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6405 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006406 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006407 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006408 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006409 }
6410
6411 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006412 * Returns true if the view should handle {@link #onHoverEvent}
6413 * by calling {@link #setHovered} to change its hovered state.
6414 *
6415 * @return True if the view is hoverable.
6416 */
6417 private boolean isHoverable() {
6418 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006419 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006420 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6421 return false;
6422 }
6423
6424 return (viewFlags & CLICKABLE) == CLICKABLE
6425 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6426 }
6427
6428 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006429 * Returns true if the view is currently hovered.
6430 *
6431 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006432 *
6433 * @see #setHovered
6434 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006435 */
Jeff Brown10b62902011-06-20 16:40:37 -07006436 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006437 public boolean isHovered() {
6438 return (mPrivateFlags & HOVERED) != 0;
6439 }
6440
6441 /**
6442 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006443 * <p>
6444 * Calling this method also changes the drawable state of the view. This
6445 * enables the view to react to hover by using different drawable resources
6446 * to change its appearance.
6447 * </p><p>
6448 * The {@link #onHoverChanged} method is called when the hovered state changes.
6449 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006450 *
6451 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006452 *
6453 * @see #isHovered
6454 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006455 */
6456 public void setHovered(boolean hovered) {
6457 if (hovered) {
6458 if ((mPrivateFlags & HOVERED) == 0) {
6459 mPrivateFlags |= HOVERED;
6460 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006461 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006462 }
6463 } else {
6464 if ((mPrivateFlags & HOVERED) != 0) {
6465 mPrivateFlags &= ~HOVERED;
6466 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006467 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006468 }
6469 }
6470 }
6471
6472 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006473 * Implement this method to handle hover state changes.
6474 * <p>
6475 * This method is called whenever the hover state changes as a result of a
6476 * call to {@link #setHovered}.
6477 * </p>
6478 *
6479 * @param hovered The current hover state, as returned by {@link #isHovered}.
6480 *
6481 * @see #isHovered
6482 * @see #setHovered
6483 */
6484 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006485 }
6486
6487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488 * Implement this method to handle touch screen motion events.
6489 *
6490 * @param event The motion event.
6491 * @return True if the event was handled, false otherwise.
6492 */
6493 public boolean onTouchEvent(MotionEvent event) {
6494 final int viewFlags = mViewFlags;
6495
6496 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006497 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6498 mPrivateFlags &= ~PRESSED;
6499 refreshDrawableState();
6500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 // A disabled view that is clickable still consumes the touch
6502 // events, it just doesn't respond to them.
6503 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6504 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6505 }
6506
6507 if (mTouchDelegate != null) {
6508 if (mTouchDelegate.onTouchEvent(event)) {
6509 return true;
6510 }
6511 }
6512
6513 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6514 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6515 switch (event.getAction()) {
6516 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006517 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6518 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006519 // take focus if we don't have it already and we should in
6520 // touch mode.
6521 boolean focusTaken = false;
6522 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6523 focusTaken = requestFocus();
6524 }
6525
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006526 if (prepressed) {
6527 // The button is being released before we actually
6528 // showed it as pressed. Make it show the pressed
6529 // state now (before scheduling the click) to ensure
6530 // the user sees it.
6531 mPrivateFlags |= PRESSED;
6532 refreshDrawableState();
6533 }
Joe Malin32736f02011-01-19 16:14:20 -08006534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 if (!mHasPerformedLongPress) {
6536 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006537 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538
6539 // Only perform take click actions if we were in the pressed state
6540 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006541 // Use a Runnable and post this rather than calling
6542 // performClick directly. This lets other visual state
6543 // of the view update before click actions start.
6544 if (mPerformClick == null) {
6545 mPerformClick = new PerformClick();
6546 }
6547 if (!post(mPerformClick)) {
6548 performClick();
6549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 }
6551 }
6552
6553 if (mUnsetPressedState == null) {
6554 mUnsetPressedState = new UnsetPressedState();
6555 }
6556
Adam Powelle14579b2009-12-16 18:39:52 -08006557 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006558 postDelayed(mUnsetPressedState,
6559 ViewConfiguration.getPressedStateDuration());
6560 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 // If the post failed, unpress right now
6562 mUnsetPressedState.run();
6563 }
Adam Powelle14579b2009-12-16 18:39:52 -08006564 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006565 }
6566 break;
6567
6568 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006569 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006570
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006571 if (performButtonActionOnTouchDown(event)) {
6572 break;
6573 }
6574
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006575 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006576 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006577
6578 // For views inside a scrolling container, delay the pressed feedback for
6579 // a short period in case this is a scroll.
6580 if (isInScrollingContainer) {
6581 mPrivateFlags |= PREPRESSED;
6582 if (mPendingCheckForTap == null) {
6583 mPendingCheckForTap = new CheckForTap();
6584 }
6585 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6586 } else {
6587 // Not inside a scrolling container, so show the feedback right away
6588 mPrivateFlags |= PRESSED;
6589 refreshDrawableState();
6590 checkForLongClick(0);
6591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006592 break;
6593
6594 case MotionEvent.ACTION_CANCEL:
6595 mPrivateFlags &= ~PRESSED;
6596 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006597 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 break;
6599
6600 case MotionEvent.ACTION_MOVE:
6601 final int x = (int) event.getX();
6602 final int y = (int) event.getY();
6603
6604 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006605 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006607 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006609 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006610 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611
6612 // Need to switch from pressed to not pressed
6613 mPrivateFlags &= ~PRESSED;
6614 refreshDrawableState();
6615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 }
6617 break;
6618 }
6619 return true;
6620 }
6621
6622 return false;
6623 }
6624
6625 /**
Adam Powell10298662011-08-14 18:26:30 -07006626 * @hide
6627 */
6628 public boolean isInScrollingContainer() {
6629 ViewParent p = getParent();
6630 while (p != null && p instanceof ViewGroup) {
6631 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6632 return true;
6633 }
6634 p = p.getParent();
6635 }
6636 return false;
6637 }
6638
6639 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006640 * Remove the longpress detection timer.
6641 */
6642 private void removeLongPressCallback() {
6643 if (mPendingCheckForLongPress != null) {
6644 removeCallbacks(mPendingCheckForLongPress);
6645 }
6646 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006647
6648 /**
6649 * Remove the pending click action
6650 */
6651 private void removePerformClickCallback() {
6652 if (mPerformClick != null) {
6653 removeCallbacks(mPerformClick);
6654 }
6655 }
6656
Adam Powelle14579b2009-12-16 18:39:52 -08006657 /**
Romain Guya440b002010-02-24 15:57:54 -08006658 * Remove the prepress detection timer.
6659 */
6660 private void removeUnsetPressCallback() {
6661 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6662 setPressed(false);
6663 removeCallbacks(mUnsetPressedState);
6664 }
6665 }
6666
6667 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006668 * Remove the tap detection timer.
6669 */
6670 private void removeTapCallback() {
6671 if (mPendingCheckForTap != null) {
6672 mPrivateFlags &= ~PREPRESSED;
6673 removeCallbacks(mPendingCheckForTap);
6674 }
6675 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006676
6677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 * Cancels a pending long press. Your subclass can use this if you
6679 * want the context menu to come up if the user presses and holds
6680 * at the same place, but you don't want it to come up if they press
6681 * and then move around enough to cause scrolling.
6682 */
6683 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006684 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006685
6686 /*
6687 * The prepressed state handled by the tap callback is a display
6688 * construct, but the tap callback will post a long press callback
6689 * less its own timeout. Remove it here.
6690 */
6691 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 }
6693
6694 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006695 * Remove the pending callback for sending a
6696 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6697 */
6698 private void removeSendViewScrolledAccessibilityEventCallback() {
6699 if (mSendViewScrolledAccessibilityEvent != null) {
6700 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6701 }
6702 }
6703
6704 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006705 * Sets the TouchDelegate for this View.
6706 */
6707 public void setTouchDelegate(TouchDelegate delegate) {
6708 mTouchDelegate = delegate;
6709 }
6710
6711 /**
6712 * Gets the TouchDelegate for this View.
6713 */
6714 public TouchDelegate getTouchDelegate() {
6715 return mTouchDelegate;
6716 }
6717
6718 /**
6719 * Set flags controlling behavior of this view.
6720 *
6721 * @param flags Constant indicating the value which should be set
6722 * @param mask Constant indicating the bit range that should be changed
6723 */
6724 void setFlags(int flags, int mask) {
6725 int old = mViewFlags;
6726 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6727
6728 int changed = mViewFlags ^ old;
6729 if (changed == 0) {
6730 return;
6731 }
6732 int privateFlags = mPrivateFlags;
6733
6734 /* Check if the FOCUSABLE bit has changed */
6735 if (((changed & FOCUSABLE_MASK) != 0) &&
6736 ((privateFlags & HAS_BOUNDS) !=0)) {
6737 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6738 && ((privateFlags & FOCUSED) != 0)) {
6739 /* Give up focus if we are no longer focusable */
6740 clearFocus();
6741 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6742 && ((privateFlags & FOCUSED) == 0)) {
6743 /*
6744 * Tell the view system that we are now available to take focus
6745 * if no one else already has it.
6746 */
6747 if (mParent != null) mParent.focusableViewAvailable(this);
6748 }
6749 }
6750
6751 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6752 if ((changed & VISIBILITY_MASK) != 0) {
6753 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006754 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006755 * it was not visible. Marking it drawn ensures that the invalidation will
6756 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006758 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006759 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760
6761 needGlobalAttributesUpdate(true);
6762
6763 // a view becoming visible is worth notifying the parent
6764 // about in case nothing has focus. even if this specific view
6765 // isn't focusable, it may contain something that is, so let
6766 // the root view try to give this focus if nothing else does.
6767 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6768 mParent.focusableViewAvailable(this);
6769 }
6770 }
6771 }
6772
6773 /* Check if the GONE bit has changed */
6774 if ((changed & GONE) != 0) {
6775 needGlobalAttributesUpdate(false);
6776 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777
Romain Guyecd80ee2009-12-03 17:13:02 -08006778 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6779 if (hasFocus()) clearFocus();
6780 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006781 if (mParent instanceof View) {
6782 // GONE views noop invalidation, so invalidate the parent
6783 ((View) mParent).invalidate(true);
6784 }
6785 // Mark the view drawn to ensure that it gets invalidated properly the next
6786 // time it is visible and gets invalidated
6787 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 }
6789 if (mAttachInfo != null) {
6790 mAttachInfo.mViewVisibilityChanged = true;
6791 }
6792 }
6793
6794 /* Check if the VISIBLE bit has changed */
6795 if ((changed & INVISIBLE) != 0) {
6796 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006797 /*
6798 * If this view is becoming invisible, set the DRAWN flag so that
6799 * the next invalidate() will not be skipped.
6800 */
6801 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802
6803 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6804 // root view becoming invisible shouldn't clear focus
6805 if (getRootView() != this) {
6806 clearFocus();
6807 }
6808 }
6809 if (mAttachInfo != null) {
6810 mAttachInfo.mViewVisibilityChanged = true;
6811 }
6812 }
6813
Adam Powell326d8082009-12-09 15:10:07 -08006814 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006815 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006816 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6817 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006818 } else if (mParent != null) {
6819 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006820 }
Adam Powell326d8082009-12-09 15:10:07 -08006821 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6822 }
6823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6825 destroyDrawingCache();
6826 }
6827
6828 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6829 destroyDrawingCache();
6830 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006831 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 }
6833
6834 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6835 destroyDrawingCache();
6836 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6837 }
6838
6839 if ((changed & DRAW_MASK) != 0) {
6840 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6841 if (mBGDrawable != null) {
6842 mPrivateFlags &= ~SKIP_DRAW;
6843 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6844 } else {
6845 mPrivateFlags |= SKIP_DRAW;
6846 }
6847 } else {
6848 mPrivateFlags &= ~SKIP_DRAW;
6849 }
6850 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006851 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006852 }
6853
6854 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006855 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006856 mParent.recomputeViewAttributes(this);
6857 }
6858 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006859
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006860 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006861 requestLayout();
6862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 }
6864
6865 /**
6866 * Change the view's z order in the tree, so it's on top of other sibling
6867 * views
6868 */
6869 public void bringToFront() {
6870 if (mParent != null) {
6871 mParent.bringChildToFront(this);
6872 }
6873 }
6874
6875 /**
6876 * This is called in response to an internal scroll in this view (i.e., the
6877 * view scrolled its own contents). This is typically as a result of
6878 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6879 * called.
6880 *
6881 * @param l Current horizontal scroll origin.
6882 * @param t Current vertical scroll origin.
6883 * @param oldl Previous horizontal scroll origin.
6884 * @param oldt Previous vertical scroll origin.
6885 */
6886 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006887 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6888 postSendViewScrolledAccessibilityEventCallback();
6889 }
6890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891 mBackgroundSizeChanged = true;
6892
6893 final AttachInfo ai = mAttachInfo;
6894 if (ai != null) {
6895 ai.mViewScrollChanged = true;
6896 }
6897 }
6898
6899 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006900 * Interface definition for a callback to be invoked when the layout bounds of a view
6901 * changes due to layout processing.
6902 */
6903 public interface OnLayoutChangeListener {
6904 /**
6905 * Called when the focus state of a view has changed.
6906 *
6907 * @param v The view whose state has changed.
6908 * @param left The new value of the view's left property.
6909 * @param top The new value of the view's top property.
6910 * @param right The new value of the view's right property.
6911 * @param bottom The new value of the view's bottom property.
6912 * @param oldLeft The previous value of the view's left property.
6913 * @param oldTop The previous value of the view's top property.
6914 * @param oldRight The previous value of the view's right property.
6915 * @param oldBottom The previous value of the view's bottom property.
6916 */
6917 void onLayoutChange(View v, int left, int top, int right, int bottom,
6918 int oldLeft, int oldTop, int oldRight, int oldBottom);
6919 }
6920
6921 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 * This is called during layout when the size of this view has changed. If
6923 * you were just added to the view hierarchy, you're called with the old
6924 * values of 0.
6925 *
6926 * @param w Current width of this view.
6927 * @param h Current height of this view.
6928 * @param oldw Old width of this view.
6929 * @param oldh Old height of this view.
6930 */
6931 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6932 }
6933
6934 /**
6935 * Called by draw to draw the child views. This may be overridden
6936 * by derived classes to gain control just before its children are drawn
6937 * (but after its own view has been drawn).
6938 * @param canvas the canvas on which to draw the view
6939 */
6940 protected void dispatchDraw(Canvas canvas) {
6941 }
6942
6943 /**
6944 * Gets the parent of this view. Note that the parent is a
6945 * ViewParent and not necessarily a View.
6946 *
6947 * @return Parent of this view.
6948 */
6949 public final ViewParent getParent() {
6950 return mParent;
6951 }
6952
6953 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006954 * Set the horizontal scrolled position of your view. This will cause a call to
6955 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6956 * invalidated.
6957 * @param value the x position to scroll to
6958 */
6959 public void setScrollX(int value) {
6960 scrollTo(value, mScrollY);
6961 }
6962
6963 /**
6964 * Set the vertical scrolled position of your view. This will cause a call to
6965 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6966 * invalidated.
6967 * @param value the y position to scroll to
6968 */
6969 public void setScrollY(int value) {
6970 scrollTo(mScrollX, value);
6971 }
6972
6973 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006974 * Return the scrolled left position of this view. This is the left edge of
6975 * the displayed part of your view. You do not need to draw any pixels
6976 * farther left, since those are outside of the frame of your view on
6977 * screen.
6978 *
6979 * @return The left edge of the displayed part of your view, in pixels.
6980 */
6981 public final int getScrollX() {
6982 return mScrollX;
6983 }
6984
6985 /**
6986 * Return the scrolled top position of this view. This is the top edge of
6987 * the displayed part of your view. You do not need to draw any pixels above
6988 * it, since those are outside of the frame of your view on screen.
6989 *
6990 * @return The top edge of the displayed part of your view, in pixels.
6991 */
6992 public final int getScrollY() {
6993 return mScrollY;
6994 }
6995
6996 /**
6997 * Return the width of the your view.
6998 *
6999 * @return The width of your view, in pixels.
7000 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007001 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 public final int getWidth() {
7003 return mRight - mLeft;
7004 }
7005
7006 /**
7007 * Return the height of your view.
7008 *
7009 * @return The height of your view, in pixels.
7010 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007011 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 public final int getHeight() {
7013 return mBottom - mTop;
7014 }
7015
7016 /**
7017 * Return the visible drawing bounds of your view. Fills in the output
7018 * rectangle with the values from getScrollX(), getScrollY(),
7019 * getWidth(), and getHeight().
7020 *
7021 * @param outRect The (scrolled) drawing bounds of the view.
7022 */
7023 public void getDrawingRect(Rect outRect) {
7024 outRect.left = mScrollX;
7025 outRect.top = mScrollY;
7026 outRect.right = mScrollX + (mRight - mLeft);
7027 outRect.bottom = mScrollY + (mBottom - mTop);
7028 }
7029
7030 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007031 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7032 * raw width component (that is the result is masked by
7033 * {@link #MEASURED_SIZE_MASK}).
7034 *
7035 * @return The raw measured width of this view.
7036 */
7037 public final int getMeasuredWidth() {
7038 return mMeasuredWidth & MEASURED_SIZE_MASK;
7039 }
7040
7041 /**
7042 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007043 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007044 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007045 * This should be used during measurement and layout calculations only. Use
7046 * {@link #getWidth()} to see how wide a view is after layout.
7047 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007048 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007049 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007050 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051 return mMeasuredWidth;
7052 }
7053
7054 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007055 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7056 * raw width component (that is the result is masked by
7057 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007058 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007059 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 */
7061 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007062 return mMeasuredHeight & MEASURED_SIZE_MASK;
7063 }
7064
7065 /**
7066 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007067 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007068 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7069 * This should be used during measurement and layout calculations only. Use
7070 * {@link #getHeight()} to see how wide a view is after layout.
7071 *
7072 * @return The measured width of this view as a bit mask.
7073 */
7074 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007075 return mMeasuredHeight;
7076 }
7077
7078 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007079 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7080 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7081 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7082 * and the height component is at the shifted bits
7083 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7084 */
7085 public final int getMeasuredState() {
7086 return (mMeasuredWidth&MEASURED_STATE_MASK)
7087 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7088 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7089 }
7090
7091 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007092 * The transform matrix of this view, which is calculated based on the current
7093 * roation, scale, and pivot properties.
7094 *
7095 * @see #getRotation()
7096 * @see #getScaleX()
7097 * @see #getScaleY()
7098 * @see #getPivotX()
7099 * @see #getPivotY()
7100 * @return The current transform matrix for the view
7101 */
7102 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007103 if (mTransformationInfo != null) {
7104 updateMatrix();
7105 return mTransformationInfo.mMatrix;
7106 }
7107 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007108 }
7109
7110 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007111 * Utility function to determine if the value is far enough away from zero to be
7112 * considered non-zero.
7113 * @param value A floating point value to check for zero-ness
7114 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7115 */
7116 private static boolean nonzero(float value) {
7117 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7118 }
7119
7120 /**
Jeff Brown86671742010-09-30 20:00:15 -07007121 * Returns true if the transform matrix is the identity matrix.
7122 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007123 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007124 * @return True if the transform matrix is the identity matrix, false otherwise.
7125 */
Jeff Brown86671742010-09-30 20:00:15 -07007126 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007127 if (mTransformationInfo != null) {
7128 updateMatrix();
7129 return mTransformationInfo.mMatrixIsIdentity;
7130 }
7131 return true;
7132 }
7133
7134 void ensureTransformationInfo() {
7135 if (mTransformationInfo == null) {
7136 mTransformationInfo = new TransformationInfo();
7137 }
Jeff Brown86671742010-09-30 20:00:15 -07007138 }
7139
7140 /**
7141 * Recomputes the transform matrix if necessary.
7142 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007143 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007144 final TransformationInfo info = mTransformationInfo;
7145 if (info == null) {
7146 return;
7147 }
7148 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007149 // transform-related properties have changed since the last time someone
7150 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007151
7152 // Figure out if we need to update the pivot point
7153 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007154 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7155 info.mPrevWidth = mRight - mLeft;
7156 info.mPrevHeight = mBottom - mTop;
7157 info.mPivotX = info.mPrevWidth / 2f;
7158 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007159 }
7160 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007161 info.mMatrix.reset();
7162 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7163 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7164 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7165 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007166 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007167 if (info.mCamera == null) {
7168 info.mCamera = new Camera();
7169 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007170 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007171 info.mCamera.save();
7172 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7173 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7174 info.mCamera.getMatrix(info.matrix3D);
7175 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7176 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7177 info.mPivotY + info.mTranslationY);
7178 info.mMatrix.postConcat(info.matrix3D);
7179 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007180 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007181 info.mMatrixDirty = false;
7182 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7183 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007184 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007185 }
7186
7187 /**
7188 * Utility method to retrieve the inverse of the current mMatrix property.
7189 * We cache the matrix to avoid recalculating it when transform properties
7190 * have not changed.
7191 *
7192 * @return The inverse of the current matrix of this view.
7193 */
Jeff Brown86671742010-09-30 20:00:15 -07007194 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007195 final TransformationInfo info = mTransformationInfo;
7196 if (info != null) {
7197 updateMatrix();
7198 if (info.mInverseMatrixDirty) {
7199 if (info.mInverseMatrix == null) {
7200 info.mInverseMatrix = new Matrix();
7201 }
7202 info.mMatrix.invert(info.mInverseMatrix);
7203 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007204 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007205 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007206 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007207 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007208 }
7209
7210 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007211 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7212 * views are drawn) from the camera to this view. The camera's distance
7213 * affects 3D transformations, for instance rotations around the X and Y
7214 * axis. If the rotationX or rotationY properties are changed and this view is
7215 * large (more than half the size of the screen), it is recommended to always
7216 * use a camera distance that's greater than the height (X axis rotation) or
7217 * the width (Y axis rotation) of this view.</p>
7218 *
7219 * <p>The distance of the camera from the view plane can have an affect on the
7220 * perspective distortion of the view when it is rotated around the x or y axis.
7221 * For example, a large distance will result in a large viewing angle, and there
7222 * will not be much perspective distortion of the view as it rotates. A short
7223 * distance may cause much more perspective distortion upon rotation, and can
7224 * also result in some drawing artifacts if the rotated view ends up partially
7225 * behind the camera (which is why the recommendation is to use a distance at
7226 * least as far as the size of the view, if the view is to be rotated.)</p>
7227 *
7228 * <p>The distance is expressed in "depth pixels." The default distance depends
7229 * on the screen density. For instance, on a medium density display, the
7230 * default distance is 1280. On a high density display, the default distance
7231 * is 1920.</p>
7232 *
7233 * <p>If you want to specify a distance that leads to visually consistent
7234 * results across various densities, use the following formula:</p>
7235 * <pre>
7236 * float scale = context.getResources().getDisplayMetrics().density;
7237 * view.setCameraDistance(distance * scale);
7238 * </pre>
7239 *
7240 * <p>The density scale factor of a high density display is 1.5,
7241 * and 1920 = 1280 * 1.5.</p>
7242 *
7243 * @param distance The distance in "depth pixels", if negative the opposite
7244 * value is used
7245 *
7246 * @see #setRotationX(float)
7247 * @see #setRotationY(float)
7248 */
7249 public void setCameraDistance(float distance) {
7250 invalidateParentCaches();
7251 invalidate(false);
7252
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007253 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007254 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007255 final TransformationInfo info = mTransformationInfo;
7256 if (info.mCamera == null) {
7257 info.mCamera = new Camera();
7258 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007259 }
7260
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007261 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7262 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007263
7264 invalidate(false);
7265 }
7266
7267 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007268 * The degrees that the view is rotated around the pivot point.
7269 *
Romain Guya5364ee2011-02-24 14:46:04 -08007270 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007271 * @see #getPivotX()
7272 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007273 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007274 * @return The degrees of rotation.
7275 */
7276 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007277 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007278 }
7279
7280 /**
Chet Haase897247b2010-09-09 14:54:47 -07007281 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7282 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007283 *
7284 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007285 *
7286 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007287 * @see #getPivotX()
7288 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007289 * @see #setRotationX(float)
7290 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007291 *
7292 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007293 */
7294 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007295 ensureTransformationInfo();
7296 final TransformationInfo info = mTransformationInfo;
7297 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007298 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007299 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007300 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007301 info.mRotation = rotation;
7302 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007303 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007304 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007305 }
7306 }
7307
7308 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007309 * The degrees that the view is rotated around the vertical axis through the pivot point.
7310 *
7311 * @see #getPivotX()
7312 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007313 * @see #setRotationY(float)
7314 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007315 * @return The degrees of Y rotation.
7316 */
7317 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007318 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007319 }
7320
7321 /**
Chet Haase897247b2010-09-09 14:54:47 -07007322 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7323 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7324 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007325 *
7326 * When rotating large views, it is recommended to adjust the camera distance
7327 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007328 *
7329 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007330 *
7331 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007332 * @see #getPivotX()
7333 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007334 * @see #setRotation(float)
7335 * @see #setRotationX(float)
7336 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007337 *
7338 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007339 */
7340 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007341 ensureTransformationInfo();
7342 final TransformationInfo info = mTransformationInfo;
7343 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007344 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007345 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007346 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007347 info.mRotationY = rotationY;
7348 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007349 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007350 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007351 }
7352 }
7353
7354 /**
7355 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7356 *
7357 * @see #getPivotX()
7358 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007359 * @see #setRotationX(float)
7360 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007361 * @return The degrees of X rotation.
7362 */
7363 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007364 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007365 }
7366
7367 /**
Chet Haase897247b2010-09-09 14:54:47 -07007368 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7369 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7370 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007371 *
7372 * When rotating large views, it is recommended to adjust the camera distance
7373 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007374 *
7375 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007376 *
7377 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007378 * @see #getPivotX()
7379 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007380 * @see #setRotation(float)
7381 * @see #setRotationY(float)
7382 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007383 *
7384 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007385 */
7386 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007387 ensureTransformationInfo();
7388 final TransformationInfo info = mTransformationInfo;
7389 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007390 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007391 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007392 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007393 info.mRotationX = rotationX;
7394 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007395 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007396 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007397 }
7398 }
7399
7400 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007401 * The amount that the view is scaled in x around the pivot point, as a proportion of
7402 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7403 *
Joe Onorato93162322010-09-16 15:42:01 -04007404 * <p>By default, this is 1.0f.
7405 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007406 * @see #getPivotX()
7407 * @see #getPivotY()
7408 * @return The scaling factor.
7409 */
7410 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007411 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007412 }
7413
7414 /**
7415 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7416 * the view's unscaled width. A value of 1 means that no scaling is applied.
7417 *
7418 * @param scaleX The scaling factor.
7419 * @see #getPivotX()
7420 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007421 *
7422 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007423 */
7424 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007425 ensureTransformationInfo();
7426 final TransformationInfo info = mTransformationInfo;
7427 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007428 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007429 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007430 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007431 info.mScaleX = scaleX;
7432 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007433 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007434 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007435 }
7436 }
7437
7438 /**
7439 * The amount that the view is scaled in y around the pivot point, as a proportion of
7440 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7441 *
Joe Onorato93162322010-09-16 15:42:01 -04007442 * <p>By default, this is 1.0f.
7443 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007444 * @see #getPivotX()
7445 * @see #getPivotY()
7446 * @return The scaling factor.
7447 */
7448 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007449 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007450 }
7451
7452 /**
7453 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7454 * the view's unscaled width. A value of 1 means that no scaling is applied.
7455 *
7456 * @param scaleY The scaling factor.
7457 * @see #getPivotX()
7458 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007459 *
7460 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007461 */
7462 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007463 ensureTransformationInfo();
7464 final TransformationInfo info = mTransformationInfo;
7465 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007466 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007467 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007468 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007469 info.mScaleY = scaleY;
7470 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007471 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007472 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007473 }
7474 }
7475
7476 /**
7477 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7478 * and {@link #setScaleX(float) scaled}.
7479 *
7480 * @see #getRotation()
7481 * @see #getScaleX()
7482 * @see #getScaleY()
7483 * @see #getPivotY()
7484 * @return The x location of the pivot point.
7485 */
7486 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007487 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007488 }
7489
7490 /**
7491 * Sets the x location of the point around which the view is
7492 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007493 * By default, the pivot point is centered on the object.
7494 * Setting this property disables this behavior and causes the view to use only the
7495 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007496 *
7497 * @param pivotX The x location of the pivot point.
7498 * @see #getRotation()
7499 * @see #getScaleX()
7500 * @see #getScaleY()
7501 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007502 *
7503 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007504 */
7505 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007506 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007507 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007508 final TransformationInfo info = mTransformationInfo;
7509 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007510 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007511 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007512 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007513 info.mPivotX = pivotX;
7514 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007515 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007516 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007517 }
7518 }
7519
7520 /**
7521 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7522 * and {@link #setScaleY(float) scaled}.
7523 *
7524 * @see #getRotation()
7525 * @see #getScaleX()
7526 * @see #getScaleY()
7527 * @see #getPivotY()
7528 * @return The y location of the pivot point.
7529 */
7530 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007531 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007532 }
7533
7534 /**
7535 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007536 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7537 * Setting this property disables this behavior and causes the view to use only the
7538 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007539 *
7540 * @param pivotY The y location of the pivot point.
7541 * @see #getRotation()
7542 * @see #getScaleX()
7543 * @see #getScaleY()
7544 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007545 *
7546 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007547 */
7548 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007549 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007550 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007551 final TransformationInfo info = mTransformationInfo;
7552 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007553 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007554 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007555 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007556 info.mPivotY = pivotY;
7557 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007558 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007559 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007560 }
7561 }
7562
7563 /**
7564 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7565 * completely transparent and 1 means the view is completely opaque.
7566 *
Joe Onorato93162322010-09-16 15:42:01 -04007567 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007568 * @return The opacity of the view.
7569 */
7570 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007571 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007572 }
7573
7574 /**
Romain Guy171c5922011-01-06 10:04:23 -08007575 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7576 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007577 *
Romain Guy171c5922011-01-06 10:04:23 -08007578 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7579 * responsible for applying the opacity itself. Otherwise, calling this method is
7580 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007581 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007582 *
7583 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007584 *
Joe Malin32736f02011-01-19 16:14:20 -08007585 * @see #setLayerType(int, android.graphics.Paint)
7586 *
Chet Haase73066682010-11-29 15:55:32 -08007587 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007588 */
7589 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007590 ensureTransformationInfo();
7591 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007592 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007593 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007594 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007595 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007596 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007597 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007598 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007599 invalidate(false);
7600 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007601 }
7602
7603 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007604 * Faster version of setAlpha() which performs the same steps except there are
7605 * no calls to invalidate(). The caller of this function should perform proper invalidation
7606 * on the parent and this object. The return value indicates whether the subclass handles
7607 * alpha (the return value for onSetAlpha()).
7608 *
7609 * @param alpha The new value for the alpha property
7610 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7611 */
7612 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007613 ensureTransformationInfo();
7614 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007615 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7616 if (subclassHandlesAlpha) {
7617 mPrivateFlags |= ALPHA_SET;
7618 } else {
7619 mPrivateFlags &= ~ALPHA_SET;
7620 }
7621 return subclassHandlesAlpha;
7622 }
7623
7624 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007625 * Top position of this view relative to its parent.
7626 *
7627 * @return The top of this view, in pixels.
7628 */
7629 @ViewDebug.CapturedViewProperty
7630 public final int getTop() {
7631 return mTop;
7632 }
7633
7634 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007635 * Sets the top position of this view relative to its parent. This method is meant to be called
7636 * by the layout system and should not generally be called otherwise, because the property
7637 * may be changed at any time by the layout.
7638 *
7639 * @param top The top of this view, in pixels.
7640 */
7641 public final void setTop(int top) {
7642 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007643 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007644 final boolean matrixIsIdentity = mTransformationInfo == null
7645 || mTransformationInfo.mMatrixIsIdentity;
7646 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007647 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007648 int minTop;
7649 int yLoc;
7650 if (top < mTop) {
7651 minTop = top;
7652 yLoc = top - mTop;
7653 } else {
7654 minTop = mTop;
7655 yLoc = 0;
7656 }
Chet Haasee9140a72011-02-16 16:23:29 -08007657 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007658 }
7659 } else {
7660 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007661 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007662 }
7663
Chet Haaseed032702010-10-01 14:05:54 -07007664 int width = mRight - mLeft;
7665 int oldHeight = mBottom - mTop;
7666
Chet Haase21cd1382010-09-01 17:42:29 -07007667 mTop = top;
7668
Chet Haaseed032702010-10-01 14:05:54 -07007669 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7670
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007671 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007672 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7673 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007674 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007675 }
Chet Haase21cd1382010-09-01 17:42:29 -07007676 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007677 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007678 }
Chet Haase55dbb652010-12-21 20:15:08 -08007679 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007680 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007681 }
7682 }
7683
7684 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007685 * Bottom position of this view relative to its parent.
7686 *
7687 * @return The bottom of this view, in pixels.
7688 */
7689 @ViewDebug.CapturedViewProperty
7690 public final int getBottom() {
7691 return mBottom;
7692 }
7693
7694 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007695 * True if this view has changed since the last time being drawn.
7696 *
7697 * @return The dirty state of this view.
7698 */
7699 public boolean isDirty() {
7700 return (mPrivateFlags & DIRTY_MASK) != 0;
7701 }
7702
7703 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007704 * Sets the bottom position of this view relative to its parent. This method is meant to be
7705 * called by the layout system and should not generally be called otherwise, because the
7706 * property may be changed at any time by the layout.
7707 *
7708 * @param bottom The bottom of this view, in pixels.
7709 */
7710 public final void setBottom(int bottom) {
7711 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007712 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007713 final boolean matrixIsIdentity = mTransformationInfo == null
7714 || mTransformationInfo.mMatrixIsIdentity;
7715 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007716 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007717 int maxBottom;
7718 if (bottom < mBottom) {
7719 maxBottom = mBottom;
7720 } else {
7721 maxBottom = bottom;
7722 }
Chet Haasee9140a72011-02-16 16:23:29 -08007723 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007724 }
7725 } else {
7726 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007727 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007728 }
7729
Chet Haaseed032702010-10-01 14:05:54 -07007730 int width = mRight - mLeft;
7731 int oldHeight = mBottom - mTop;
7732
Chet Haase21cd1382010-09-01 17:42:29 -07007733 mBottom = bottom;
7734
Chet Haaseed032702010-10-01 14:05:54 -07007735 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7736
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007737 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007738 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7739 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007740 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007741 }
Chet Haase21cd1382010-09-01 17:42:29 -07007742 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007743 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007744 }
Chet Haase55dbb652010-12-21 20:15:08 -08007745 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007746 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007747 }
7748 }
7749
7750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 * Left position of this view relative to its parent.
7752 *
7753 * @return The left edge of this view, in pixels.
7754 */
7755 @ViewDebug.CapturedViewProperty
7756 public final int getLeft() {
7757 return mLeft;
7758 }
7759
7760 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007761 * Sets the left position of this view relative to its parent. This method is meant to be called
7762 * by the layout system and should not generally be called otherwise, because the property
7763 * may be changed at any time by the layout.
7764 *
7765 * @param left The bottom of this view, in pixels.
7766 */
7767 public final void setLeft(int left) {
7768 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007769 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007770 final boolean matrixIsIdentity = mTransformationInfo == null
7771 || mTransformationInfo.mMatrixIsIdentity;
7772 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007773 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007774 int minLeft;
7775 int xLoc;
7776 if (left < mLeft) {
7777 minLeft = left;
7778 xLoc = left - mLeft;
7779 } else {
7780 minLeft = mLeft;
7781 xLoc = 0;
7782 }
Chet Haasee9140a72011-02-16 16:23:29 -08007783 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007784 }
7785 } else {
7786 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007787 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007788 }
7789
Chet Haaseed032702010-10-01 14:05:54 -07007790 int oldWidth = mRight - mLeft;
7791 int height = mBottom - mTop;
7792
Chet Haase21cd1382010-09-01 17:42:29 -07007793 mLeft = left;
7794
Chet Haaseed032702010-10-01 14:05:54 -07007795 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7796
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007797 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007798 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7799 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007800 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007801 }
Chet Haase21cd1382010-09-01 17:42:29 -07007802 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007803 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007804 }
Chet Haase55dbb652010-12-21 20:15:08 -08007805 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007806 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007807 }
7808 }
7809
7810 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 * Right position of this view relative to its parent.
7812 *
7813 * @return The right edge of this view, in pixels.
7814 */
7815 @ViewDebug.CapturedViewProperty
7816 public final int getRight() {
7817 return mRight;
7818 }
7819
7820 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007821 * Sets the right position of this view relative to its parent. This method is meant to be called
7822 * by the layout system and should not generally be called otherwise, because the property
7823 * may be changed at any time by the layout.
7824 *
7825 * @param right The bottom of this view, in pixels.
7826 */
7827 public final void setRight(int right) {
7828 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007829 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007830 final boolean matrixIsIdentity = mTransformationInfo == null
7831 || mTransformationInfo.mMatrixIsIdentity;
7832 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007833 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007834 int maxRight;
7835 if (right < mRight) {
7836 maxRight = mRight;
7837 } else {
7838 maxRight = right;
7839 }
Chet Haasee9140a72011-02-16 16:23:29 -08007840 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007841 }
7842 } else {
7843 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007844 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007845 }
7846
Chet Haaseed032702010-10-01 14:05:54 -07007847 int oldWidth = mRight - mLeft;
7848 int height = mBottom - mTop;
7849
Chet Haase21cd1382010-09-01 17:42:29 -07007850 mRight = right;
7851
Chet Haaseed032702010-10-01 14:05:54 -07007852 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7853
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007854 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007855 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7856 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007857 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007858 }
Chet Haase21cd1382010-09-01 17:42:29 -07007859 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007860 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007861 }
Chet Haase55dbb652010-12-21 20:15:08 -08007862 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007863 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007864 }
7865 }
7866
7867 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007868 * The visual x position of this view, in pixels. This is equivalent to the
7869 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007870 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007871 *
Chet Haasedf030d22010-07-30 17:22:38 -07007872 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007873 */
Chet Haasedf030d22010-07-30 17:22:38 -07007874 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007875 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007876 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007877
Chet Haasedf030d22010-07-30 17:22:38 -07007878 /**
7879 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7880 * {@link #setTranslationX(float) translationX} property to be the difference between
7881 * the x value passed in and the current {@link #getLeft() left} property.
7882 *
7883 * @param x The visual x position of this view, in pixels.
7884 */
7885 public void setX(float x) {
7886 setTranslationX(x - mLeft);
7887 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007888
Chet Haasedf030d22010-07-30 17:22:38 -07007889 /**
7890 * The visual y position of this view, in pixels. This is equivalent to the
7891 * {@link #setTranslationY(float) translationY} property plus the current
7892 * {@link #getTop() top} property.
7893 *
7894 * @return The visual y position of this view, in pixels.
7895 */
7896 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007897 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007898 }
7899
7900 /**
7901 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7902 * {@link #setTranslationY(float) translationY} property to be the difference between
7903 * the y value passed in and the current {@link #getTop() top} property.
7904 *
7905 * @param y The visual y position of this view, in pixels.
7906 */
7907 public void setY(float y) {
7908 setTranslationY(y - mTop);
7909 }
7910
7911
7912 /**
7913 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7914 * This position is post-layout, in addition to wherever the object's
7915 * layout placed it.
7916 *
7917 * @return The horizontal position of this view relative to its left position, in pixels.
7918 */
7919 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007920 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007921 }
7922
7923 /**
7924 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7925 * This effectively positions the object post-layout, in addition to wherever the object's
7926 * layout placed it.
7927 *
7928 * @param translationX The horizontal position of this view relative to its left position,
7929 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007930 *
7931 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007932 */
7933 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007934 ensureTransformationInfo();
7935 final TransformationInfo info = mTransformationInfo;
7936 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007937 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007938 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007939 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007940 info.mTranslationX = translationX;
7941 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007942 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007943 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007944 }
7945 }
7946
7947 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007948 * The horizontal location of this view relative to its {@link #getTop() top} position.
7949 * This position is post-layout, in addition to wherever the object's
7950 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007951 *
Chet Haasedf030d22010-07-30 17:22:38 -07007952 * @return The vertical position of this view relative to its top position,
7953 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007954 */
Chet Haasedf030d22010-07-30 17:22:38 -07007955 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007956 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007957 }
7958
7959 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007960 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7961 * This effectively positions the object post-layout, in addition to wherever the object's
7962 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007963 *
Chet Haasedf030d22010-07-30 17:22:38 -07007964 * @param translationY The vertical position of this view relative to its top position,
7965 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007966 *
7967 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007968 */
Chet Haasedf030d22010-07-30 17:22:38 -07007969 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007970 ensureTransformationInfo();
7971 final TransformationInfo info = mTransformationInfo;
7972 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007973 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007974 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007975 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007976 info.mTranslationY = translationY;
7977 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007978 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007979 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007980 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007981 }
7982
7983 /**
Romain Guyda489792011-02-03 01:05:15 -08007984 * @hide
7985 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007986 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007987 ensureTransformationInfo();
7988 final TransformationInfo info = mTransformationInfo;
7989 info.mTranslationX = x;
7990 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007991 }
7992
7993 /**
7994 * @hide
7995 */
7996 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007997 ensureTransformationInfo();
7998 final TransformationInfo info = mTransformationInfo;
7999 info.mTranslationY = y;
8000 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08008001 }
8002
8003 /**
8004 * @hide
8005 */
Romain Guyda489792011-02-03 01:05:15 -08008006 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008007 ensureTransformationInfo();
8008 final TransformationInfo info = mTransformationInfo;
8009 info.mTranslationX = x - mLeft;
8010 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08008011 }
8012
8013 /**
8014 * @hide
8015 */
8016 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008017 ensureTransformationInfo();
8018 final TransformationInfo info = mTransformationInfo;
8019 info.mTranslationY = y - mTop;
8020 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08008021 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008022
Romain Guyda489792011-02-03 01:05:15 -08008023 /**
8024 * @hide
8025 */
8026 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008027 ensureTransformationInfo();
8028 final TransformationInfo info = mTransformationInfo;
8029 info.mScaleX = x;
8030 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08008031 }
8032
8033 /**
8034 * @hide
8035 */
8036 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008037 ensureTransformationInfo();
8038 final TransformationInfo info = mTransformationInfo;
8039 info.mScaleY = y;
8040 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08008041 }
8042
8043 /**
8044 * @hide
8045 */
8046 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008047 ensureTransformationInfo();
8048 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08008049 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008050
Romain Guyda489792011-02-03 01:05:15 -08008051 /**
8052 * @hide
8053 */
8054 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008055 ensureTransformationInfo();
8056 final TransformationInfo info = mTransformationInfo;
8057 info.mRotationY = y;
8058 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08008059 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008060
Romain Guyda489792011-02-03 01:05:15 -08008061 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008062 * Hit rectangle in parent's coordinates
8063 *
8064 * @param outRect The hit rectangle of the view.
8065 */
8066 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008067 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008068 final TransformationInfo info = mTransformationInfo;
8069 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008070 outRect.set(mLeft, mTop, mRight, mBottom);
8071 } else {
8072 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008073 tmpRect.set(-info.mPivotX, -info.mPivotY,
8074 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8075 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008076 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8077 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008078 }
8079 }
8080
8081 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008082 * Determines whether the given point, in local coordinates is inside the view.
8083 */
8084 /*package*/ final boolean pointInView(float localX, float localY) {
8085 return localX >= 0 && localX < (mRight - mLeft)
8086 && localY >= 0 && localY < (mBottom - mTop);
8087 }
8088
8089 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008090 * Utility method to determine whether the given point, in local coordinates,
8091 * is inside the view, where the area of the view is expanded by the slop factor.
8092 * This method is called while processing touch-move events to determine if the event
8093 * is still within the view.
8094 */
8095 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008096 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008097 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008098 }
8099
8100 /**
8101 * When a view has focus and the user navigates away from it, the next view is searched for
8102 * starting from the rectangle filled in by this method.
8103 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008104 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8105 * of the view. However, if your view maintains some idea of internal selection,
8106 * such as a cursor, or a selected row or column, you should override this method and
8107 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008108 *
8109 * @param r The rectangle to fill in, in this view's coordinates.
8110 */
8111 public void getFocusedRect(Rect r) {
8112 getDrawingRect(r);
8113 }
8114
8115 /**
8116 * If some part of this view is not clipped by any of its parents, then
8117 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008118 * coordinates (without taking possible View rotations into account), offset
8119 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8120 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 *
8122 * @param r If true is returned, r holds the global coordinates of the
8123 * visible portion of this view.
8124 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8125 * between this view and its root. globalOffet may be null.
8126 * @return true if r is non-empty (i.e. part of the view is visible at the
8127 * root level.
8128 */
8129 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8130 int width = mRight - mLeft;
8131 int height = mBottom - mTop;
8132 if (width > 0 && height > 0) {
8133 r.set(0, 0, width, height);
8134 if (globalOffset != null) {
8135 globalOffset.set(-mScrollX, -mScrollY);
8136 }
8137 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8138 }
8139 return false;
8140 }
8141
8142 public final boolean getGlobalVisibleRect(Rect r) {
8143 return getGlobalVisibleRect(r, null);
8144 }
8145
8146 public final boolean getLocalVisibleRect(Rect r) {
8147 Point offset = new Point();
8148 if (getGlobalVisibleRect(r, offset)) {
8149 r.offset(-offset.x, -offset.y); // make r local
8150 return true;
8151 }
8152 return false;
8153 }
8154
8155 /**
8156 * Offset this view's vertical location by the specified number of pixels.
8157 *
8158 * @param offset the number of pixels to offset the view by
8159 */
8160 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008161 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008162 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008163 final boolean matrixIsIdentity = mTransformationInfo == null
8164 || mTransformationInfo.mMatrixIsIdentity;
8165 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008166 final ViewParent p = mParent;
8167 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008168 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008169 int minTop;
8170 int maxBottom;
8171 int yLoc;
8172 if (offset < 0) {
8173 minTop = mTop + offset;
8174 maxBottom = mBottom;
8175 yLoc = offset;
8176 } else {
8177 minTop = mTop;
8178 maxBottom = mBottom + offset;
8179 yLoc = 0;
8180 }
8181 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8182 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008183 }
8184 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008185 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008186 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008187
Chet Haasec3aa3612010-06-17 08:50:37 -07008188 mTop += offset;
8189 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008190
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008191 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008192 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008193 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008194 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008195 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197 }
8198
8199 /**
8200 * Offset this view's horizontal location by the specified amount of pixels.
8201 *
8202 * @param offset the numer of pixels to offset the view by
8203 */
8204 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008205 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008206 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008207 final boolean matrixIsIdentity = mTransformationInfo == null
8208 || mTransformationInfo.mMatrixIsIdentity;
8209 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008210 final ViewParent p = mParent;
8211 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008212 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008213 int minLeft;
8214 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008215 if (offset < 0) {
8216 minLeft = mLeft + offset;
8217 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008218 } else {
8219 minLeft = mLeft;
8220 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008221 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008222 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008223 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008224 }
8225 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008226 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008227 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008228
Chet Haasec3aa3612010-06-17 08:50:37 -07008229 mLeft += offset;
8230 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008231
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008232 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008233 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008234 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008235 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008236 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 }
8239
8240 /**
8241 * Get the LayoutParams associated with this view. All views should have
8242 * layout parameters. These supply parameters to the <i>parent</i> of this
8243 * view specifying how it should be arranged. There are many subclasses of
8244 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8245 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008246 *
8247 * This method may return null if this View is not attached to a parent
8248 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8249 * was not invoked successfully. When a View is attached to a parent
8250 * ViewGroup, this method must not return null.
8251 *
8252 * @return The LayoutParams associated with this view, or null if no
8253 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008255 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 public ViewGroup.LayoutParams getLayoutParams() {
8257 return mLayoutParams;
8258 }
8259
8260 /**
8261 * Set the layout parameters associated with this view. These supply
8262 * parameters to the <i>parent</i> of this view specifying how it should be
8263 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8264 * correspond to the different subclasses of ViewGroup that are responsible
8265 * for arranging their children.
8266 *
Romain Guy01c174b2011-02-22 11:51:06 -08008267 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268 */
8269 public void setLayoutParams(ViewGroup.LayoutParams params) {
8270 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008271 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008272 }
8273 mLayoutParams = params;
8274 requestLayout();
8275 }
8276
8277 /**
8278 * Set the scrolled position of your view. This will cause a call to
8279 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8280 * invalidated.
8281 * @param x the x position to scroll to
8282 * @param y the y position to scroll to
8283 */
8284 public void scrollTo(int x, int y) {
8285 if (mScrollX != x || mScrollY != y) {
8286 int oldX = mScrollX;
8287 int oldY = mScrollY;
8288 mScrollX = x;
8289 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008290 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008292 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008293 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295 }
8296 }
8297
8298 /**
8299 * Move the scrolled position of your view. This will cause a call to
8300 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8301 * invalidated.
8302 * @param x the amount of pixels to scroll by horizontally
8303 * @param y the amount of pixels to scroll by vertically
8304 */
8305 public void scrollBy(int x, int y) {
8306 scrollTo(mScrollX + x, mScrollY + y);
8307 }
8308
8309 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008310 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8311 * animation to fade the scrollbars out after a default delay. If a subclass
8312 * provides animated scrolling, the start delay should equal the duration
8313 * of the scrolling animation.</p>
8314 *
8315 * <p>The animation starts only if at least one of the scrollbars is
8316 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8317 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8318 * this method returns true, and false otherwise. If the animation is
8319 * started, this method calls {@link #invalidate()}; in that case the
8320 * caller should not call {@link #invalidate()}.</p>
8321 *
8322 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008323 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008324 *
8325 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8326 * and {@link #scrollTo(int, int)}.</p>
8327 *
8328 * @return true if the animation is played, false otherwise
8329 *
8330 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008331 * @see #scrollBy(int, int)
8332 * @see #scrollTo(int, int)
8333 * @see #isHorizontalScrollBarEnabled()
8334 * @see #isVerticalScrollBarEnabled()
8335 * @see #setHorizontalScrollBarEnabled(boolean)
8336 * @see #setVerticalScrollBarEnabled(boolean)
8337 */
8338 protected boolean awakenScrollBars() {
8339 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008340 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008341 }
8342
8343 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008344 * Trigger the scrollbars to draw.
8345 * This method differs from awakenScrollBars() only in its default duration.
8346 * initialAwakenScrollBars() will show the scroll bars for longer than
8347 * usual to give the user more of a chance to notice them.
8348 *
8349 * @return true if the animation is played, false otherwise.
8350 */
8351 private boolean initialAwakenScrollBars() {
8352 return mScrollCache != null &&
8353 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8354 }
8355
8356 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008357 * <p>
8358 * Trigger the scrollbars to draw. When invoked this method starts an
8359 * animation to fade the scrollbars out after a fixed delay. If a subclass
8360 * provides animated scrolling, the start delay should equal the duration of
8361 * the scrolling animation.
8362 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008363 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008364 * <p>
8365 * The animation starts only if at least one of the scrollbars is enabled,
8366 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8367 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8368 * this method returns true, and false otherwise. If the animation is
8369 * started, this method calls {@link #invalidate()}; in that case the caller
8370 * should not call {@link #invalidate()}.
8371 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008372 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008373 * <p>
8374 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008375 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008376 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008377 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008378 * @param startDelay the delay, in milliseconds, after which the animation
8379 * should start; when the delay is 0, the animation starts
8380 * immediately
8381 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008382 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008383 * @see #scrollBy(int, int)
8384 * @see #scrollTo(int, int)
8385 * @see #isHorizontalScrollBarEnabled()
8386 * @see #isVerticalScrollBarEnabled()
8387 * @see #setHorizontalScrollBarEnabled(boolean)
8388 * @see #setVerticalScrollBarEnabled(boolean)
8389 */
8390 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008391 return awakenScrollBars(startDelay, true);
8392 }
Joe Malin32736f02011-01-19 16:14:20 -08008393
Mike Cleron290947b2009-09-29 18:34:32 -07008394 /**
8395 * <p>
8396 * Trigger the scrollbars to draw. When invoked this method starts an
8397 * animation to fade the scrollbars out after a fixed delay. If a subclass
8398 * provides animated scrolling, the start delay should equal the duration of
8399 * the scrolling animation.
8400 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008401 *
Mike Cleron290947b2009-09-29 18:34:32 -07008402 * <p>
8403 * The animation starts only if at least one of the scrollbars is enabled,
8404 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8405 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8406 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008407 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008408 * is set to true; in that case the caller
8409 * should not call {@link #invalidate()}.
8410 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008411 *
Mike Cleron290947b2009-09-29 18:34:32 -07008412 * <p>
8413 * This method should be invoked everytime a subclass directly updates the
8414 * scroll parameters.
8415 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008416 *
Mike Cleron290947b2009-09-29 18:34:32 -07008417 * @param startDelay the delay, in milliseconds, after which the animation
8418 * should start; when the delay is 0, the animation starts
8419 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008420 *
Mike Cleron290947b2009-09-29 18:34:32 -07008421 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008422 *
Mike Cleron290947b2009-09-29 18:34:32 -07008423 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008424 *
Mike Cleron290947b2009-09-29 18:34:32 -07008425 * @see #scrollBy(int, int)
8426 * @see #scrollTo(int, int)
8427 * @see #isHorizontalScrollBarEnabled()
8428 * @see #isVerticalScrollBarEnabled()
8429 * @see #setHorizontalScrollBarEnabled(boolean)
8430 * @see #setVerticalScrollBarEnabled(boolean)
8431 */
8432 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008433 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008434
Mike Cleronf116bf82009-09-27 19:14:12 -07008435 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8436 return false;
8437 }
8438
8439 if (scrollCache.scrollBar == null) {
8440 scrollCache.scrollBar = new ScrollBarDrawable();
8441 }
8442
8443 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8444
Mike Cleron290947b2009-09-29 18:34:32 -07008445 if (invalidate) {
8446 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008447 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008448 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008449
8450 if (scrollCache.state == ScrollabilityCache.OFF) {
8451 // FIXME: this is copied from WindowManagerService.
8452 // We should get this value from the system when it
8453 // is possible to do so.
8454 final int KEY_REPEAT_FIRST_DELAY = 750;
8455 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8456 }
8457
8458 // Tell mScrollCache when we should start fading. This may
8459 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008460 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008461 scrollCache.fadeStartTime = fadeStartTime;
8462 scrollCache.state = ScrollabilityCache.ON;
8463
8464 // Schedule our fader to run, unscheduling any old ones first
8465 if (mAttachInfo != null) {
8466 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8467 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8468 }
8469
8470 return true;
8471 }
8472
8473 return false;
8474 }
8475
8476 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008477 * Do not invalidate views which are not visible and which are not running an animation. They
8478 * will not get drawn and they should not set dirty flags as if they will be drawn
8479 */
8480 private boolean skipInvalidate() {
8481 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8482 (!(mParent instanceof ViewGroup) ||
8483 !((ViewGroup) mParent).isViewTransitioning(this));
8484 }
8485 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008486 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008487 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8488 * in the future. This must be called from a UI thread. To call from a non-UI
8489 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 *
8491 * WARNING: This method is destructive to dirty.
8492 * @param dirty the rectangle representing the bounds of the dirty region
8493 */
8494 public void invalidate(Rect dirty) {
8495 if (ViewDebug.TRACE_HIERARCHY) {
8496 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8497 }
8498
Chet Haaseaceafe62011-08-26 15:44:33 -07008499 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008500 return;
8501 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008502 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008503 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8504 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008506 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008507 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008508 final ViewParent p = mParent;
8509 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008510 //noinspection PointlessBooleanExpression,ConstantConditions
8511 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8512 if (p != null && ai != null && ai.mHardwareAccelerated) {
8513 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008514 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008515 p.invalidateChild(this, null);
8516 return;
8517 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 if (p != null && ai != null) {
8520 final int scrollX = mScrollX;
8521 final int scrollY = mScrollY;
8522 final Rect r = ai.mTmpInvalRect;
8523 r.set(dirty.left - scrollX, dirty.top - scrollY,
8524 dirty.right - scrollX, dirty.bottom - scrollY);
8525 mParent.invalidateChild(this, r);
8526 }
8527 }
8528 }
8529
8530 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008531 * 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 -08008532 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008533 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8534 * will be called at some point in the future. This must be called from
8535 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 * @param l the left position of the dirty region
8537 * @param t the top position of the dirty region
8538 * @param r the right position of the dirty region
8539 * @param b the bottom position of the dirty region
8540 */
8541 public void invalidate(int l, int t, int r, int b) {
8542 if (ViewDebug.TRACE_HIERARCHY) {
8543 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8544 }
8545
Chet Haaseaceafe62011-08-26 15:44:33 -07008546 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008547 return;
8548 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008549 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008550 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8551 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008552 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008553 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008554 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 final ViewParent p = mParent;
8556 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008557 //noinspection PointlessBooleanExpression,ConstantConditions
8558 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8559 if (p != null && ai != null && ai.mHardwareAccelerated) {
8560 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008561 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008562 p.invalidateChild(this, null);
8563 return;
8564 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 if (p != null && ai != null && l < r && t < b) {
8567 final int scrollX = mScrollX;
8568 final int scrollY = mScrollY;
8569 final Rect tmpr = ai.mTmpInvalRect;
8570 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8571 p.invalidateChild(this, tmpr);
8572 }
8573 }
8574 }
8575
8576 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008577 * Invalidate the whole view. If the view is visible,
8578 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8579 * the future. This must be called from a UI thread. To call from a non-UI thread,
8580 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008581 */
8582 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008583 invalidate(true);
8584 }
Joe Malin32736f02011-01-19 16:14:20 -08008585
Chet Haaseed032702010-10-01 14:05:54 -07008586 /**
8587 * This is where the invalidate() work actually happens. A full invalidate()
8588 * causes the drawing cache to be invalidated, but this function can be called with
8589 * invalidateCache set to false to skip that invalidation step for cases that do not
8590 * need it (for example, a component that remains at the same dimensions with the same
8591 * content).
8592 *
8593 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8594 * well. This is usually true for a full invalidate, but may be set to false if the
8595 * View's contents or dimensions have not changed.
8596 */
Romain Guy849d0a32011-02-01 17:20:48 -08008597 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008598 if (ViewDebug.TRACE_HIERARCHY) {
8599 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8600 }
8601
Chet Haaseaceafe62011-08-26 15:44:33 -07008602 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008603 return;
8604 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008605 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008606 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008607 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8608 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008609 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008610 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008611 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008612 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008613 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008616 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008617 //noinspection PointlessBooleanExpression,ConstantConditions
8618 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8619 if (p != null && ai != null && ai.mHardwareAccelerated) {
8620 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008621 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008622 p.invalidateChild(this, null);
8623 return;
8624 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008625 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 if (p != null && ai != null) {
8628 final Rect r = ai.mTmpInvalRect;
8629 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8630 // Don't call invalidate -- we don't want to internally scroll
8631 // our own bounds
8632 p.invalidateChild(this, r);
8633 }
8634 }
8635 }
8636
8637 /**
Romain Guyda489792011-02-03 01:05:15 -08008638 * @hide
8639 */
8640 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008641 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008642 return;
8643 }
Romain Guyda489792011-02-03 01:05:15 -08008644 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8645 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8646 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8647 if (mParent instanceof View) {
8648 ((View) mParent).mPrivateFlags |= INVALIDATED;
8649 }
8650 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008651 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008652 mPrivateFlags |= INVALIDATED;
8653 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008654 if (mParent != null && mAttachInfo != null) {
8655 if (mAttachInfo.mHardwareAccelerated) {
8656 mParent.invalidateChild(this, null);
8657 } else {
8658 final Rect r = mAttachInfo.mTmpInvalRect;
8659 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8660 // Don't call invalidate -- we don't want to internally scroll
8661 // our own bounds
8662 mParent.invalidateChild(this, r);
8663 }
Romain Guyda489792011-02-03 01:05:15 -08008664 }
8665 }
8666 }
8667
8668 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008669 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008670 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8671 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008672 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8673 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008674 *
8675 * @hide
8676 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008677 protected void invalidateParentCaches() {
8678 if (mParent instanceof View) {
8679 ((View) mParent).mPrivateFlags |= INVALIDATED;
8680 }
8681 }
Joe Malin32736f02011-01-19 16:14:20 -08008682
Romain Guy0fd89bf2011-01-26 15:41:30 -08008683 /**
8684 * Used to indicate that the parent of this view should be invalidated. This functionality
8685 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8686 * which is necessary when various parent-managed properties of the view change, such as
8687 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8688 * an invalidation event to the parent.
8689 *
8690 * @hide
8691 */
8692 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008693 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008694 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008695 }
8696 }
8697
8698 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008699 * Indicates whether this View is opaque. An opaque View guarantees that it will
8700 * draw all the pixels overlapping its bounds using a fully opaque color.
8701 *
8702 * Subclasses of View should override this method whenever possible to indicate
8703 * whether an instance is opaque. Opaque Views are treated in a special way by
8704 * the View hierarchy, possibly allowing it to perform optimizations during
8705 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008706 *
Romain Guy24443ea2009-05-11 11:56:30 -07008707 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008708 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008709 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008710 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008711 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008712 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8713 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008714 }
8715
Adam Powell20232d02010-12-08 21:08:53 -08008716 /**
8717 * @hide
8718 */
8719 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008720 // Opaque if:
8721 // - Has a background
8722 // - Background is opaque
8723 // - Doesn't have scrollbars or scrollbars are inside overlay
8724
8725 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8726 mPrivateFlags |= OPAQUE_BACKGROUND;
8727 } else {
8728 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8729 }
8730
8731 final int flags = mViewFlags;
8732 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8733 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8734 mPrivateFlags |= OPAQUE_SCROLLBARS;
8735 } else {
8736 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8737 }
8738 }
8739
8740 /**
8741 * @hide
8742 */
8743 protected boolean hasOpaqueScrollbars() {
8744 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008745 }
8746
8747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 * @return A handler associated with the thread running the View. This
8749 * handler can be used to pump events in the UI events queue.
8750 */
8751 public Handler getHandler() {
8752 if (mAttachInfo != null) {
8753 return mAttachInfo.mHandler;
8754 }
8755 return null;
8756 }
8757
8758 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008759 * <p>Causes the Runnable to be added to the message queue.
8760 * The runnable will be run on the user interface thread.</p>
8761 *
8762 * <p>This method can be invoked from outside of the UI thread
8763 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008764 *
8765 * @param action The Runnable that will be executed.
8766 *
8767 * @return Returns true if the Runnable was successfully placed in to the
8768 * message queue. Returns false on failure, usually because the
8769 * looper processing the message queue is exiting.
8770 */
8771 public boolean post(Runnable action) {
8772 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008773 AttachInfo attachInfo = mAttachInfo;
8774 if (attachInfo != null) {
8775 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008776 } else {
8777 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008778 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 return true;
8780 }
8781
8782 return handler.post(action);
8783 }
8784
8785 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008786 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008787 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008788 * The runnable will be run on the user interface thread.</p>
8789 *
8790 * <p>This method can be invoked from outside of the UI thread
8791 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008792 *
8793 * @param action The Runnable that will be executed.
8794 * @param delayMillis The delay (in milliseconds) until the Runnable
8795 * will be executed.
8796 *
8797 * @return true if the Runnable was successfully placed in to the
8798 * message queue. Returns false on failure, usually because the
8799 * looper processing the message queue is exiting. Note that a
8800 * result of true does not mean the Runnable will be processed --
8801 * if the looper is quit before the delivery time of the message
8802 * occurs then the message will be dropped.
8803 */
8804 public boolean postDelayed(Runnable action, long delayMillis) {
8805 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008806 AttachInfo attachInfo = mAttachInfo;
8807 if (attachInfo != null) {
8808 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 } else {
8810 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008811 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008812 return true;
8813 }
8814
8815 return handler.postDelayed(action, delayMillis);
8816 }
8817
8818 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008819 * <p>Removes the specified Runnable from the message queue.</p>
8820 *
8821 * <p>This method can be invoked from outside of the UI thread
8822 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008823 *
8824 * @param action The Runnable to remove from the message handling queue
8825 *
8826 * @return true if this view could ask the Handler to remove the Runnable,
8827 * false otherwise. When the returned value is true, the Runnable
8828 * may or may not have been actually removed from the message queue
8829 * (for instance, if the Runnable was not in the queue already.)
8830 */
8831 public boolean removeCallbacks(Runnable action) {
8832 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008833 AttachInfo attachInfo = mAttachInfo;
8834 if (attachInfo != null) {
8835 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 } else {
8837 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008838 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008839 return true;
8840 }
8841
8842 handler.removeCallbacks(action);
8843 return true;
8844 }
8845
8846 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008847 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8848 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008849 *
Romain Guye63a4f32011-08-11 11:33:31 -07008850 * <p>This method can be invoked from outside of the UI thread
8851 * only when this View is attached to a window.</p>
8852 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 * @see #invalidate()
8854 */
8855 public void postInvalidate() {
8856 postInvalidateDelayed(0);
8857 }
8858
8859 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008860 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8861 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8862 *
8863 * <p>This method can be invoked from outside of the UI thread
8864 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 *
8866 * @param left The left coordinate of the rectangle to invalidate.
8867 * @param top The top coordinate of the rectangle to invalidate.
8868 * @param right The right coordinate of the rectangle to invalidate.
8869 * @param bottom The bottom coordinate of the rectangle to invalidate.
8870 *
8871 * @see #invalidate(int, int, int, int)
8872 * @see #invalidate(Rect)
8873 */
8874 public void postInvalidate(int left, int top, int right, int bottom) {
8875 postInvalidateDelayed(0, left, top, right, bottom);
8876 }
8877
8878 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008879 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8880 * loop. Waits for the specified amount of time.</p>
8881 *
8882 * <p>This method can be invoked from outside of the UI thread
8883 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008884 *
8885 * @param delayMilliseconds the duration in milliseconds to delay the
8886 * invalidation by
8887 */
8888 public void postInvalidateDelayed(long delayMilliseconds) {
8889 // We try only with the AttachInfo because there's no point in invalidating
8890 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008891 AttachInfo attachInfo = mAttachInfo;
8892 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008893 Message msg = Message.obtain();
8894 msg.what = AttachInfo.INVALIDATE_MSG;
8895 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008896 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008897 }
8898 }
8899
8900 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008901 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8902 * through the event loop. Waits for the specified amount of time.</p>
8903 *
8904 * <p>This method can be invoked from outside of the UI thread
8905 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 *
8907 * @param delayMilliseconds the duration in milliseconds to delay the
8908 * invalidation by
8909 * @param left The left coordinate of the rectangle to invalidate.
8910 * @param top The top coordinate of the rectangle to invalidate.
8911 * @param right The right coordinate of the rectangle to invalidate.
8912 * @param bottom The bottom coordinate of the rectangle to invalidate.
8913 */
8914 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8915 int right, int bottom) {
8916
8917 // We try only with the AttachInfo because there's no point in invalidating
8918 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008919 AttachInfo attachInfo = mAttachInfo;
8920 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008921 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8922 info.target = this;
8923 info.left = left;
8924 info.top = top;
8925 info.right = right;
8926 info.bottom = bottom;
8927
8928 final Message msg = Message.obtain();
8929 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8930 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008931 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932 }
8933 }
8934
8935 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008936 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8937 * This event is sent at most once every
8938 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8939 */
8940 private void postSendViewScrolledAccessibilityEventCallback() {
8941 if (mSendViewScrolledAccessibilityEvent == null) {
8942 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8943 }
8944 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8945 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8946 postDelayed(mSendViewScrolledAccessibilityEvent,
8947 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8948 }
8949 }
8950
8951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 * Called by a parent to request that a child update its values for mScrollX
8953 * and mScrollY if necessary. This will typically be done if the child is
8954 * animating a scroll using a {@link android.widget.Scroller Scroller}
8955 * object.
8956 */
8957 public void computeScroll() {
8958 }
8959
8960 /**
8961 * <p>Indicate whether the horizontal edges are faded when the view is
8962 * scrolled horizontally.</p>
8963 *
8964 * @return true if the horizontal edges should are faded on scroll, false
8965 * otherwise
8966 *
8967 * @see #setHorizontalFadingEdgeEnabled(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 isHorizontalFadingEdgeEnabled() {
8971 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8972 }
8973
8974 /**
8975 * <p>Define whether the horizontal edges should be faded when this view
8976 * is scrolled horizontally.</p>
8977 *
8978 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8979 * be faded when the view is scrolled
8980 * horizontally
8981 *
8982 * @see #isHorizontalFadingEdgeEnabled()
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 setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8986 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8987 if (horizontalFadingEdgeEnabled) {
8988 initScrollCache();
8989 }
8990
8991 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8992 }
8993 }
8994
8995 /**
8996 * <p>Indicate whether the vertical edges are faded when the view is
8997 * scrolled horizontally.</p>
8998 *
8999 * @return true if the vertical edges should are faded on scroll, false
9000 * otherwise
9001 *
9002 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009003 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009004 */
9005 public boolean isVerticalFadingEdgeEnabled() {
9006 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9007 }
9008
9009 /**
9010 * <p>Define whether the vertical edges should be faded when this view
9011 * is scrolled vertically.</p>
9012 *
9013 * @param verticalFadingEdgeEnabled true if the vertical edges should
9014 * be faded when the view is scrolled
9015 * vertically
9016 *
9017 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009018 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 */
9020 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9021 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9022 if (verticalFadingEdgeEnabled) {
9023 initScrollCache();
9024 }
9025
9026 mViewFlags ^= FADING_EDGE_VERTICAL;
9027 }
9028 }
9029
9030 /**
9031 * Returns the strength, or intensity, of the top faded edge. The strength is
9032 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9033 * returns 0.0 or 1.0 but no value in between.
9034 *
9035 * Subclasses should override this method to provide a smoother fade transition
9036 * when scrolling occurs.
9037 *
9038 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9039 */
9040 protected float getTopFadingEdgeStrength() {
9041 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9042 }
9043
9044 /**
9045 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9046 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9047 * returns 0.0 or 1.0 but no value in between.
9048 *
9049 * Subclasses should override this method to provide a smoother fade transition
9050 * when scrolling occurs.
9051 *
9052 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9053 */
9054 protected float getBottomFadingEdgeStrength() {
9055 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9056 computeVerticalScrollRange() ? 1.0f : 0.0f;
9057 }
9058
9059 /**
9060 * Returns the strength, or intensity, of the left faded edge. The strength is
9061 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9062 * returns 0.0 or 1.0 but no value in between.
9063 *
9064 * Subclasses should override this method to provide a smoother fade transition
9065 * when scrolling occurs.
9066 *
9067 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9068 */
9069 protected float getLeftFadingEdgeStrength() {
9070 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9071 }
9072
9073 /**
9074 * Returns the strength, or intensity, of the right faded edge. The strength is
9075 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9076 * returns 0.0 or 1.0 but no value in between.
9077 *
9078 * Subclasses should override this method to provide a smoother fade transition
9079 * when scrolling occurs.
9080 *
9081 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9082 */
9083 protected float getRightFadingEdgeStrength() {
9084 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9085 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9086 }
9087
9088 /**
9089 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9090 * scrollbar is not drawn by default.</p>
9091 *
9092 * @return true if the horizontal scrollbar should be painted, false
9093 * otherwise
9094 *
9095 * @see #setHorizontalScrollBarEnabled(boolean)
9096 */
9097 public boolean isHorizontalScrollBarEnabled() {
9098 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9099 }
9100
9101 /**
9102 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9103 * scrollbar is not drawn by default.</p>
9104 *
9105 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9106 * be painted
9107 *
9108 * @see #isHorizontalScrollBarEnabled()
9109 */
9110 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9111 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9112 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009113 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009114 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 }
9116 }
9117
9118 /**
9119 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9120 * scrollbar is not drawn by default.</p>
9121 *
9122 * @return true if the vertical scrollbar should be painted, false
9123 * otherwise
9124 *
9125 * @see #setVerticalScrollBarEnabled(boolean)
9126 */
9127 public boolean isVerticalScrollBarEnabled() {
9128 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9129 }
9130
9131 /**
9132 * <p>Define whether the vertical scrollbar should be drawn or not. The
9133 * scrollbar is not drawn by default.</p>
9134 *
9135 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9136 * be painted
9137 *
9138 * @see #isVerticalScrollBarEnabled()
9139 */
9140 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9141 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9142 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009143 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009144 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009145 }
9146 }
9147
Adam Powell20232d02010-12-08 21:08:53 -08009148 /**
9149 * @hide
9150 */
9151 protected void recomputePadding() {
9152 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153 }
Joe Malin32736f02011-01-19 16:14:20 -08009154
Mike Cleronfe81d382009-09-28 14:22:16 -07009155 /**
9156 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009157 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009158 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009159 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009160 */
9161 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9162 initScrollCache();
9163 final ScrollabilityCache scrollabilityCache = mScrollCache;
9164 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009165 if (fadeScrollbars) {
9166 scrollabilityCache.state = ScrollabilityCache.OFF;
9167 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009168 scrollabilityCache.state = ScrollabilityCache.ON;
9169 }
9170 }
Joe Malin32736f02011-01-19 16:14:20 -08009171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172 /**
Joe Malin32736f02011-01-19 16:14:20 -08009173 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009174 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009175 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009176 * @return true if scrollbar fading is enabled
9177 */
9178 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009179 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009180 }
Joe Malin32736f02011-01-19 16:14:20 -08009181
Mike Cleron52f0a642009-09-28 18:21:37 -07009182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009183 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9184 * inset. When inset, they add to the padding of the view. And the scrollbars
9185 * can be drawn inside the padding area or on the edge of the view. For example,
9186 * if a view has a background drawable and you want to draw the scrollbars
9187 * inside the padding specified by the drawable, you can use
9188 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9189 * appear at the edge of the view, ignoring the padding, then you can use
9190 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9191 * @param style the style of the scrollbars. Should be one of
9192 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9193 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9194 * @see #SCROLLBARS_INSIDE_OVERLAY
9195 * @see #SCROLLBARS_INSIDE_INSET
9196 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9197 * @see #SCROLLBARS_OUTSIDE_INSET
9198 */
9199 public void setScrollBarStyle(int style) {
9200 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9201 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009202 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009203 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009204 }
9205 }
9206
9207 /**
9208 * <p>Returns the current scrollbar style.</p>
9209 * @return the current scrollbar style
9210 * @see #SCROLLBARS_INSIDE_OVERLAY
9211 * @see #SCROLLBARS_INSIDE_INSET
9212 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9213 * @see #SCROLLBARS_OUTSIDE_INSET
9214 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009215 @ViewDebug.ExportedProperty(mapping = {
9216 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9217 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9218 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9219 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9220 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009221 public int getScrollBarStyle() {
9222 return mViewFlags & SCROLLBARS_STYLE_MASK;
9223 }
9224
9225 /**
9226 * <p>Compute the horizontal range that the horizontal scrollbar
9227 * represents.</p>
9228 *
9229 * <p>The range is expressed in arbitrary units that must be the same as the
9230 * units used by {@link #computeHorizontalScrollExtent()} and
9231 * {@link #computeHorizontalScrollOffset()}.</p>
9232 *
9233 * <p>The default range is the drawing width of this view.</p>
9234 *
9235 * @return the total horizontal range represented by the horizontal
9236 * scrollbar
9237 *
9238 * @see #computeHorizontalScrollExtent()
9239 * @see #computeHorizontalScrollOffset()
9240 * @see android.widget.ScrollBarDrawable
9241 */
9242 protected int computeHorizontalScrollRange() {
9243 return getWidth();
9244 }
9245
9246 /**
9247 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9248 * within the horizontal range. This value is used to compute the position
9249 * of the thumb within the scrollbar's track.</p>
9250 *
9251 * <p>The range is expressed in arbitrary units that must be the same as the
9252 * units used by {@link #computeHorizontalScrollRange()} and
9253 * {@link #computeHorizontalScrollExtent()}.</p>
9254 *
9255 * <p>The default offset is the scroll offset of this view.</p>
9256 *
9257 * @return the horizontal offset of the scrollbar's thumb
9258 *
9259 * @see #computeHorizontalScrollRange()
9260 * @see #computeHorizontalScrollExtent()
9261 * @see android.widget.ScrollBarDrawable
9262 */
9263 protected int computeHorizontalScrollOffset() {
9264 return mScrollX;
9265 }
9266
9267 /**
9268 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9269 * within the horizontal range. This value is used to compute the length
9270 * of the thumb within the scrollbar's track.</p>
9271 *
9272 * <p>The range is expressed in arbitrary units that must be the same as the
9273 * units used by {@link #computeHorizontalScrollRange()} and
9274 * {@link #computeHorizontalScrollOffset()}.</p>
9275 *
9276 * <p>The default extent is the drawing width of this view.</p>
9277 *
9278 * @return the horizontal extent of the scrollbar's thumb
9279 *
9280 * @see #computeHorizontalScrollRange()
9281 * @see #computeHorizontalScrollOffset()
9282 * @see android.widget.ScrollBarDrawable
9283 */
9284 protected int computeHorizontalScrollExtent() {
9285 return getWidth();
9286 }
9287
9288 /**
9289 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9290 *
9291 * <p>The range is expressed in arbitrary units that must be the same as the
9292 * units used by {@link #computeVerticalScrollExtent()} and
9293 * {@link #computeVerticalScrollOffset()}.</p>
9294 *
9295 * @return the total vertical range represented by the vertical scrollbar
9296 *
9297 * <p>The default range is the drawing height of this view.</p>
9298 *
9299 * @see #computeVerticalScrollExtent()
9300 * @see #computeVerticalScrollOffset()
9301 * @see android.widget.ScrollBarDrawable
9302 */
9303 protected int computeVerticalScrollRange() {
9304 return getHeight();
9305 }
9306
9307 /**
9308 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9309 * within the horizontal range. This value is used to compute the position
9310 * of the thumb within the scrollbar's track.</p>
9311 *
9312 * <p>The range is expressed in arbitrary units that must be the same as the
9313 * units used by {@link #computeVerticalScrollRange()} and
9314 * {@link #computeVerticalScrollExtent()}.</p>
9315 *
9316 * <p>The default offset is the scroll offset of this view.</p>
9317 *
9318 * @return the vertical offset of the scrollbar's thumb
9319 *
9320 * @see #computeVerticalScrollRange()
9321 * @see #computeVerticalScrollExtent()
9322 * @see android.widget.ScrollBarDrawable
9323 */
9324 protected int computeVerticalScrollOffset() {
9325 return mScrollY;
9326 }
9327
9328 /**
9329 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9330 * within the vertical range. This value is used to compute the length
9331 * of the thumb within the scrollbar's track.</p>
9332 *
9333 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009334 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009335 * {@link #computeVerticalScrollOffset()}.</p>
9336 *
9337 * <p>The default extent is the drawing height of this view.</p>
9338 *
9339 * @return the vertical extent of the scrollbar's thumb
9340 *
9341 * @see #computeVerticalScrollRange()
9342 * @see #computeVerticalScrollOffset()
9343 * @see android.widget.ScrollBarDrawable
9344 */
9345 protected int computeVerticalScrollExtent() {
9346 return getHeight();
9347 }
9348
9349 /**
Adam Powell69159442011-06-13 17:53:06 -07009350 * Check if this view can be scrolled horizontally in a certain direction.
9351 *
9352 * @param direction Negative to check scrolling left, positive to check scrolling right.
9353 * @return true if this view can be scrolled in the specified direction, false otherwise.
9354 */
9355 public boolean canScrollHorizontally(int direction) {
9356 final int offset = computeHorizontalScrollOffset();
9357 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9358 if (range == 0) return false;
9359 if (direction < 0) {
9360 return offset > 0;
9361 } else {
9362 return offset < range - 1;
9363 }
9364 }
9365
9366 /**
9367 * Check if this view can be scrolled vertically in a certain direction.
9368 *
9369 * @param direction Negative to check scrolling up, positive to check scrolling down.
9370 * @return true if this view can be scrolled in the specified direction, false otherwise.
9371 */
9372 public boolean canScrollVertically(int direction) {
9373 final int offset = computeVerticalScrollOffset();
9374 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9375 if (range == 0) return false;
9376 if (direction < 0) {
9377 return offset > 0;
9378 } else {
9379 return offset < range - 1;
9380 }
9381 }
9382
9383 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009384 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9385 * scrollbars are painted only if they have been awakened first.</p>
9386 *
9387 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009388 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009389 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009390 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009391 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009392 // scrollbars are drawn only when the animation is running
9393 final ScrollabilityCache cache = mScrollCache;
9394 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009395
Mike Cleronf116bf82009-09-27 19:14:12 -07009396 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009397
Mike Cleronf116bf82009-09-27 19:14:12 -07009398 if (state == ScrollabilityCache.OFF) {
9399 return;
9400 }
Joe Malin32736f02011-01-19 16:14:20 -08009401
Mike Cleronf116bf82009-09-27 19:14:12 -07009402 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009403
Mike Cleronf116bf82009-09-27 19:14:12 -07009404 if (state == ScrollabilityCache.FADING) {
9405 // We're fading -- get our fade interpolation
9406 if (cache.interpolatorValues == null) {
9407 cache.interpolatorValues = new float[1];
9408 }
Joe Malin32736f02011-01-19 16:14:20 -08009409
Mike Cleronf116bf82009-09-27 19:14:12 -07009410 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009411
Mike Cleronf116bf82009-09-27 19:14:12 -07009412 // Stops the animation if we're done
9413 if (cache.scrollBarInterpolator.timeToValues(values) ==
9414 Interpolator.Result.FREEZE_END) {
9415 cache.state = ScrollabilityCache.OFF;
9416 } else {
9417 cache.scrollBar.setAlpha(Math.round(values[0]));
9418 }
Joe Malin32736f02011-01-19 16:14:20 -08009419
9420 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009421 // drawing. We only want this when we're fading so that
9422 // we prevent excessive redraws
9423 invalidate = true;
9424 } else {
9425 // We're just on -- but we may have been fading before so
9426 // reset alpha
9427 cache.scrollBar.setAlpha(255);
9428 }
9429
Joe Malin32736f02011-01-19 16:14:20 -08009430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 final int viewFlags = mViewFlags;
9432
9433 final boolean drawHorizontalScrollBar =
9434 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9435 final boolean drawVerticalScrollBar =
9436 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9437 && !isVerticalScrollBarHidden();
9438
9439 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9440 final int width = mRight - mLeft;
9441 final int height = mBottom - mTop;
9442
9443 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009444
Mike Reede8853fc2009-09-04 14:01:48 -04009445 final int scrollX = mScrollX;
9446 final int scrollY = mScrollY;
9447 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9448
Mike Cleronf116bf82009-09-27 19:14:12 -07009449 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009451 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009452 int size = scrollBar.getSize(false);
9453 if (size <= 0) {
9454 size = cache.scrollBarSize;
9455 }
9456
Mike Cleronf116bf82009-09-27 19:14:12 -07009457 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009458 computeHorizontalScrollOffset(),
9459 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009460 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009461 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009462 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009463 left = scrollX + (mPaddingLeft & inside);
9464 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9465 bottom = top + size;
9466 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9467 if (invalidate) {
9468 invalidate(left, top, right, bottom);
9469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 }
9471
9472 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009473 int size = scrollBar.getSize(true);
9474 if (size <= 0) {
9475 size = cache.scrollBarSize;
9476 }
9477
Mike Reede8853fc2009-09-04 14:01:48 -04009478 scrollBar.setParameters(computeVerticalScrollRange(),
9479 computeVerticalScrollOffset(),
9480 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009481 switch (mVerticalScrollbarPosition) {
9482 default:
9483 case SCROLLBAR_POSITION_DEFAULT:
9484 case SCROLLBAR_POSITION_RIGHT:
9485 left = scrollX + width - size - (mUserPaddingRight & inside);
9486 break;
9487 case SCROLLBAR_POSITION_LEFT:
9488 left = scrollX + (mUserPaddingLeft & inside);
9489 break;
9490 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009491 top = scrollY + (mPaddingTop & inside);
9492 right = left + size;
9493 bottom = scrollY + height - (mUserPaddingBottom & inside);
9494 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9495 if (invalidate) {
9496 invalidate(left, top, right, bottom);
9497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009498 }
9499 }
9500 }
9501 }
Romain Guy8506ab42009-06-11 17:35:47 -07009502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009503 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009504 * 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 -08009505 * FastScroller is visible.
9506 * @return whether to temporarily hide the vertical scrollbar
9507 * @hide
9508 */
9509 protected boolean isVerticalScrollBarHidden() {
9510 return false;
9511 }
9512
9513 /**
9514 * <p>Draw the horizontal scrollbar if
9515 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9516 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009517 * @param canvas the canvas on which to draw the scrollbar
9518 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009519 *
9520 * @see #isHorizontalScrollBarEnabled()
9521 * @see #computeHorizontalScrollRange()
9522 * @see #computeHorizontalScrollExtent()
9523 * @see #computeHorizontalScrollOffset()
9524 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009525 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009526 */
Romain Guy8fb95422010-08-17 18:38:51 -07009527 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9528 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009529 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009530 scrollBar.draw(canvas);
9531 }
Mike Reede8853fc2009-09-04 14:01:48 -04009532
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009534 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9535 * returns true.</p>
9536 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009537 * @param canvas the canvas on which to draw the scrollbar
9538 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 *
9540 * @see #isVerticalScrollBarEnabled()
9541 * @see #computeVerticalScrollRange()
9542 * @see #computeVerticalScrollExtent()
9543 * @see #computeVerticalScrollOffset()
9544 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009545 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009546 */
Romain Guy8fb95422010-08-17 18:38:51 -07009547 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9548 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009549 scrollBar.setBounds(l, t, r, b);
9550 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009551 }
9552
9553 /**
9554 * Implement this to do your drawing.
9555 *
9556 * @param canvas the canvas on which the background will be drawn
9557 */
9558 protected void onDraw(Canvas canvas) {
9559 }
9560
9561 /*
9562 * Caller is responsible for calling requestLayout if necessary.
9563 * (This allows addViewInLayout to not request a new layout.)
9564 */
9565 void assignParent(ViewParent parent) {
9566 if (mParent == null) {
9567 mParent = parent;
9568 } else if (parent == null) {
9569 mParent = null;
9570 } else {
9571 throw new RuntimeException("view " + this + " being added, but"
9572 + " it already has a parent");
9573 }
9574 }
9575
9576 /**
9577 * This is called when the view is attached to a window. At this point it
9578 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009579 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9580 * however it may be called any time before the first onDraw -- including
9581 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009582 *
9583 * @see #onDetachedFromWindow()
9584 */
9585 protected void onAttachedToWindow() {
9586 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9587 mParent.requestTransparentRegion(this);
9588 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009589 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9590 initialAwakenScrollBars();
9591 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9592 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009593 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009594 // Order is important here: LayoutDirection MUST be resolved before Padding
9595 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009596 resolveLayoutDirectionIfNeeded();
9597 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009598 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009599 if (isFocused()) {
9600 InputMethodManager imm = InputMethodManager.peekInstance();
9601 imm.focusIn(this);
9602 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009603 }
Cibu Johny86666632010-02-22 13:01:02 -08009604
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009605 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009606 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9607 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009608 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009609 private void resolveLayoutDirectionIfNeeded() {
9610 // Do not resolve if it is not needed
9611 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9612
9613 // Clear any previous layout direction resolution
9614 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9615
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009616 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9617 // TextDirectionHeuristic
9618 resetResolvedTextDirection();
9619
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009620 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009621 switch (getLayoutDirection()) {
9622 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009623 // We cannot do the resolution if there is no parent
9624 if (mParent == null) return;
9625
Cibu Johny86666632010-02-22 13:01:02 -08009626 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009627 if (mParent instanceof ViewGroup) {
9628 ViewGroup viewGroup = ((ViewGroup) mParent);
9629
9630 // Check if the parent view group can resolve
9631 if (! viewGroup.canResolveLayoutDirection()) {
9632 return;
9633 }
9634 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9635 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9636 }
Cibu Johny86666632010-02-22 13:01:02 -08009637 }
9638 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009639 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009640 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009641 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009642 case LAYOUT_DIRECTION_LOCALE:
9643 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009644 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009645 }
9646 break;
9647 default:
9648 // Nothing to do, LTR by default
9649 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009650
9651 // Set to resolved
9652 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9653 }
9654
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009655 /**
9656 * @hide
9657 */
9658 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009659 // If the user specified the absolute padding (either with android:padding or
9660 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9661 // use the default padding or the padding from the background drawable
9662 // (stored at this point in mPadding*)
9663 switch (getResolvedLayoutDirection()) {
9664 case LAYOUT_DIRECTION_RTL:
9665 // Start user padding override Right user padding. Otherwise, if Right user
9666 // padding is not defined, use the default Right padding. If Right user padding
9667 // is defined, just use it.
9668 if (mUserPaddingStart >= 0) {
9669 mUserPaddingRight = mUserPaddingStart;
9670 } else if (mUserPaddingRight < 0) {
9671 mUserPaddingRight = mPaddingRight;
9672 }
9673 if (mUserPaddingEnd >= 0) {
9674 mUserPaddingLeft = mUserPaddingEnd;
9675 } else if (mUserPaddingLeft < 0) {
9676 mUserPaddingLeft = mPaddingLeft;
9677 }
9678 break;
9679 case LAYOUT_DIRECTION_LTR:
9680 default:
9681 // Start user padding override Left user padding. Otherwise, if Left user
9682 // padding is not defined, use the default left padding. If Left user padding
9683 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009684 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009685 mUserPaddingLeft = mUserPaddingStart;
9686 } else if (mUserPaddingLeft < 0) {
9687 mUserPaddingLeft = mPaddingLeft;
9688 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009689 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009690 mUserPaddingRight = mUserPaddingEnd;
9691 } else if (mUserPaddingRight < 0) {
9692 mUserPaddingRight = mPaddingRight;
9693 }
9694 }
9695
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009696 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9697
9698 recomputePadding();
9699 }
9700
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009701 /**
9702 * Return true if layout direction resolution can be done
9703 *
9704 * @hide
9705 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009706 protected boolean canResolveLayoutDirection() {
9707 switch (getLayoutDirection()) {
9708 case LAYOUT_DIRECTION_INHERIT:
9709 return (mParent != null);
9710 default:
9711 return true;
9712 }
9713 }
9714
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009715 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009716 * Reset the resolved layout direction.
9717 *
9718 * Subclasses need to override this method to clear cached information that depends on the
9719 * resolved layout direction, or to inform child views that inherit their layout direction.
9720 * Overrides must also call the superclass implementation at the start of their implementation.
9721 *
9722 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009723 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009724 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009725 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009726 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009727 }
9728
9729 /**
9730 * Check if a Locale is corresponding to a RTL script.
9731 *
9732 * @param locale Locale to check
9733 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009734 *
9735 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009736 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009737 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009738 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9739 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009740 }
9741
9742 /**
9743 * This is called when the view is detached from a window. At this point it
9744 * no longer has a surface for drawing.
9745 *
9746 * @see #onAttachedToWindow()
9747 */
9748 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009749 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009750
Romain Guya440b002010-02-24 15:57:54 -08009751 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009752 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009753 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009754 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009756 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009757
Romain Guy6d7475d2011-07-27 16:28:21 -07009758 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009759
Chet Haasedaf98e92011-01-10 14:10:36 -08009760 if (mDisplayList != null) {
9761 mDisplayList.invalidate();
9762 }
9763
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009764 if (mAttachInfo != null) {
9765 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009766 }
9767
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009768 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009769
9770 resetResolvedLayoutDirection();
9771 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009772 }
9773
9774 /**
9775 * @return The number of times this view has been attached to a window
9776 */
9777 protected int getWindowAttachCount() {
9778 return mWindowAttachCount;
9779 }
9780
9781 /**
9782 * Retrieve a unique token identifying the window this view is attached to.
9783 * @return Return the window's token for use in
9784 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9785 */
9786 public IBinder getWindowToken() {
9787 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9788 }
9789
9790 /**
9791 * Retrieve a unique token identifying the top-level "real" window of
9792 * the window that this view is attached to. That is, this is like
9793 * {@link #getWindowToken}, except if the window this view in is a panel
9794 * window (attached to another containing window), then the token of
9795 * the containing window is returned instead.
9796 *
9797 * @return Returns the associated window token, either
9798 * {@link #getWindowToken()} or the containing window's token.
9799 */
9800 public IBinder getApplicationWindowToken() {
9801 AttachInfo ai = mAttachInfo;
9802 if (ai != null) {
9803 IBinder appWindowToken = ai.mPanelParentWindowToken;
9804 if (appWindowToken == null) {
9805 appWindowToken = ai.mWindowToken;
9806 }
9807 return appWindowToken;
9808 }
9809 return null;
9810 }
9811
9812 /**
9813 * Retrieve private session object this view hierarchy is using to
9814 * communicate with the window manager.
9815 * @return the session object to communicate with the window manager
9816 */
9817 /*package*/ IWindowSession getWindowSession() {
9818 return mAttachInfo != null ? mAttachInfo.mSession : null;
9819 }
9820
9821 /**
9822 * @param info the {@link android.view.View.AttachInfo} to associated with
9823 * this view
9824 */
9825 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9826 //System.out.println("Attached! " + this);
9827 mAttachInfo = info;
9828 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009829 // We will need to evaluate the drawable state at least once.
9830 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009831 if (mFloatingTreeObserver != null) {
9832 info.mTreeObserver.merge(mFloatingTreeObserver);
9833 mFloatingTreeObserver = null;
9834 }
9835 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9836 mAttachInfo.mScrollContainers.add(this);
9837 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9838 }
9839 performCollectViewAttributes(visibility);
9840 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009841
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009842 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009843 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009844 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009845 if (listeners != null && listeners.size() > 0) {
9846 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9847 // perform the dispatching. The iterator is a safe guard against listeners that
9848 // could mutate the list by calling the various add/remove methods. This prevents
9849 // the array from being modified while we iterate it.
9850 for (OnAttachStateChangeListener listener : listeners) {
9851 listener.onViewAttachedToWindow(this);
9852 }
9853 }
9854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009855 int vis = info.mWindowVisibility;
9856 if (vis != GONE) {
9857 onWindowVisibilityChanged(vis);
9858 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009859 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9860 // If nobody has evaluated the drawable state yet, then do it now.
9861 refreshDrawableState();
9862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009863 }
9864
9865 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 AttachInfo info = mAttachInfo;
9867 if (info != null) {
9868 int vis = info.mWindowVisibility;
9869 if (vis != GONE) {
9870 onWindowVisibilityChanged(GONE);
9871 }
9872 }
9873
9874 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009875
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009876 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009877 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009878 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009879 if (listeners != null && listeners.size() > 0) {
9880 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9881 // perform the dispatching. The iterator is a safe guard against listeners that
9882 // could mutate the list by calling the various add/remove methods. This prevents
9883 // the array from being modified while we iterate it.
9884 for (OnAttachStateChangeListener listener : listeners) {
9885 listener.onViewDetachedFromWindow(this);
9886 }
9887 }
9888
Romain Guy01d5edc2011-01-28 11:28:53 -08009889 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009890 mAttachInfo.mScrollContainers.remove(this);
9891 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9892 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 mAttachInfo = null;
9895 }
9896
9897 /**
9898 * Store this view hierarchy's frozen state into the given container.
9899 *
9900 * @param container The SparseArray in which to save the view's state.
9901 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009902 * @see #restoreHierarchyState(android.util.SparseArray)
9903 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9904 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009905 */
9906 public void saveHierarchyState(SparseArray<Parcelable> container) {
9907 dispatchSaveInstanceState(container);
9908 }
9909
9910 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009911 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9912 * this view and its children. May be overridden to modify how freezing happens to a
9913 * 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 -08009914 *
9915 * @param container The SparseArray in which to save the view's state.
9916 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009917 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9918 * @see #saveHierarchyState(android.util.SparseArray)
9919 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009920 */
9921 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9922 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9923 mPrivateFlags &= ~SAVE_STATE_CALLED;
9924 Parcelable state = onSaveInstanceState();
9925 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9926 throw new IllegalStateException(
9927 "Derived class did not call super.onSaveInstanceState()");
9928 }
9929 if (state != null) {
9930 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9931 // + ": " + state);
9932 container.put(mID, state);
9933 }
9934 }
9935 }
9936
9937 /**
9938 * Hook allowing a view to generate a representation of its internal state
9939 * that can later be used to create a new instance with that same state.
9940 * This state should only contain information that is not persistent or can
9941 * not be reconstructed later. For example, you will never store your
9942 * current position on screen because that will be computed again when a
9943 * new instance of the view is placed in its view hierarchy.
9944 * <p>
9945 * Some examples of things you may store here: the current cursor position
9946 * in a text view (but usually not the text itself since that is stored in a
9947 * content provider or other persistent storage), the currently selected
9948 * item in a list view.
9949 *
9950 * @return Returns a Parcelable object containing the view's current dynamic
9951 * state, or null if there is nothing interesting to save. The
9952 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009953 * @see #onRestoreInstanceState(android.os.Parcelable)
9954 * @see #saveHierarchyState(android.util.SparseArray)
9955 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 * @see #setSaveEnabled(boolean)
9957 */
9958 protected Parcelable onSaveInstanceState() {
9959 mPrivateFlags |= SAVE_STATE_CALLED;
9960 return BaseSavedState.EMPTY_STATE;
9961 }
9962
9963 /**
9964 * Restore this view hierarchy's frozen state from the given container.
9965 *
9966 * @param container The SparseArray which holds previously frozen states.
9967 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009968 * @see #saveHierarchyState(android.util.SparseArray)
9969 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9970 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009971 */
9972 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9973 dispatchRestoreInstanceState(container);
9974 }
9975
9976 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009977 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9978 * state for this view and its children. May be overridden to modify how restoring
9979 * happens to a view's children; for example, some views may want to not store state
9980 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009981 *
9982 * @param container The SparseArray which holds previously saved state.
9983 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009984 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9985 * @see #restoreHierarchyState(android.util.SparseArray)
9986 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009987 */
9988 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9989 if (mID != NO_ID) {
9990 Parcelable state = container.get(mID);
9991 if (state != null) {
9992 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9993 // + ": " + state);
9994 mPrivateFlags &= ~SAVE_STATE_CALLED;
9995 onRestoreInstanceState(state);
9996 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9997 throw new IllegalStateException(
9998 "Derived class did not call super.onRestoreInstanceState()");
9999 }
10000 }
10001 }
10002 }
10003
10004 /**
10005 * Hook allowing a view to re-apply a representation of its internal state that had previously
10006 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10007 * null state.
10008 *
10009 * @param state The frozen state that had previously been returned by
10010 * {@link #onSaveInstanceState}.
10011 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010012 * @see #onSaveInstanceState()
10013 * @see #restoreHierarchyState(android.util.SparseArray)
10014 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 */
10016 protected void onRestoreInstanceState(Parcelable state) {
10017 mPrivateFlags |= SAVE_STATE_CALLED;
10018 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010019 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10020 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010021 + "when two views of different type have the same id in the same hierarchy. "
10022 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010023 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 }
10025 }
10026
10027 /**
10028 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10029 *
10030 * @return the drawing start time in milliseconds
10031 */
10032 public long getDrawingTime() {
10033 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10034 }
10035
10036 /**
10037 * <p>Enables or disables the duplication of the parent's state into this view. When
10038 * duplication is enabled, this view gets its drawable state from its parent rather
10039 * than from its own internal properties.</p>
10040 *
10041 * <p>Note: in the current implementation, setting this property to true after the
10042 * view was added to a ViewGroup might have no effect at all. This property should
10043 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10044 *
10045 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10046 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010047 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010048 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10049 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050 *
10051 * @param enabled True to enable duplication of the parent's drawable state, false
10052 * to disable it.
10053 *
10054 * @see #getDrawableState()
10055 * @see #isDuplicateParentStateEnabled()
10056 */
10057 public void setDuplicateParentStateEnabled(boolean enabled) {
10058 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10059 }
10060
10061 /**
10062 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10063 *
10064 * @return True if this view's drawable state is duplicated from the parent,
10065 * false otherwise
10066 *
10067 * @see #getDrawableState()
10068 * @see #setDuplicateParentStateEnabled(boolean)
10069 */
10070 public boolean isDuplicateParentStateEnabled() {
10071 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10072 }
10073
10074 /**
Romain Guy171c5922011-01-06 10:04:23 -080010075 * <p>Specifies the type of layer backing this view. The layer can be
10076 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10077 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010078 *
Romain Guy171c5922011-01-06 10:04:23 -080010079 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10080 * instance that controls how the layer is composed on screen. The following
10081 * properties of the paint are taken into account when composing the layer:</p>
10082 * <ul>
10083 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10084 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10085 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10086 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010087 *
Romain Guy171c5922011-01-06 10:04:23 -080010088 * <p>If this view has an alpha value set to < 1.0 by calling
10089 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10090 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10091 * equivalent to setting a hardware layer on this view and providing a paint with
10092 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010093 *
Romain Guy171c5922011-01-06 10:04:23 -080010094 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10095 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10096 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010097 *
Romain Guy171c5922011-01-06 10:04:23 -080010098 * @param layerType The ype of layer to use with this view, must be one of
10099 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10100 * {@link #LAYER_TYPE_HARDWARE}
10101 * @param paint The paint used to compose the layer. This argument is optional
10102 * and can be null. It is ignored when the layer type is
10103 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010104 *
10105 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010106 * @see #LAYER_TYPE_NONE
10107 * @see #LAYER_TYPE_SOFTWARE
10108 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010109 * @see #setAlpha(float)
10110 *
Romain Guy171c5922011-01-06 10:04:23 -080010111 * @attr ref android.R.styleable#View_layerType
10112 */
10113 public void setLayerType(int layerType, Paint paint) {
10114 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010115 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010116 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10117 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010118
Romain Guyd6cd5722011-01-17 14:42:41 -080010119 if (layerType == mLayerType) {
10120 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10121 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010122 invalidateParentCaches();
10123 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010124 }
10125 return;
10126 }
Romain Guy171c5922011-01-06 10:04:23 -080010127
10128 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010129 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010130 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010131 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010132 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010133 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010134 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010135 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010136 default:
10137 break;
Romain Guy171c5922011-01-06 10:04:23 -080010138 }
10139
10140 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010141 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10142 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10143 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010144
Romain Guy0fd89bf2011-01-26 15:41:30 -080010145 invalidateParentCaches();
10146 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010147 }
10148
10149 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010150 * Indicates whether this view has a static layer. A view with layer type
10151 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10152 * dynamic.
10153 */
10154 boolean hasStaticLayer() {
10155 return mLayerType == LAYER_TYPE_NONE;
10156 }
10157
10158 /**
Romain Guy171c5922011-01-06 10:04:23 -080010159 * Indicates what type of layer is currently associated with this view. By default
10160 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10161 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10162 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010163 *
Romain Guy171c5922011-01-06 10:04:23 -080010164 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10165 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010166 *
10167 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010168 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010169 * @see #LAYER_TYPE_NONE
10170 * @see #LAYER_TYPE_SOFTWARE
10171 * @see #LAYER_TYPE_HARDWARE
10172 */
10173 public int getLayerType() {
10174 return mLayerType;
10175 }
Joe Malin32736f02011-01-19 16:14:20 -080010176
Romain Guy6c319ca2011-01-11 14:29:25 -080010177 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010178 * Forces this view's layer to be created and this view to be rendered
10179 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10180 * invoking this method will have no effect.
10181 *
10182 * This method can for instance be used to render a view into its layer before
10183 * starting an animation. If this view is complex, rendering into the layer
10184 * before starting the animation will avoid skipping frames.
10185 *
10186 * @throws IllegalStateException If this view is not attached to a window
10187 *
10188 * @see #setLayerType(int, android.graphics.Paint)
10189 */
10190 public void buildLayer() {
10191 if (mLayerType == LAYER_TYPE_NONE) return;
10192
10193 if (mAttachInfo == null) {
10194 throw new IllegalStateException("This view must be attached to a window first");
10195 }
10196
10197 switch (mLayerType) {
10198 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010199 if (mAttachInfo.mHardwareRenderer != null &&
10200 mAttachInfo.mHardwareRenderer.isEnabled() &&
10201 mAttachInfo.mHardwareRenderer.validate()) {
10202 getHardwareLayer();
10203 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010204 break;
10205 case LAYER_TYPE_SOFTWARE:
10206 buildDrawingCache(true);
10207 break;
10208 }
10209 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010210
10211 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10212 void flushLayer() {
10213 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10214 mHardwareLayer.flush();
10215 }
10216 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010217
10218 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010219 * <p>Returns a hardware layer that can be used to draw this view again
10220 * without executing its draw method.</p>
10221 *
10222 * @return A HardwareLayer ready to render, or null if an error occurred.
10223 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010224 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010225 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10226 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010227 return null;
10228 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010229
10230 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010231
10232 final int width = mRight - mLeft;
10233 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010234
Romain Guy6c319ca2011-01-11 14:29:25 -080010235 if (width == 0 || height == 0) {
10236 return null;
10237 }
10238
10239 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10240 if (mHardwareLayer == null) {
10241 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10242 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010243 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010244 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10245 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010246 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010247 }
10248
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010249 // The layer is not valid if the underlying GPU resources cannot be allocated
10250 if (!mHardwareLayer.isValid()) {
10251 return null;
10252 }
10253
Romain Guy59a12ca2011-06-09 17:48:21 -070010254 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010255 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010256
10257 // Make sure all the GPU resources have been properly allocated
10258 if (canvas == null) {
10259 mHardwareLayer.end(currentCanvas);
10260 return null;
10261 }
10262
Romain Guy5e7f7662011-01-24 22:35:56 -080010263 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010264 try {
10265 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010266 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010267 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010268
Chet Haase88172fe2011-03-07 17:36:33 -080010269 final int restoreCount = canvas.save();
10270
Romain Guy6c319ca2011-01-11 14:29:25 -080010271 computeScroll();
10272 canvas.translate(-mScrollX, -mScrollY);
10273
Romain Guy6c319ca2011-01-11 14:29:25 -080010274 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010275
Romain Guy6c319ca2011-01-11 14:29:25 -080010276 // Fast path for layouts with no backgrounds
10277 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10278 mPrivateFlags &= ~DIRTY_MASK;
10279 dispatchDraw(canvas);
10280 } else {
10281 draw(canvas);
10282 }
Joe Malin32736f02011-01-19 16:14:20 -080010283
Chet Haase88172fe2011-03-07 17:36:33 -080010284 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010285 } finally {
10286 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010287 mHardwareLayer.end(currentCanvas);
10288 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010289 }
10290 }
10291
10292 return mHardwareLayer;
10293 }
Romain Guy171c5922011-01-06 10:04:23 -080010294
Romain Guy589b0bb2011-10-10 13:57:47 -070010295 /**
10296 * Destroys this View's hardware layer if possible.
10297 *
10298 * @return True if the layer was destroyed, false otherwise.
10299 *
10300 * @see #setLayerType(int, android.graphics.Paint)
10301 * @see #LAYER_TYPE_HARDWARE
10302 */
Romain Guy65b345f2011-07-27 18:51:50 -070010303 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010304 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010305 AttachInfo info = mAttachInfo;
10306 if (info != null && info.mHardwareRenderer != null &&
10307 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10308 mHardwareLayer.destroy();
10309 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010310
Romain Guy9c4b79a2011-11-10 19:23:58 -080010311 invalidate(true);
10312 invalidateParentCaches();
10313 }
Romain Guy65b345f2011-07-27 18:51:50 -070010314 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010315 }
Romain Guy65b345f2011-07-27 18:51:50 -070010316 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010317 }
10318
Romain Guy171c5922011-01-06 10:04:23 -080010319 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010320 * Destroys all hardware rendering resources. This method is invoked
10321 * when the system needs to reclaim resources. Upon execution of this
10322 * method, you should free any OpenGL resources created by the view.
10323 *
10324 * Note: you <strong>must</strong> call
10325 * <code>super.destroyHardwareResources()</code> when overriding
10326 * this method.
10327 *
10328 * @hide
10329 */
10330 protected void destroyHardwareResources() {
10331 destroyLayer();
10332 }
10333
10334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10336 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10337 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10338 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10339 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10340 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010341 *
Romain Guy171c5922011-01-06 10:04:23 -080010342 * <p>Enabling the drawing cache is similar to
10343 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010344 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10345 * drawing cache has no effect on rendering because the system uses a different mechanism
10346 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10347 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10348 * for information on how to enable software and hardware layers.</p>
10349 *
10350 * <p>This API can be used to manually generate
10351 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10352 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353 *
10354 * @param enabled true to enable the drawing cache, false otherwise
10355 *
10356 * @see #isDrawingCacheEnabled()
10357 * @see #getDrawingCache()
10358 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010359 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010360 */
10361 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010362 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10364 }
10365
10366 /**
10367 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10368 *
10369 * @return true if the drawing cache is enabled
10370 *
10371 * @see #setDrawingCacheEnabled(boolean)
10372 * @see #getDrawingCache()
10373 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010374 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 public boolean isDrawingCacheEnabled() {
10376 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10377 }
10378
10379 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010380 * Debugging utility which recursively outputs the dirty state of a view and its
10381 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010382 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010383 * @hide
10384 */
Romain Guy676b1732011-02-14 14:45:33 -080010385 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010386 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10387 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10388 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10389 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10390 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10391 if (clear) {
10392 mPrivateFlags &= clearMask;
10393 }
10394 if (this instanceof ViewGroup) {
10395 ViewGroup parent = (ViewGroup) this;
10396 final int count = parent.getChildCount();
10397 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010398 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010399 child.outputDirtyFlags(indent + " ", clear, clearMask);
10400 }
10401 }
10402 }
10403
10404 /**
10405 * This method is used by ViewGroup to cause its children to restore or recreate their
10406 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10407 * to recreate its own display list, which would happen if it went through the normal
10408 * draw/dispatchDraw mechanisms.
10409 *
10410 * @hide
10411 */
10412 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010413
10414 /**
10415 * A view that is not attached or hardware accelerated cannot create a display list.
10416 * This method checks these conditions and returns the appropriate result.
10417 *
10418 * @return true if view has the ability to create a display list, false otherwise.
10419 *
10420 * @hide
10421 */
10422 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010423 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010424 }
Joe Malin32736f02011-01-19 16:14:20 -080010425
Chet Haasedaf98e92011-01-10 14:10:36 -080010426 /**
Romain Guyb051e892010-09-28 19:09:36 -070010427 * <p>Returns a display list that can be used to draw this view again
10428 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010429 *
Romain Guyb051e892010-09-28 19:09:36 -070010430 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010431 *
10432 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010433 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010434 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010435 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010436 return null;
10437 }
10438
Chet Haasedaf98e92011-01-10 14:10:36 -080010439 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10440 mDisplayList == null || !mDisplayList.isValid() ||
10441 mRecreateDisplayList)) {
10442 // Don't need to recreate the display list, just need to tell our
10443 // children to restore/recreate theirs
10444 if (mDisplayList != null && mDisplayList.isValid() &&
10445 !mRecreateDisplayList) {
10446 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10447 mPrivateFlags &= ~DIRTY_MASK;
10448 dispatchGetDisplayList();
10449
10450 return mDisplayList;
10451 }
10452
10453 // If we got here, we're recreating it. Mark it as such to ensure that
10454 // we copy in child display lists into ours in drawChild()
10455 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010456 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010457 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010458 // If we're creating a new display list, make sure our parent gets invalidated
10459 // since they will need to recreate their display list to account for this
10460 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010461 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010462 }
Romain Guyb051e892010-09-28 19:09:36 -070010463
10464 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010465 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010466 try {
10467 int width = mRight - mLeft;
10468 int height = mBottom - mTop;
10469
10470 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010471 // The dirty rect should always be null for a display list
10472 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010473
Chet Haasedaf98e92011-01-10 14:10:36 -080010474 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010475
10476 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010477 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010478 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010479 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010480
Romain Guyb051e892010-09-28 19:09:36 -070010481 // Fast path for layouts with no backgrounds
10482 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010483 dispatchDraw(canvas);
10484 } else {
10485 draw(canvas);
10486 }
Romain Guyb051e892010-09-28 19:09:36 -070010487 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010488 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010489 canvas.onPostDraw();
10490
10491 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010492 }
Chet Haase77785f92011-01-25 23:22:09 -080010493 } else {
10494 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10495 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010496 }
10497
10498 return mDisplayList;
10499 }
10500
10501 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010502 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010503 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010504 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010505 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010506 * @see #getDrawingCache(boolean)
10507 */
10508 public Bitmap getDrawingCache() {
10509 return getDrawingCache(false);
10510 }
10511
10512 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010513 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10514 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10515 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10516 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10517 * request the drawing cache by calling this method and draw it on screen if the
10518 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010519 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010520 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10521 * this method will create a bitmap of the same size as this view. Because this bitmap
10522 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10523 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10524 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10525 * size than the view. This implies that your application must be able to handle this
10526 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010527 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010528 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10529 * the current density of the screen when the application is in compatibility
10530 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010531 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010532 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 * @see #setDrawingCacheEnabled(boolean)
10535 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010536 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537 * @see #destroyDrawingCache()
10538 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010539 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010540 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10541 return null;
10542 }
10543 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010544 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010545 }
Romain Guy02890fd2010-08-06 17:58:44 -070010546 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 }
10548
10549 /**
10550 * <p>Frees the resources used by the drawing cache. If you call
10551 * {@link #buildDrawingCache()} manually without calling
10552 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10553 * should cleanup the cache with this method afterwards.</p>
10554 *
10555 * @see #setDrawingCacheEnabled(boolean)
10556 * @see #buildDrawingCache()
10557 * @see #getDrawingCache()
10558 */
10559 public void destroyDrawingCache() {
10560 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010561 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 mDrawingCache = null;
10563 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010564 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010565 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010566 mUnscaledDrawingCache = null;
10567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 }
10569
10570 /**
10571 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010572 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 * view will always be drawn on top of a solid color.
10574 *
10575 * @param color The background color to use for the drawing cache's bitmap
10576 *
10577 * @see #setDrawingCacheEnabled(boolean)
10578 * @see #buildDrawingCache()
10579 * @see #getDrawingCache()
10580 */
10581 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010582 if (color != mDrawingCacheBackgroundColor) {
10583 mDrawingCacheBackgroundColor = color;
10584 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 }
10587
10588 /**
10589 * @see #setDrawingCacheBackgroundColor(int)
10590 *
10591 * @return The background color to used for the drawing cache's bitmap
10592 */
10593 public int getDrawingCacheBackgroundColor() {
10594 return mDrawingCacheBackgroundColor;
10595 }
10596
10597 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010598 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010599 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010600 * @see #buildDrawingCache(boolean)
10601 */
10602 public void buildDrawingCache() {
10603 buildDrawingCache(false);
10604 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010605
Romain Guyfbd8f692009-06-26 14:51:58 -070010606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10608 *
10609 * <p>If you call {@link #buildDrawingCache()} manually without calling
10610 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10611 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010612 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010613 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10614 * this method will create a bitmap of the same size as this view. Because this bitmap
10615 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10616 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10617 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10618 * size than the view. This implies that your application must be able to handle this
10619 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010620 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010621 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10622 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010623 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010624 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010625 *
10626 * @see #getDrawingCache()
10627 * @see #destroyDrawingCache()
10628 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010629 public void buildDrawingCache(boolean autoScale) {
10630 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010631 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010632 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633
10634 if (ViewDebug.TRACE_HIERARCHY) {
10635 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637
Romain Guy8506ab42009-06-11 17:35:47 -070010638 int width = mRight - mLeft;
10639 int height = mBottom - mTop;
10640
10641 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010642 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010643
Romain Guye1123222009-06-29 14:24:56 -070010644 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010645 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10646 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648
10649 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010650 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010651 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010652
10653 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010654 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010655 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010656 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10657 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010658 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 return;
10660 }
10661
10662 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010663 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664
10665 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 Bitmap.Config quality;
10667 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010668 // Never pick ARGB_4444 because it looks awful
10669 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010670 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10671 case DRAWING_CACHE_QUALITY_AUTO:
10672 quality = Bitmap.Config.ARGB_8888;
10673 break;
10674 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010675 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676 break;
10677 case DRAWING_CACHE_QUALITY_HIGH:
10678 quality = Bitmap.Config.ARGB_8888;
10679 break;
10680 default:
10681 quality = Bitmap.Config.ARGB_8888;
10682 break;
10683 }
10684 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010685 // Optimization for translucent windows
10686 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010687 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688 }
10689
10690 // Try to cleanup memory
10691 if (bitmap != null) bitmap.recycle();
10692
10693 try {
10694 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010695 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010696 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010697 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010698 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010699 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010700 }
Adam Powell26153a32010-11-08 15:22:27 -080010701 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 } catch (OutOfMemoryError e) {
10703 // If there is not enough memory to create the bitmap cache, just
10704 // ignore the issue as bitmap caches are not required to draw the
10705 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010706 if (autoScale) {
10707 mDrawingCache = null;
10708 } else {
10709 mUnscaledDrawingCache = null;
10710 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010711 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712 return;
10713 }
10714
10715 clear = drawingCacheBackgroundColor != 0;
10716 }
10717
10718 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010719 if (attachInfo != null) {
10720 canvas = attachInfo.mCanvas;
10721 if (canvas == null) {
10722 canvas = new Canvas();
10723 }
10724 canvas.setBitmap(bitmap);
10725 // Temporarily clobber the cached Canvas in case one of our children
10726 // is also using a drawing cache. Without this, the children would
10727 // steal the canvas by attaching their own bitmap to it and bad, bad
10728 // thing would happen (invisible views, corrupted drawings, etc.)
10729 attachInfo.mCanvas = null;
10730 } else {
10731 // This case should hopefully never or seldom happen
10732 canvas = new Canvas(bitmap);
10733 }
10734
10735 if (clear) {
10736 bitmap.eraseColor(drawingCacheBackgroundColor);
10737 }
10738
10739 computeScroll();
10740 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010741
Romain Guye1123222009-06-29 14:24:56 -070010742 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010743 final float scale = attachInfo.mApplicationScale;
10744 canvas.scale(scale, scale);
10745 }
Joe Malin32736f02011-01-19 16:14:20 -080010746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010747 canvas.translate(-mScrollX, -mScrollY);
10748
Romain Guy5bcdff42009-05-14 21:27:18 -070010749 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010750 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10751 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010752 mPrivateFlags |= DRAWING_CACHE_VALID;
10753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010754
10755 // Fast path for layouts with no backgrounds
10756 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10757 if (ViewDebug.TRACE_HIERARCHY) {
10758 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10759 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010760 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 dispatchDraw(canvas);
10762 } else {
10763 draw(canvas);
10764 }
10765
10766 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010767 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768
10769 if (attachInfo != null) {
10770 // Restore the cached Canvas for our siblings
10771 attachInfo.mCanvas = canvas;
10772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010773 }
10774 }
10775
10776 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010777 * Create a snapshot of the view into a bitmap. We should probably make
10778 * some form of this public, but should think about the API.
10779 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010780 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010781 int width = mRight - mLeft;
10782 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010783
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010784 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010785 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010786 width = (int) ((width * scale) + 0.5f);
10787 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010788
Romain Guy8c11e312009-09-14 15:15:30 -070010789 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010790 if (bitmap == null) {
10791 throw new OutOfMemoryError();
10792 }
10793
Romain Guyc529d8d2011-09-06 15:01:39 -070010794 Resources resources = getResources();
10795 if (resources != null) {
10796 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10797 }
Joe Malin32736f02011-01-19 16:14:20 -080010798
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010799 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010800 if (attachInfo != null) {
10801 canvas = attachInfo.mCanvas;
10802 if (canvas == null) {
10803 canvas = new Canvas();
10804 }
10805 canvas.setBitmap(bitmap);
10806 // Temporarily clobber the cached Canvas in case one of our children
10807 // is also using a drawing cache. Without this, the children would
10808 // steal the canvas by attaching their own bitmap to it and bad, bad
10809 // things would happen (invisible views, corrupted drawings, etc.)
10810 attachInfo.mCanvas = null;
10811 } else {
10812 // This case should hopefully never or seldom happen
10813 canvas = new Canvas(bitmap);
10814 }
10815
Romain Guy5bcdff42009-05-14 21:27:18 -070010816 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010817 bitmap.eraseColor(backgroundColor);
10818 }
10819
10820 computeScroll();
10821 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010822 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010823 canvas.translate(-mScrollX, -mScrollY);
10824
Romain Guy5bcdff42009-05-14 21:27:18 -070010825 // Temporarily remove the dirty mask
10826 int flags = mPrivateFlags;
10827 mPrivateFlags &= ~DIRTY_MASK;
10828
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010829 // Fast path for layouts with no backgrounds
10830 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10831 dispatchDraw(canvas);
10832 } else {
10833 draw(canvas);
10834 }
10835
Romain Guy5bcdff42009-05-14 21:27:18 -070010836 mPrivateFlags = flags;
10837
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010838 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010839 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010840
10841 if (attachInfo != null) {
10842 // Restore the cached Canvas for our siblings
10843 attachInfo.mCanvas = canvas;
10844 }
Romain Guy8506ab42009-06-11 17:35:47 -070010845
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010846 return bitmap;
10847 }
10848
10849 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010850 * Indicates whether this View is currently in edit mode. A View is usually
10851 * in edit mode when displayed within a developer tool. For instance, if
10852 * this View is being drawn by a visual user interface builder, this method
10853 * should return true.
10854 *
10855 * Subclasses should check the return value of this method to provide
10856 * different behaviors if their normal behavior might interfere with the
10857 * host environment. For instance: the class spawns a thread in its
10858 * constructor, the drawing code relies on device-specific features, etc.
10859 *
10860 * This method is usually checked in the drawing code of custom widgets.
10861 *
10862 * @return True if this View is in edit mode, false otherwise.
10863 */
10864 public boolean isInEditMode() {
10865 return false;
10866 }
10867
10868 /**
10869 * If the View draws content inside its padding and enables fading edges,
10870 * it needs to support padding offsets. Padding offsets are added to the
10871 * fading edges to extend the length of the fade so that it covers pixels
10872 * drawn inside the padding.
10873 *
10874 * Subclasses of this class should override this method if they need
10875 * to draw content inside the padding.
10876 *
10877 * @return True if padding offset must be applied, false otherwise.
10878 *
10879 * @see #getLeftPaddingOffset()
10880 * @see #getRightPaddingOffset()
10881 * @see #getTopPaddingOffset()
10882 * @see #getBottomPaddingOffset()
10883 *
10884 * @since CURRENT
10885 */
10886 protected boolean isPaddingOffsetRequired() {
10887 return false;
10888 }
10889
10890 /**
10891 * Amount by which to extend the left fading region. Called only when
10892 * {@link #isPaddingOffsetRequired()} returns true.
10893 *
10894 * @return The left padding offset in pixels.
10895 *
10896 * @see #isPaddingOffsetRequired()
10897 *
10898 * @since CURRENT
10899 */
10900 protected int getLeftPaddingOffset() {
10901 return 0;
10902 }
10903
10904 /**
10905 * Amount by which to extend the right fading region. Called only when
10906 * {@link #isPaddingOffsetRequired()} returns true.
10907 *
10908 * @return The right padding offset in pixels.
10909 *
10910 * @see #isPaddingOffsetRequired()
10911 *
10912 * @since CURRENT
10913 */
10914 protected int getRightPaddingOffset() {
10915 return 0;
10916 }
10917
10918 /**
10919 * Amount by which to extend the top fading region. Called only when
10920 * {@link #isPaddingOffsetRequired()} returns true.
10921 *
10922 * @return The top padding offset in pixels.
10923 *
10924 * @see #isPaddingOffsetRequired()
10925 *
10926 * @since CURRENT
10927 */
10928 protected int getTopPaddingOffset() {
10929 return 0;
10930 }
10931
10932 /**
10933 * Amount by which to extend the bottom fading region. Called only when
10934 * {@link #isPaddingOffsetRequired()} returns true.
10935 *
10936 * @return The bottom padding offset in pixels.
10937 *
10938 * @see #isPaddingOffsetRequired()
10939 *
10940 * @since CURRENT
10941 */
10942 protected int getBottomPaddingOffset() {
10943 return 0;
10944 }
10945
10946 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010947 * @hide
10948 * @param offsetRequired
10949 */
10950 protected int getFadeTop(boolean offsetRequired) {
10951 int top = mPaddingTop;
10952 if (offsetRequired) top += getTopPaddingOffset();
10953 return top;
10954 }
10955
10956 /**
10957 * @hide
10958 * @param offsetRequired
10959 */
10960 protected int getFadeHeight(boolean offsetRequired) {
10961 int padding = mPaddingTop;
10962 if (offsetRequired) padding += getTopPaddingOffset();
10963 return mBottom - mTop - mPaddingBottom - padding;
10964 }
10965
10966 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010967 * <p>Indicates whether this view is attached to an hardware accelerated
10968 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010969 *
Romain Guy2bffd262010-09-12 17:40:02 -070010970 * <p>Even if this method returns true, it does not mean that every call
10971 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10972 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10973 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10974 * window is hardware accelerated,
10975 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10976 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010977 *
Romain Guy2bffd262010-09-12 17:40:02 -070010978 * @return True if the view is attached to a window and the window is
10979 * hardware accelerated; false in any other case.
10980 */
10981 public boolean isHardwareAccelerated() {
10982 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10983 }
Joe Malin32736f02011-01-19 16:14:20 -080010984
Romain Guy2bffd262010-09-12 17:40:02 -070010985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010986 * Manually render this view (and all of its children) to the given Canvas.
10987 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010988 * called. When implementing a view, implement
10989 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10990 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010991 *
10992 * @param canvas The Canvas to which the View is rendered.
10993 */
10994 public void draw(Canvas canvas) {
10995 if (ViewDebug.TRACE_HIERARCHY) {
10996 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10997 }
10998
Romain Guy5bcdff42009-05-14 21:27:18 -070010999 final int privateFlags = mPrivateFlags;
11000 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11001 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11002 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011004 /*
11005 * Draw traversal performs several drawing steps which must be executed
11006 * in the appropriate order:
11007 *
11008 * 1. Draw the background
11009 * 2. If necessary, save the canvas' layers to prepare for fading
11010 * 3. Draw view's content
11011 * 4. Draw children
11012 * 5. If necessary, draw the fading edges and restore layers
11013 * 6. Draw decorations (scrollbars for instance)
11014 */
11015
11016 // Step 1, draw the background, if needed
11017 int saveCount;
11018
Romain Guy24443ea2009-05-11 11:56:30 -070011019 if (!dirtyOpaque) {
11020 final Drawable background = mBGDrawable;
11021 if (background != null) {
11022 final int scrollX = mScrollX;
11023 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011024
Romain Guy24443ea2009-05-11 11:56:30 -070011025 if (mBackgroundSizeChanged) {
11026 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11027 mBackgroundSizeChanged = false;
11028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011029
Romain Guy24443ea2009-05-11 11:56:30 -070011030 if ((scrollX | scrollY) == 0) {
11031 background.draw(canvas);
11032 } else {
11033 canvas.translate(scrollX, scrollY);
11034 background.draw(canvas);
11035 canvas.translate(-scrollX, -scrollY);
11036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 }
11038 }
11039
11040 // skip step 2 & 5 if possible (common case)
11041 final int viewFlags = mViewFlags;
11042 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11043 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11044 if (!verticalEdges && !horizontalEdges) {
11045 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011046 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011047
11048 // Step 4, draw the children
11049 dispatchDraw(canvas);
11050
11051 // Step 6, draw decorations (scrollbars)
11052 onDrawScrollBars(canvas);
11053
11054 // we're done...
11055 return;
11056 }
11057
11058 /*
11059 * Here we do the full fledged routine...
11060 * (this is an uncommon case where speed matters less,
11061 * this is why we repeat some of the tests that have been
11062 * done above)
11063 */
11064
11065 boolean drawTop = false;
11066 boolean drawBottom = false;
11067 boolean drawLeft = false;
11068 boolean drawRight = false;
11069
11070 float topFadeStrength = 0.0f;
11071 float bottomFadeStrength = 0.0f;
11072 float leftFadeStrength = 0.0f;
11073 float rightFadeStrength = 0.0f;
11074
11075 // Step 2, save the canvas' layers
11076 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011077
11078 final boolean offsetRequired = isPaddingOffsetRequired();
11079 if (offsetRequired) {
11080 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011081 }
11082
11083 int left = mScrollX + paddingLeft;
11084 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011085 int top = mScrollY + getFadeTop(offsetRequired);
11086 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011087
11088 if (offsetRequired) {
11089 right += getRightPaddingOffset();
11090 bottom += getBottomPaddingOffset();
11091 }
11092
11093 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011094 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11095 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011096
11097 // clip the fade length if top and bottom fades overlap
11098 // overlapping fades produce odd-looking artifacts
11099 if (verticalEdges && (top + length > bottom - length)) {
11100 length = (bottom - top) / 2;
11101 }
11102
11103 // also clip horizontal fades if necessary
11104 if (horizontalEdges && (left + length > right - length)) {
11105 length = (right - left) / 2;
11106 }
11107
11108 if (verticalEdges) {
11109 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011110 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011112 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011113 }
11114
11115 if (horizontalEdges) {
11116 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011117 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011118 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011119 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011120 }
11121
11122 saveCount = canvas.getSaveCount();
11123
11124 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011125 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011126 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11127
11128 if (drawTop) {
11129 canvas.saveLayer(left, top, right, top + length, null, flags);
11130 }
11131
11132 if (drawBottom) {
11133 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11134 }
11135
11136 if (drawLeft) {
11137 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11138 }
11139
11140 if (drawRight) {
11141 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11142 }
11143 } else {
11144 scrollabilityCache.setFadeColor(solidColor);
11145 }
11146
11147 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011148 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011149
11150 // Step 4, draw the children
11151 dispatchDraw(canvas);
11152
11153 // Step 5, draw the fade effect and restore layers
11154 final Paint p = scrollabilityCache.paint;
11155 final Matrix matrix = scrollabilityCache.matrix;
11156 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157
11158 if (drawTop) {
11159 matrix.setScale(1, fadeHeight * topFadeStrength);
11160 matrix.postTranslate(left, top);
11161 fade.setLocalMatrix(matrix);
11162 canvas.drawRect(left, top, right, top + length, p);
11163 }
11164
11165 if (drawBottom) {
11166 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11167 matrix.postRotate(180);
11168 matrix.postTranslate(left, bottom);
11169 fade.setLocalMatrix(matrix);
11170 canvas.drawRect(left, bottom - length, right, bottom, p);
11171 }
11172
11173 if (drawLeft) {
11174 matrix.setScale(1, fadeHeight * leftFadeStrength);
11175 matrix.postRotate(-90);
11176 matrix.postTranslate(left, top);
11177 fade.setLocalMatrix(matrix);
11178 canvas.drawRect(left, top, left + length, bottom, p);
11179 }
11180
11181 if (drawRight) {
11182 matrix.setScale(1, fadeHeight * rightFadeStrength);
11183 matrix.postRotate(90);
11184 matrix.postTranslate(right, top);
11185 fade.setLocalMatrix(matrix);
11186 canvas.drawRect(right - length, top, right, bottom, p);
11187 }
11188
11189 canvas.restoreToCount(saveCount);
11190
11191 // Step 6, draw decorations (scrollbars)
11192 onDrawScrollBars(canvas);
11193 }
11194
11195 /**
11196 * Override this if your view is known to always be drawn on top of a solid color background,
11197 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11198 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11199 * should be set to 0xFF.
11200 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011201 * @see #setVerticalFadingEdgeEnabled(boolean)
11202 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203 *
11204 * @return The known solid color background for this view, or 0 if the color may vary
11205 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011206 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011207 public int getSolidColor() {
11208 return 0;
11209 }
11210
11211 /**
11212 * Build a human readable string representation of the specified view flags.
11213 *
11214 * @param flags the view flags to convert to a string
11215 * @return a String representing the supplied flags
11216 */
11217 private static String printFlags(int flags) {
11218 String output = "";
11219 int numFlags = 0;
11220 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11221 output += "TAKES_FOCUS";
11222 numFlags++;
11223 }
11224
11225 switch (flags & VISIBILITY_MASK) {
11226 case INVISIBLE:
11227 if (numFlags > 0) {
11228 output += " ";
11229 }
11230 output += "INVISIBLE";
11231 // USELESS HERE numFlags++;
11232 break;
11233 case GONE:
11234 if (numFlags > 0) {
11235 output += " ";
11236 }
11237 output += "GONE";
11238 // USELESS HERE numFlags++;
11239 break;
11240 default:
11241 break;
11242 }
11243 return output;
11244 }
11245
11246 /**
11247 * Build a human readable string representation of the specified private
11248 * view flags.
11249 *
11250 * @param privateFlags the private view flags to convert to a string
11251 * @return a String representing the supplied flags
11252 */
11253 private static String printPrivateFlags(int privateFlags) {
11254 String output = "";
11255 int numFlags = 0;
11256
11257 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11258 output += "WANTS_FOCUS";
11259 numFlags++;
11260 }
11261
11262 if ((privateFlags & FOCUSED) == FOCUSED) {
11263 if (numFlags > 0) {
11264 output += " ";
11265 }
11266 output += "FOCUSED";
11267 numFlags++;
11268 }
11269
11270 if ((privateFlags & SELECTED) == SELECTED) {
11271 if (numFlags > 0) {
11272 output += " ";
11273 }
11274 output += "SELECTED";
11275 numFlags++;
11276 }
11277
11278 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11279 if (numFlags > 0) {
11280 output += " ";
11281 }
11282 output += "IS_ROOT_NAMESPACE";
11283 numFlags++;
11284 }
11285
11286 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11287 if (numFlags > 0) {
11288 output += " ";
11289 }
11290 output += "HAS_BOUNDS";
11291 numFlags++;
11292 }
11293
11294 if ((privateFlags & DRAWN) == DRAWN) {
11295 if (numFlags > 0) {
11296 output += " ";
11297 }
11298 output += "DRAWN";
11299 // USELESS HERE numFlags++;
11300 }
11301 return output;
11302 }
11303
11304 /**
11305 * <p>Indicates whether or not this view's layout will be requested during
11306 * the next hierarchy layout pass.</p>
11307 *
11308 * @return true if the layout will be forced during next layout pass
11309 */
11310 public boolean isLayoutRequested() {
11311 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11312 }
11313
11314 /**
11315 * Assign a size and position to a view and all of its
11316 * descendants
11317 *
11318 * <p>This is the second phase of the layout mechanism.
11319 * (The first is measuring). In this phase, each parent calls
11320 * layout on all of its children to position them.
11321 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011322 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011323 *
Chet Haase9c087442011-01-12 16:20:16 -080011324 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011325 * Derived classes with children should override
11326 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011327 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011328 *
11329 * @param l Left position, relative to parent
11330 * @param t Top position, relative to parent
11331 * @param r Right position, relative to parent
11332 * @param b Bottom position, relative to parent
11333 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011334 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011335 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011336 int oldL = mLeft;
11337 int oldT = mTop;
11338 int oldB = mBottom;
11339 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011340 boolean changed = setFrame(l, t, r, b);
11341 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11342 if (ViewDebug.TRACE_HIERARCHY) {
11343 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11344 }
11345
11346 onLayout(changed, l, t, r, b);
11347 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011348
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011349 ListenerInfo li = mListenerInfo;
11350 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011351 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011352 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011353 int numListeners = listenersCopy.size();
11354 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011355 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011356 }
11357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011358 }
11359 mPrivateFlags &= ~FORCE_LAYOUT;
11360 }
11361
11362 /**
11363 * Called from layout when this view should
11364 * assign a size and position to each of its children.
11365 *
11366 * Derived classes with children should override
11367 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011368 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011369 * @param changed This is a new size or position for this view
11370 * @param left Left position, relative to parent
11371 * @param top Top position, relative to parent
11372 * @param right Right position, relative to parent
11373 * @param bottom Bottom position, relative to parent
11374 */
11375 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11376 }
11377
11378 /**
11379 * Assign a size and position to this view.
11380 *
11381 * This is called from layout.
11382 *
11383 * @param left Left position, relative to parent
11384 * @param top Top position, relative to parent
11385 * @param right Right position, relative to parent
11386 * @param bottom Bottom position, relative to parent
11387 * @return true if the new size and position are different than the
11388 * previous ones
11389 * {@hide}
11390 */
11391 protected boolean setFrame(int left, int top, int right, int bottom) {
11392 boolean changed = false;
11393
11394 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011395 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011396 + right + "," + bottom + ")");
11397 }
11398
11399 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11400 changed = true;
11401
11402 // Remember our drawn bit
11403 int drawn = mPrivateFlags & DRAWN;
11404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011405 int oldWidth = mRight - mLeft;
11406 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011407 int newWidth = right - left;
11408 int newHeight = bottom - top;
11409 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11410
11411 // Invalidate our old position
11412 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011413
11414 mLeft = left;
11415 mTop = top;
11416 mRight = right;
11417 mBottom = bottom;
11418
11419 mPrivateFlags |= HAS_BOUNDS;
11420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011421
Chet Haase75755e22011-07-18 17:48:25 -070011422 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011423 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11424 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011425 if (mTransformationInfo != null) {
11426 mTransformationInfo.mMatrixDirty = true;
11427 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011429 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11430 }
11431
11432 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11433 // If we are visible, force the DRAWN bit to on so that
11434 // this invalidate will go through (at least to our parent).
11435 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011436 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011437 // the DRAWN bit.
11438 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011439 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011440 // parent display list may need to be recreated based on a change in the bounds
11441 // of any child
11442 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011443 }
11444
11445 // Reset drawn bit to original value (invalidate turns it off)
11446 mPrivateFlags |= drawn;
11447
11448 mBackgroundSizeChanged = true;
11449 }
11450 return changed;
11451 }
11452
11453 /**
11454 * Finalize inflating a view from XML. This is called as the last phase
11455 * of inflation, after all child views have been added.
11456 *
11457 * <p>Even if the subclass overrides onFinishInflate, they should always be
11458 * sure to call the super method, so that we get called.
11459 */
11460 protected void onFinishInflate() {
11461 }
11462
11463 /**
11464 * Returns the resources associated with this view.
11465 *
11466 * @return Resources object.
11467 */
11468 public Resources getResources() {
11469 return mResources;
11470 }
11471
11472 /**
11473 * Invalidates the specified Drawable.
11474 *
11475 * @param drawable the drawable to invalidate
11476 */
11477 public void invalidateDrawable(Drawable drawable) {
11478 if (verifyDrawable(drawable)) {
11479 final Rect dirty = drawable.getBounds();
11480 final int scrollX = mScrollX;
11481 final int scrollY = mScrollY;
11482
11483 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11484 dirty.right + scrollX, dirty.bottom + scrollY);
11485 }
11486 }
11487
11488 /**
11489 * Schedules an action on a drawable to occur at a specified time.
11490 *
11491 * @param who the recipient of the action
11492 * @param what the action to run on the drawable
11493 * @param when the time at which the action must occur. Uses the
11494 * {@link SystemClock#uptimeMillis} timebase.
11495 */
11496 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011497 if (verifyDrawable(who) && what != null) {
11498 if (mAttachInfo != null) {
11499 mAttachInfo.mHandler.postAtTime(what, who, when);
11500 } else {
11501 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011503 }
11504 }
11505
11506 /**
11507 * Cancels a scheduled action on a drawable.
11508 *
11509 * @param who the recipient of the action
11510 * @param what the action to cancel
11511 */
11512 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011513 if (verifyDrawable(who) && what != null) {
11514 if (mAttachInfo != null) {
11515 mAttachInfo.mHandler.removeCallbacks(what, who);
11516 } else {
11517 ViewRootImpl.getRunQueue().removeCallbacks(what);
11518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011519 }
11520 }
11521
11522 /**
11523 * Unschedule any events associated with the given Drawable. This can be
11524 * used when selecting a new Drawable into a view, so that the previous
11525 * one is completely unscheduled.
11526 *
11527 * @param who The Drawable to unschedule.
11528 *
11529 * @see #drawableStateChanged
11530 */
11531 public void unscheduleDrawable(Drawable who) {
11532 if (mAttachInfo != null) {
11533 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11534 }
11535 }
11536
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011537 /**
11538 * Return the layout direction of a given Drawable.
11539 *
11540 * @param who the Drawable to query
11541 *
11542 * @hide
11543 */
11544 public int getResolvedLayoutDirection(Drawable who) {
11545 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011546 }
11547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011548 /**
11549 * If your view subclass is displaying its own Drawable objects, it should
11550 * override this function and return true for any Drawable it is
11551 * displaying. This allows animations for those drawables to be
11552 * scheduled.
11553 *
11554 * <p>Be sure to call through to the super class when overriding this
11555 * function.
11556 *
11557 * @param who The Drawable to verify. Return true if it is one you are
11558 * displaying, else return the result of calling through to the
11559 * super class.
11560 *
11561 * @return boolean If true than the Drawable is being displayed in the
11562 * view; else false and it is not allowed to animate.
11563 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011564 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11565 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011566 */
11567 protected boolean verifyDrawable(Drawable who) {
11568 return who == mBGDrawable;
11569 }
11570
11571 /**
11572 * This function is called whenever the state of the view changes in such
11573 * a way that it impacts the state of drawables being shown.
11574 *
11575 * <p>Be sure to call through to the superclass when overriding this
11576 * function.
11577 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011578 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011579 */
11580 protected void drawableStateChanged() {
11581 Drawable d = mBGDrawable;
11582 if (d != null && d.isStateful()) {
11583 d.setState(getDrawableState());
11584 }
11585 }
11586
11587 /**
11588 * Call this to force a view to update its drawable state. This will cause
11589 * drawableStateChanged to be called on this view. Views that are interested
11590 * in the new state should call getDrawableState.
11591 *
11592 * @see #drawableStateChanged
11593 * @see #getDrawableState
11594 */
11595 public void refreshDrawableState() {
11596 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11597 drawableStateChanged();
11598
11599 ViewParent parent = mParent;
11600 if (parent != null) {
11601 parent.childDrawableStateChanged(this);
11602 }
11603 }
11604
11605 /**
11606 * Return an array of resource IDs of the drawable states representing the
11607 * current state of the view.
11608 *
11609 * @return The current drawable state
11610 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011611 * @see Drawable#setState(int[])
11612 * @see #drawableStateChanged()
11613 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011614 */
11615 public final int[] getDrawableState() {
11616 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11617 return mDrawableState;
11618 } else {
11619 mDrawableState = onCreateDrawableState(0);
11620 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11621 return mDrawableState;
11622 }
11623 }
11624
11625 /**
11626 * Generate the new {@link android.graphics.drawable.Drawable} state for
11627 * this view. This is called by the view
11628 * system when the cached Drawable state is determined to be invalid. To
11629 * retrieve the current state, you should use {@link #getDrawableState}.
11630 *
11631 * @param extraSpace if non-zero, this is the number of extra entries you
11632 * would like in the returned array in which you can place your own
11633 * states.
11634 *
11635 * @return Returns an array holding the current {@link Drawable} state of
11636 * the view.
11637 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011638 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011639 */
11640 protected int[] onCreateDrawableState(int extraSpace) {
11641 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11642 mParent instanceof View) {
11643 return ((View) mParent).onCreateDrawableState(extraSpace);
11644 }
11645
11646 int[] drawableState;
11647
11648 int privateFlags = mPrivateFlags;
11649
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011650 int viewStateIndex = 0;
11651 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11652 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11653 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011654 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011655 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11656 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011657 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11658 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011659 // This is set if HW acceleration is requested, even if the current
11660 // process doesn't allow it. This is just to allow app preview
11661 // windows to better match their app.
11662 viewStateIndex |= VIEW_STATE_ACCELERATED;
11663 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011664 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011665
Christopher Tate3d4bf172011-03-28 16:16:46 -070011666 final int privateFlags2 = mPrivateFlags2;
11667 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11668 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011670 drawableState = VIEW_STATE_SETS[viewStateIndex];
11671
11672 //noinspection ConstantIfStatement
11673 if (false) {
11674 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11675 Log.i("View", toString()
11676 + " pressed=" + ((privateFlags & PRESSED) != 0)
11677 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11678 + " fo=" + hasFocus()
11679 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011680 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011681 + ": " + Arrays.toString(drawableState));
11682 }
11683
11684 if (extraSpace == 0) {
11685 return drawableState;
11686 }
11687
11688 final int[] fullState;
11689 if (drawableState != null) {
11690 fullState = new int[drawableState.length + extraSpace];
11691 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11692 } else {
11693 fullState = new int[extraSpace];
11694 }
11695
11696 return fullState;
11697 }
11698
11699 /**
11700 * Merge your own state values in <var>additionalState</var> into the base
11701 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011702 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011703 *
11704 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011705 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011706 * own additional state values.
11707 *
11708 * @param additionalState The additional state values you would like
11709 * added to <var>baseState</var>; this array is not modified.
11710 *
11711 * @return As a convenience, the <var>baseState</var> array you originally
11712 * passed into the function is returned.
11713 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011714 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011715 */
11716 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11717 final int N = baseState.length;
11718 int i = N - 1;
11719 while (i >= 0 && baseState[i] == 0) {
11720 i--;
11721 }
11722 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11723 return baseState;
11724 }
11725
11726 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011727 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11728 * on all Drawable objects associated with this view.
11729 */
11730 public void jumpDrawablesToCurrentState() {
11731 if (mBGDrawable != null) {
11732 mBGDrawable.jumpToCurrentState();
11733 }
11734 }
11735
11736 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011737 * Sets the background color for this view.
11738 * @param color the color of the background
11739 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011740 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011741 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011742 if (mBGDrawable instanceof ColorDrawable) {
11743 ((ColorDrawable) mBGDrawable).setColor(color);
11744 } else {
11745 setBackgroundDrawable(new ColorDrawable(color));
11746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011747 }
11748
11749 /**
11750 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011751 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011752 * @param resid The identifier of the resource.
11753 * @attr ref android.R.styleable#View_background
11754 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011755 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011756 public void setBackgroundResource(int resid) {
11757 if (resid != 0 && resid == mBackgroundResource) {
11758 return;
11759 }
11760
11761 Drawable d= null;
11762 if (resid != 0) {
11763 d = mResources.getDrawable(resid);
11764 }
11765 setBackgroundDrawable(d);
11766
11767 mBackgroundResource = resid;
11768 }
11769
11770 /**
11771 * Set the background to a given Drawable, or remove the background. If the
11772 * background has padding, this View's padding is set to the background's
11773 * padding. However, when a background is removed, this View's padding isn't
11774 * touched. If setting the padding is desired, please use
11775 * {@link #setPadding(int, int, int, int)}.
11776 *
11777 * @param d The Drawable to use as the background, or null to remove the
11778 * background
11779 */
11780 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011781 if (d == mBGDrawable) {
11782 return;
11783 }
11784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011785 boolean requestLayout = false;
11786
11787 mBackgroundResource = 0;
11788
11789 /*
11790 * Regardless of whether we're setting a new background or not, we want
11791 * to clear the previous drawable.
11792 */
11793 if (mBGDrawable != null) {
11794 mBGDrawable.setCallback(null);
11795 unscheduleDrawable(mBGDrawable);
11796 }
11797
11798 if (d != null) {
11799 Rect padding = sThreadLocal.get();
11800 if (padding == null) {
11801 padding = new Rect();
11802 sThreadLocal.set(padding);
11803 }
11804 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011805 switch (d.getResolvedLayoutDirectionSelf()) {
11806 case LAYOUT_DIRECTION_RTL:
11807 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11808 break;
11809 case LAYOUT_DIRECTION_LTR:
11810 default:
11811 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011813 }
11814
11815 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11816 // if it has a different minimum size, we should layout again
11817 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11818 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11819 requestLayout = true;
11820 }
11821
11822 d.setCallback(this);
11823 if (d.isStateful()) {
11824 d.setState(getDrawableState());
11825 }
11826 d.setVisible(getVisibility() == VISIBLE, false);
11827 mBGDrawable = d;
11828
11829 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11830 mPrivateFlags &= ~SKIP_DRAW;
11831 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11832 requestLayout = true;
11833 }
11834 } else {
11835 /* Remove the background */
11836 mBGDrawable = null;
11837
11838 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11839 /*
11840 * This view ONLY drew the background before and we're removing
11841 * the background, so now it won't draw anything
11842 * (hence we SKIP_DRAW)
11843 */
11844 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11845 mPrivateFlags |= SKIP_DRAW;
11846 }
11847
11848 /*
11849 * When the background is set, we try to apply its padding to this
11850 * View. When the background is removed, we don't touch this View's
11851 * padding. This is noted in the Javadocs. Hence, we don't need to
11852 * requestLayout(), the invalidate() below is sufficient.
11853 */
11854
11855 // The old background's minimum size could have affected this
11856 // View's layout, so let's requestLayout
11857 requestLayout = true;
11858 }
11859
Romain Guy8f1344f52009-05-15 16:03:59 -070011860 computeOpaqueFlags();
11861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011862 if (requestLayout) {
11863 requestLayout();
11864 }
11865
11866 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011867 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011868 }
11869
11870 /**
11871 * Gets the background drawable
11872 * @return The drawable used as the background for this view, if any.
11873 */
11874 public Drawable getBackground() {
11875 return mBGDrawable;
11876 }
11877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011878 /**
11879 * Sets the padding. The view may add on the space required to display
11880 * the scrollbars, depending on the style and visibility of the scrollbars.
11881 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11882 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11883 * from the values set in this call.
11884 *
11885 * @attr ref android.R.styleable#View_padding
11886 * @attr ref android.R.styleable#View_paddingBottom
11887 * @attr ref android.R.styleable#View_paddingLeft
11888 * @attr ref android.R.styleable#View_paddingRight
11889 * @attr ref android.R.styleable#View_paddingTop
11890 * @param left the left padding in pixels
11891 * @param top the top padding in pixels
11892 * @param right the right padding in pixels
11893 * @param bottom the bottom padding in pixels
11894 */
11895 public void setPadding(int left, int top, int right, int bottom) {
11896 boolean changed = false;
11897
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011898 mUserPaddingRelative = false;
11899
Adam Powell20232d02010-12-08 21:08:53 -080011900 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011901 mUserPaddingRight = right;
11902 mUserPaddingBottom = bottom;
11903
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011904 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011905
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011906 // Common case is there are no scroll bars.
11907 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011908 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011909 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011910 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011911 switch (mVerticalScrollbarPosition) {
11912 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011913 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11914 left += offset;
11915 } else {
11916 right += offset;
11917 }
11918 break;
Adam Powell20232d02010-12-08 21:08:53 -080011919 case SCROLLBAR_POSITION_RIGHT:
11920 right += offset;
11921 break;
11922 case SCROLLBAR_POSITION_LEFT:
11923 left += offset;
11924 break;
11925 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011926 }
Adam Powell20232d02010-12-08 21:08:53 -080011927 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011928 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11929 ? 0 : getHorizontalScrollbarHeight();
11930 }
11931 }
Romain Guy8506ab42009-06-11 17:35:47 -070011932
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011933 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011934 changed = true;
11935 mPaddingLeft = left;
11936 }
11937 if (mPaddingTop != top) {
11938 changed = true;
11939 mPaddingTop = top;
11940 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011941 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011942 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011943 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011944 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011945 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011946 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011947 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011948 }
11949
11950 if (changed) {
11951 requestLayout();
11952 }
11953 }
11954
11955 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011956 * Sets the relative padding. The view may add on the space required to display
11957 * the scrollbars, depending on the style and visibility of the scrollbars.
11958 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11959 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11960 * from the values set in this call.
11961 *
11962 * @attr ref android.R.styleable#View_padding
11963 * @attr ref android.R.styleable#View_paddingBottom
11964 * @attr ref android.R.styleable#View_paddingStart
11965 * @attr ref android.R.styleable#View_paddingEnd
11966 * @attr ref android.R.styleable#View_paddingTop
11967 * @param start the start padding in pixels
11968 * @param top the top padding in pixels
11969 * @param end the end padding in pixels
11970 * @param bottom the bottom padding in pixels
11971 *
11972 * @hide
11973 */
11974 public void setPaddingRelative(int start, int top, int end, int bottom) {
11975 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011976
11977 mUserPaddingStart = start;
11978 mUserPaddingEnd = end;
11979
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011980 switch(getResolvedLayoutDirection()) {
11981 case LAYOUT_DIRECTION_RTL:
11982 setPadding(end, top, start, bottom);
11983 break;
11984 case LAYOUT_DIRECTION_LTR:
11985 default:
11986 setPadding(start, top, end, bottom);
11987 }
11988 }
11989
11990 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011991 * Returns the top padding of this view.
11992 *
11993 * @return the top padding in pixels
11994 */
11995 public int getPaddingTop() {
11996 return mPaddingTop;
11997 }
11998
11999 /**
12000 * Returns the bottom padding of this view. If there are inset and enabled
12001 * scrollbars, this value may include the space required to display the
12002 * scrollbars as well.
12003 *
12004 * @return the bottom padding in pixels
12005 */
12006 public int getPaddingBottom() {
12007 return mPaddingBottom;
12008 }
12009
12010 /**
12011 * Returns the left padding of this view. If there are inset and enabled
12012 * scrollbars, this value may include the space required to display the
12013 * scrollbars as well.
12014 *
12015 * @return the left padding in pixels
12016 */
12017 public int getPaddingLeft() {
12018 return mPaddingLeft;
12019 }
12020
12021 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012022 * Returns the start padding of this view. If there are inset and enabled
12023 * scrollbars, this value may include the space required to display the
12024 * scrollbars as well.
12025 *
12026 * @return the start padding in pixels
12027 *
12028 * @hide
12029 */
12030 public int getPaddingStart() {
12031 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12032 mPaddingRight : mPaddingLeft;
12033 }
12034
12035 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012036 * Returns the right padding of this view. If there are inset and enabled
12037 * scrollbars, this value may include the space required to display the
12038 * scrollbars as well.
12039 *
12040 * @return the right padding in pixels
12041 */
12042 public int getPaddingRight() {
12043 return mPaddingRight;
12044 }
12045
12046 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012047 * Returns the end padding of this view. If there are inset and enabled
12048 * scrollbars, this value may include the space required to display the
12049 * scrollbars as well.
12050 *
12051 * @return the end padding in pixels
12052 *
12053 * @hide
12054 */
12055 public int getPaddingEnd() {
12056 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12057 mPaddingLeft : mPaddingRight;
12058 }
12059
12060 /**
12061 * Return if the padding as been set thru relative values
12062 * {@link #setPaddingRelative(int, int, int, int)} or thru
12063 * @attr ref android.R.styleable#View_paddingStart or
12064 * @attr ref android.R.styleable#View_paddingEnd
12065 *
12066 * @return true if the padding is relative or false if it is not.
12067 *
12068 * @hide
12069 */
12070 public boolean isPaddingRelative() {
12071 return mUserPaddingRelative;
12072 }
12073
12074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012075 * Changes the selection state of this view. A view can be selected or not.
12076 * Note that selection is not the same as focus. Views are typically
12077 * selected in the context of an AdapterView like ListView or GridView;
12078 * the selected view is the view that is highlighted.
12079 *
12080 * @param selected true if the view must be selected, false otherwise
12081 */
12082 public void setSelected(boolean selected) {
12083 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12084 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012085 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012086 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012087 refreshDrawableState();
12088 dispatchSetSelected(selected);
12089 }
12090 }
12091
12092 /**
12093 * Dispatch setSelected to all of this View's children.
12094 *
12095 * @see #setSelected(boolean)
12096 *
12097 * @param selected The new selected state
12098 */
12099 protected void dispatchSetSelected(boolean selected) {
12100 }
12101
12102 /**
12103 * Indicates the selection state of this view.
12104 *
12105 * @return true if the view is selected, false otherwise
12106 */
12107 @ViewDebug.ExportedProperty
12108 public boolean isSelected() {
12109 return (mPrivateFlags & SELECTED) != 0;
12110 }
12111
12112 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012113 * Changes the activated state of this view. A view can be activated or not.
12114 * Note that activation is not the same as selection. Selection is
12115 * a transient property, representing the view (hierarchy) the user is
12116 * currently interacting with. Activation is a longer-term state that the
12117 * user can move views in and out of. For example, in a list view with
12118 * single or multiple selection enabled, the views in the current selection
12119 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12120 * here.) The activated state is propagated down to children of the view it
12121 * is set on.
12122 *
12123 * @param activated true if the view must be activated, false otherwise
12124 */
12125 public void setActivated(boolean activated) {
12126 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12127 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012128 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012129 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012130 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012131 }
12132 }
12133
12134 /**
12135 * Dispatch setActivated to all of this View's children.
12136 *
12137 * @see #setActivated(boolean)
12138 *
12139 * @param activated The new activated state
12140 */
12141 protected void dispatchSetActivated(boolean activated) {
12142 }
12143
12144 /**
12145 * Indicates the activation state of this view.
12146 *
12147 * @return true if the view is activated, false otherwise
12148 */
12149 @ViewDebug.ExportedProperty
12150 public boolean isActivated() {
12151 return (mPrivateFlags & ACTIVATED) != 0;
12152 }
12153
12154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012155 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12156 * observer can be used to get notifications when global events, like
12157 * layout, happen.
12158 *
12159 * The returned ViewTreeObserver observer is not guaranteed to remain
12160 * valid for the lifetime of this View. If the caller of this method keeps
12161 * a long-lived reference to ViewTreeObserver, it should always check for
12162 * the return value of {@link ViewTreeObserver#isAlive()}.
12163 *
12164 * @return The ViewTreeObserver for this view's hierarchy.
12165 */
12166 public ViewTreeObserver getViewTreeObserver() {
12167 if (mAttachInfo != null) {
12168 return mAttachInfo.mTreeObserver;
12169 }
12170 if (mFloatingTreeObserver == null) {
12171 mFloatingTreeObserver = new ViewTreeObserver();
12172 }
12173 return mFloatingTreeObserver;
12174 }
12175
12176 /**
12177 * <p>Finds the topmost view in the current view hierarchy.</p>
12178 *
12179 * @return the topmost view containing this view
12180 */
12181 public View getRootView() {
12182 if (mAttachInfo != null) {
12183 final View v = mAttachInfo.mRootView;
12184 if (v != null) {
12185 return v;
12186 }
12187 }
Romain Guy8506ab42009-06-11 17:35:47 -070012188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012189 View parent = this;
12190
12191 while (parent.mParent != null && parent.mParent instanceof View) {
12192 parent = (View) parent.mParent;
12193 }
12194
12195 return parent;
12196 }
12197
12198 /**
12199 * <p>Computes the coordinates of this view on the screen. The argument
12200 * must be an array of two integers. After the method returns, the array
12201 * contains the x and y location in that order.</p>
12202 *
12203 * @param location an array of two integers in which to hold the coordinates
12204 */
12205 public void getLocationOnScreen(int[] location) {
12206 getLocationInWindow(location);
12207
12208 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012209 if (info != null) {
12210 location[0] += info.mWindowLeft;
12211 location[1] += info.mWindowTop;
12212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012213 }
12214
12215 /**
12216 * <p>Computes the coordinates of this view in its window. The argument
12217 * must be an array of two integers. After the method returns, the array
12218 * contains the x and y location in that order.</p>
12219 *
12220 * @param location an array of two integers in which to hold the coordinates
12221 */
12222 public void getLocationInWindow(int[] location) {
12223 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012224 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012225 }
12226
Gilles Debunne6583ce52011-12-06 18:09:02 -080012227 if (mAttachInfo == null) {
12228 // When the view is not attached to a window, this method does not make sense
12229 location[0] = location[1] = 0;
12230 return;
12231 }
12232
Gilles Debunnecea45132011-11-24 02:19:27 +010012233 float[] position = mAttachInfo.mTmpTransformLocation;
12234 position[0] = position[1] = 0.0f;
12235
12236 if (!hasIdentityMatrix()) {
12237 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012239
Gilles Debunnecea45132011-11-24 02:19:27 +010012240 position[0] += mLeft;
12241 position[1] += mTop;
12242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012243 ViewParent viewParent = mParent;
12244 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012245 final View view = (View) viewParent;
12246
12247 position[0] -= view.mScrollX;
12248 position[1] -= view.mScrollY;
12249
12250 if (!view.hasIdentityMatrix()) {
12251 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012252 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012253
12254 position[0] += view.mLeft;
12255 position[1] += view.mTop;
12256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012257 viewParent = view.mParent;
12258 }
Romain Guy8506ab42009-06-11 17:35:47 -070012259
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012260 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012261 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012262 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12263 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012264 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012265
12266 location[0] = (int) (position[0] + 0.5f);
12267 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012268 }
12269
12270 /**
12271 * {@hide}
12272 * @param id the id of the view to be found
12273 * @return the view of the specified id, null if cannot be found
12274 */
12275 protected View findViewTraversal(int id) {
12276 if (id == mID) {
12277 return this;
12278 }
12279 return null;
12280 }
12281
12282 /**
12283 * {@hide}
12284 * @param tag the tag of the view to be found
12285 * @return the view of specified tag, null if cannot be found
12286 */
12287 protected View findViewWithTagTraversal(Object tag) {
12288 if (tag != null && tag.equals(mTag)) {
12289 return this;
12290 }
12291 return null;
12292 }
12293
12294 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012295 * {@hide}
12296 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012297 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012298 * @return The first view that matches the predicate or null.
12299 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012300 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012301 if (predicate.apply(this)) {
12302 return this;
12303 }
12304 return null;
12305 }
12306
12307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012308 * Look for a child view with the given id. If this view has the given
12309 * id, return this view.
12310 *
12311 * @param id The id to search for.
12312 * @return The view that has the given id in the hierarchy or null
12313 */
12314 public final View findViewById(int id) {
12315 if (id < 0) {
12316 return null;
12317 }
12318 return findViewTraversal(id);
12319 }
12320
12321 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012322 * Finds a view by its unuque and stable accessibility id.
12323 *
12324 * @param accessibilityId The searched accessibility id.
12325 * @return The found view.
12326 */
12327 final View findViewByAccessibilityId(int accessibilityId) {
12328 if (accessibilityId < 0) {
12329 return null;
12330 }
12331 return findViewByAccessibilityIdTraversal(accessibilityId);
12332 }
12333
12334 /**
12335 * Performs the traversal to find a view by its unuque and stable accessibility id.
12336 *
12337 * <strong>Note:</strong>This method does not stop at the root namespace
12338 * boundary since the user can touch the screen at an arbitrary location
12339 * potentially crossing the root namespace bounday which will send an
12340 * accessibility event to accessibility services and they should be able
12341 * to obtain the event source. Also accessibility ids are guaranteed to be
12342 * unique in the window.
12343 *
12344 * @param accessibilityId The accessibility id.
12345 * @return The found view.
12346 */
12347 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12348 if (getAccessibilityViewId() == accessibilityId) {
12349 return this;
12350 }
12351 return null;
12352 }
12353
12354 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012355 * Look for a child view with the given tag. If this view has the given
12356 * tag, return this view.
12357 *
12358 * @param tag The tag to search for, using "tag.equals(getTag())".
12359 * @return The View that has the given tag in the hierarchy or null
12360 */
12361 public final View findViewWithTag(Object tag) {
12362 if (tag == null) {
12363 return null;
12364 }
12365 return findViewWithTagTraversal(tag);
12366 }
12367
12368 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012369 * {@hide}
12370 * Look for a child view that matches the specified predicate.
12371 * If this view matches the predicate, return this view.
12372 *
12373 * @param predicate The predicate to evaluate.
12374 * @return The first view that matches the predicate or null.
12375 */
12376 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012377 return findViewByPredicateTraversal(predicate, null);
12378 }
12379
12380 /**
12381 * {@hide}
12382 * Look for a child view that matches the specified predicate,
12383 * starting with the specified view and its descendents and then
12384 * recusively searching the ancestors and siblings of that view
12385 * until this view is reached.
12386 *
12387 * This method is useful in cases where the predicate does not match
12388 * a single unique view (perhaps multiple views use the same id)
12389 * and we are trying to find the view that is "closest" in scope to the
12390 * starting view.
12391 *
12392 * @param start The view to start from.
12393 * @param predicate The predicate to evaluate.
12394 * @return The first view that matches the predicate or null.
12395 */
12396 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12397 View childToSkip = null;
12398 for (;;) {
12399 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12400 if (view != null || start == this) {
12401 return view;
12402 }
12403
12404 ViewParent parent = start.getParent();
12405 if (parent == null || !(parent instanceof View)) {
12406 return null;
12407 }
12408
12409 childToSkip = start;
12410 start = (View) parent;
12411 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012412 }
12413
12414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012415 * Sets the identifier for this view. The identifier does not have to be
12416 * unique in this view's hierarchy. The identifier should be a positive
12417 * number.
12418 *
12419 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012420 * @see #getId()
12421 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012422 *
12423 * @param id a number used to identify the view
12424 *
12425 * @attr ref android.R.styleable#View_id
12426 */
12427 public void setId(int id) {
12428 mID = id;
12429 }
12430
12431 /**
12432 * {@hide}
12433 *
12434 * @param isRoot true if the view belongs to the root namespace, false
12435 * otherwise
12436 */
12437 public void setIsRootNamespace(boolean isRoot) {
12438 if (isRoot) {
12439 mPrivateFlags |= IS_ROOT_NAMESPACE;
12440 } else {
12441 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12442 }
12443 }
12444
12445 /**
12446 * {@hide}
12447 *
12448 * @return true if the view belongs to the root namespace, false otherwise
12449 */
12450 public boolean isRootNamespace() {
12451 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12452 }
12453
12454 /**
12455 * Returns this view's identifier.
12456 *
12457 * @return a positive integer used to identify the view or {@link #NO_ID}
12458 * if the view has no ID
12459 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012460 * @see #setId(int)
12461 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012462 * @attr ref android.R.styleable#View_id
12463 */
12464 @ViewDebug.CapturedViewProperty
12465 public int getId() {
12466 return mID;
12467 }
12468
12469 /**
12470 * Returns this view's tag.
12471 *
12472 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012473 *
12474 * @see #setTag(Object)
12475 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012476 */
12477 @ViewDebug.ExportedProperty
12478 public Object getTag() {
12479 return mTag;
12480 }
12481
12482 /**
12483 * Sets the tag associated with this view. A tag can be used to mark
12484 * a view in its hierarchy and does not have to be unique within the
12485 * hierarchy. Tags can also be used to store data within a view without
12486 * resorting to another data structure.
12487 *
12488 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012489 *
12490 * @see #getTag()
12491 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012492 */
12493 public void setTag(final Object tag) {
12494 mTag = tag;
12495 }
12496
12497 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012498 * Returns the tag associated with this view and the specified key.
12499 *
12500 * @param key The key identifying the tag
12501 *
12502 * @return the Object stored in this view as a tag
12503 *
12504 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012505 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012506 */
12507 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012508 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012509 return null;
12510 }
12511
12512 /**
12513 * Sets a tag associated with this view and a key. A tag can be used
12514 * to mark a view in its hierarchy and does not have to be unique within
12515 * the hierarchy. Tags can also be used to store data within a view
12516 * without resorting to another data structure.
12517 *
12518 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012519 * application to ensure it is unique (see the <a
12520 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12521 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012522 * the Android framework or not associated with any package will cause
12523 * an {@link IllegalArgumentException} to be thrown.
12524 *
12525 * @param key The key identifying the tag
12526 * @param tag An Object to tag the view with
12527 *
12528 * @throws IllegalArgumentException If they specified key is not valid
12529 *
12530 * @see #setTag(Object)
12531 * @see #getTag(int)
12532 */
12533 public void setTag(int key, final Object tag) {
12534 // If the package id is 0x00 or 0x01, it's either an undefined package
12535 // or a framework id
12536 if ((key >>> 24) < 2) {
12537 throw new IllegalArgumentException("The key must be an application-specific "
12538 + "resource id.");
12539 }
12540
Adam Powell2b2f6d62011-09-23 11:15:39 -070012541 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012542 }
12543
12544 /**
12545 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12546 * framework id.
12547 *
12548 * @hide
12549 */
12550 public void setTagInternal(int key, Object tag) {
12551 if ((key >>> 24) != 0x1) {
12552 throw new IllegalArgumentException("The key must be a framework-specific "
12553 + "resource id.");
12554 }
12555
Adam Powell2b2f6d62011-09-23 11:15:39 -070012556 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012557 }
12558
Adam Powell2b2f6d62011-09-23 11:15:39 -070012559 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012560 if (mKeyedTags == null) {
12561 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012562 }
12563
Adam Powell7db82ac2011-09-22 19:44:04 -070012564 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012565 }
12566
12567 /**
Romain Guy13922e02009-05-12 17:56:14 -070012568 * @param consistency The type of consistency. See ViewDebug for more information.
12569 *
12570 * @hide
12571 */
12572 protected boolean dispatchConsistencyCheck(int consistency) {
12573 return onConsistencyCheck(consistency);
12574 }
12575
12576 /**
12577 * Method that subclasses should implement to check their consistency. The type of
12578 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012579 *
Romain Guy13922e02009-05-12 17:56:14 -070012580 * @param consistency The type of consistency. See ViewDebug for more information.
12581 *
12582 * @throws IllegalStateException if the view is in an inconsistent state.
12583 *
12584 * @hide
12585 */
12586 protected boolean onConsistencyCheck(int consistency) {
12587 boolean result = true;
12588
12589 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12590 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12591
12592 if (checkLayout) {
12593 if (getParent() == null) {
12594 result = false;
12595 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12596 "View " + this + " does not have a parent.");
12597 }
12598
12599 if (mAttachInfo == null) {
12600 result = false;
12601 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12602 "View " + this + " is not attached to a window.");
12603 }
12604 }
12605
12606 if (checkDrawing) {
12607 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12608 // from their draw() method
12609
12610 if ((mPrivateFlags & DRAWN) != DRAWN &&
12611 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12612 result = false;
12613 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12614 "View " + this + " was invalidated but its drawing cache is valid.");
12615 }
12616 }
12617
12618 return result;
12619 }
12620
12621 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012622 * Prints information about this view in the log output, with the tag
12623 * {@link #VIEW_LOG_TAG}.
12624 *
12625 * @hide
12626 */
12627 public void debug() {
12628 debug(0);
12629 }
12630
12631 /**
12632 * Prints information about this view in the log output, with the tag
12633 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12634 * indentation defined by the <code>depth</code>.
12635 *
12636 * @param depth the indentation level
12637 *
12638 * @hide
12639 */
12640 protected void debug(int depth) {
12641 String output = debugIndent(depth - 1);
12642
12643 output += "+ " + this;
12644 int id = getId();
12645 if (id != -1) {
12646 output += " (id=" + id + ")";
12647 }
12648 Object tag = getTag();
12649 if (tag != null) {
12650 output += " (tag=" + tag + ")";
12651 }
12652 Log.d(VIEW_LOG_TAG, output);
12653
12654 if ((mPrivateFlags & FOCUSED) != 0) {
12655 output = debugIndent(depth) + " FOCUSED";
12656 Log.d(VIEW_LOG_TAG, output);
12657 }
12658
12659 output = debugIndent(depth);
12660 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12661 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12662 + "} ";
12663 Log.d(VIEW_LOG_TAG, output);
12664
12665 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12666 || mPaddingBottom != 0) {
12667 output = debugIndent(depth);
12668 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12669 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12670 Log.d(VIEW_LOG_TAG, output);
12671 }
12672
12673 output = debugIndent(depth);
12674 output += "mMeasureWidth=" + mMeasuredWidth +
12675 " mMeasureHeight=" + mMeasuredHeight;
12676 Log.d(VIEW_LOG_TAG, output);
12677
12678 output = debugIndent(depth);
12679 if (mLayoutParams == null) {
12680 output += "BAD! no layout params";
12681 } else {
12682 output = mLayoutParams.debug(output);
12683 }
12684 Log.d(VIEW_LOG_TAG, output);
12685
12686 output = debugIndent(depth);
12687 output += "flags={";
12688 output += View.printFlags(mViewFlags);
12689 output += "}";
12690 Log.d(VIEW_LOG_TAG, output);
12691
12692 output = debugIndent(depth);
12693 output += "privateFlags={";
12694 output += View.printPrivateFlags(mPrivateFlags);
12695 output += "}";
12696 Log.d(VIEW_LOG_TAG, output);
12697 }
12698
12699 /**
12700 * Creates an string of whitespaces used for indentation.
12701 *
12702 * @param depth the indentation level
12703 * @return a String containing (depth * 2 + 3) * 2 white spaces
12704 *
12705 * @hide
12706 */
12707 protected static String debugIndent(int depth) {
12708 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12709 for (int i = 0; i < (depth * 2) + 3; i++) {
12710 spaces.append(' ').append(' ');
12711 }
12712 return spaces.toString();
12713 }
12714
12715 /**
12716 * <p>Return the offset of the widget's text baseline from the widget's top
12717 * boundary. If this widget does not support baseline alignment, this
12718 * method returns -1. </p>
12719 *
12720 * @return the offset of the baseline within the widget's bounds or -1
12721 * if baseline alignment is not supported
12722 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012723 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012724 public int getBaseline() {
12725 return -1;
12726 }
12727
12728 /**
12729 * Call this when something has changed which has invalidated the
12730 * layout of this view. This will schedule a layout pass of the view
12731 * tree.
12732 */
12733 public void requestLayout() {
12734 if (ViewDebug.TRACE_HIERARCHY) {
12735 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12736 }
12737
12738 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012739 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012740
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012741 if (mParent != null) {
12742 if (mLayoutParams != null) {
12743 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12744 }
12745 if (!mParent.isLayoutRequested()) {
12746 mParent.requestLayout();
12747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012748 }
12749 }
12750
12751 /**
12752 * Forces this view to be laid out during the next layout pass.
12753 * This method does not call requestLayout() or forceLayout()
12754 * on the parent.
12755 */
12756 public void forceLayout() {
12757 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012758 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012759 }
12760
12761 /**
12762 * <p>
12763 * This is called to find out how big a view should be. The parent
12764 * supplies constraint information in the width and height parameters.
12765 * </p>
12766 *
12767 * <p>
12768 * The actual mesurement work of a view is performed in
12769 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12770 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12771 * </p>
12772 *
12773 *
12774 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12775 * parent
12776 * @param heightMeasureSpec Vertical space requirements as imposed by the
12777 * parent
12778 *
12779 * @see #onMeasure(int, int)
12780 */
12781 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12782 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12783 widthMeasureSpec != mOldWidthMeasureSpec ||
12784 heightMeasureSpec != mOldHeightMeasureSpec) {
12785
12786 // first clears the measured dimension flag
12787 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12788
12789 if (ViewDebug.TRACE_HIERARCHY) {
12790 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12791 }
12792
12793 // measure ourselves, this should set the measured dimension flag back
12794 onMeasure(widthMeasureSpec, heightMeasureSpec);
12795
12796 // flag not set, setMeasuredDimension() was not invoked, we raise
12797 // an exception to warn the developer
12798 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12799 throw new IllegalStateException("onMeasure() did not set the"
12800 + " measured dimension by calling"
12801 + " setMeasuredDimension()");
12802 }
12803
12804 mPrivateFlags |= LAYOUT_REQUIRED;
12805 }
12806
12807 mOldWidthMeasureSpec = widthMeasureSpec;
12808 mOldHeightMeasureSpec = heightMeasureSpec;
12809 }
12810
12811 /**
12812 * <p>
12813 * Measure the view and its content to determine the measured width and the
12814 * measured height. This method is invoked by {@link #measure(int, int)} and
12815 * should be overriden by subclasses to provide accurate and efficient
12816 * measurement of their contents.
12817 * </p>
12818 *
12819 * <p>
12820 * <strong>CONTRACT:</strong> When overriding this method, you
12821 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12822 * measured width and height of this view. Failure to do so will trigger an
12823 * <code>IllegalStateException</code>, thrown by
12824 * {@link #measure(int, int)}. Calling the superclass'
12825 * {@link #onMeasure(int, int)} is a valid use.
12826 * </p>
12827 *
12828 * <p>
12829 * The base class implementation of measure defaults to the background size,
12830 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12831 * override {@link #onMeasure(int, int)} to provide better measurements of
12832 * their content.
12833 * </p>
12834 *
12835 * <p>
12836 * If this method is overridden, it is the subclass's responsibility to make
12837 * sure the measured height and width are at least the view's minimum height
12838 * and width ({@link #getSuggestedMinimumHeight()} and
12839 * {@link #getSuggestedMinimumWidth()}).
12840 * </p>
12841 *
12842 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12843 * The requirements are encoded with
12844 * {@link android.view.View.MeasureSpec}.
12845 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12846 * The requirements are encoded with
12847 * {@link android.view.View.MeasureSpec}.
12848 *
12849 * @see #getMeasuredWidth()
12850 * @see #getMeasuredHeight()
12851 * @see #setMeasuredDimension(int, int)
12852 * @see #getSuggestedMinimumHeight()
12853 * @see #getSuggestedMinimumWidth()
12854 * @see android.view.View.MeasureSpec#getMode(int)
12855 * @see android.view.View.MeasureSpec#getSize(int)
12856 */
12857 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12858 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12859 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12860 }
12861
12862 /**
12863 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12864 * measured width and measured height. Failing to do so will trigger an
12865 * exception at measurement time.</p>
12866 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012867 * @param measuredWidth The measured width of this view. May be a complex
12868 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12869 * {@link #MEASURED_STATE_TOO_SMALL}.
12870 * @param measuredHeight The measured height of this view. May be a complex
12871 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12872 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012873 */
12874 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12875 mMeasuredWidth = measuredWidth;
12876 mMeasuredHeight = measuredHeight;
12877
12878 mPrivateFlags |= MEASURED_DIMENSION_SET;
12879 }
12880
12881 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012882 * Merge two states as returned by {@link #getMeasuredState()}.
12883 * @param curState The current state as returned from a view or the result
12884 * of combining multiple views.
12885 * @param newState The new view state to combine.
12886 * @return Returns a new integer reflecting the combination of the two
12887 * states.
12888 */
12889 public static int combineMeasuredStates(int curState, int newState) {
12890 return curState | newState;
12891 }
12892
12893 /**
12894 * Version of {@link #resolveSizeAndState(int, int, int)}
12895 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12896 */
12897 public static int resolveSize(int size, int measureSpec) {
12898 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12899 }
12900
12901 /**
12902 * Utility to reconcile a desired size and state, with constraints imposed
12903 * by a MeasureSpec. Will take the desired size, unless a different size
12904 * is imposed by the constraints. The returned value is a compound integer,
12905 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12906 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12907 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012908 *
12909 * @param size How big the view wants to be
12910 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012911 * @return Size information bit mask as defined by
12912 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012913 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012914 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012915 int result = size;
12916 int specMode = MeasureSpec.getMode(measureSpec);
12917 int specSize = MeasureSpec.getSize(measureSpec);
12918 switch (specMode) {
12919 case MeasureSpec.UNSPECIFIED:
12920 result = size;
12921 break;
12922 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012923 if (specSize < size) {
12924 result = specSize | MEASURED_STATE_TOO_SMALL;
12925 } else {
12926 result = size;
12927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012928 break;
12929 case MeasureSpec.EXACTLY:
12930 result = specSize;
12931 break;
12932 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012933 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012934 }
12935
12936 /**
12937 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012938 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012939 * by the MeasureSpec.
12940 *
12941 * @param size Default size for this view
12942 * @param measureSpec Constraints imposed by the parent
12943 * @return The size this view should be.
12944 */
12945 public static int getDefaultSize(int size, int measureSpec) {
12946 int result = size;
12947 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012948 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012949
12950 switch (specMode) {
12951 case MeasureSpec.UNSPECIFIED:
12952 result = size;
12953 break;
12954 case MeasureSpec.AT_MOST:
12955 case MeasureSpec.EXACTLY:
12956 result = specSize;
12957 break;
12958 }
12959 return result;
12960 }
12961
12962 /**
12963 * Returns the suggested minimum height that the view should use. This
12964 * returns the maximum of the view's minimum height
12965 * and the background's minimum height
12966 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12967 * <p>
12968 * When being used in {@link #onMeasure(int, int)}, the caller should still
12969 * ensure the returned height is within the requirements of the parent.
12970 *
12971 * @return The suggested minimum height of the view.
12972 */
12973 protected int getSuggestedMinimumHeight() {
12974 int suggestedMinHeight = mMinHeight;
12975
12976 if (mBGDrawable != null) {
12977 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12978 if (suggestedMinHeight < bgMinHeight) {
12979 suggestedMinHeight = bgMinHeight;
12980 }
12981 }
12982
12983 return suggestedMinHeight;
12984 }
12985
12986 /**
12987 * Returns the suggested minimum width that the view should use. This
12988 * returns the maximum of the view's minimum width)
12989 * and the background's minimum width
12990 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12991 * <p>
12992 * When being used in {@link #onMeasure(int, int)}, the caller should still
12993 * ensure the returned width is within the requirements of the parent.
12994 *
12995 * @return The suggested minimum width of the view.
12996 */
12997 protected int getSuggestedMinimumWidth() {
12998 int suggestedMinWidth = mMinWidth;
12999
13000 if (mBGDrawable != null) {
13001 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13002 if (suggestedMinWidth < bgMinWidth) {
13003 suggestedMinWidth = bgMinWidth;
13004 }
13005 }
13006
13007 return suggestedMinWidth;
13008 }
13009
13010 /**
13011 * Sets the minimum height of the view. It is not guaranteed the view will
13012 * be able to achieve this minimum height (for example, if its parent layout
13013 * constrains it with less available height).
13014 *
13015 * @param minHeight The minimum height the view will try to be.
13016 */
13017 public void setMinimumHeight(int minHeight) {
13018 mMinHeight = minHeight;
13019 }
13020
13021 /**
13022 * Sets the minimum width of the view. It is not guaranteed the view will
13023 * be able to achieve this minimum width (for example, if its parent layout
13024 * constrains it with less available width).
13025 *
13026 * @param minWidth The minimum width the view will try to be.
13027 */
13028 public void setMinimumWidth(int minWidth) {
13029 mMinWidth = minWidth;
13030 }
13031
13032 /**
13033 * Get the animation currently associated with this view.
13034 *
13035 * @return The animation that is currently playing or
13036 * scheduled to play for this view.
13037 */
13038 public Animation getAnimation() {
13039 return mCurrentAnimation;
13040 }
13041
13042 /**
13043 * Start the specified animation now.
13044 *
13045 * @param animation the animation to start now
13046 */
13047 public void startAnimation(Animation animation) {
13048 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13049 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013050 invalidateParentCaches();
13051 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013052 }
13053
13054 /**
13055 * Cancels any animations for this view.
13056 */
13057 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013058 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013059 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013061 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013062 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013063 }
13064
13065 /**
13066 * Sets the next animation to play for this view.
13067 * If you want the animation to play immediately, use
13068 * startAnimation. This method provides allows fine-grained
13069 * control over the start time and invalidation, but you
13070 * must make sure that 1) the animation has a start time set, and
13071 * 2) the view will be invalidated when the animation is supposed to
13072 * start.
13073 *
13074 * @param animation The next animation, or null.
13075 */
13076 public void setAnimation(Animation animation) {
13077 mCurrentAnimation = animation;
13078 if (animation != null) {
13079 animation.reset();
13080 }
13081 }
13082
13083 /**
13084 * Invoked by a parent ViewGroup to notify the start of the animation
13085 * currently associated with this view. If you override this method,
13086 * always call super.onAnimationStart();
13087 *
13088 * @see #setAnimation(android.view.animation.Animation)
13089 * @see #getAnimation()
13090 */
13091 protected void onAnimationStart() {
13092 mPrivateFlags |= ANIMATION_STARTED;
13093 }
13094
13095 /**
13096 * Invoked by a parent ViewGroup to notify the end of the animation
13097 * currently associated with this view. If you override this method,
13098 * always call super.onAnimationEnd();
13099 *
13100 * @see #setAnimation(android.view.animation.Animation)
13101 * @see #getAnimation()
13102 */
13103 protected void onAnimationEnd() {
13104 mPrivateFlags &= ~ANIMATION_STARTED;
13105 }
13106
13107 /**
13108 * Invoked if there is a Transform that involves alpha. Subclass that can
13109 * draw themselves with the specified alpha should return true, and then
13110 * respect that alpha when their onDraw() is called. If this returns false
13111 * then the view may be redirected to draw into an offscreen buffer to
13112 * fulfill the request, which will look fine, but may be slower than if the
13113 * subclass handles it internally. The default implementation returns false.
13114 *
13115 * @param alpha The alpha (0..255) to apply to the view's drawing
13116 * @return true if the view can draw with the specified alpha.
13117 */
13118 protected boolean onSetAlpha(int alpha) {
13119 return false;
13120 }
13121
13122 /**
13123 * This is used by the RootView to perform an optimization when
13124 * the view hierarchy contains one or several SurfaceView.
13125 * SurfaceView is always considered transparent, but its children are not,
13126 * therefore all View objects remove themselves from the global transparent
13127 * region (passed as a parameter to this function).
13128 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013129 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013130 *
13131 * @return Returns true if the effective visibility of the view at this
13132 * point is opaque, regardless of the transparent region; returns false
13133 * if it is possible for underlying windows to be seen behind the view.
13134 *
13135 * {@hide}
13136 */
13137 public boolean gatherTransparentRegion(Region region) {
13138 final AttachInfo attachInfo = mAttachInfo;
13139 if (region != null && attachInfo != null) {
13140 final int pflags = mPrivateFlags;
13141 if ((pflags & SKIP_DRAW) == 0) {
13142 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13143 // remove it from the transparent region.
13144 final int[] location = attachInfo.mTransparentLocation;
13145 getLocationInWindow(location);
13146 region.op(location[0], location[1], location[0] + mRight - mLeft,
13147 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13148 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13149 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13150 // exists, so we remove the background drawable's non-transparent
13151 // parts from this transparent region.
13152 applyDrawableToTransparentRegion(mBGDrawable, region);
13153 }
13154 }
13155 return true;
13156 }
13157
13158 /**
13159 * Play a sound effect for this view.
13160 *
13161 * <p>The framework will play sound effects for some built in actions, such as
13162 * clicking, but you may wish to play these effects in your widget,
13163 * for instance, for internal navigation.
13164 *
13165 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13166 * {@link #isSoundEffectsEnabled()} is true.
13167 *
13168 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13169 */
13170 public void playSoundEffect(int soundConstant) {
13171 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13172 return;
13173 }
13174 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13175 }
13176
13177 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013178 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013179 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013180 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013181 *
13182 * <p>The framework will provide haptic feedback for some built in actions,
13183 * such as long presses, but you may wish to provide feedback for your
13184 * own widget.
13185 *
13186 * <p>The feedback will only be performed if
13187 * {@link #isHapticFeedbackEnabled()} is true.
13188 *
13189 * @param feedbackConstant One of the constants defined in
13190 * {@link HapticFeedbackConstants}
13191 */
13192 public boolean performHapticFeedback(int feedbackConstant) {
13193 return performHapticFeedback(feedbackConstant, 0);
13194 }
13195
13196 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013197 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013198 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013199 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013200 *
13201 * @param feedbackConstant One of the constants defined in
13202 * {@link HapticFeedbackConstants}
13203 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13204 */
13205 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13206 if (mAttachInfo == null) {
13207 return false;
13208 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013209 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013210 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013211 && !isHapticFeedbackEnabled()) {
13212 return false;
13213 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013214 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13215 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013216 }
13217
13218 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013219 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013220 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13221 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013222 */
13223 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013224 if (visibility != mSystemUiVisibility) {
13225 mSystemUiVisibility = visibility;
13226 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13227 mParent.recomputeViewAttributes(this);
13228 }
Joe Onorato664644d2011-01-23 17:53:23 -080013229 }
13230 }
13231
13232 /**
13233 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013234 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13235 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013236 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013237 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013238 return mSystemUiVisibility;
13239 }
13240
Scott Mainec6331b2011-05-24 16:55:56 -070013241 /**
13242 * Set a listener to receive callbacks when the visibility of the system bar changes.
13243 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13244 */
Joe Onorato664644d2011-01-23 17:53:23 -080013245 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013246 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013247 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13248 mParent.recomputeViewAttributes(this);
13249 }
13250 }
13251
13252 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013253 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13254 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013255 */
13256 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013257 ListenerInfo li = mListenerInfo;
13258 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13259 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013260 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013261 }
13262 }
13263
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013264 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13265 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13266 if (val != mSystemUiVisibility) {
13267 setSystemUiVisibility(val);
13268 }
13269 }
13270
Joe Onorato664644d2011-01-23 17:53:23 -080013271 /**
Joe Malin32736f02011-01-19 16:14:20 -080013272 * Creates an image that the system displays during the drag and drop
13273 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13274 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13275 * appearance as the given View. The default also positions the center of the drag shadow
13276 * directly under the touch point. If no View is provided (the constructor with no parameters
13277 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13278 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13279 * default is an invisible drag shadow.
13280 * <p>
13281 * You are not required to use the View you provide to the constructor as the basis of the
13282 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13283 * anything you want as the drag shadow.
13284 * </p>
13285 * <p>
13286 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13287 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13288 * size and position of the drag shadow. It uses this data to construct a
13289 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13290 * so that your application can draw the shadow image in the Canvas.
13291 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013292 *
13293 * <div class="special reference">
13294 * <h3>Developer Guides</h3>
13295 * <p>For a guide to implementing drag and drop features, read the
13296 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13297 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013298 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013299 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013300 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013301
13302 /**
Joe Malin32736f02011-01-19 16:14:20 -080013303 * Constructs a shadow image builder based on a View. By default, the resulting drag
13304 * shadow will have the same appearance and dimensions as the View, with the touch point
13305 * over the center of the View.
13306 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013307 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013308 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013309 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013310 }
13311
Christopher Tate17ed60c2011-01-18 12:50:26 -080013312 /**
13313 * Construct a shadow builder object with no associated View. This
13314 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13315 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13316 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013317 * reference to any View object. If they are not overridden, then the result is an
13318 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013319 */
13320 public DragShadowBuilder() {
13321 mView = new WeakReference<View>(null);
13322 }
13323
13324 /**
13325 * Returns the View object that had been passed to the
13326 * {@link #View.DragShadowBuilder(View)}
13327 * constructor. If that View parameter was {@code null} or if the
13328 * {@link #View.DragShadowBuilder()}
13329 * constructor was used to instantiate the builder object, this method will return
13330 * null.
13331 *
13332 * @return The View object associate with this builder object.
13333 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013334 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013335 final public View getView() {
13336 return mView.get();
13337 }
13338
Christopher Tate2c095f32010-10-04 14:13:40 -070013339 /**
Joe Malin32736f02011-01-19 16:14:20 -080013340 * Provides the metrics for the shadow image. These include the dimensions of
13341 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013342 * be centered under the touch location while dragging.
13343 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013344 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013345 * same as the dimensions of the View itself and centers the shadow under
13346 * the touch point.
13347 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013348 *
Joe Malin32736f02011-01-19 16:14:20 -080013349 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13350 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13351 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13352 * image.
13353 *
13354 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13355 * shadow image that should be underneath the touch point during the drag and drop
13356 * operation. Your application must set {@link android.graphics.Point#x} to the
13357 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013358 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013359 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013360 final View view = mView.get();
13361 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013362 shadowSize.set(view.getWidth(), view.getHeight());
13363 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013364 } else {
13365 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13366 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013367 }
13368
13369 /**
Joe Malin32736f02011-01-19 16:14:20 -080013370 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13371 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013372 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013373 *
Joe Malin32736f02011-01-19 16:14:20 -080013374 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013375 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013376 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013377 final View view = mView.get();
13378 if (view != null) {
13379 view.draw(canvas);
13380 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013381 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013382 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013383 }
13384 }
13385
13386 /**
Joe Malin32736f02011-01-19 16:14:20 -080013387 * Starts a drag and drop operation. When your application calls this method, it passes a
13388 * {@link android.view.View.DragShadowBuilder} object to the system. The
13389 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13390 * to get metrics for the drag shadow, and then calls the object's
13391 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13392 * <p>
13393 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13394 * drag events to all the View objects in your application that are currently visible. It does
13395 * this either by calling the View object's drag listener (an implementation of
13396 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13397 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13398 * Both are passed a {@link android.view.DragEvent} object that has a
13399 * {@link android.view.DragEvent#getAction()} value of
13400 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13401 * </p>
13402 * <p>
13403 * Your application can invoke startDrag() on any attached View object. The View object does not
13404 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13405 * be related to the View the user selected for dragging.
13406 * </p>
13407 * @param data A {@link android.content.ClipData} object pointing to the data to be
13408 * transferred by the drag and drop operation.
13409 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13410 * drag shadow.
13411 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13412 * drop operation. This Object is put into every DragEvent object sent by the system during the
13413 * current drag.
13414 * <p>
13415 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13416 * to the target Views. For example, it can contain flags that differentiate between a
13417 * a copy operation and a move operation.
13418 * </p>
13419 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13420 * so the parameter should be set to 0.
13421 * @return {@code true} if the method completes successfully, or
13422 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13423 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013424 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013425 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013426 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013427 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013428 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013429 }
13430 boolean okay = false;
13431
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013432 Point shadowSize = new Point();
13433 Point shadowTouchPoint = new Point();
13434 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013435
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013436 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13437 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13438 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013439 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013440
Chris Tatea32dcf72010-10-14 12:13:50 -070013441 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013442 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13443 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013444 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013445 Surface surface = new Surface();
13446 try {
13447 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013448 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013449 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013450 + " surface=" + surface);
13451 if (token != null) {
13452 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013453 try {
Chris Tate6b391282010-10-14 15:48:59 -070013454 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013455 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013456 } finally {
13457 surface.unlockCanvasAndPost(canvas);
13458 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013459
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013460 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013461
13462 // Cache the local state object for delivery with DragEvents
13463 root.setLocalDragState(myLocalState);
13464
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013465 // repurpose 'shadowSize' for the last touch point
13466 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013467
Christopher Tatea53146c2010-09-07 11:57:52 -070013468 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013469 shadowSize.x, shadowSize.y,
13470 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013471 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013472
13473 // Off and running! Release our local surface instance; the drag
13474 // shadow surface is now managed by the system process.
13475 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013476 }
13477 } catch (Exception e) {
13478 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13479 surface.destroy();
13480 }
13481
13482 return okay;
13483 }
13484
Christopher Tatea53146c2010-09-07 11:57:52 -070013485 /**
Joe Malin32736f02011-01-19 16:14:20 -080013486 * Handles drag events sent by the system following a call to
13487 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13488 *<p>
13489 * When the system calls this method, it passes a
13490 * {@link android.view.DragEvent} object. A call to
13491 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13492 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13493 * operation.
13494 * @param event The {@link android.view.DragEvent} sent by the system.
13495 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13496 * in DragEvent, indicating the type of drag event represented by this object.
13497 * @return {@code true} if the method was successful, otherwise {@code false}.
13498 * <p>
13499 * The method should return {@code true} in response to an action type of
13500 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13501 * operation.
13502 * </p>
13503 * <p>
13504 * The method should also return {@code true} in response to an action type of
13505 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13506 * {@code false} if it didn't.
13507 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013508 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013509 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013510 return false;
13511 }
13512
13513 /**
Joe Malin32736f02011-01-19 16:14:20 -080013514 * Detects if this View is enabled and has a drag event listener.
13515 * If both are true, then it calls the drag event listener with the
13516 * {@link android.view.DragEvent} it received. If the drag event listener returns
13517 * {@code true}, then dispatchDragEvent() returns {@code true}.
13518 * <p>
13519 * For all other cases, the method calls the
13520 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13521 * method and returns its result.
13522 * </p>
13523 * <p>
13524 * This ensures that a drag event is always consumed, even if the View does not have a drag
13525 * event listener. However, if the View has a listener and the listener returns true, then
13526 * onDragEvent() is not called.
13527 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013528 */
13529 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013530 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013531 ListenerInfo li = mListenerInfo;
13532 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13533 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013534 return true;
13535 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013536 return onDragEvent(event);
13537 }
13538
Christopher Tate3d4bf172011-03-28 16:16:46 -070013539 boolean canAcceptDrag() {
13540 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13541 }
13542
Christopher Tatea53146c2010-09-07 11:57:52 -070013543 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013544 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13545 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013546 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013547 */
13548 public void onCloseSystemDialogs(String reason) {
13549 }
Joe Malin32736f02011-01-19 16:14:20 -080013550
Dianne Hackbornffa42482009-09-23 22:20:11 -070013551 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013552 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013553 * update a Region being computed for
13554 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013555 * that any non-transparent parts of the Drawable are removed from the
13556 * given transparent region.
13557 *
13558 * @param dr The Drawable whose transparency is to be applied to the region.
13559 * @param region A Region holding the current transparency information,
13560 * where any parts of the region that are set are considered to be
13561 * transparent. On return, this region will be modified to have the
13562 * transparency information reduced by the corresponding parts of the
13563 * Drawable that are not transparent.
13564 * {@hide}
13565 */
13566 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13567 if (DBG) {
13568 Log.i("View", "Getting transparent region for: " + this);
13569 }
13570 final Region r = dr.getTransparentRegion();
13571 final Rect db = dr.getBounds();
13572 final AttachInfo attachInfo = mAttachInfo;
13573 if (r != null && attachInfo != null) {
13574 final int w = getRight()-getLeft();
13575 final int h = getBottom()-getTop();
13576 if (db.left > 0) {
13577 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13578 r.op(0, 0, db.left, h, Region.Op.UNION);
13579 }
13580 if (db.right < w) {
13581 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13582 r.op(db.right, 0, w, h, Region.Op.UNION);
13583 }
13584 if (db.top > 0) {
13585 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13586 r.op(0, 0, w, db.top, Region.Op.UNION);
13587 }
13588 if (db.bottom < h) {
13589 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13590 r.op(0, db.bottom, w, h, Region.Op.UNION);
13591 }
13592 final int[] location = attachInfo.mTransparentLocation;
13593 getLocationInWindow(location);
13594 r.translate(location[0], location[1]);
13595 region.op(r, Region.Op.INTERSECT);
13596 } else {
13597 region.op(db, Region.Op.DIFFERENCE);
13598 }
13599 }
13600
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013601 private void checkForLongClick(int delayOffset) {
13602 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13603 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013604
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013605 if (mPendingCheckForLongPress == null) {
13606 mPendingCheckForLongPress = new CheckForLongPress();
13607 }
13608 mPendingCheckForLongPress.rememberWindowAttachCount();
13609 postDelayed(mPendingCheckForLongPress,
13610 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013612 }
13613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013614 /**
13615 * Inflate a view from an XML resource. This convenience method wraps the {@link
13616 * LayoutInflater} class, which provides a full range of options for view inflation.
13617 *
13618 * @param context The Context object for your activity or application.
13619 * @param resource The resource ID to inflate
13620 * @param root A view group that will be the parent. Used to properly inflate the
13621 * layout_* parameters.
13622 * @see LayoutInflater
13623 */
13624 public static View inflate(Context context, int resource, ViewGroup root) {
13625 LayoutInflater factory = LayoutInflater.from(context);
13626 return factory.inflate(resource, root);
13627 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013629 /**
Adam Powell637d3372010-08-25 14:37:03 -070013630 * Scroll the view with standard behavior for scrolling beyond the normal
13631 * content boundaries. Views that call this method should override
13632 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13633 * results of an over-scroll operation.
13634 *
13635 * Views can use this method to handle any touch or fling-based scrolling.
13636 *
13637 * @param deltaX Change in X in pixels
13638 * @param deltaY Change in Y in pixels
13639 * @param scrollX Current X scroll value in pixels before applying deltaX
13640 * @param scrollY Current Y scroll value in pixels before applying deltaY
13641 * @param scrollRangeX Maximum content scroll range along the X axis
13642 * @param scrollRangeY Maximum content scroll range along the Y axis
13643 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13644 * along the X axis.
13645 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13646 * along the Y axis.
13647 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13648 * @return true if scrolling was clamped to an over-scroll boundary along either
13649 * axis, false otherwise.
13650 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013651 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013652 protected boolean overScrollBy(int deltaX, int deltaY,
13653 int scrollX, int scrollY,
13654 int scrollRangeX, int scrollRangeY,
13655 int maxOverScrollX, int maxOverScrollY,
13656 boolean isTouchEvent) {
13657 final int overScrollMode = mOverScrollMode;
13658 final boolean canScrollHorizontal =
13659 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13660 final boolean canScrollVertical =
13661 computeVerticalScrollRange() > computeVerticalScrollExtent();
13662 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13663 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13664 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13665 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13666
13667 int newScrollX = scrollX + deltaX;
13668 if (!overScrollHorizontal) {
13669 maxOverScrollX = 0;
13670 }
13671
13672 int newScrollY = scrollY + deltaY;
13673 if (!overScrollVertical) {
13674 maxOverScrollY = 0;
13675 }
13676
13677 // Clamp values if at the limits and record
13678 final int left = -maxOverScrollX;
13679 final int right = maxOverScrollX + scrollRangeX;
13680 final int top = -maxOverScrollY;
13681 final int bottom = maxOverScrollY + scrollRangeY;
13682
13683 boolean clampedX = false;
13684 if (newScrollX > right) {
13685 newScrollX = right;
13686 clampedX = true;
13687 } else if (newScrollX < left) {
13688 newScrollX = left;
13689 clampedX = true;
13690 }
13691
13692 boolean clampedY = false;
13693 if (newScrollY > bottom) {
13694 newScrollY = bottom;
13695 clampedY = true;
13696 } else if (newScrollY < top) {
13697 newScrollY = top;
13698 clampedY = true;
13699 }
13700
13701 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13702
13703 return clampedX || clampedY;
13704 }
13705
13706 /**
13707 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13708 * respond to the results of an over-scroll operation.
13709 *
13710 * @param scrollX New X scroll value in pixels
13711 * @param scrollY New Y scroll value in pixels
13712 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13713 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13714 */
13715 protected void onOverScrolled(int scrollX, int scrollY,
13716 boolean clampedX, boolean clampedY) {
13717 // Intentionally empty.
13718 }
13719
13720 /**
13721 * Returns the over-scroll mode for this view. The result will be
13722 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13723 * (allow over-scrolling only if the view content is larger than the container),
13724 * or {@link #OVER_SCROLL_NEVER}.
13725 *
13726 * @return This view's over-scroll mode.
13727 */
13728 public int getOverScrollMode() {
13729 return mOverScrollMode;
13730 }
13731
13732 /**
13733 * Set the over-scroll mode for this view. Valid over-scroll modes are
13734 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13735 * (allow over-scrolling only if the view content is larger than the container),
13736 * or {@link #OVER_SCROLL_NEVER}.
13737 *
13738 * Setting the over-scroll mode of a view will have an effect only if the
13739 * view is capable of scrolling.
13740 *
13741 * @param overScrollMode The new over-scroll mode for this view.
13742 */
13743 public void setOverScrollMode(int overScrollMode) {
13744 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13745 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13746 overScrollMode != OVER_SCROLL_NEVER) {
13747 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13748 }
13749 mOverScrollMode = overScrollMode;
13750 }
13751
13752 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013753 * Gets a scale factor that determines the distance the view should scroll
13754 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13755 * @return The vertical scroll scale factor.
13756 * @hide
13757 */
13758 protected float getVerticalScrollFactor() {
13759 if (mVerticalScrollFactor == 0) {
13760 TypedValue outValue = new TypedValue();
13761 if (!mContext.getTheme().resolveAttribute(
13762 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13763 throw new IllegalStateException(
13764 "Expected theme to define listPreferredItemHeight.");
13765 }
13766 mVerticalScrollFactor = outValue.getDimension(
13767 mContext.getResources().getDisplayMetrics());
13768 }
13769 return mVerticalScrollFactor;
13770 }
13771
13772 /**
13773 * Gets a scale factor that determines the distance the view should scroll
13774 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13775 * @return The horizontal scroll scale factor.
13776 * @hide
13777 */
13778 protected float getHorizontalScrollFactor() {
13779 // TODO: Should use something else.
13780 return getVerticalScrollFactor();
13781 }
13782
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013783 /**
13784 * Return the value specifying the text direction or policy that was set with
13785 * {@link #setTextDirection(int)}.
13786 *
13787 * @return the defined text direction. It can be one of:
13788 *
13789 * {@link #TEXT_DIRECTION_INHERIT},
13790 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13791 * {@link #TEXT_DIRECTION_ANY_RTL},
13792 * {@link #TEXT_DIRECTION_LTR},
13793 * {@link #TEXT_DIRECTION_RTL},
13794 *
13795 * @hide
13796 */
13797 public int getTextDirection() {
13798 return mTextDirection;
13799 }
13800
13801 /**
13802 * Set the text direction.
13803 *
13804 * @param textDirection the direction to set. Should be one of:
13805 *
13806 * {@link #TEXT_DIRECTION_INHERIT},
13807 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13808 * {@link #TEXT_DIRECTION_ANY_RTL},
13809 * {@link #TEXT_DIRECTION_LTR},
13810 * {@link #TEXT_DIRECTION_RTL},
13811 *
13812 * @hide
13813 */
13814 public void setTextDirection(int textDirection) {
13815 if (textDirection != mTextDirection) {
13816 mTextDirection = textDirection;
13817 resetResolvedTextDirection();
13818 requestLayout();
13819 }
13820 }
13821
13822 /**
13823 * Return the resolved text direction.
13824 *
13825 * @return the resolved text direction. Return one of:
13826 *
Doug Feltcb3791202011-07-07 11:57:48 -070013827 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13828 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013829 * {@link #TEXT_DIRECTION_LTR},
13830 * {@link #TEXT_DIRECTION_RTL},
13831 *
13832 * @hide
13833 */
13834 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013835 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013836 resolveTextDirection();
13837 }
13838 return mResolvedTextDirection;
13839 }
13840
13841 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013842 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013843 *
13844 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013845 */
13846 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013847 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13848 mResolvedTextDirection = mTextDirection;
13849 return;
13850 }
13851 if (mParent != null && mParent instanceof ViewGroup) {
13852 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13853 return;
13854 }
13855 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013856 }
13857
13858 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013859 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013860 *
13861 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013862 */
13863 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013864 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013865 }
13866
Chet Haaseb39f0512011-05-24 14:36:40 -070013867 //
13868 // Properties
13869 //
13870 /**
13871 * A Property wrapper around the <code>alpha</code> functionality handled by the
13872 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13873 */
Romain Guy02ccac62011-06-24 13:20:23 -070013874 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013875 @Override
13876 public void setValue(View object, float value) {
13877 object.setAlpha(value);
13878 }
13879
13880 @Override
13881 public Float get(View object) {
13882 return object.getAlpha();
13883 }
13884 };
13885
13886 /**
13887 * A Property wrapper around the <code>translationX</code> functionality handled by the
13888 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13889 */
13890 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13891 @Override
13892 public void setValue(View object, float value) {
13893 object.setTranslationX(value);
13894 }
13895
13896 @Override
13897 public Float get(View object) {
13898 return object.getTranslationX();
13899 }
13900 };
13901
13902 /**
13903 * A Property wrapper around the <code>translationY</code> functionality handled by the
13904 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13905 */
13906 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13907 @Override
13908 public void setValue(View object, float value) {
13909 object.setTranslationY(value);
13910 }
13911
13912 @Override
13913 public Float get(View object) {
13914 return object.getTranslationY();
13915 }
13916 };
13917
13918 /**
13919 * A Property wrapper around the <code>x</code> functionality handled by the
13920 * {@link View#setX(float)} and {@link View#getX()} methods.
13921 */
13922 public static Property<View, Float> X = new FloatProperty<View>("x") {
13923 @Override
13924 public void setValue(View object, float value) {
13925 object.setX(value);
13926 }
13927
13928 @Override
13929 public Float get(View object) {
13930 return object.getX();
13931 }
13932 };
13933
13934 /**
13935 * A Property wrapper around the <code>y</code> functionality handled by the
13936 * {@link View#setY(float)} and {@link View#getY()} methods.
13937 */
13938 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13939 @Override
13940 public void setValue(View object, float value) {
13941 object.setY(value);
13942 }
13943
13944 @Override
13945 public Float get(View object) {
13946 return object.getY();
13947 }
13948 };
13949
13950 /**
13951 * A Property wrapper around the <code>rotation</code> functionality handled by the
13952 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13953 */
13954 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13955 @Override
13956 public void setValue(View object, float value) {
13957 object.setRotation(value);
13958 }
13959
13960 @Override
13961 public Float get(View object) {
13962 return object.getRotation();
13963 }
13964 };
13965
13966 /**
13967 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13968 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13969 */
13970 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13971 @Override
13972 public void setValue(View object, float value) {
13973 object.setRotationX(value);
13974 }
13975
13976 @Override
13977 public Float get(View object) {
13978 return object.getRotationX();
13979 }
13980 };
13981
13982 /**
13983 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13984 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13985 */
13986 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13987 @Override
13988 public void setValue(View object, float value) {
13989 object.setRotationY(value);
13990 }
13991
13992 @Override
13993 public Float get(View object) {
13994 return object.getRotationY();
13995 }
13996 };
13997
13998 /**
13999 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14000 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14001 */
14002 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
14003 @Override
14004 public void setValue(View object, float value) {
14005 object.setScaleX(value);
14006 }
14007
14008 @Override
14009 public Float get(View object) {
14010 return object.getScaleX();
14011 }
14012 };
14013
14014 /**
14015 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14016 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14017 */
14018 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
14019 @Override
14020 public void setValue(View object, float value) {
14021 object.setScaleY(value);
14022 }
14023
14024 @Override
14025 public Float get(View object) {
14026 return object.getScaleY();
14027 }
14028 };
14029
Jeff Brown33bbfd22011-02-24 20:55:35 -080014030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014031 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14032 * Each MeasureSpec represents a requirement for either the width or the height.
14033 * A MeasureSpec is comprised of a size and a mode. There are three possible
14034 * modes:
14035 * <dl>
14036 * <dt>UNSPECIFIED</dt>
14037 * <dd>
14038 * The parent has not imposed any constraint on the child. It can be whatever size
14039 * it wants.
14040 * </dd>
14041 *
14042 * <dt>EXACTLY</dt>
14043 * <dd>
14044 * The parent has determined an exact size for the child. The child is going to be
14045 * given those bounds regardless of how big it wants to be.
14046 * </dd>
14047 *
14048 * <dt>AT_MOST</dt>
14049 * <dd>
14050 * The child can be as large as it wants up to the specified size.
14051 * </dd>
14052 * </dl>
14053 *
14054 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14055 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14056 */
14057 public static class MeasureSpec {
14058 private static final int MODE_SHIFT = 30;
14059 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14060
14061 /**
14062 * Measure specification mode: The parent has not imposed any constraint
14063 * on the child. It can be whatever size it wants.
14064 */
14065 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14066
14067 /**
14068 * Measure specification mode: The parent has determined an exact size
14069 * for the child. The child is going to be given those bounds regardless
14070 * of how big it wants to be.
14071 */
14072 public static final int EXACTLY = 1 << MODE_SHIFT;
14073
14074 /**
14075 * Measure specification mode: The child can be as large as it wants up
14076 * to the specified size.
14077 */
14078 public static final int AT_MOST = 2 << MODE_SHIFT;
14079
14080 /**
14081 * Creates a measure specification based on the supplied size and mode.
14082 *
14083 * The mode must always be one of the following:
14084 * <ul>
14085 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14086 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14087 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14088 * </ul>
14089 *
14090 * @param size the size of the measure specification
14091 * @param mode the mode of the measure specification
14092 * @return the measure specification based on size and mode
14093 */
14094 public static int makeMeasureSpec(int size, int mode) {
14095 return size + mode;
14096 }
14097
14098 /**
14099 * Extracts the mode from the supplied measure specification.
14100 *
14101 * @param measureSpec the measure specification to extract the mode from
14102 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14103 * {@link android.view.View.MeasureSpec#AT_MOST} or
14104 * {@link android.view.View.MeasureSpec#EXACTLY}
14105 */
14106 public static int getMode(int measureSpec) {
14107 return (measureSpec & MODE_MASK);
14108 }
14109
14110 /**
14111 * Extracts the size from the supplied measure specification.
14112 *
14113 * @param measureSpec the measure specification to extract the size from
14114 * @return the size in pixels defined in the supplied measure specification
14115 */
14116 public static int getSize(int measureSpec) {
14117 return (measureSpec & ~MODE_MASK);
14118 }
14119
14120 /**
14121 * Returns a String representation of the specified measure
14122 * specification.
14123 *
14124 * @param measureSpec the measure specification to convert to a String
14125 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14126 */
14127 public static String toString(int measureSpec) {
14128 int mode = getMode(measureSpec);
14129 int size = getSize(measureSpec);
14130
14131 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14132
14133 if (mode == UNSPECIFIED)
14134 sb.append("UNSPECIFIED ");
14135 else if (mode == EXACTLY)
14136 sb.append("EXACTLY ");
14137 else if (mode == AT_MOST)
14138 sb.append("AT_MOST ");
14139 else
14140 sb.append(mode).append(" ");
14141
14142 sb.append(size);
14143 return sb.toString();
14144 }
14145 }
14146
14147 class CheckForLongPress implements Runnable {
14148
14149 private int mOriginalWindowAttachCount;
14150
14151 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014152 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014153 && mOriginalWindowAttachCount == mWindowAttachCount) {
14154 if (performLongClick()) {
14155 mHasPerformedLongPress = true;
14156 }
14157 }
14158 }
14159
14160 public void rememberWindowAttachCount() {
14161 mOriginalWindowAttachCount = mWindowAttachCount;
14162 }
14163 }
Joe Malin32736f02011-01-19 16:14:20 -080014164
Adam Powelle14579b2009-12-16 18:39:52 -080014165 private final class CheckForTap implements Runnable {
14166 public void run() {
14167 mPrivateFlags &= ~PREPRESSED;
14168 mPrivateFlags |= PRESSED;
14169 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014170 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014171 }
14172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014173
Adam Powella35d7682010-03-12 14:48:13 -080014174 private final class PerformClick implements Runnable {
14175 public void run() {
14176 performClick();
14177 }
14178 }
14179
Dianne Hackborn63042d62011-01-26 18:56:29 -080014180 /** @hide */
14181 public void hackTurnOffWindowResizeAnim(boolean off) {
14182 mAttachInfo.mTurnOffWindowResizeAnim = off;
14183 }
Joe Malin32736f02011-01-19 16:14:20 -080014184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014185 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014186 * This method returns a ViewPropertyAnimator object, which can be used to animate
14187 * specific properties on this View.
14188 *
14189 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14190 */
14191 public ViewPropertyAnimator animate() {
14192 if (mAnimator == null) {
14193 mAnimator = new ViewPropertyAnimator(this);
14194 }
14195 return mAnimator;
14196 }
14197
14198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014199 * Interface definition for a callback to be invoked when a key event is
14200 * dispatched to this view. The callback will be invoked before the key
14201 * event is given to the view.
14202 */
14203 public interface OnKeyListener {
14204 /**
14205 * Called when a key is dispatched to a view. This allows listeners to
14206 * get a chance to respond before the target view.
14207 *
14208 * @param v The view the key has been dispatched to.
14209 * @param keyCode The code for the physical key that was pressed
14210 * @param event The KeyEvent object containing full information about
14211 * the event.
14212 * @return True if the listener has consumed the event, false otherwise.
14213 */
14214 boolean onKey(View v, int keyCode, KeyEvent event);
14215 }
14216
14217 /**
14218 * Interface definition for a callback to be invoked when a touch event is
14219 * dispatched to this view. The callback will be invoked before the touch
14220 * event is given to the view.
14221 */
14222 public interface OnTouchListener {
14223 /**
14224 * Called when a touch event is dispatched to a view. This allows listeners to
14225 * get a chance to respond before the target view.
14226 *
14227 * @param v The view the touch event has been dispatched to.
14228 * @param event The MotionEvent object containing full information about
14229 * the event.
14230 * @return True if the listener has consumed the event, false otherwise.
14231 */
14232 boolean onTouch(View v, MotionEvent event);
14233 }
14234
14235 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014236 * Interface definition for a callback to be invoked when a hover event is
14237 * dispatched to this view. The callback will be invoked before the hover
14238 * event is given to the view.
14239 */
14240 public interface OnHoverListener {
14241 /**
14242 * Called when a hover event is dispatched to a view. This allows listeners to
14243 * get a chance to respond before the target view.
14244 *
14245 * @param v The view the hover event has been dispatched to.
14246 * @param event The MotionEvent object containing full information about
14247 * the event.
14248 * @return True if the listener has consumed the event, false otherwise.
14249 */
14250 boolean onHover(View v, MotionEvent event);
14251 }
14252
14253 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014254 * Interface definition for a callback to be invoked when a generic motion event is
14255 * dispatched to this view. The callback will be invoked before the generic motion
14256 * event is given to the view.
14257 */
14258 public interface OnGenericMotionListener {
14259 /**
14260 * Called when a generic motion event is dispatched to a view. This allows listeners to
14261 * get a chance to respond before the target view.
14262 *
14263 * @param v The view the generic motion event has been dispatched to.
14264 * @param event The MotionEvent object containing full information about
14265 * the event.
14266 * @return True if the listener has consumed the event, false otherwise.
14267 */
14268 boolean onGenericMotion(View v, MotionEvent event);
14269 }
14270
14271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014272 * Interface definition for a callback to be invoked when a view has been clicked and held.
14273 */
14274 public interface OnLongClickListener {
14275 /**
14276 * Called when a view has been clicked and held.
14277 *
14278 * @param v The view that was clicked and held.
14279 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014280 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014281 */
14282 boolean onLongClick(View v);
14283 }
14284
14285 /**
Chris Tate32affef2010-10-18 15:29:21 -070014286 * Interface definition for a callback to be invoked when a drag is being dispatched
14287 * to this view. The callback will be invoked before the hosting view's own
14288 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14289 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014290 *
14291 * <div class="special reference">
14292 * <h3>Developer Guides</h3>
14293 * <p>For a guide to implementing drag and drop features, read the
14294 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14295 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014296 */
14297 public interface OnDragListener {
14298 /**
14299 * Called when a drag event is dispatched to a view. This allows listeners
14300 * to get a chance to override base View behavior.
14301 *
Joe Malin32736f02011-01-19 16:14:20 -080014302 * @param v The View that received the drag event.
14303 * @param event The {@link android.view.DragEvent} object for the drag event.
14304 * @return {@code true} if the drag event was handled successfully, or {@code false}
14305 * if the drag event was not handled. Note that {@code false} will trigger the View
14306 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014307 */
14308 boolean onDrag(View v, DragEvent event);
14309 }
14310
14311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014312 * Interface definition for a callback to be invoked when the focus state of
14313 * a view changed.
14314 */
14315 public interface OnFocusChangeListener {
14316 /**
14317 * Called when the focus state of a view has changed.
14318 *
14319 * @param v The view whose state has changed.
14320 * @param hasFocus The new focus state of v.
14321 */
14322 void onFocusChange(View v, boolean hasFocus);
14323 }
14324
14325 /**
14326 * Interface definition for a callback to be invoked when a view is clicked.
14327 */
14328 public interface OnClickListener {
14329 /**
14330 * Called when a view has been clicked.
14331 *
14332 * @param v The view that was clicked.
14333 */
14334 void onClick(View v);
14335 }
14336
14337 /**
14338 * Interface definition for a callback to be invoked when the context menu
14339 * for this view is being built.
14340 */
14341 public interface OnCreateContextMenuListener {
14342 /**
14343 * Called when the context menu for this view is being built. It is not
14344 * safe to hold onto the menu after this method returns.
14345 *
14346 * @param menu The context menu that is being built
14347 * @param v The view for which the context menu is being built
14348 * @param menuInfo Extra information about the item for which the
14349 * context menu should be shown. This information will vary
14350 * depending on the class of v.
14351 */
14352 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14353 }
14354
Joe Onorato664644d2011-01-23 17:53:23 -080014355 /**
14356 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014357 * visibility. This reports <strong>global</strong> changes to the system UI
14358 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014359 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014360 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014361 */
14362 public interface OnSystemUiVisibilityChangeListener {
14363 /**
14364 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014365 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014366 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014367 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014368 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14369 * <strong>global</strong> state of the UI visibility flags, not what your
14370 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014371 */
14372 public void onSystemUiVisibilityChange(int visibility);
14373 }
14374
Adam Powell4afd62b2011-02-18 15:02:18 -080014375 /**
14376 * Interface definition for a callback to be invoked when this view is attached
14377 * or detached from its window.
14378 */
14379 public interface OnAttachStateChangeListener {
14380 /**
14381 * Called when the view is attached to a window.
14382 * @param v The view that was attached
14383 */
14384 public void onViewAttachedToWindow(View v);
14385 /**
14386 * Called when the view is detached from a window.
14387 * @param v The view that was detached
14388 */
14389 public void onViewDetachedFromWindow(View v);
14390 }
14391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014392 private final class UnsetPressedState implements Runnable {
14393 public void run() {
14394 setPressed(false);
14395 }
14396 }
14397
14398 /**
14399 * Base class for derived classes that want to save and restore their own
14400 * state in {@link android.view.View#onSaveInstanceState()}.
14401 */
14402 public static class BaseSavedState extends AbsSavedState {
14403 /**
14404 * Constructor used when reading from a parcel. Reads the state of the superclass.
14405 *
14406 * @param source
14407 */
14408 public BaseSavedState(Parcel source) {
14409 super(source);
14410 }
14411
14412 /**
14413 * Constructor called by derived classes when creating their SavedState objects
14414 *
14415 * @param superState The state of the superclass of this view
14416 */
14417 public BaseSavedState(Parcelable superState) {
14418 super(superState);
14419 }
14420
14421 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14422 new Parcelable.Creator<BaseSavedState>() {
14423 public BaseSavedState createFromParcel(Parcel in) {
14424 return new BaseSavedState(in);
14425 }
14426
14427 public BaseSavedState[] newArray(int size) {
14428 return new BaseSavedState[size];
14429 }
14430 };
14431 }
14432
14433 /**
14434 * A set of information given to a view when it is attached to its parent
14435 * window.
14436 */
14437 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014438 interface Callbacks {
14439 void playSoundEffect(int effectId);
14440 boolean performHapticFeedback(int effectId, boolean always);
14441 }
14442
14443 /**
14444 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14445 * to a Handler. This class contains the target (View) to invalidate and
14446 * the coordinates of the dirty rectangle.
14447 *
14448 * For performance purposes, this class also implements a pool of up to
14449 * POOL_LIMIT objects that get reused. This reduces memory allocations
14450 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014451 */
Romain Guyd928d682009-03-31 17:52:16 -070014452 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014453 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014454 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14455 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014456 public InvalidateInfo newInstance() {
14457 return new InvalidateInfo();
14458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014459
Romain Guyd928d682009-03-31 17:52:16 -070014460 public void onAcquired(InvalidateInfo element) {
14461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014462
Romain Guyd928d682009-03-31 17:52:16 -070014463 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014464 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014465 }
14466 }, POOL_LIMIT)
14467 );
14468
14469 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014470 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014471
14472 View target;
14473
14474 int left;
14475 int top;
14476 int right;
14477 int bottom;
14478
Romain Guyd928d682009-03-31 17:52:16 -070014479 public void setNextPoolable(InvalidateInfo element) {
14480 mNext = element;
14481 }
14482
14483 public InvalidateInfo getNextPoolable() {
14484 return mNext;
14485 }
14486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014487 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014488 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014489 }
14490
14491 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014492 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014493 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014494
14495 public boolean isPooled() {
14496 return mIsPooled;
14497 }
14498
14499 public void setPooled(boolean isPooled) {
14500 mIsPooled = isPooled;
14501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014502 }
14503
14504 final IWindowSession mSession;
14505
14506 final IWindow mWindow;
14507
14508 final IBinder mWindowToken;
14509
14510 final Callbacks mRootCallbacks;
14511
Romain Guy59a12ca2011-06-09 17:48:21 -070014512 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014514 /**
14515 * The top view of the hierarchy.
14516 */
14517 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014519 IBinder mPanelParentWindowToken;
14520 Surface mSurface;
14521
Romain Guyb051e892010-09-28 19:09:36 -070014522 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014523 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014524 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014526 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014527 * Scale factor used by the compatibility mode
14528 */
14529 float mApplicationScale;
14530
14531 /**
14532 * Indicates whether the application is in compatibility mode
14533 */
14534 boolean mScalingRequired;
14535
14536 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014537 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014538 */
14539 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014540
Dianne Hackborn63042d62011-01-26 18:56:29 -080014541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014542 * Left position of this view's window
14543 */
14544 int mWindowLeft;
14545
14546 /**
14547 * Top position of this view's window
14548 */
14549 int mWindowTop;
14550
14551 /**
Adam Powell26153a32010-11-08 15:22:27 -080014552 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014553 */
Adam Powell26153a32010-11-08 15:22:27 -080014554 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014555
14556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014557 * For windows that are full-screen but using insets to layout inside
14558 * of the screen decorations, these are the current insets for the
14559 * content of the window.
14560 */
14561 final Rect mContentInsets = new Rect();
14562
14563 /**
14564 * For windows that are full-screen but using insets to layout inside
14565 * of the screen decorations, these are the current insets for the
14566 * actual visible parts of the window.
14567 */
14568 final Rect mVisibleInsets = new Rect();
14569
14570 /**
14571 * The internal insets given by this window. This value is
14572 * supplied by the client (through
14573 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14574 * be given to the window manager when changed to be used in laying
14575 * out windows behind it.
14576 */
14577 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14578 = new ViewTreeObserver.InternalInsetsInfo();
14579
14580 /**
14581 * All views in the window's hierarchy that serve as scroll containers,
14582 * used to determine if the window can be resized or must be panned
14583 * to adjust for a soft input area.
14584 */
14585 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14586
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014587 final KeyEvent.DispatcherState mKeyDispatchState
14588 = new KeyEvent.DispatcherState();
14589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014590 /**
14591 * Indicates whether the view's window currently has the focus.
14592 */
14593 boolean mHasWindowFocus;
14594
14595 /**
14596 * The current visibility of the window.
14597 */
14598 int mWindowVisibility;
14599
14600 /**
14601 * Indicates the time at which drawing started to occur.
14602 */
14603 long mDrawingTime;
14604
14605 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014606 * Indicates whether or not ignoring the DIRTY_MASK flags.
14607 */
14608 boolean mIgnoreDirtyState;
14609
14610 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014611 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14612 * to avoid clearing that flag prematurely.
14613 */
14614 boolean mSetIgnoreDirtyState = false;
14615
14616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014617 * Indicates whether the view's window is currently in touch mode.
14618 */
14619 boolean mInTouchMode;
14620
14621 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014622 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014623 * the next time it performs a traversal
14624 */
14625 boolean mRecomputeGlobalAttributes;
14626
14627 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014628 * Always report new attributes at next traversal.
14629 */
14630 boolean mForceReportNewAttributes;
14631
14632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014633 * Set during a traveral if any views want to keep the screen on.
14634 */
14635 boolean mKeepScreenOn;
14636
14637 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014638 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14639 */
14640 int mSystemUiVisibility;
14641
14642 /**
14643 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14644 * attached.
14645 */
14646 boolean mHasSystemUiListeners;
14647
14648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014649 * Set if the visibility of any views has changed.
14650 */
14651 boolean mViewVisibilityChanged;
14652
14653 /**
14654 * Set to true if a view has been scrolled.
14655 */
14656 boolean mViewScrollChanged;
14657
14658 /**
14659 * Global to the view hierarchy used as a temporary for dealing with
14660 * x/y points in the transparent region computations.
14661 */
14662 final int[] mTransparentLocation = new int[2];
14663
14664 /**
14665 * Global to the view hierarchy used as a temporary for dealing with
14666 * x/y points in the ViewGroup.invalidateChild implementation.
14667 */
14668 final int[] mInvalidateChildLocation = new int[2];
14669
Chet Haasec3aa3612010-06-17 08:50:37 -070014670
14671 /**
14672 * Global to the view hierarchy used as a temporary for dealing with
14673 * x/y location when view is transformed.
14674 */
14675 final float[] mTmpTransformLocation = new float[2];
14676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014677 /**
14678 * The view tree observer used to dispatch global events like
14679 * layout, pre-draw, touch mode change, etc.
14680 */
14681 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14682
14683 /**
14684 * A Canvas used by the view hierarchy to perform bitmap caching.
14685 */
14686 Canvas mCanvas;
14687
14688 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014689 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014690 * handler can be used to pump events in the UI events queue.
14691 */
14692 final Handler mHandler;
14693
14694 /**
14695 * Identifier for messages requesting the view to be invalidated.
14696 * Such messages should be sent to {@link #mHandler}.
14697 */
14698 static final int INVALIDATE_MSG = 0x1;
14699
14700 /**
14701 * Identifier for messages requesting the view to invalidate a region.
14702 * Such messages should be sent to {@link #mHandler}.
14703 */
14704 static final int INVALIDATE_RECT_MSG = 0x2;
14705
14706 /**
14707 * Temporary for use in computing invalidate rectangles while
14708 * calling up the hierarchy.
14709 */
14710 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014711
14712 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014713 * Temporary for use in computing hit areas with transformed views
14714 */
14715 final RectF mTmpTransformRect = new RectF();
14716
14717 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014718 * Temporary list for use in collecting focusable descendents of a view.
14719 */
14720 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014722 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014723 * The id of the window for accessibility purposes.
14724 */
14725 int mAccessibilityWindowId = View.NO_ID;
14726
14727 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014728 * Creates a new set of attachment information with the specified
14729 * events handler and thread.
14730 *
14731 * @param handler the events handler the view must use
14732 */
14733 AttachInfo(IWindowSession session, IWindow window,
14734 Handler handler, Callbacks effectPlayer) {
14735 mSession = session;
14736 mWindow = window;
14737 mWindowToken = window.asBinder();
14738 mHandler = handler;
14739 mRootCallbacks = effectPlayer;
14740 }
14741 }
14742
14743 /**
14744 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14745 * is supported. This avoids keeping too many unused fields in most
14746 * instances of View.</p>
14747 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014748 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014749
Mike Cleronf116bf82009-09-27 19:14:12 -070014750 /**
14751 * Scrollbars are not visible
14752 */
14753 public static final int OFF = 0;
14754
14755 /**
14756 * Scrollbars are visible
14757 */
14758 public static final int ON = 1;
14759
14760 /**
14761 * Scrollbars are fading away
14762 */
14763 public static final int FADING = 2;
14764
14765 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014767 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014768 public int scrollBarDefaultDelayBeforeFade;
14769 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014770
14771 public int scrollBarSize;
14772 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014773 public float[] interpolatorValues;
14774 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014775
14776 public final Paint paint;
14777 public final Matrix matrix;
14778 public Shader shader;
14779
Mike Cleronf116bf82009-09-27 19:14:12 -070014780 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14781
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014782 private static final float[] OPAQUE = { 255 };
14783 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014784
Mike Cleronf116bf82009-09-27 19:14:12 -070014785 /**
14786 * When fading should start. This time moves into the future every time
14787 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14788 */
14789 public long fadeStartTime;
14790
14791
14792 /**
14793 * The current state of the scrollbars: ON, OFF, or FADING
14794 */
14795 public int state = OFF;
14796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014797 private int mLastColor;
14798
Mike Cleronf116bf82009-09-27 19:14:12 -070014799 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014800 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14801 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014802 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14803 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014804
14805 paint = new Paint();
14806 matrix = new Matrix();
14807 // use use a height of 1, and then wack the matrix each time we
14808 // actually use it.
14809 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014811 paint.setShader(shader);
14812 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014813 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014814 }
Romain Guy8506ab42009-06-11 17:35:47 -070014815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014816 public void setFadeColor(int color) {
14817 if (color != 0 && color != mLastColor) {
14818 mLastColor = color;
14819 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014820
Romain Guye55e1a72009-08-27 10:42:26 -070014821 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14822 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014824 paint.setShader(shader);
14825 // Restore the default transfer mode (src_over)
14826 paint.setXfermode(null);
14827 }
14828 }
Joe Malin32736f02011-01-19 16:14:20 -080014829
Mike Cleronf116bf82009-09-27 19:14:12 -070014830 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014831 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014832 if (now >= fadeStartTime) {
14833
14834 // the animation fades the scrollbars out by changing
14835 // the opacity (alpha) from fully opaque to fully
14836 // transparent
14837 int nextFrame = (int) now;
14838 int framesCount = 0;
14839
14840 Interpolator interpolator = scrollBarInterpolator;
14841
14842 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014843 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014844
14845 // End transparent
14846 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014847 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014848
14849 state = FADING;
14850
14851 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014852 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014853 }
14854 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014855 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014856
Svetoslav Ganova0156172011-06-26 17:55:44 -070014857 /**
14858 * Resuable callback for sending
14859 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14860 */
14861 private class SendViewScrolledAccessibilityEvent implements Runnable {
14862 public volatile boolean mIsPending;
14863
14864 public void run() {
14865 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14866 mIsPending = false;
14867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014868 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014869
14870 /**
14871 * <p>
14872 * This class represents a delegate that can be registered in a {@link View}
14873 * to enhance accessibility support via composition rather via inheritance.
14874 * It is specifically targeted to widget developers that extend basic View
14875 * classes i.e. classes in package android.view, that would like their
14876 * applications to be backwards compatible.
14877 * </p>
14878 * <p>
14879 * A scenario in which a developer would like to use an accessibility delegate
14880 * is overriding a method introduced in a later API version then the minimal API
14881 * version supported by the application. For example, the method
14882 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14883 * in API version 4 when the accessibility APIs were first introduced. If a
14884 * developer would like his application to run on API version 4 devices (assuming
14885 * all other APIs used by the application are version 4 or lower) and take advantage
14886 * of this method, instead of overriding the method which would break the application's
14887 * backwards compatibility, he can override the corresponding method in this
14888 * delegate and register the delegate in the target View if the API version of
14889 * the system is high enough i.e. the API version is same or higher to the API
14890 * version that introduced
14891 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14892 * </p>
14893 * <p>
14894 * Here is an example implementation:
14895 * </p>
14896 * <code><pre><p>
14897 * if (Build.VERSION.SDK_INT >= 14) {
14898 * // If the API version is equal of higher than the version in
14899 * // which onInitializeAccessibilityNodeInfo was introduced we
14900 * // register a delegate with a customized implementation.
14901 * View view = findViewById(R.id.view_id);
14902 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14903 * public void onInitializeAccessibilityNodeInfo(View host,
14904 * AccessibilityNodeInfo info) {
14905 * // Let the default implementation populate the info.
14906 * super.onInitializeAccessibilityNodeInfo(host, info);
14907 * // Set some other information.
14908 * info.setEnabled(host.isEnabled());
14909 * }
14910 * });
14911 * }
14912 * </code></pre></p>
14913 * <p>
14914 * This delegate contains methods that correspond to the accessibility methods
14915 * in View. If a delegate has been specified the implementation in View hands
14916 * off handling to the corresponding method in this delegate. The default
14917 * implementation the delegate methods behaves exactly as the corresponding
14918 * method in View for the case of no accessibility delegate been set. Hence,
14919 * to customize the behavior of a View method, clients can override only the
14920 * corresponding delegate method without altering the behavior of the rest
14921 * accessibility related methods of the host view.
14922 * </p>
14923 */
14924 public static class AccessibilityDelegate {
14925
14926 /**
14927 * Sends an accessibility event of the given type. If accessibility is not
14928 * enabled this method has no effect.
14929 * <p>
14930 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14931 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14932 * been set.
14933 * </p>
14934 *
14935 * @param host The View hosting the delegate.
14936 * @param eventType The type of the event to send.
14937 *
14938 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14939 */
14940 public void sendAccessibilityEvent(View host, int eventType) {
14941 host.sendAccessibilityEventInternal(eventType);
14942 }
14943
14944 /**
14945 * Sends an accessibility event. This method behaves exactly as
14946 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14947 * empty {@link AccessibilityEvent} and does not perform a check whether
14948 * accessibility is enabled.
14949 * <p>
14950 * The default implementation behaves as
14951 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14952 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14953 * the case of no accessibility delegate been set.
14954 * </p>
14955 *
14956 * @param host The View hosting the delegate.
14957 * @param event The event to send.
14958 *
14959 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14960 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14961 */
14962 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14963 host.sendAccessibilityEventUncheckedInternal(event);
14964 }
14965
14966 /**
14967 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14968 * to its children for adding their text content to the event.
14969 * <p>
14970 * The default implementation behaves as
14971 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14972 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14973 * the case of no accessibility delegate been set.
14974 * </p>
14975 *
14976 * @param host The View hosting the delegate.
14977 * @param event The event.
14978 * @return True if the event population was completed.
14979 *
14980 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14981 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14982 */
14983 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14984 return host.dispatchPopulateAccessibilityEventInternal(event);
14985 }
14986
14987 /**
14988 * Gives a chance to the host View to populate the accessibility event with its
14989 * text content.
14990 * <p>
14991 * The default implementation behaves as
14992 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14993 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14994 * the case of no accessibility delegate been set.
14995 * </p>
14996 *
14997 * @param host The View hosting the delegate.
14998 * @param event The accessibility event which to populate.
14999 *
15000 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15001 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15002 */
15003 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15004 host.onPopulateAccessibilityEventInternal(event);
15005 }
15006
15007 /**
15008 * Initializes an {@link AccessibilityEvent} with information about the
15009 * the host View which is the event source.
15010 * <p>
15011 * The default implementation behaves as
15012 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15013 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15014 * the case of no accessibility delegate been set.
15015 * </p>
15016 *
15017 * @param host The View hosting the delegate.
15018 * @param event The event to initialize.
15019 *
15020 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15021 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15022 */
15023 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15024 host.onInitializeAccessibilityEventInternal(event);
15025 }
15026
15027 /**
15028 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15029 * <p>
15030 * The default implementation behaves as
15031 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15032 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15033 * the case of no accessibility delegate been set.
15034 * </p>
15035 *
15036 * @param host The View hosting the delegate.
15037 * @param info The instance to initialize.
15038 *
15039 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15040 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15041 */
15042 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15043 host.onInitializeAccessibilityNodeInfoInternal(info);
15044 }
15045
15046 /**
15047 * Called when a child of the host View has requested sending an
15048 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15049 * to augment the event.
15050 * <p>
15051 * The default implementation behaves as
15052 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15053 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15054 * the case of no accessibility delegate been set.
15055 * </p>
15056 *
15057 * @param host The View hosting the delegate.
15058 * @param child The child which requests sending the event.
15059 * @param event The event to be sent.
15060 * @return True if the event should be sent
15061 *
15062 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15063 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15064 */
15065 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15066 AccessibilityEvent event) {
15067 return host.onRequestSendAccessibilityEventInternal(child, event);
15068 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015069
15070 /**
15071 * Gets the provider for managing a virtual view hierarchy rooted at this View
15072 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15073 * that explore the window content.
15074 * <p>
15075 * The default implementation behaves as
15076 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15077 * the case of no accessibility delegate been set.
15078 * </p>
15079 *
15080 * @return The provider.
15081 *
15082 * @see AccessibilityNodeProvider
15083 */
15084 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15085 return null;
15086 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015088}