blob: b972abd52b8b3351caa1f2c57d6f3a5471afac13 [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 /**
Chet Haasea33de552012-02-03 16:28:24 -08002169 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002170 */
2171 @ViewDebug.ExportedProperty
2172 float mPivotX = 0f;
2173
2174 /**
Chet Haasea33de552012-02-03 16:28:24 -08002175 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002176 */
2177 @ViewDebug.ExportedProperty
2178 float mPivotY = 0f;
2179
2180 /**
2181 * The opacity of the View. This is a value from 0 to 1, where 0 means
2182 * completely transparent and 1 means completely opaque.
2183 */
2184 @ViewDebug.ExportedProperty
2185 float mAlpha = 1f;
2186 }
2187
2188 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002189
Joe Malin32736f02011-01-19 16:14:20 -08002190 private boolean mLastIsOpaque;
2191
Chet Haasefd2b0022010-08-06 13:08:56 -07002192 /**
2193 * Convenience value to check for float values that are close enough to zero to be considered
2194 * zero.
2195 */
Romain Guy2542d192010-08-18 11:47:12 -07002196 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002197
2198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 * The distance in pixels from the left edge of this view's parent
2200 * to the left edge of this view.
2201 * {@hide}
2202 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002203 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 protected int mLeft;
2205 /**
2206 * The distance in pixels from the left edge of this view's parent
2207 * to the right edge of this view.
2208 * {@hide}
2209 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002210 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 protected int mRight;
2212 /**
2213 * The distance in pixels from the top edge of this view's parent
2214 * to the top edge of this view.
2215 * {@hide}
2216 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002217 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 protected int mTop;
2219 /**
2220 * The distance in pixels from the top edge of this view's parent
2221 * to the bottom edge of this view.
2222 * {@hide}
2223 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002224 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 protected int mBottom;
2226
2227 /**
2228 * The offset, in pixels, by which the content of this view is scrolled
2229 * horizontally.
2230 * {@hide}
2231 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002232 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 protected int mScrollX;
2234 /**
2235 * The offset, in pixels, by which the content of this view is scrolled
2236 * vertically.
2237 * {@hide}
2238 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002239 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 protected int mScrollY;
2241
2242 /**
2243 * The left padding in pixels, that is the distance in pixels between the
2244 * left edge of this view and the left edge of its content.
2245 * {@hide}
2246 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002247 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 protected int mPaddingLeft;
2249 /**
2250 * The right padding in pixels, that is the distance in pixels between the
2251 * right edge of this view and the right edge of its content.
2252 * {@hide}
2253 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002254 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 protected int mPaddingRight;
2256 /**
2257 * The top padding in pixels, that is the distance in pixels between the
2258 * top edge of this view and the top edge of its content.
2259 * {@hide}
2260 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002261 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 protected int mPaddingTop;
2263 /**
2264 * The bottom padding in pixels, that is the distance in pixels between the
2265 * bottom edge of this view and the bottom edge of its content.
2266 * {@hide}
2267 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002268 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 protected int mPaddingBottom;
2270
2271 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002272 * Briefly describes the view and is primarily used for accessibility support.
2273 */
2274 private CharSequence mContentDescription;
2275
2276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002278 *
2279 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002281 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002282 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283
2284 /**
2285 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002286 *
2287 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002289 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002290 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002292 /**
Adam Powell20232d02010-12-08 21:08:53 -08002293 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002294 *
2295 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002296 */
2297 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002298 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002299
2300 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002301 * Cache if the user padding is relative.
2302 *
2303 */
2304 @ViewDebug.ExportedProperty(category = "padding")
2305 boolean mUserPaddingRelative;
2306
2307 /**
2308 * Cache the paddingStart set by the user to append to the scrollbar's size.
2309 *
2310 */
2311 @ViewDebug.ExportedProperty(category = "padding")
2312 int mUserPaddingStart;
2313
2314 /**
2315 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2316 *
2317 */
2318 @ViewDebug.ExportedProperty(category = "padding")
2319 int mUserPaddingEnd;
2320
2321 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002322 * @hide
2323 */
2324 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2325 /**
2326 * @hide
2327 */
2328 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 private Drawable mBGDrawable;
2331
2332 private int mBackgroundResource;
2333 private boolean mBackgroundSizeChanged;
2334
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002335 static class ListenerInfo {
2336 /**
2337 * Listener used to dispatch focus change events.
2338 * This field should be made private, so it is hidden from the SDK.
2339 * {@hide}
2340 */
2341 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002343 /**
2344 * Listeners for layout change events.
2345 */
2346 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002347
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002348 /**
2349 * Listeners for attach events.
2350 */
2351 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002352
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002353 /**
2354 * Listener used to dispatch click events.
2355 * This field should be made private, so it is hidden from the SDK.
2356 * {@hide}
2357 */
2358 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002360 /**
2361 * Listener used to dispatch long click events.
2362 * This field should be made private, so it is hidden from the SDK.
2363 * {@hide}
2364 */
2365 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002367 /**
2368 * Listener used to build the context menu.
2369 * This field should be made private, so it is hidden from the SDK.
2370 * {@hide}
2371 */
2372 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002374 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002376 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002378 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002379
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002380 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002381
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002382 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002383
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002384 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2385 }
2386
2387 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 /**
2390 * The application environment this view lives in.
2391 * This field should be made private, so it is hidden from the SDK.
2392 * {@hide}
2393 */
2394 protected Context mContext;
2395
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002396 private final Resources mResources;
2397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 private ScrollabilityCache mScrollCache;
2399
2400 private int[] mDrawableState = null;
2401
Romain Guy0211a0a2011-02-14 16:34:59 -08002402 /**
2403 * Set to true when drawing cache is enabled and cannot be created.
2404 *
2405 * @hide
2406 */
2407 public boolean mCachingFailed;
2408
Romain Guy02890fd2010-08-06 17:58:44 -07002409 private Bitmap mDrawingCache;
2410 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002411 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002412 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413
2414 /**
2415 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2416 * the user may specify which view to go to next.
2417 */
2418 private int mNextFocusLeftId = View.NO_ID;
2419
2420 /**
2421 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2422 * the user may specify which view to go to next.
2423 */
2424 private int mNextFocusRightId = View.NO_ID;
2425
2426 /**
2427 * When this view has focus and the next focus is {@link #FOCUS_UP},
2428 * the user may specify which view to go to next.
2429 */
2430 private int mNextFocusUpId = View.NO_ID;
2431
2432 /**
2433 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2434 * the user may specify which view to go to next.
2435 */
2436 private int mNextFocusDownId = View.NO_ID;
2437
Jeff Brown4e6319b2010-12-13 10:36:51 -08002438 /**
2439 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2440 * the user may specify which view to go to next.
2441 */
2442 int mNextFocusForwardId = View.NO_ID;
2443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002445 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002446 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002447 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 private UnsetPressedState mUnsetPressedState;
2450
2451 /**
2452 * Whether the long press's action has been invoked. The tap's action is invoked on the
2453 * up event while a long press is invoked as soon as the long press duration is reached, so
2454 * a long press could be performed before the tap is checked, in which case the tap's action
2455 * should not be invoked.
2456 */
2457 private boolean mHasPerformedLongPress;
2458
2459 /**
2460 * The minimum height of the view. We'll try our best to have the height
2461 * of this view to at least this amount.
2462 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002463 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 private int mMinHeight;
2465
2466 /**
2467 * The minimum width of the view. We'll try our best to have the width
2468 * of this view to at least this amount.
2469 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002470 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 private int mMinWidth;
2472
2473 /**
2474 * The delegate to handle touch events that are physically in this view
2475 * but should be handled by another view.
2476 */
2477 private TouchDelegate mTouchDelegate = null;
2478
2479 /**
2480 * Solid color to use as a background when creating the drawing cache. Enables
2481 * the cache to use 16 bit bitmaps instead of 32 bit.
2482 */
2483 private int mDrawingCacheBackgroundColor = 0;
2484
2485 /**
2486 * Special tree observer used when mAttachInfo is null.
2487 */
2488 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002489
Adam Powelle14579b2009-12-16 18:39:52 -08002490 /**
2491 * Cache the touch slop from the context that created the view.
2492 */
2493 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002496 * Object that handles automatic animation of view properties.
2497 */
2498 private ViewPropertyAnimator mAnimator = null;
2499
2500 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002501 * Flag indicating that a drag can cross window boundaries. When
2502 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2503 * with this flag set, all visible applications will be able to participate
2504 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002505 *
2506 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002507 */
2508 public static final int DRAG_FLAG_GLOBAL = 1;
2509
2510 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002511 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2512 */
2513 private float mVerticalScrollFactor;
2514
2515 /**
Adam Powell20232d02010-12-08 21:08:53 -08002516 * Position of the vertical scroll bar.
2517 */
2518 private int mVerticalScrollbarPosition;
2519
2520 /**
2521 * Position the scroll bar at the default position as determined by the system.
2522 */
2523 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2524
2525 /**
2526 * Position the scroll bar along the left edge.
2527 */
2528 public static final int SCROLLBAR_POSITION_LEFT = 1;
2529
2530 /**
2531 * Position the scroll bar along the right edge.
2532 */
2533 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2534
2535 /**
Romain Guy171c5922011-01-06 10:04:23 -08002536 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002537 *
2538 * @see #getLayerType()
2539 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002540 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002541 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002542 */
2543 public static final int LAYER_TYPE_NONE = 0;
2544
2545 /**
2546 * <p>Indicates that the view has a software layer. A software layer is backed
2547 * by a bitmap and causes the view to be rendered using Android's software
2548 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002549 *
Romain Guy171c5922011-01-06 10:04:23 -08002550 * <p>Software layers have various usages:</p>
2551 * <p>When the application is not using hardware acceleration, a software layer
2552 * is useful to apply a specific color filter and/or blending mode and/or
2553 * translucency to a view and all its children.</p>
2554 * <p>When the application is using hardware acceleration, a software layer
2555 * is useful to render drawing primitives not supported by the hardware
2556 * accelerated pipeline. It can also be used to cache a complex view tree
2557 * into a texture and reduce the complexity of drawing operations. For instance,
2558 * when animating a complex view tree with a translation, a software layer can
2559 * be used to render the view tree only once.</p>
2560 * <p>Software layers should be avoided when the affected view tree updates
2561 * often. Every update will require to re-render the software layer, which can
2562 * potentially be slow (particularly when hardware acceleration is turned on
2563 * since the layer will have to be uploaded into a hardware texture after every
2564 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002565 *
2566 * @see #getLayerType()
2567 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002568 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002569 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002570 */
2571 public static final int LAYER_TYPE_SOFTWARE = 1;
2572
2573 /**
2574 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2575 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2576 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2577 * rendering pipeline, but only if hardware acceleration is turned on for the
2578 * view hierarchy. When hardware acceleration is turned off, hardware layers
2579 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002580 *
Romain Guy171c5922011-01-06 10:04:23 -08002581 * <p>A hardware layer is useful to apply a specific color filter and/or
2582 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002583 * <p>A hardware layer can be used to cache a complex view tree into a
2584 * texture and reduce the complexity of drawing operations. For instance,
2585 * when animating a complex view tree with a translation, a hardware layer can
2586 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002587 * <p>A hardware layer can also be used to increase the rendering quality when
2588 * rotation transformations are applied on a view. It can also be used to
2589 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002590 *
2591 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002592 * @see #setLayerType(int, android.graphics.Paint)
2593 * @see #LAYER_TYPE_NONE
2594 * @see #LAYER_TYPE_SOFTWARE
2595 */
2596 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002597
Romain Guy3aaff3a2011-01-12 14:18:47 -08002598 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2599 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2600 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2601 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2602 })
Romain Guy171c5922011-01-06 10:04:23 -08002603 int mLayerType = LAYER_TYPE_NONE;
2604 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002605 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002606
2607 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002608 * Set to true when the view is sending hover accessibility events because it
2609 * is the innermost hovered view.
2610 */
2611 private boolean mSendingHoverAccessibilityEvents;
2612
2613 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002614 * Delegate for injecting accessiblity functionality.
2615 */
2616 AccessibilityDelegate mAccessibilityDelegate;
2617
2618 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002619 * Text direction is inherited thru {@link ViewGroup}
2620 * @hide
2621 */
2622 public static final int TEXT_DIRECTION_INHERIT = 0;
2623
2624 /**
2625 * Text direction is using "first strong algorithm". The first strong directional character
2626 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002627 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002628 *
2629 * @hide
2630 */
2631 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2632
2633 /**
2634 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2635 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002636 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002637 *
2638 * @hide
2639 */
2640 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2641
2642 /**
2643 * Text direction is forced to LTR.
2644 *
2645 * @hide
2646 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002647 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002648
2649 /**
2650 * Text direction is forced to RTL.
2651 *
2652 * @hide
2653 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002654 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002655
2656 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002657 * Text direction is coming from the system Locale.
2658 *
2659 * @hide
2660 */
2661 public static final int TEXT_DIRECTION_LOCALE = 5;
2662
2663 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002664 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002665 *
2666 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002667 */
2668 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2669
2670 /**
2671 * The text direction that has been defined by {@link #setTextDirection(int)}.
2672 *
2673 * {@hide}
2674 */
2675 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002676 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2677 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2678 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2679 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002680 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2681 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002682 })
Doug Feltcb3791202011-07-07 11:57:48 -07002683 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002684
2685 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002686 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002687 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002688 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2689 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002690 *
2691 * {@hide}
2692 */
2693 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002694 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2695 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2696 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002697 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002698 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2699 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002700 })
Doug Feltcb3791202011-07-07 11:57:48 -07002701 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002702
2703 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002704 * Consistency verifier for debugging purposes.
2705 * @hide
2706 */
2707 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2708 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2709 new InputEventConsistencyVerifier(this, 0) : null;
2710
2711 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 * Simple constructor to use when creating a view from code.
2713 *
2714 * @param context The Context the view is running in, through which it can
2715 * access the current theme, resources, etc.
2716 */
2717 public View(Context context) {
2718 mContext = context;
2719 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002720 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002721 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002722 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002723 mUserPaddingStart = -1;
2724 mUserPaddingEnd = -1;
2725 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 }
2727
2728 /**
2729 * Constructor that is called when inflating a view from XML. This is called
2730 * when a view is being constructed from an XML file, supplying attributes
2731 * that were specified in the XML file. This version uses a default style of
2732 * 0, so the only attribute values applied are those in the Context's Theme
2733 * and the given AttributeSet.
2734 *
2735 * <p>
2736 * The method onFinishInflate() will be called after all children have been
2737 * added.
2738 *
2739 * @param context The Context the view is running in, through which it can
2740 * access the current theme, resources, etc.
2741 * @param attrs The attributes of the XML tag that is inflating the view.
2742 * @see #View(Context, AttributeSet, int)
2743 */
2744 public View(Context context, AttributeSet attrs) {
2745 this(context, attrs, 0);
2746 }
2747
2748 /**
2749 * Perform inflation from XML and apply a class-specific base style. This
2750 * constructor of View allows subclasses to use their own base style when
2751 * they are inflating. For example, a Button class's constructor would call
2752 * this version of the super class constructor and supply
2753 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2754 * the theme's button style to modify all of the base view attributes (in
2755 * particular its background) as well as the Button class's attributes.
2756 *
2757 * @param context The Context the view is running in, through which it can
2758 * access the current theme, resources, etc.
2759 * @param attrs The attributes of the XML tag that is inflating the view.
2760 * @param defStyle The default style to apply to this view. If 0, no style
2761 * will be applied (beyond what is included in the theme). This may
2762 * either be an attribute resource, whose value will be retrieved
2763 * from the current theme, or an explicit style resource.
2764 * @see #View(Context, AttributeSet)
2765 */
2766 public View(Context context, AttributeSet attrs, int defStyle) {
2767 this(context);
2768
2769 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2770 defStyle, 0);
2771
2772 Drawable background = null;
2773
2774 int leftPadding = -1;
2775 int topPadding = -1;
2776 int rightPadding = -1;
2777 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002778 int startPadding = -1;
2779 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780
2781 int padding = -1;
2782
2783 int viewFlagValues = 0;
2784 int viewFlagMasks = 0;
2785
2786 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 int x = 0;
2789 int y = 0;
2790
Chet Haase73066682010-11-29 15:55:32 -08002791 float tx = 0;
2792 float ty = 0;
2793 float rotation = 0;
2794 float rotationX = 0;
2795 float rotationY = 0;
2796 float sx = 1f;
2797 float sy = 1f;
2798 boolean transformSet = false;
2799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2801
Adam Powell637d3372010-08-25 14:37:03 -07002802 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 final int N = a.getIndexCount();
2804 for (int i = 0; i < N; i++) {
2805 int attr = a.getIndex(i);
2806 switch (attr) {
2807 case com.android.internal.R.styleable.View_background:
2808 background = a.getDrawable(attr);
2809 break;
2810 case com.android.internal.R.styleable.View_padding:
2811 padding = a.getDimensionPixelSize(attr, -1);
2812 break;
2813 case com.android.internal.R.styleable.View_paddingLeft:
2814 leftPadding = a.getDimensionPixelSize(attr, -1);
2815 break;
2816 case com.android.internal.R.styleable.View_paddingTop:
2817 topPadding = a.getDimensionPixelSize(attr, -1);
2818 break;
2819 case com.android.internal.R.styleable.View_paddingRight:
2820 rightPadding = a.getDimensionPixelSize(attr, -1);
2821 break;
2822 case com.android.internal.R.styleable.View_paddingBottom:
2823 bottomPadding = a.getDimensionPixelSize(attr, -1);
2824 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002825 case com.android.internal.R.styleable.View_paddingStart:
2826 startPadding = a.getDimensionPixelSize(attr, -1);
2827 break;
2828 case com.android.internal.R.styleable.View_paddingEnd:
2829 endPadding = a.getDimensionPixelSize(attr, -1);
2830 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 case com.android.internal.R.styleable.View_scrollX:
2832 x = a.getDimensionPixelOffset(attr, 0);
2833 break;
2834 case com.android.internal.R.styleable.View_scrollY:
2835 y = a.getDimensionPixelOffset(attr, 0);
2836 break;
Chet Haase73066682010-11-29 15:55:32 -08002837 case com.android.internal.R.styleable.View_alpha:
2838 setAlpha(a.getFloat(attr, 1f));
2839 break;
2840 case com.android.internal.R.styleable.View_transformPivotX:
2841 setPivotX(a.getDimensionPixelOffset(attr, 0));
2842 break;
2843 case com.android.internal.R.styleable.View_transformPivotY:
2844 setPivotY(a.getDimensionPixelOffset(attr, 0));
2845 break;
2846 case com.android.internal.R.styleable.View_translationX:
2847 tx = a.getDimensionPixelOffset(attr, 0);
2848 transformSet = true;
2849 break;
2850 case com.android.internal.R.styleable.View_translationY:
2851 ty = a.getDimensionPixelOffset(attr, 0);
2852 transformSet = true;
2853 break;
2854 case com.android.internal.R.styleable.View_rotation:
2855 rotation = a.getFloat(attr, 0);
2856 transformSet = true;
2857 break;
2858 case com.android.internal.R.styleable.View_rotationX:
2859 rotationX = a.getFloat(attr, 0);
2860 transformSet = true;
2861 break;
2862 case com.android.internal.R.styleable.View_rotationY:
2863 rotationY = a.getFloat(attr, 0);
2864 transformSet = true;
2865 break;
2866 case com.android.internal.R.styleable.View_scaleX:
2867 sx = a.getFloat(attr, 1f);
2868 transformSet = true;
2869 break;
2870 case com.android.internal.R.styleable.View_scaleY:
2871 sy = a.getFloat(attr, 1f);
2872 transformSet = true;
2873 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 case com.android.internal.R.styleable.View_id:
2875 mID = a.getResourceId(attr, NO_ID);
2876 break;
2877 case com.android.internal.R.styleable.View_tag:
2878 mTag = a.getText(attr);
2879 break;
2880 case com.android.internal.R.styleable.View_fitsSystemWindows:
2881 if (a.getBoolean(attr, false)) {
2882 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2883 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2884 }
2885 break;
2886 case com.android.internal.R.styleable.View_focusable:
2887 if (a.getBoolean(attr, false)) {
2888 viewFlagValues |= FOCUSABLE;
2889 viewFlagMasks |= FOCUSABLE_MASK;
2890 }
2891 break;
2892 case com.android.internal.R.styleable.View_focusableInTouchMode:
2893 if (a.getBoolean(attr, false)) {
2894 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2895 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2896 }
2897 break;
2898 case com.android.internal.R.styleable.View_clickable:
2899 if (a.getBoolean(attr, false)) {
2900 viewFlagValues |= CLICKABLE;
2901 viewFlagMasks |= CLICKABLE;
2902 }
2903 break;
2904 case com.android.internal.R.styleable.View_longClickable:
2905 if (a.getBoolean(attr, false)) {
2906 viewFlagValues |= LONG_CLICKABLE;
2907 viewFlagMasks |= LONG_CLICKABLE;
2908 }
2909 break;
2910 case com.android.internal.R.styleable.View_saveEnabled:
2911 if (!a.getBoolean(attr, true)) {
2912 viewFlagValues |= SAVE_DISABLED;
2913 viewFlagMasks |= SAVE_DISABLED_MASK;
2914 }
2915 break;
2916 case com.android.internal.R.styleable.View_duplicateParentState:
2917 if (a.getBoolean(attr, false)) {
2918 viewFlagValues |= DUPLICATE_PARENT_STATE;
2919 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2920 }
2921 break;
2922 case com.android.internal.R.styleable.View_visibility:
2923 final int visibility = a.getInt(attr, 0);
2924 if (visibility != 0) {
2925 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2926 viewFlagMasks |= VISIBILITY_MASK;
2927 }
2928 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002929 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002930 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002931 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002932 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002933 final int layoutDirection = a.getInt(attr, -1);
2934 if (layoutDirection != -1) {
2935 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002936 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002937 // Set to default (LAYOUT_DIRECTION_INHERIT)
2938 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002939 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002940 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002941 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 case com.android.internal.R.styleable.View_drawingCacheQuality:
2943 final int cacheQuality = a.getInt(attr, 0);
2944 if (cacheQuality != 0) {
2945 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2946 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2947 }
2948 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002949 case com.android.internal.R.styleable.View_contentDescription:
2950 mContentDescription = a.getString(attr);
2951 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2953 if (!a.getBoolean(attr, true)) {
2954 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2955 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2956 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002957 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2959 if (!a.getBoolean(attr, true)) {
2960 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2961 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2962 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002963 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 case R.styleable.View_scrollbars:
2965 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2966 if (scrollbars != SCROLLBARS_NONE) {
2967 viewFlagValues |= scrollbars;
2968 viewFlagMasks |= SCROLLBARS_MASK;
2969 initializeScrollbars(a);
2970 }
2971 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002972 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002974 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2975 // Ignore the attribute starting with ICS
2976 break;
2977 }
2978 // With builds < ICS, fall through and apply fading edges
2979 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2981 if (fadingEdge != FADING_EDGE_NONE) {
2982 viewFlagValues |= fadingEdge;
2983 viewFlagMasks |= FADING_EDGE_MASK;
2984 initializeFadingEdge(a);
2985 }
2986 break;
2987 case R.styleable.View_scrollbarStyle:
2988 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2989 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2990 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2991 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2992 }
2993 break;
2994 case R.styleable.View_isScrollContainer:
2995 setScrollContainer = true;
2996 if (a.getBoolean(attr, false)) {
2997 setScrollContainer(true);
2998 }
2999 break;
3000 case com.android.internal.R.styleable.View_keepScreenOn:
3001 if (a.getBoolean(attr, false)) {
3002 viewFlagValues |= KEEP_SCREEN_ON;
3003 viewFlagMasks |= KEEP_SCREEN_ON;
3004 }
3005 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003006 case R.styleable.View_filterTouchesWhenObscured:
3007 if (a.getBoolean(attr, false)) {
3008 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3009 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3010 }
3011 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 case R.styleable.View_nextFocusLeft:
3013 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3014 break;
3015 case R.styleable.View_nextFocusRight:
3016 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3017 break;
3018 case R.styleable.View_nextFocusUp:
3019 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3020 break;
3021 case R.styleable.View_nextFocusDown:
3022 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3023 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003024 case R.styleable.View_nextFocusForward:
3025 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3026 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 case R.styleable.View_minWidth:
3028 mMinWidth = a.getDimensionPixelSize(attr, 0);
3029 break;
3030 case R.styleable.View_minHeight:
3031 mMinHeight = a.getDimensionPixelSize(attr, 0);
3032 break;
Romain Guy9a817362009-05-01 10:57:14 -07003033 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003034 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003035 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003036 + "be used within a restricted context");
3037 }
3038
Romain Guy9a817362009-05-01 10:57:14 -07003039 final String handlerName = a.getString(attr);
3040 if (handlerName != null) {
3041 setOnClickListener(new OnClickListener() {
3042 private Method mHandler;
3043
3044 public void onClick(View v) {
3045 if (mHandler == null) {
3046 try {
3047 mHandler = getContext().getClass().getMethod(handlerName,
3048 View.class);
3049 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003050 int id = getId();
3051 String idText = id == NO_ID ? "" : " with id '"
3052 + getContext().getResources().getResourceEntryName(
3053 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003054 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003055 handlerName + "(View) in the activity "
3056 + getContext().getClass() + " for onClick handler"
3057 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003058 }
3059 }
3060
3061 try {
3062 mHandler.invoke(getContext(), View.this);
3063 } catch (IllegalAccessException e) {
3064 throw new IllegalStateException("Could not execute non "
3065 + "public method of the activity", e);
3066 } catch (InvocationTargetException e) {
3067 throw new IllegalStateException("Could not execute "
3068 + "method of the activity", e);
3069 }
3070 }
3071 });
3072 }
3073 break;
Adam Powell637d3372010-08-25 14:37:03 -07003074 case R.styleable.View_overScrollMode:
3075 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3076 break;
Adam Powell20232d02010-12-08 21:08:53 -08003077 case R.styleable.View_verticalScrollbarPosition:
3078 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3079 break;
Romain Guy171c5922011-01-06 10:04:23 -08003080 case R.styleable.View_layerType:
3081 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3082 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003083 case R.styleable.View_textDirection:
3084 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3085 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 }
3087 }
3088
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003089 a.recycle();
3090
Adam Powell637d3372010-08-25 14:37:03 -07003091 setOverScrollMode(overScrollMode);
3092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 if (background != null) {
3094 setBackgroundDrawable(background);
3095 }
3096
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003097 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3098
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003099 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3100 // layout direction). Those cached values will be used later during padding resolution.
3101 mUserPaddingStart = startPadding;
3102 mUserPaddingEnd = endPadding;
3103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 if (padding >= 0) {
3105 leftPadding = padding;
3106 topPadding = padding;
3107 rightPadding = padding;
3108 bottomPadding = padding;
3109 }
3110
3111 // If the user specified the padding (either with android:padding or
3112 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3113 // use the default padding or the padding from the background drawable
3114 // (stored at this point in mPadding*)
3115 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3116 topPadding >= 0 ? topPadding : mPaddingTop,
3117 rightPadding >= 0 ? rightPadding : mPaddingRight,
3118 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3119
3120 if (viewFlagMasks != 0) {
3121 setFlags(viewFlagValues, viewFlagMasks);
3122 }
3123
3124 // Needs to be called after mViewFlags is set
3125 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3126 recomputePadding();
3127 }
3128
3129 if (x != 0 || y != 0) {
3130 scrollTo(x, y);
3131 }
3132
Chet Haase73066682010-11-29 15:55:32 -08003133 if (transformSet) {
3134 setTranslationX(tx);
3135 setTranslationY(ty);
3136 setRotation(rotation);
3137 setRotationX(rotationX);
3138 setRotationY(rotationY);
3139 setScaleX(sx);
3140 setScaleY(sy);
3141 }
3142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3144 setScrollContainer(true);
3145 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003146
3147 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 }
3149
3150 /**
3151 * Non-public constructor for use in testing
3152 */
3153 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003154 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 }
3156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 /**
3158 * <p>
3159 * Initializes the fading edges from a given set of styled attributes. This
3160 * method should be called by subclasses that need fading edges and when an
3161 * instance of these subclasses is created programmatically rather than
3162 * being inflated from XML. This method is automatically called when the XML
3163 * is inflated.
3164 * </p>
3165 *
3166 * @param a the styled attributes set to initialize the fading edges from
3167 */
3168 protected void initializeFadingEdge(TypedArray a) {
3169 initScrollCache();
3170
3171 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3172 R.styleable.View_fadingEdgeLength,
3173 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3174 }
3175
3176 /**
3177 * Returns the size of the vertical faded edges used to indicate that more
3178 * content in this view is visible.
3179 *
3180 * @return The size in pixels of the vertical faded edge or 0 if vertical
3181 * faded edges are not enabled for this view.
3182 * @attr ref android.R.styleable#View_fadingEdgeLength
3183 */
3184 public int getVerticalFadingEdgeLength() {
3185 if (isVerticalFadingEdgeEnabled()) {
3186 ScrollabilityCache cache = mScrollCache;
3187 if (cache != null) {
3188 return cache.fadingEdgeLength;
3189 }
3190 }
3191 return 0;
3192 }
3193
3194 /**
3195 * Set the size of the faded edge used to indicate that more content in this
3196 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003197 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3198 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3199 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 *
3201 * @param length The size in pixels of the faded edge used to indicate that more
3202 * content in this view is visible.
3203 */
3204 public void setFadingEdgeLength(int length) {
3205 initScrollCache();
3206 mScrollCache.fadingEdgeLength = length;
3207 }
3208
3209 /**
3210 * Returns the size of the horizontal faded edges used to indicate that more
3211 * content in this view is visible.
3212 *
3213 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3214 * faded edges are not enabled for this view.
3215 * @attr ref android.R.styleable#View_fadingEdgeLength
3216 */
3217 public int getHorizontalFadingEdgeLength() {
3218 if (isHorizontalFadingEdgeEnabled()) {
3219 ScrollabilityCache cache = mScrollCache;
3220 if (cache != null) {
3221 return cache.fadingEdgeLength;
3222 }
3223 }
3224 return 0;
3225 }
3226
3227 /**
3228 * Returns the width of the vertical scrollbar.
3229 *
3230 * @return The width in pixels of the vertical scrollbar or 0 if there
3231 * is no vertical scrollbar.
3232 */
3233 public int getVerticalScrollbarWidth() {
3234 ScrollabilityCache cache = mScrollCache;
3235 if (cache != null) {
3236 ScrollBarDrawable scrollBar = cache.scrollBar;
3237 if (scrollBar != null) {
3238 int size = scrollBar.getSize(true);
3239 if (size <= 0) {
3240 size = cache.scrollBarSize;
3241 }
3242 return size;
3243 }
3244 return 0;
3245 }
3246 return 0;
3247 }
3248
3249 /**
3250 * Returns the height of the horizontal scrollbar.
3251 *
3252 * @return The height in pixels of the horizontal scrollbar or 0 if
3253 * there is no horizontal scrollbar.
3254 */
3255 protected int getHorizontalScrollbarHeight() {
3256 ScrollabilityCache cache = mScrollCache;
3257 if (cache != null) {
3258 ScrollBarDrawable scrollBar = cache.scrollBar;
3259 if (scrollBar != null) {
3260 int size = scrollBar.getSize(false);
3261 if (size <= 0) {
3262 size = cache.scrollBarSize;
3263 }
3264 return size;
3265 }
3266 return 0;
3267 }
3268 return 0;
3269 }
3270
3271 /**
3272 * <p>
3273 * Initializes the scrollbars from a given set of styled attributes. This
3274 * method should be called by subclasses that need scrollbars and when an
3275 * instance of these subclasses is created programmatically rather than
3276 * being inflated from XML. This method is automatically called when the XML
3277 * is inflated.
3278 * </p>
3279 *
3280 * @param a the styled attributes set to initialize the scrollbars from
3281 */
3282 protected void initializeScrollbars(TypedArray a) {
3283 initScrollCache();
3284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003286
Mike Cleronf116bf82009-09-27 19:14:12 -07003287 if (scrollabilityCache.scrollBar == null) {
3288 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3289 }
Joe Malin32736f02011-01-19 16:14:20 -08003290
Romain Guy8bda2482010-03-02 11:42:11 -08003291 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292
Mike Cleronf116bf82009-09-27 19:14:12 -07003293 if (!fadeScrollbars) {
3294 scrollabilityCache.state = ScrollabilityCache.ON;
3295 }
3296 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003297
3298
Mike Cleronf116bf82009-09-27 19:14:12 -07003299 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3300 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3301 .getScrollBarFadeDuration());
3302 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3303 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3304 ViewConfiguration.getScrollDefaultDelay());
3305
Joe Malin32736f02011-01-19 16:14:20 -08003306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3308 com.android.internal.R.styleable.View_scrollbarSize,
3309 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3310
3311 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3312 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3313
3314 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3315 if (thumb != null) {
3316 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3317 }
3318
3319 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3320 false);
3321 if (alwaysDraw) {
3322 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3323 }
3324
3325 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3326 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3327
3328 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3329 if (thumb != null) {
3330 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3331 }
3332
3333 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3334 false);
3335 if (alwaysDraw) {
3336 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3337 }
3338
3339 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003340 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 }
3342
3343 /**
3344 * <p>
3345 * Initalizes the scrollability cache if necessary.
3346 * </p>
3347 */
3348 private void initScrollCache() {
3349 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003350 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 }
3352 }
3353
3354 /**
Adam Powell20232d02010-12-08 21:08:53 -08003355 * Set the position of the vertical scroll bar. Should be one of
3356 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3357 * {@link #SCROLLBAR_POSITION_RIGHT}.
3358 *
3359 * @param position Where the vertical scroll bar should be positioned.
3360 */
3361 public void setVerticalScrollbarPosition(int position) {
3362 if (mVerticalScrollbarPosition != position) {
3363 mVerticalScrollbarPosition = position;
3364 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003365 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003366 }
3367 }
3368
3369 /**
3370 * @return The position where the vertical scroll bar will show, if applicable.
3371 * @see #setVerticalScrollbarPosition(int)
3372 */
3373 public int getVerticalScrollbarPosition() {
3374 return mVerticalScrollbarPosition;
3375 }
3376
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003377 ListenerInfo getListenerInfo() {
3378 if (mListenerInfo != null) {
3379 return mListenerInfo;
3380 }
3381 mListenerInfo = new ListenerInfo();
3382 return mListenerInfo;
3383 }
3384
Adam Powell20232d02010-12-08 21:08:53 -08003385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 * Register a callback to be invoked when focus of this view changed.
3387 *
3388 * @param l The callback that will run.
3389 */
3390 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003391 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 }
3393
3394 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003395 * Add a listener that will be called when the bounds of the view change due to
3396 * layout processing.
3397 *
3398 * @param listener The listener that will be called when layout bounds change.
3399 */
3400 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003401 ListenerInfo li = getListenerInfo();
3402 if (li.mOnLayoutChangeListeners == null) {
3403 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003404 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003405 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3406 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003407 }
Chet Haase21cd1382010-09-01 17:42:29 -07003408 }
3409
3410 /**
3411 * Remove a listener for layout changes.
3412 *
3413 * @param listener The listener for layout bounds change.
3414 */
3415 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003416 ListenerInfo li = mListenerInfo;
3417 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003418 return;
3419 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003420 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003421 }
3422
3423 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003424 * Add a listener for attach state changes.
3425 *
3426 * This listener will be called whenever this view is attached or detached
3427 * from a window. Remove the listener using
3428 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3429 *
3430 * @param listener Listener to attach
3431 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3432 */
3433 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003434 ListenerInfo li = getListenerInfo();
3435 if (li.mOnAttachStateChangeListeners == null) {
3436 li.mOnAttachStateChangeListeners
3437 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003438 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003439 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003440 }
3441
3442 /**
3443 * Remove a listener for attach state changes. The listener will receive no further
3444 * notification of window attach/detach events.
3445 *
3446 * @param listener Listener to remove
3447 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3448 */
3449 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003450 ListenerInfo li = mListenerInfo;
3451 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003452 return;
3453 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003454 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003455 }
3456
3457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 * Returns the focus-change callback registered for this view.
3459 *
3460 * @return The callback, or null if one is not registered.
3461 */
3462 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003463 ListenerInfo li = mListenerInfo;
3464 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 }
3466
3467 /**
3468 * Register a callback to be invoked when this view is clicked. If this view is not
3469 * clickable, it becomes clickable.
3470 *
3471 * @param l The callback that will run
3472 *
3473 * @see #setClickable(boolean)
3474 */
3475 public void setOnClickListener(OnClickListener l) {
3476 if (!isClickable()) {
3477 setClickable(true);
3478 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003479 getListenerInfo().mOnClickListener = l;
3480 }
3481
3482 /**
3483 * Return whether this view has an attached OnClickListener. Returns
3484 * true if there is a listener, false if there is none.
3485 */
3486 public boolean hasOnClickListeners() {
3487 ListenerInfo li = mListenerInfo;
3488 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 }
3490
3491 /**
3492 * Register a callback to be invoked when this view is clicked and held. If this view is not
3493 * long clickable, it becomes long clickable.
3494 *
3495 * @param l The callback that will run
3496 *
3497 * @see #setLongClickable(boolean)
3498 */
3499 public void setOnLongClickListener(OnLongClickListener l) {
3500 if (!isLongClickable()) {
3501 setLongClickable(true);
3502 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003503 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 }
3505
3506 /**
3507 * Register a callback to be invoked when the context menu for this view is
3508 * being built. If this view is not long clickable, it becomes long clickable.
3509 *
3510 * @param l The callback that will run
3511 *
3512 */
3513 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3514 if (!isLongClickable()) {
3515 setLongClickable(true);
3516 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003517 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 }
3519
3520 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003521 * Call this view's OnClickListener, if it is defined. Performs all normal
3522 * actions associated with clicking: reporting accessibility event, playing
3523 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 *
3525 * @return True there was an assigned OnClickListener that was called, false
3526 * otherwise is returned.
3527 */
3528 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003529 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3530
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003531 ListenerInfo li = mListenerInfo;
3532 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003534 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 return true;
3536 }
3537
3538 return false;
3539 }
3540
3541 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003542 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3543 * this only calls the listener, and does not do any associated clicking
3544 * actions like reporting an accessibility event.
3545 *
3546 * @return True there was an assigned OnClickListener that was called, false
3547 * otherwise is returned.
3548 */
3549 public boolean callOnClick() {
3550 ListenerInfo li = mListenerInfo;
3551 if (li != null && li.mOnClickListener != null) {
3552 li.mOnClickListener.onClick(this);
3553 return true;
3554 }
3555 return false;
3556 }
3557
3558 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003559 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3560 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003562 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 */
3564 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003565 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003568 ListenerInfo li = mListenerInfo;
3569 if (li != null && li.mOnLongClickListener != null) {
3570 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 }
3572 if (!handled) {
3573 handled = showContextMenu();
3574 }
3575 if (handled) {
3576 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3577 }
3578 return handled;
3579 }
3580
3581 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003582 * Performs button-related actions during a touch down event.
3583 *
3584 * @param event The event.
3585 * @return True if the down was consumed.
3586 *
3587 * @hide
3588 */
3589 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3590 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3591 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3592 return true;
3593 }
3594 }
3595 return false;
3596 }
3597
3598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 * Bring up the context menu for this view.
3600 *
3601 * @return Whether a context menu was displayed.
3602 */
3603 public boolean showContextMenu() {
3604 return getParent().showContextMenuForChild(this);
3605 }
3606
3607 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003608 * Bring up the context menu for this view, referring to the item under the specified point.
3609 *
3610 * @param x The referenced x coordinate.
3611 * @param y The referenced y coordinate.
3612 * @param metaState The keyboard modifiers that were pressed.
3613 * @return Whether a context menu was displayed.
3614 *
3615 * @hide
3616 */
3617 public boolean showContextMenu(float x, float y, int metaState) {
3618 return showContextMenu();
3619 }
3620
3621 /**
Adam Powell6e346362010-07-23 10:18:23 -07003622 * Start an action mode.
3623 *
3624 * @param callback Callback that will control the lifecycle of the action mode
3625 * @return The new action mode if it is started, null otherwise
3626 *
3627 * @see ActionMode
3628 */
3629 public ActionMode startActionMode(ActionMode.Callback callback) {
3630 return getParent().startActionModeForChild(this, callback);
3631 }
3632
3633 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 * Register a callback to be invoked when a key is pressed in this view.
3635 * @param l the key listener to attach to this view
3636 */
3637 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003638 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 }
3640
3641 /**
3642 * Register a callback to be invoked when a touch event is sent to this view.
3643 * @param l the touch listener to attach to this view
3644 */
3645 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003646 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 }
3648
3649 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003650 * Register a callback to be invoked when a generic motion event is sent to this view.
3651 * @param l the generic motion listener to attach to this view
3652 */
3653 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003654 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003655 }
3656
3657 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003658 * Register a callback to be invoked when a hover event is sent to this view.
3659 * @param l the hover listener to attach to this view
3660 */
3661 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003662 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003663 }
3664
3665 /**
Joe Malin32736f02011-01-19 16:14:20 -08003666 * Register a drag event listener callback object for this View. The parameter is
3667 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3668 * View, the system calls the
3669 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3670 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003671 */
3672 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003673 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003674 }
3675
3676 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003677 * Give this view focus. This will cause
3678 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 *
3680 * Note: this does not check whether this {@link View} should get focus, it just
3681 * gives it focus no matter what. It should only be called internally by framework
3682 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3683 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003684 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3685 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 * focus moved when requestFocus() is called. It may not always
3687 * apply, in which case use the default View.FOCUS_DOWN.
3688 * @param previouslyFocusedRect The rectangle of the view that had focus
3689 * prior in this View's coordinate system.
3690 */
3691 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3692 if (DBG) {
3693 System.out.println(this + " requestFocus()");
3694 }
3695
3696 if ((mPrivateFlags & FOCUSED) == 0) {
3697 mPrivateFlags |= FOCUSED;
3698
3699 if (mParent != null) {
3700 mParent.requestChildFocus(this, this);
3701 }
3702
3703 onFocusChanged(true, direction, previouslyFocusedRect);
3704 refreshDrawableState();
3705 }
3706 }
3707
3708 /**
3709 * Request that a rectangle of this view be visible on the screen,
3710 * scrolling if necessary just enough.
3711 *
3712 * <p>A View should call this if it maintains some notion of which part
3713 * of its content is interesting. For example, a text editing view
3714 * should call this when its cursor moves.
3715 *
3716 * @param rectangle The rectangle.
3717 * @return Whether any parent scrolled.
3718 */
3719 public boolean requestRectangleOnScreen(Rect rectangle) {
3720 return requestRectangleOnScreen(rectangle, false);
3721 }
3722
3723 /**
3724 * Request that a rectangle of this view be visible on the screen,
3725 * scrolling if necessary just enough.
3726 *
3727 * <p>A View should call this if it maintains some notion of which part
3728 * of its content is interesting. For example, a text editing view
3729 * should call this when its cursor moves.
3730 *
3731 * <p>When <code>immediate</code> is set to true, scrolling will not be
3732 * animated.
3733 *
3734 * @param rectangle The rectangle.
3735 * @param immediate True to forbid animated scrolling, false otherwise
3736 * @return Whether any parent scrolled.
3737 */
3738 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3739 View child = this;
3740 ViewParent parent = mParent;
3741 boolean scrolled = false;
3742 while (parent != null) {
3743 scrolled |= parent.requestChildRectangleOnScreen(child,
3744 rectangle, immediate);
3745
3746 // offset rect so next call has the rectangle in the
3747 // coordinate system of its direct child.
3748 rectangle.offset(child.getLeft(), child.getTop());
3749 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3750
3751 if (!(parent instanceof View)) {
3752 break;
3753 }
Romain Guy8506ab42009-06-11 17:35:47 -07003754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 child = (View) parent;
3756 parent = child.getParent();
3757 }
3758 return scrolled;
3759 }
3760
3761 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003762 * Called when this view wants to give up focus. If focus is cleared
3763 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3764 * <p>
3765 * <strong>Note:</strong> When a View clears focus the framework is trying
3766 * to give focus to the first focusable View from the top. Hence, if this
3767 * View is the first from the top that can take focus, then its focus will
3768 * not be cleared nor will the focus change callback be invoked.
3769 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 */
3771 public void clearFocus() {
3772 if (DBG) {
3773 System.out.println(this + " clearFocus()");
3774 }
3775
3776 if ((mPrivateFlags & FOCUSED) != 0) {
Svetoslav Ganovc6fd88e2012-01-25 23:01:44 -08003777 // If this is the first focusable do not clear focus since the we
3778 // try to give it focus every time a view clears its focus. Hence,
3779 // the view that would gain focus already has it.
3780 View firstFocusable = getFirstFocusable();
3781 if (firstFocusable == this) {
3782 return;
3783 }
3784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 mPrivateFlags &= ~FOCUSED;
3786
3787 if (mParent != null) {
3788 mParent.clearChildFocus(this);
3789 }
3790
3791 onFocusChanged(false, 0, null);
3792 refreshDrawableState();
Svetoslav Ganovc6fd88e2012-01-25 23:01:44 -08003793
3794 // The view cleared focus and invoked the callbacks, so now is the
3795 // time to give focus to the the first focusable to ensure that the
3796 // gain focus is announced after clear focus.
3797 if (firstFocusable != null) {
3798 firstFocusable.requestFocus(FOCUS_FORWARD);
3799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 }
3801 }
3802
Svetoslav Ganovc6fd88e2012-01-25 23:01:44 -08003803 private View getFirstFocusable() {
3804 ViewRootImpl viewRoot = getViewRootImpl();
3805 if (viewRoot != null) {
3806 return viewRoot.focusSearch(null, FOCUS_FORWARD);
3807 }
3808 return null;
3809 }
3810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 /**
3812 * Called to clear the focus of a view that is about to be removed.
3813 * Doesn't call clearChildFocus, which prevents this view from taking
3814 * focus again before it has been removed from the parent
3815 */
3816 void clearFocusForRemoval() {
3817 if ((mPrivateFlags & FOCUSED) != 0) {
3818 mPrivateFlags &= ~FOCUSED;
3819
3820 onFocusChanged(false, 0, null);
3821 refreshDrawableState();
3822 }
3823 }
3824
3825 /**
3826 * Called internally by the view system when a new view is getting focus.
3827 * This is what clears the old focus.
3828 */
3829 void unFocus() {
3830 if (DBG) {
3831 System.out.println(this + " unFocus()");
3832 }
3833
3834 if ((mPrivateFlags & FOCUSED) != 0) {
3835 mPrivateFlags &= ~FOCUSED;
3836
3837 onFocusChanged(false, 0, null);
3838 refreshDrawableState();
3839 }
3840 }
3841
3842 /**
3843 * Returns true if this view has focus iteself, or is the ancestor of the
3844 * view that has focus.
3845 *
3846 * @return True if this view has or contains focus, false otherwise.
3847 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003848 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 public boolean hasFocus() {
3850 return (mPrivateFlags & FOCUSED) != 0;
3851 }
3852
3853 /**
3854 * Returns true if this view is focusable or if it contains a reachable View
3855 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3856 * is a View whose parents do not block descendants focus.
3857 *
3858 * Only {@link #VISIBLE} views are considered focusable.
3859 *
3860 * @return True if the view is focusable or if the view contains a focusable
3861 * View, false otherwise.
3862 *
3863 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3864 */
3865 public boolean hasFocusable() {
3866 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3867 }
3868
3869 /**
3870 * Called by the view system when the focus state of this view changes.
3871 * When the focus change event is caused by directional navigation, direction
3872 * and previouslyFocusedRect provide insight into where the focus is coming from.
3873 * When overriding, be sure to call up through to the super class so that
3874 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003875 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 * @param gainFocus True if the View has focus; false otherwise.
3877 * @param direction The direction focus has moved when requestFocus()
3878 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003879 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3880 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3881 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3883 * system, of the previously focused view. If applicable, this will be
3884 * passed in as finer grained information about where the focus is coming
3885 * from (in addition to direction). Will be <code>null</code> otherwise.
3886 */
3887 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003888 if (gainFocus) {
3889 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3890 }
3891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 InputMethodManager imm = InputMethodManager.peekInstance();
3893 if (!gainFocus) {
3894 if (isPressed()) {
3895 setPressed(false);
3896 }
3897 if (imm != null && mAttachInfo != null
3898 && mAttachInfo.mHasWindowFocus) {
3899 imm.focusOut(this);
3900 }
Romain Guya2431d02009-04-30 16:30:00 -07003901 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 } else if (imm != null && mAttachInfo != null
3903 && mAttachInfo.mHasWindowFocus) {
3904 imm.focusIn(this);
3905 }
Romain Guy8506ab42009-06-11 17:35:47 -07003906
Romain Guy0fd89bf2011-01-26 15:41:30 -08003907 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003908 ListenerInfo li = mListenerInfo;
3909 if (li != null && li.mOnFocusChangeListener != null) {
3910 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 }
Joe Malin32736f02011-01-19 16:14:20 -08003912
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003913 if (mAttachInfo != null) {
3914 mAttachInfo.mKeyDispatchState.reset(this);
3915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 }
3917
3918 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003919 * Sends an accessibility event of the given type. If accessiiblity is
3920 * not enabled this method has no effect. The default implementation calls
3921 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3922 * to populate information about the event source (this View), then calls
3923 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3924 * populate the text content of the event source including its descendants,
3925 * and last calls
3926 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3927 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003928 * <p>
3929 * If an {@link AccessibilityDelegate} has been specified via calling
3930 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3931 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3932 * responsible for handling this call.
3933 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003934 *
Scott Mainb303d832011-10-12 16:45:18 -07003935 * @param eventType The type of the event to send, as defined by several types from
3936 * {@link android.view.accessibility.AccessibilityEvent}, such as
3937 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3938 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003939 *
3940 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3941 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3942 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003943 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003944 */
3945 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003946 if (mAccessibilityDelegate != null) {
3947 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3948 } else {
3949 sendAccessibilityEventInternal(eventType);
3950 }
3951 }
3952
3953 /**
3954 * @see #sendAccessibilityEvent(int)
3955 *
3956 * Note: Called from the default {@link AccessibilityDelegate}.
3957 */
3958 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003959 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3960 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3961 }
3962 }
3963
3964 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003965 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3966 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003967 * perform a check whether accessibility is enabled.
3968 * <p>
3969 * If an {@link AccessibilityDelegate} has been specified via calling
3970 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3971 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3972 * is responsible for handling this call.
3973 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003974 *
3975 * @param event The event to send.
3976 *
3977 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003978 */
3979 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003980 if (mAccessibilityDelegate != null) {
3981 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3982 } else {
3983 sendAccessibilityEventUncheckedInternal(event);
3984 }
3985 }
3986
3987 /**
3988 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3989 *
3990 * Note: Called from the default {@link AccessibilityDelegate}.
3991 */
3992 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003993 if (!isShown()) {
3994 return;
3995 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003996 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003997 // Only a subset of accessibility events populates text content.
3998 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3999 dispatchPopulateAccessibilityEvent(event);
4000 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004001 // In the beginning we called #isShown(), so we know that getParent() is not null.
4002 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004003 }
4004
4005 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004006 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4007 * to its children for adding their text content to the event. Note that the
4008 * event text is populated in a separate dispatch path since we add to the
4009 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004010 * A typical implementation will call
4011 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4012 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4013 * on each child. Override this method if custom population of the event text
4014 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004015 * <p>
4016 * If an {@link AccessibilityDelegate} has been specified via calling
4017 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4018 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4019 * is responsible for handling this call.
4020 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004021 * <p>
4022 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4023 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4024 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004025 *
4026 * @param event The event.
4027 *
4028 * @return True if the event population was completed.
4029 */
4030 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004031 if (mAccessibilityDelegate != null) {
4032 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4033 } else {
4034 return dispatchPopulateAccessibilityEventInternal(event);
4035 }
4036 }
4037
4038 /**
4039 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4040 *
4041 * Note: Called from the default {@link AccessibilityDelegate}.
4042 */
4043 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004044 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004045 return false;
4046 }
4047
4048 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004049 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004050 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004051 * text content. While this method is free to modify event
4052 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004053 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4054 * <p>
4055 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004056 * to the text added by the super implementation:
4057 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004058 * super.onPopulateAccessibilityEvent(event);
4059 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4060 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4061 * mCurrentDate.getTimeInMillis(), flags);
4062 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004063 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004064 * <p>
4065 * If an {@link AccessibilityDelegate} has been specified via calling
4066 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4067 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4068 * is responsible for handling this call.
4069 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004070 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4071 * information to the event, in case the default implementation has basic information to add.
4072 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004073 *
4074 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004075 *
4076 * @see #sendAccessibilityEvent(int)
4077 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004078 */
4079 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004080 if (mAccessibilityDelegate != null) {
4081 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4082 } else {
4083 onPopulateAccessibilityEventInternal(event);
4084 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004085 }
4086
4087 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004088 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4089 *
4090 * Note: Called from the default {@link AccessibilityDelegate}.
4091 */
4092 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4093
4094 }
4095
4096 /**
4097 * Initializes an {@link AccessibilityEvent} with information about
4098 * this View which is the event source. In other words, the source of
4099 * an accessibility event is the view whose state change triggered firing
4100 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004101 * <p>
4102 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004103 * to properties set by the super implementation:
4104 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4105 * super.onInitializeAccessibilityEvent(event);
4106 * event.setPassword(true);
4107 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004108 * <p>
4109 * If an {@link AccessibilityDelegate} has been specified via calling
4110 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4111 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4112 * is responsible for handling this call.
4113 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004114 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4115 * information to the event, in case the default implementation has basic information to add.
4116 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004117 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004118 *
4119 * @see #sendAccessibilityEvent(int)
4120 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4121 */
4122 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004123 if (mAccessibilityDelegate != null) {
4124 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4125 } else {
4126 onInitializeAccessibilityEventInternal(event);
4127 }
4128 }
4129
4130 /**
4131 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4132 *
4133 * Note: Called from the default {@link AccessibilityDelegate}.
4134 */
4135 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004136 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004137 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004138 event.setPackageName(getContext().getPackageName());
4139 event.setEnabled(isEnabled());
4140 event.setContentDescription(mContentDescription);
4141
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004142 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4143 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4144 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4145 FOCUSABLES_ALL);
4146 event.setItemCount(focusablesTempList.size());
4147 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4148 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004149 }
4150 }
4151
4152 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004153 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4154 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4155 * This method is responsible for obtaining an accessibility node info from a
4156 * pool of reusable instances and calling
4157 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4158 * initialize the former.
4159 * <p>
4160 * Note: The client is responsible for recycling the obtained instance by calling
4161 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4162 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004163 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004164 * @return A populated {@link AccessibilityNodeInfo}.
4165 *
4166 * @see AccessibilityNodeInfo
4167 */
4168 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004169 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4170 if (provider != null) {
4171 return provider.createAccessibilityNodeInfo(View.NO_ID);
4172 } else {
4173 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4174 onInitializeAccessibilityNodeInfo(info);
4175 return info;
4176 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004177 }
4178
4179 /**
4180 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4181 * The base implementation sets:
4182 * <ul>
4183 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004184 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4185 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004186 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4187 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4188 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4189 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4190 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4191 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4192 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4193 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4194 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4195 * </ul>
4196 * <p>
4197 * Subclasses should override this method, call the super implementation,
4198 * and set additional attributes.
4199 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004200 * <p>
4201 * If an {@link AccessibilityDelegate} has been specified via calling
4202 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4203 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4204 * is responsible for handling this call.
4205 * </p>
4206 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004207 * @param info The instance to initialize.
4208 */
4209 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004210 if (mAccessibilityDelegate != null) {
4211 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4212 } else {
4213 onInitializeAccessibilityNodeInfoInternal(info);
4214 }
4215 }
4216
4217 /**
4218 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4219 *
4220 * Note: Called from the default {@link AccessibilityDelegate}.
4221 */
4222 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004223 Rect bounds = mAttachInfo.mTmpInvalRect;
4224 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004225 info.setBoundsInParent(bounds);
4226
4227 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4228 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004229 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004230 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4231 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004232
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004233 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4234 ViewParent parent = getParent();
4235 if (parent instanceof View) {
4236 View parentView = (View) parent;
4237 info.setParent(parentView);
4238 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004239 }
4240
4241 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004242 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004243 info.setContentDescription(getContentDescription());
4244
4245 info.setEnabled(isEnabled());
4246 info.setClickable(isClickable());
4247 info.setFocusable(isFocusable());
4248 info.setFocused(isFocused());
4249 info.setSelected(isSelected());
4250 info.setLongClickable(isLongClickable());
4251
4252 // TODO: These make sense only if we are in an AdapterView but all
4253 // views can be selected. Maybe from accessiiblity perspective
4254 // we should report as selectable view in an AdapterView.
4255 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4256 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4257
4258 if (isFocusable()) {
4259 if (isFocused()) {
4260 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4261 } else {
4262 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4263 }
4264 }
4265 }
4266
4267 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004268 * Sets a delegate for implementing accessibility support via compositon as
4269 * opposed to inheritance. The delegate's primary use is for implementing
4270 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4271 *
4272 * @param delegate The delegate instance.
4273 *
4274 * @see AccessibilityDelegate
4275 */
4276 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4277 mAccessibilityDelegate = delegate;
4278 }
4279
4280 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004281 * Gets the provider for managing a virtual view hierarchy rooted at this View
4282 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4283 * that explore the window content.
4284 * <p>
4285 * If this method returns an instance, this instance is responsible for managing
4286 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4287 * View including the one representing the View itself. Similarly the returned
4288 * instance is responsible for performing accessibility actions on any virtual
4289 * view or the root view itself.
4290 * </p>
4291 * <p>
4292 * If an {@link AccessibilityDelegate} has been specified via calling
4293 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4294 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4295 * is responsible for handling this call.
4296 * </p>
4297 *
4298 * @return The provider.
4299 *
4300 * @see AccessibilityNodeProvider
4301 */
4302 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4303 if (mAccessibilityDelegate != null) {
4304 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4305 } else {
4306 return null;
4307 }
4308 }
4309
4310 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004311 * Gets the unique identifier of this view on the screen for accessibility purposes.
4312 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4313 *
4314 * @return The view accessibility id.
4315 *
4316 * @hide
4317 */
4318 public int getAccessibilityViewId() {
4319 if (mAccessibilityViewId == NO_ID) {
4320 mAccessibilityViewId = sNextAccessibilityViewId++;
4321 }
4322 return mAccessibilityViewId;
4323 }
4324
4325 /**
4326 * Gets the unique identifier of the window in which this View reseides.
4327 *
4328 * @return The window accessibility id.
4329 *
4330 * @hide
4331 */
4332 public int getAccessibilityWindowId() {
4333 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4334 }
4335
4336 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004337 * Gets the {@link View} description. It briefly describes the view and is
4338 * primarily used for accessibility support. Set this property to enable
4339 * better accessibility support for your application. This is especially
4340 * true for views that do not have textual representation (For example,
4341 * ImageButton).
4342 *
4343 * @return The content descriptiopn.
4344 *
4345 * @attr ref android.R.styleable#View_contentDescription
4346 */
4347 public CharSequence getContentDescription() {
4348 return mContentDescription;
4349 }
4350
4351 /**
4352 * Sets the {@link View} description. It briefly describes the view and is
4353 * primarily used for accessibility support. Set this property to enable
4354 * better accessibility support for your application. This is especially
4355 * true for views that do not have textual representation (For example,
4356 * ImageButton).
4357 *
4358 * @param contentDescription The content description.
4359 *
4360 * @attr ref android.R.styleable#View_contentDescription
4361 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004362 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004363 public void setContentDescription(CharSequence contentDescription) {
4364 mContentDescription = contentDescription;
4365 }
4366
4367 /**
Romain Guya2431d02009-04-30 16:30:00 -07004368 * Invoked whenever this view loses focus, either by losing window focus or by losing
4369 * focus within its window. This method can be used to clear any state tied to the
4370 * focus. For instance, if a button is held pressed with the trackball and the window
4371 * loses focus, this method can be used to cancel the press.
4372 *
4373 * Subclasses of View overriding this method should always call super.onFocusLost().
4374 *
4375 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004376 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004377 *
4378 * @hide pending API council approval
4379 */
4380 protected void onFocusLost() {
4381 resetPressedState();
4382 }
4383
4384 private void resetPressedState() {
4385 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4386 return;
4387 }
4388
4389 if (isPressed()) {
4390 setPressed(false);
4391
4392 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004393 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004394 }
4395 }
4396 }
4397
4398 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 * Returns true if this view has focus
4400 *
4401 * @return True if this view has focus, false otherwise.
4402 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004403 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 public boolean isFocused() {
4405 return (mPrivateFlags & FOCUSED) != 0;
4406 }
4407
4408 /**
4409 * Find the view in the hierarchy rooted at this view that currently has
4410 * focus.
4411 *
4412 * @return The view that currently has focus, or null if no focused view can
4413 * be found.
4414 */
4415 public View findFocus() {
4416 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4417 }
4418
4419 /**
4420 * Change whether this view is one of the set of scrollable containers in
4421 * its window. This will be used to determine whether the window can
4422 * resize or must pan when a soft input area is open -- scrollable
4423 * containers allow the window to use resize mode since the container
4424 * will appropriately shrink.
4425 */
4426 public void setScrollContainer(boolean isScrollContainer) {
4427 if (isScrollContainer) {
4428 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4429 mAttachInfo.mScrollContainers.add(this);
4430 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4431 }
4432 mPrivateFlags |= SCROLL_CONTAINER;
4433 } else {
4434 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4435 mAttachInfo.mScrollContainers.remove(this);
4436 }
4437 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4438 }
4439 }
4440
4441 /**
4442 * Returns the quality of the drawing cache.
4443 *
4444 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4445 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4446 *
4447 * @see #setDrawingCacheQuality(int)
4448 * @see #setDrawingCacheEnabled(boolean)
4449 * @see #isDrawingCacheEnabled()
4450 *
4451 * @attr ref android.R.styleable#View_drawingCacheQuality
4452 */
4453 public int getDrawingCacheQuality() {
4454 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4455 }
4456
4457 /**
4458 * Set the drawing cache quality of this view. This value is used only when the
4459 * drawing cache is enabled
4460 *
4461 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4462 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4463 *
4464 * @see #getDrawingCacheQuality()
4465 * @see #setDrawingCacheEnabled(boolean)
4466 * @see #isDrawingCacheEnabled()
4467 *
4468 * @attr ref android.R.styleable#View_drawingCacheQuality
4469 */
4470 public void setDrawingCacheQuality(int quality) {
4471 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4472 }
4473
4474 /**
4475 * Returns whether the screen should remain on, corresponding to the current
4476 * value of {@link #KEEP_SCREEN_ON}.
4477 *
4478 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4479 *
4480 * @see #setKeepScreenOn(boolean)
4481 *
4482 * @attr ref android.R.styleable#View_keepScreenOn
4483 */
4484 public boolean getKeepScreenOn() {
4485 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4486 }
4487
4488 /**
4489 * Controls whether the screen should remain on, modifying the
4490 * value of {@link #KEEP_SCREEN_ON}.
4491 *
4492 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4493 *
4494 * @see #getKeepScreenOn()
4495 *
4496 * @attr ref android.R.styleable#View_keepScreenOn
4497 */
4498 public void setKeepScreenOn(boolean keepScreenOn) {
4499 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4500 }
4501
4502 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004503 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4504 * @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 -08004505 *
4506 * @attr ref android.R.styleable#View_nextFocusLeft
4507 */
4508 public int getNextFocusLeftId() {
4509 return mNextFocusLeftId;
4510 }
4511
4512 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004513 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4514 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4515 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 *
4517 * @attr ref android.R.styleable#View_nextFocusLeft
4518 */
4519 public void setNextFocusLeftId(int nextFocusLeftId) {
4520 mNextFocusLeftId = nextFocusLeftId;
4521 }
4522
4523 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004524 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4525 * @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 -08004526 *
4527 * @attr ref android.R.styleable#View_nextFocusRight
4528 */
4529 public int getNextFocusRightId() {
4530 return mNextFocusRightId;
4531 }
4532
4533 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004534 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4535 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4536 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 *
4538 * @attr ref android.R.styleable#View_nextFocusRight
4539 */
4540 public void setNextFocusRightId(int nextFocusRightId) {
4541 mNextFocusRightId = nextFocusRightId;
4542 }
4543
4544 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004545 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4546 * @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 -08004547 *
4548 * @attr ref android.R.styleable#View_nextFocusUp
4549 */
4550 public int getNextFocusUpId() {
4551 return mNextFocusUpId;
4552 }
4553
4554 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004555 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4556 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4557 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558 *
4559 * @attr ref android.R.styleable#View_nextFocusUp
4560 */
4561 public void setNextFocusUpId(int nextFocusUpId) {
4562 mNextFocusUpId = nextFocusUpId;
4563 }
4564
4565 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004566 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4567 * @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 -08004568 *
4569 * @attr ref android.R.styleable#View_nextFocusDown
4570 */
4571 public int getNextFocusDownId() {
4572 return mNextFocusDownId;
4573 }
4574
4575 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004576 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4577 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4578 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 *
4580 * @attr ref android.R.styleable#View_nextFocusDown
4581 */
4582 public void setNextFocusDownId(int nextFocusDownId) {
4583 mNextFocusDownId = nextFocusDownId;
4584 }
4585
4586 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004587 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4588 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4589 *
4590 * @attr ref android.R.styleable#View_nextFocusForward
4591 */
4592 public int getNextFocusForwardId() {
4593 return mNextFocusForwardId;
4594 }
4595
4596 /**
4597 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4598 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4599 * decide automatically.
4600 *
4601 * @attr ref android.R.styleable#View_nextFocusForward
4602 */
4603 public void setNextFocusForwardId(int nextFocusForwardId) {
4604 mNextFocusForwardId = nextFocusForwardId;
4605 }
4606
4607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 * Returns the visibility of this view and all of its ancestors
4609 *
4610 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4611 */
4612 public boolean isShown() {
4613 View current = this;
4614 //noinspection ConstantConditions
4615 do {
4616 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4617 return false;
4618 }
4619 ViewParent parent = current.mParent;
4620 if (parent == null) {
4621 return false; // We are not attached to the view root
4622 }
4623 if (!(parent instanceof View)) {
4624 return true;
4625 }
4626 current = (View) parent;
4627 } while (current != null);
4628
4629 return false;
4630 }
4631
4632 /**
4633 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4634 * is set
4635 *
4636 * @param insets Insets for system windows
4637 *
4638 * @return True if this view applied the insets, false otherwise
4639 */
4640 protected boolean fitSystemWindows(Rect insets) {
4641 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4642 mPaddingLeft = insets.left;
4643 mPaddingTop = insets.top;
4644 mPaddingRight = insets.right;
4645 mPaddingBottom = insets.bottom;
4646 requestLayout();
4647 return true;
4648 }
4649 return false;
4650 }
4651
4652 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004653 * Set whether or not this view should account for system screen decorations
4654 * such as the status bar and inset its content. This allows this view to be
4655 * positioned in absolute screen coordinates and remain visible to the user.
4656 *
4657 * <p>This should only be used by top-level window decor views.
4658 *
4659 * @param fitSystemWindows true to inset content for system screen decorations, false for
4660 * default behavior.
4661 *
4662 * @attr ref android.R.styleable#View_fitsSystemWindows
4663 */
4664 public void setFitsSystemWindows(boolean fitSystemWindows) {
4665 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4666 }
4667
4668 /**
4669 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4670 * will account for system screen decorations such as the status bar and inset its
4671 * content. This allows the view to be positioned in absolute screen coordinates
4672 * and remain visible to the user.
4673 *
4674 * @return true if this view will adjust its content bounds for system screen decorations.
4675 *
4676 * @attr ref android.R.styleable#View_fitsSystemWindows
4677 */
4678 public boolean fitsSystemWindows() {
4679 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4680 }
4681
4682 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 * Returns the visibility status for this view.
4684 *
4685 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4686 * @attr ref android.R.styleable#View_visibility
4687 */
4688 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004689 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4690 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4691 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 })
4693 public int getVisibility() {
4694 return mViewFlags & VISIBILITY_MASK;
4695 }
4696
4697 /**
4698 * Set the enabled state of this view.
4699 *
4700 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4701 * @attr ref android.R.styleable#View_visibility
4702 */
4703 @RemotableViewMethod
4704 public void setVisibility(int visibility) {
4705 setFlags(visibility, VISIBILITY_MASK);
4706 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4707 }
4708
4709 /**
4710 * Returns the enabled status for this view. The interpretation of the
4711 * enabled state varies by subclass.
4712 *
4713 * @return True if this view is enabled, false otherwise.
4714 */
4715 @ViewDebug.ExportedProperty
4716 public boolean isEnabled() {
4717 return (mViewFlags & ENABLED_MASK) == ENABLED;
4718 }
4719
4720 /**
4721 * Set the enabled state of this view. The interpretation of the enabled
4722 * state varies by subclass.
4723 *
4724 * @param enabled True if this view is enabled, false otherwise.
4725 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004726 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004728 if (enabled == isEnabled()) return;
4729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4731
4732 /*
4733 * The View most likely has to change its appearance, so refresh
4734 * the drawable state.
4735 */
4736 refreshDrawableState();
4737
4738 // Invalidate too, since the default behavior for views is to be
4739 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004740 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004741 }
4742
4743 /**
4744 * Set whether this view can receive the focus.
4745 *
4746 * Setting this to false will also ensure that this view is not focusable
4747 * in touch mode.
4748 *
4749 * @param focusable If true, this view can receive the focus.
4750 *
4751 * @see #setFocusableInTouchMode(boolean)
4752 * @attr ref android.R.styleable#View_focusable
4753 */
4754 public void setFocusable(boolean focusable) {
4755 if (!focusable) {
4756 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4757 }
4758 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4759 }
4760
4761 /**
4762 * Set whether this view can receive focus while in touch mode.
4763 *
4764 * Setting this to true will also ensure that this view is focusable.
4765 *
4766 * @param focusableInTouchMode If true, this view can receive the focus while
4767 * in touch mode.
4768 *
4769 * @see #setFocusable(boolean)
4770 * @attr ref android.R.styleable#View_focusableInTouchMode
4771 */
4772 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4773 // Focusable in touch mode should always be set before the focusable flag
4774 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4775 // which, in touch mode, will not successfully request focus on this view
4776 // because the focusable in touch mode flag is not set
4777 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4778 if (focusableInTouchMode) {
4779 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4780 }
4781 }
4782
4783 /**
4784 * Set whether this view should have sound effects enabled for events such as
4785 * clicking and touching.
4786 *
4787 * <p>You may wish to disable sound effects for a view if you already play sounds,
4788 * for instance, a dial key that plays dtmf tones.
4789 *
4790 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4791 * @see #isSoundEffectsEnabled()
4792 * @see #playSoundEffect(int)
4793 * @attr ref android.R.styleable#View_soundEffectsEnabled
4794 */
4795 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4796 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4797 }
4798
4799 /**
4800 * @return whether this view should have sound effects enabled for events such as
4801 * clicking and touching.
4802 *
4803 * @see #setSoundEffectsEnabled(boolean)
4804 * @see #playSoundEffect(int)
4805 * @attr ref android.R.styleable#View_soundEffectsEnabled
4806 */
4807 @ViewDebug.ExportedProperty
4808 public boolean isSoundEffectsEnabled() {
4809 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4810 }
4811
4812 /**
4813 * Set whether this view should have haptic feedback for events such as
4814 * long presses.
4815 *
4816 * <p>You may wish to disable haptic feedback if your view already controls
4817 * its own haptic feedback.
4818 *
4819 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4820 * @see #isHapticFeedbackEnabled()
4821 * @see #performHapticFeedback(int)
4822 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4823 */
4824 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4825 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4826 }
4827
4828 /**
4829 * @return whether this view should have haptic feedback enabled for events
4830 * long presses.
4831 *
4832 * @see #setHapticFeedbackEnabled(boolean)
4833 * @see #performHapticFeedback(int)
4834 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4835 */
4836 @ViewDebug.ExportedProperty
4837 public boolean isHapticFeedbackEnabled() {
4838 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4839 }
4840
4841 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004842 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004843 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004844 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4845 * {@link #LAYOUT_DIRECTION_RTL},
4846 * {@link #LAYOUT_DIRECTION_INHERIT} or
4847 * {@link #LAYOUT_DIRECTION_LOCALE}.
4848 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004849 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004850 * @hide
4851 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004852 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004853 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4854 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4855 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4856 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004857 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004858 public int getLayoutDirection() {
4859 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004860 }
4861
4862 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004863 * Set the layout direction for this view. This will propagate a reset of layout direction
4864 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004865 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004866 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4867 * {@link #LAYOUT_DIRECTION_RTL},
4868 * {@link #LAYOUT_DIRECTION_INHERIT} or
4869 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004870 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004871 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004872 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004873 * @hide
4874 */
4875 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004876 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004877 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004878 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004879 // Setting the flag will also request a layout.
4880 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4881 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004882 }
4883
4884 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004885 * Returns the resolved layout direction for this view.
4886 *
4887 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4888 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4889 *
4890 * @hide
4891 */
4892 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4893 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4894 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4895 })
4896 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004897 resolveLayoutDirectionIfNeeded();
4898 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004899 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4900 }
4901
4902 /**
4903 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4904 * layout attribute and/or the inherited value from the parent.</p>
4905 *
4906 * @return true if the layout is right-to-left.
4907 *
4908 * @hide
4909 */
4910 @ViewDebug.ExportedProperty(category = "layout")
4911 public boolean isLayoutRtl() {
4912 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4913 }
4914
4915 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 * If this view doesn't do any drawing on its own, set this flag to
4917 * allow further optimizations. By default, this flag is not set on
4918 * View, but could be set on some View subclasses such as ViewGroup.
4919 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004920 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4921 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004922 *
4923 * @param willNotDraw whether or not this View draw on its own
4924 */
4925 public void setWillNotDraw(boolean willNotDraw) {
4926 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4927 }
4928
4929 /**
4930 * Returns whether or not this View draws on its own.
4931 *
4932 * @return true if this view has nothing to draw, false otherwise
4933 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004934 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935 public boolean willNotDraw() {
4936 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4937 }
4938
4939 /**
4940 * When a View's drawing cache is enabled, drawing is redirected to an
4941 * offscreen bitmap. Some views, like an ImageView, must be able to
4942 * bypass this mechanism if they already draw a single bitmap, to avoid
4943 * unnecessary usage of the memory.
4944 *
4945 * @param willNotCacheDrawing true if this view does not cache its
4946 * drawing, false otherwise
4947 */
4948 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4949 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4950 }
4951
4952 /**
4953 * Returns whether or not this View can cache its drawing or not.
4954 *
4955 * @return true if this view does not cache its drawing, false otherwise
4956 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004957 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 public boolean willNotCacheDrawing() {
4959 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4960 }
4961
4962 /**
4963 * Indicates whether this view reacts to click events or not.
4964 *
4965 * @return true if the view is clickable, false otherwise
4966 *
4967 * @see #setClickable(boolean)
4968 * @attr ref android.R.styleable#View_clickable
4969 */
4970 @ViewDebug.ExportedProperty
4971 public boolean isClickable() {
4972 return (mViewFlags & CLICKABLE) == CLICKABLE;
4973 }
4974
4975 /**
4976 * Enables or disables click events for this view. When a view
4977 * is clickable it will change its state to "pressed" on every click.
4978 * Subclasses should set the view clickable to visually react to
4979 * user's clicks.
4980 *
4981 * @param clickable true to make the view clickable, false otherwise
4982 *
4983 * @see #isClickable()
4984 * @attr ref android.R.styleable#View_clickable
4985 */
4986 public void setClickable(boolean clickable) {
4987 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4988 }
4989
4990 /**
4991 * Indicates whether this view reacts to long click events or not.
4992 *
4993 * @return true if the view is long clickable, false otherwise
4994 *
4995 * @see #setLongClickable(boolean)
4996 * @attr ref android.R.styleable#View_longClickable
4997 */
4998 public boolean isLongClickable() {
4999 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5000 }
5001
5002 /**
5003 * Enables or disables long click events for this view. When a view is long
5004 * clickable it reacts to the user holding down the button for a longer
5005 * duration than a tap. This event can either launch the listener or a
5006 * context menu.
5007 *
5008 * @param longClickable true to make the view long clickable, false otherwise
5009 * @see #isLongClickable()
5010 * @attr ref android.R.styleable#View_longClickable
5011 */
5012 public void setLongClickable(boolean longClickable) {
5013 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5014 }
5015
5016 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005017 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 *
5019 * @see #isClickable()
5020 * @see #setClickable(boolean)
5021 *
5022 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5023 * the View's internal state from a previously set "pressed" state.
5024 */
5025 public void setPressed(boolean pressed) {
5026 if (pressed) {
5027 mPrivateFlags |= PRESSED;
5028 } else {
5029 mPrivateFlags &= ~PRESSED;
5030 }
5031 refreshDrawableState();
5032 dispatchSetPressed(pressed);
5033 }
5034
5035 /**
5036 * Dispatch setPressed to all of this View's children.
5037 *
5038 * @see #setPressed(boolean)
5039 *
5040 * @param pressed The new pressed state
5041 */
5042 protected void dispatchSetPressed(boolean pressed) {
5043 }
5044
5045 /**
5046 * Indicates whether the view is currently in pressed state. Unless
5047 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5048 * the pressed state.
5049 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005050 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 * @see #isClickable()
5052 * @see #setClickable(boolean)
5053 *
5054 * @return true if the view is currently pressed, false otherwise
5055 */
5056 public boolean isPressed() {
5057 return (mPrivateFlags & PRESSED) == PRESSED;
5058 }
5059
5060 /**
5061 * Indicates whether this view will save its state (that is,
5062 * whether its {@link #onSaveInstanceState} method will be called).
5063 *
5064 * @return Returns true if the view state saving is enabled, else false.
5065 *
5066 * @see #setSaveEnabled(boolean)
5067 * @attr ref android.R.styleable#View_saveEnabled
5068 */
5069 public boolean isSaveEnabled() {
5070 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5071 }
5072
5073 /**
5074 * Controls whether the saving of this view's state is
5075 * enabled (that is, whether its {@link #onSaveInstanceState} method
5076 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005077 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005078 * for its state to be saved. This flag can only disable the
5079 * saving of this view; any child views may still have their state saved.
5080 *
5081 * @param enabled Set to false to <em>disable</em> state saving, or true
5082 * (the default) to allow it.
5083 *
5084 * @see #isSaveEnabled()
5085 * @see #setId(int)
5086 * @see #onSaveInstanceState()
5087 * @attr ref android.R.styleable#View_saveEnabled
5088 */
5089 public void setSaveEnabled(boolean enabled) {
5090 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5091 }
5092
Jeff Brown85a31762010-09-01 17:01:00 -07005093 /**
5094 * Gets whether the framework should discard touches when the view's
5095 * window is obscured by another visible window.
5096 * Refer to the {@link View} security documentation for more details.
5097 *
5098 * @return True if touch filtering is enabled.
5099 *
5100 * @see #setFilterTouchesWhenObscured(boolean)
5101 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5102 */
5103 @ViewDebug.ExportedProperty
5104 public boolean getFilterTouchesWhenObscured() {
5105 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5106 }
5107
5108 /**
5109 * Sets whether the framework should discard touches when the view's
5110 * window is obscured by another visible window.
5111 * Refer to the {@link View} security documentation for more details.
5112 *
5113 * @param enabled True if touch filtering should be enabled.
5114 *
5115 * @see #getFilterTouchesWhenObscured
5116 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5117 */
5118 public void setFilterTouchesWhenObscured(boolean enabled) {
5119 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5120 FILTER_TOUCHES_WHEN_OBSCURED);
5121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005122
5123 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005124 * Indicates whether the entire hierarchy under this view will save its
5125 * state when a state saving traversal occurs from its parent. The default
5126 * is true; if false, these views will not be saved unless
5127 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5128 *
5129 * @return Returns true if the view state saving from parent is enabled, else false.
5130 *
5131 * @see #setSaveFromParentEnabled(boolean)
5132 */
5133 public boolean isSaveFromParentEnabled() {
5134 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5135 }
5136
5137 /**
5138 * Controls whether the entire hierarchy under this view will save its
5139 * state when a state saving traversal occurs from its parent. The default
5140 * is true; if false, these views will not be saved unless
5141 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5142 *
5143 * @param enabled Set to false to <em>disable</em> state saving, or true
5144 * (the default) to allow it.
5145 *
5146 * @see #isSaveFromParentEnabled()
5147 * @see #setId(int)
5148 * @see #onSaveInstanceState()
5149 */
5150 public void setSaveFromParentEnabled(boolean enabled) {
5151 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5152 }
5153
5154
5155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 * Returns whether this View is able to take focus.
5157 *
5158 * @return True if this view can take focus, or false otherwise.
5159 * @attr ref android.R.styleable#View_focusable
5160 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005161 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 public final boolean isFocusable() {
5163 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5164 }
5165
5166 /**
5167 * When a view is focusable, it may not want to take focus when in touch mode.
5168 * For example, a button would like focus when the user is navigating via a D-pad
5169 * so that the user can click on it, but once the user starts touching the screen,
5170 * the button shouldn't take focus
5171 * @return Whether the view is focusable in touch mode.
5172 * @attr ref android.R.styleable#View_focusableInTouchMode
5173 */
5174 @ViewDebug.ExportedProperty
5175 public final boolean isFocusableInTouchMode() {
5176 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5177 }
5178
5179 /**
5180 * Find the nearest view in the specified direction that can take focus.
5181 * This does not actually give focus to that view.
5182 *
5183 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5184 *
5185 * @return The nearest focusable in the specified direction, or null if none
5186 * can be found.
5187 */
5188 public View focusSearch(int direction) {
5189 if (mParent != null) {
5190 return mParent.focusSearch(this, direction);
5191 } else {
5192 return null;
5193 }
5194 }
5195
5196 /**
5197 * This method is the last chance for the focused view and its ancestors to
5198 * respond to an arrow key. This is called when the focused view did not
5199 * consume the key internally, nor could the view system find a new view in
5200 * the requested direction to give focus to.
5201 *
5202 * @param focused The currently focused view.
5203 * @param direction The direction focus wants to move. One of FOCUS_UP,
5204 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5205 * @return True if the this view consumed this unhandled move.
5206 */
5207 public boolean dispatchUnhandledMove(View focused, int direction) {
5208 return false;
5209 }
5210
5211 /**
5212 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005213 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005215 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5216 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 * @return The user specified next view, or null if there is none.
5218 */
5219 View findUserSetNextFocus(View root, int direction) {
5220 switch (direction) {
5221 case FOCUS_LEFT:
5222 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005223 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 case FOCUS_RIGHT:
5225 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005226 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 case FOCUS_UP:
5228 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005229 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 case FOCUS_DOWN:
5231 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005232 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005233 case FOCUS_FORWARD:
5234 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005235 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005236 case FOCUS_BACKWARD: {
5237 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005238 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005239 @Override
5240 public boolean apply(View t) {
5241 return t.mNextFocusForwardId == id;
5242 }
5243 });
5244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 }
5246 return null;
5247 }
5248
Jeff Brown4dfbec22011-08-15 14:55:37 -07005249 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5250 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5251 @Override
5252 public boolean apply(View t) {
5253 return t.mID == childViewId;
5254 }
5255 });
5256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 if (result == null) {
5258 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5259 + "by user for id " + childViewId);
5260 }
5261 return result;
5262 }
5263
5264 /**
5265 * Find and return all focusable views that are descendants of this view,
5266 * possibly including this view if it is focusable itself.
5267 *
5268 * @param direction The direction of the focus
5269 * @return A list of focusable views
5270 */
5271 public ArrayList<View> getFocusables(int direction) {
5272 ArrayList<View> result = new ArrayList<View>(24);
5273 addFocusables(result, direction);
5274 return result;
5275 }
5276
5277 /**
5278 * Add any focusable views that are descendants of this view (possibly
5279 * including this view if it is focusable itself) to views. If we are in touch mode,
5280 * only add views that are also focusable in touch mode.
5281 *
5282 * @param views Focusable views found so far
5283 * @param direction The direction of the focus
5284 */
5285 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005286 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288
svetoslavganov75986cf2009-05-14 22:28:01 -07005289 /**
5290 * Adds any focusable views that are descendants of this view (possibly
5291 * including this view if it is focusable itself) to views. This method
5292 * adds all focusable views regardless if we are in touch mode or
5293 * only views focusable in touch mode if we are in touch mode depending on
5294 * the focusable mode paramater.
5295 *
5296 * @param views Focusable views found so far or null if all we are interested is
5297 * the number of focusables.
5298 * @param direction The direction of the focus.
5299 * @param focusableMode The type of focusables to be added.
5300 *
5301 * @see #FOCUSABLES_ALL
5302 * @see #FOCUSABLES_TOUCH_MODE
5303 */
5304 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5305 if (!isFocusable()) {
5306 return;
5307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308
svetoslavganov75986cf2009-05-14 22:28:01 -07005309 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5310 isInTouchMode() && !isFocusableInTouchMode()) {
5311 return;
5312 }
5313
5314 if (views != null) {
5315 views.add(this);
5316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 }
5318
5319 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005320 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005321 * The search is performed by either the text that the View renders or the content
5322 * description that describes the view for accessibility purposes and the view does
5323 * not render or both. Clients can specify how the search is to be performed via
5324 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5325 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005326 *
5327 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005328 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005329 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005330 * @see #FIND_VIEWS_WITH_TEXT
5331 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5332 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005333 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005334 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005335 if (getAccessibilityNodeProvider() != null) {
5336 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5337 outViews.add(this);
5338 }
5339 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5340 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005341 String searchedLowerCase = searched.toString().toLowerCase();
5342 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5343 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5344 outViews.add(this);
5345 }
5346 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005347 }
5348
5349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 * Find and return all touchable views that are descendants of this view,
5351 * possibly including this view if it is touchable itself.
5352 *
5353 * @return A list of touchable views
5354 */
5355 public ArrayList<View> getTouchables() {
5356 ArrayList<View> result = new ArrayList<View>();
5357 addTouchables(result);
5358 return result;
5359 }
5360
5361 /**
5362 * Add any touchable views that are descendants of this view (possibly
5363 * including this view if it is touchable itself) to views.
5364 *
5365 * @param views Touchable views found so far
5366 */
5367 public void addTouchables(ArrayList<View> views) {
5368 final int viewFlags = mViewFlags;
5369
5370 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5371 && (viewFlags & ENABLED_MASK) == ENABLED) {
5372 views.add(this);
5373 }
5374 }
5375
5376 /**
5377 * Call this to try to give focus to a specific view or to one of its
5378 * descendants.
5379 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005380 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5381 * false), or if it is focusable and it is not focusable in touch mode
5382 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005384 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005385 * have focus, and you want your parent to look for the next one.
5386 *
5387 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5388 * {@link #FOCUS_DOWN} and <code>null</code>.
5389 *
5390 * @return Whether this view or one of its descendants actually took focus.
5391 */
5392 public final boolean requestFocus() {
5393 return requestFocus(View.FOCUS_DOWN);
5394 }
5395
5396
5397 /**
5398 * Call this to try to give focus to a specific view or to one of its
5399 * descendants and give it a hint about what direction focus is heading.
5400 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005401 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5402 * false), or if it is focusable and it is not focusable in touch mode
5403 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005404 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005405 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 * have focus, and you want your parent to look for the next one.
5407 *
5408 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5409 * <code>null</code> set for the previously focused rectangle.
5410 *
5411 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5412 * @return Whether this view or one of its descendants actually took focus.
5413 */
5414 public final boolean requestFocus(int direction) {
5415 return requestFocus(direction, null);
5416 }
5417
5418 /**
5419 * Call this to try to give focus to a specific view or to one of its descendants
5420 * and give it hints about the direction and a specific rectangle that the focus
5421 * is coming from. The rectangle can help give larger views a finer grained hint
5422 * about where focus is coming from, and therefore, where to show selection, or
5423 * forward focus change internally.
5424 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005425 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5426 * false), or if it is focusable and it is not focusable in touch mode
5427 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 *
5429 * A View will not take focus if it is not visible.
5430 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005431 * A View will not take focus if one of its parents has
5432 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5433 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005435 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005436 * have focus, and you want your parent to look for the next one.
5437 *
5438 * You may wish to override this method if your custom {@link View} has an internal
5439 * {@link View} that it wishes to forward the request to.
5440 *
5441 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5442 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5443 * to give a finer grained hint about where focus is coming from. May be null
5444 * if there is no hint.
5445 * @return Whether this view or one of its descendants actually took focus.
5446 */
5447 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5448 // need to be focusable
5449 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5450 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5451 return false;
5452 }
5453
5454 // need to be focusable in touch mode if in touch mode
5455 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005456 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5457 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 }
5459
5460 // need to not have any parents blocking us
5461 if (hasAncestorThatBlocksDescendantFocus()) {
5462 return false;
5463 }
5464
5465 handleFocusGainInternal(direction, previouslyFocusedRect);
5466 return true;
5467 }
5468
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005469 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005470 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005471 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005472 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005473 }
5474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 /**
5476 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5477 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5478 * touch mode to request focus when they are touched.
5479 *
5480 * @return Whether this view or one of its descendants actually took focus.
5481 *
5482 * @see #isInTouchMode()
5483 *
5484 */
5485 public final boolean requestFocusFromTouch() {
5486 // Leave touch mode if we need to
5487 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005488 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005489 if (viewRoot != null) {
5490 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 }
5492 }
5493 return requestFocus(View.FOCUS_DOWN);
5494 }
5495
5496 /**
5497 * @return Whether any ancestor of this view blocks descendant focus.
5498 */
5499 private boolean hasAncestorThatBlocksDescendantFocus() {
5500 ViewParent ancestor = mParent;
5501 while (ancestor instanceof ViewGroup) {
5502 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5503 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5504 return true;
5505 } else {
5506 ancestor = vgAncestor.getParent();
5507 }
5508 }
5509 return false;
5510 }
5511
5512 /**
Romain Guya440b002010-02-24 15:57:54 -08005513 * @hide
5514 */
5515 public void dispatchStartTemporaryDetach() {
5516 onStartTemporaryDetach();
5517 }
5518
5519 /**
5520 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005521 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5522 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005523 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 */
5525 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005526 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005527 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005528 }
5529
5530 /**
5531 * @hide
5532 */
5533 public void dispatchFinishTemporaryDetach() {
5534 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 }
Romain Guy8506ab42009-06-11 17:35:47 -07005536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 /**
5538 * Called after {@link #onStartTemporaryDetach} when the container is done
5539 * changing the view.
5540 */
5541 public void onFinishTemporaryDetach() {
5542 }
Romain Guy8506ab42009-06-11 17:35:47 -07005543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005545 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5546 * for this view's window. Returns null if the view is not currently attached
5547 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005548 * just use the standard high-level event callbacks like
5549 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005550 */
5551 public KeyEvent.DispatcherState getKeyDispatcherState() {
5552 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5553 }
Joe Malin32736f02011-01-19 16:14:20 -08005554
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 * Dispatch a key event before it is processed by any input method
5557 * associated with the view hierarchy. This can be used to intercept
5558 * key events in special situations before the IME consumes them; a
5559 * typical example would be handling the BACK key to update the application's
5560 * UI instead of allowing the IME to see it and close itself.
5561 *
5562 * @param event The key event to be dispatched.
5563 * @return True if the event was handled, false otherwise.
5564 */
5565 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5566 return onKeyPreIme(event.getKeyCode(), event);
5567 }
5568
5569 /**
5570 * Dispatch a key event to the next view on the focus path. This path runs
5571 * from the top of the view tree down to the currently focused view. If this
5572 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5573 * the next node down the focus path. This method also fires any key
5574 * listeners.
5575 *
5576 * @param event The key event to be dispatched.
5577 * @return True if the event was handled, false otherwise.
5578 */
5579 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005580 if (mInputEventConsistencyVerifier != null) {
5581 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583
Jeff Brown21bc5c92011-02-28 18:27:14 -08005584 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005585 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005586 ListenerInfo li = mListenerInfo;
5587 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5588 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 return true;
5590 }
5591
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005592 if (event.dispatch(this, mAttachInfo != null
5593 ? mAttachInfo.mKeyDispatchState : null, this)) {
5594 return true;
5595 }
5596
5597 if (mInputEventConsistencyVerifier != null) {
5598 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5599 }
5600 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005601 }
5602
5603 /**
5604 * Dispatches a key shortcut event.
5605 *
5606 * @param event The key event to be dispatched.
5607 * @return True if the event was handled by the view, false otherwise.
5608 */
5609 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5610 return onKeyShortcut(event.getKeyCode(), event);
5611 }
5612
5613 /**
5614 * Pass the touch screen motion event down to the target view, or this
5615 * view if it is the target.
5616 *
5617 * @param event The motion event to be dispatched.
5618 * @return True if the event was handled by the view, false otherwise.
5619 */
5620 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005621 if (mInputEventConsistencyVerifier != null) {
5622 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5623 }
5624
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005625 if (onFilterTouchEventForSecurity(event)) {
5626 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005627 ListenerInfo li = mListenerInfo;
5628 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5629 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005630 return true;
5631 }
5632
5633 if (onTouchEvent(event)) {
5634 return true;
5635 }
Jeff Brown85a31762010-09-01 17:01:00 -07005636 }
5637
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005638 if (mInputEventConsistencyVerifier != null) {
5639 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005641 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 }
5643
5644 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005645 * Filter the touch event to apply security policies.
5646 *
5647 * @param event The motion event to be filtered.
5648 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005649 *
Jeff Brown85a31762010-09-01 17:01:00 -07005650 * @see #getFilterTouchesWhenObscured
5651 */
5652 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005653 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005654 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5655 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5656 // Window is obscured, drop this touch.
5657 return false;
5658 }
5659 return true;
5660 }
5661
5662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005663 * Pass a trackball motion event down to the focused view.
5664 *
5665 * @param event The motion event to be dispatched.
5666 * @return True if the event was handled by the view, false otherwise.
5667 */
5668 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005669 if (mInputEventConsistencyVerifier != null) {
5670 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5671 }
5672
Romain Guy02ccac62011-06-24 13:20:23 -07005673 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 }
5675
5676 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005677 * Dispatch a generic motion event.
5678 * <p>
5679 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5680 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005681 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005682 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005683 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005684 *
5685 * @param event The motion event to be dispatched.
5686 * @return True if the event was handled by the view, false otherwise.
5687 */
5688 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005689 if (mInputEventConsistencyVerifier != null) {
5690 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5691 }
5692
Jeff Browna032cc02011-03-07 16:56:21 -08005693 final int source = event.getSource();
5694 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5695 final int action = event.getAction();
5696 if (action == MotionEvent.ACTION_HOVER_ENTER
5697 || action == MotionEvent.ACTION_HOVER_MOVE
5698 || action == MotionEvent.ACTION_HOVER_EXIT) {
5699 if (dispatchHoverEvent(event)) {
5700 return true;
5701 }
5702 } else if (dispatchGenericPointerEvent(event)) {
5703 return true;
5704 }
5705 } else if (dispatchGenericFocusedEvent(event)) {
5706 return true;
5707 }
5708
Jeff Brown10b62902011-06-20 16:40:37 -07005709 if (dispatchGenericMotionEventInternal(event)) {
5710 return true;
5711 }
5712
5713 if (mInputEventConsistencyVerifier != null) {
5714 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5715 }
5716 return false;
5717 }
5718
5719 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005720 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005721 ListenerInfo li = mListenerInfo;
5722 if (li != null && li.mOnGenericMotionListener != null
5723 && (mViewFlags & ENABLED_MASK) == ENABLED
5724 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005725 return true;
5726 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005727
5728 if (onGenericMotionEvent(event)) {
5729 return true;
5730 }
5731
5732 if (mInputEventConsistencyVerifier != null) {
5733 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5734 }
5735 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005736 }
5737
5738 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005739 * Dispatch a hover event.
5740 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005741 * Do not call this method directly.
5742 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005743 * </p>
5744 *
5745 * @param event The motion event to be dispatched.
5746 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005747 */
5748 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005749 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005750 ListenerInfo li = mListenerInfo;
5751 if (li != null && li.mOnHoverListener != null
5752 && (mViewFlags & ENABLED_MASK) == ENABLED
5753 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005754 return true;
5755 }
5756
Jeff Browna032cc02011-03-07 16:56:21 -08005757 return onHoverEvent(event);
5758 }
5759
5760 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005761 * Returns true if the view has a child to which it has recently sent
5762 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5763 * it does not have a hovered child, then it must be the innermost hovered view.
5764 * @hide
5765 */
5766 protected boolean hasHoveredChild() {
5767 return false;
5768 }
5769
5770 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005771 * Dispatch a generic motion event to the view under the first pointer.
5772 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005773 * Do not call this method directly.
5774 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005775 * </p>
5776 *
5777 * @param event The motion event to be dispatched.
5778 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005779 */
5780 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5781 return false;
5782 }
5783
5784 /**
5785 * Dispatch a generic motion event to the currently focused view.
5786 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005787 * Do not call this method directly.
5788 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005789 * </p>
5790 *
5791 * @param event The motion event to be dispatched.
5792 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005793 */
5794 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5795 return false;
5796 }
5797
5798 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005799 * Dispatch a pointer event.
5800 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005801 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5802 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5803 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005804 * and should not be expected to handle other pointing device features.
5805 * </p>
5806 *
5807 * @param event The motion event to be dispatched.
5808 * @return True if the event was handled by the view, false otherwise.
5809 * @hide
5810 */
5811 public final boolean dispatchPointerEvent(MotionEvent event) {
5812 if (event.isTouchEvent()) {
5813 return dispatchTouchEvent(event);
5814 } else {
5815 return dispatchGenericMotionEvent(event);
5816 }
5817 }
5818
5819 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 * Called when the window containing this view gains or loses window focus.
5821 * ViewGroups should override to route to their children.
5822 *
5823 * @param hasFocus True if the window containing this view now has focus,
5824 * false otherwise.
5825 */
5826 public void dispatchWindowFocusChanged(boolean hasFocus) {
5827 onWindowFocusChanged(hasFocus);
5828 }
5829
5830 /**
5831 * Called when the window containing this view gains or loses focus. Note
5832 * that this is separate from view focus: to receive key events, both
5833 * your view and its window must have focus. If a window is displayed
5834 * on top of yours that takes input focus, then your own window will lose
5835 * focus but the view focus will remain unchanged.
5836 *
5837 * @param hasWindowFocus True if the window containing this view now has
5838 * focus, false otherwise.
5839 */
5840 public void onWindowFocusChanged(boolean hasWindowFocus) {
5841 InputMethodManager imm = InputMethodManager.peekInstance();
5842 if (!hasWindowFocus) {
5843 if (isPressed()) {
5844 setPressed(false);
5845 }
5846 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5847 imm.focusOut(this);
5848 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005849 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005850 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005851 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5853 imm.focusIn(this);
5854 }
5855 refreshDrawableState();
5856 }
5857
5858 /**
5859 * Returns true if this view is in a window that currently has window focus.
5860 * Note that this is not the same as the view itself having focus.
5861 *
5862 * @return True if this view is in a window that currently has window focus.
5863 */
5864 public boolean hasWindowFocus() {
5865 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5866 }
5867
5868 /**
Adam Powell326d8082009-12-09 15:10:07 -08005869 * Dispatch a view visibility change down the view hierarchy.
5870 * ViewGroups should override to route to their children.
5871 * @param changedView The view whose visibility changed. Could be 'this' or
5872 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005873 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5874 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005875 */
5876 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5877 onVisibilityChanged(changedView, visibility);
5878 }
5879
5880 /**
5881 * Called when the visibility of the view or an ancestor of the view is changed.
5882 * @param changedView The view whose visibility changed. Could be 'this' or
5883 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005884 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5885 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005886 */
5887 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005888 if (visibility == VISIBLE) {
5889 if (mAttachInfo != null) {
5890 initialAwakenScrollBars();
5891 } else {
5892 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5893 }
5894 }
Adam Powell326d8082009-12-09 15:10:07 -08005895 }
5896
5897 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005898 * Dispatch a hint about whether this view is displayed. For instance, when
5899 * a View moves out of the screen, it might receives a display hint indicating
5900 * the view is not displayed. Applications should not <em>rely</em> on this hint
5901 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005902 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005903 * @param hint A hint about whether or not this view is displayed:
5904 * {@link #VISIBLE} or {@link #INVISIBLE}.
5905 */
5906 public void dispatchDisplayHint(int hint) {
5907 onDisplayHint(hint);
5908 }
5909
5910 /**
5911 * Gives this view a hint about whether is displayed or not. For instance, when
5912 * a View moves out of the screen, it might receives a display hint indicating
5913 * the view is not displayed. Applications should not <em>rely</em> on this hint
5914 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005915 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005916 * @param hint A hint about whether or not this view is displayed:
5917 * {@link #VISIBLE} or {@link #INVISIBLE}.
5918 */
5919 protected void onDisplayHint(int hint) {
5920 }
5921
5922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 * Dispatch a window visibility change down the view hierarchy.
5924 * ViewGroups should override to route to their children.
5925 *
5926 * @param visibility The new visibility of the window.
5927 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005928 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005929 */
5930 public void dispatchWindowVisibilityChanged(int visibility) {
5931 onWindowVisibilityChanged(visibility);
5932 }
5933
5934 /**
5935 * Called when the window containing has change its visibility
5936 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5937 * that this tells you whether or not your window is being made visible
5938 * to the window manager; this does <em>not</em> tell you whether or not
5939 * your window is obscured by other windows on the screen, even if it
5940 * is itself visible.
5941 *
5942 * @param visibility The new visibility of the window.
5943 */
5944 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005945 if (visibility == VISIBLE) {
5946 initialAwakenScrollBars();
5947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005948 }
5949
5950 /**
5951 * Returns the current visibility of the window this view is attached to
5952 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5953 *
5954 * @return Returns the current visibility of the view's window.
5955 */
5956 public int getWindowVisibility() {
5957 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5958 }
5959
5960 /**
5961 * Retrieve the overall visible display size in which the window this view is
5962 * attached to has been positioned in. This takes into account screen
5963 * decorations above the window, for both cases where the window itself
5964 * is being position inside of them or the window is being placed under
5965 * then and covered insets are used for the window to position its content
5966 * inside. In effect, this tells you the available area where content can
5967 * be placed and remain visible to users.
5968 *
5969 * <p>This function requires an IPC back to the window manager to retrieve
5970 * the requested information, so should not be used in performance critical
5971 * code like drawing.
5972 *
5973 * @param outRect Filled in with the visible display frame. If the view
5974 * is not attached to a window, this is simply the raw display size.
5975 */
5976 public void getWindowVisibleDisplayFrame(Rect outRect) {
5977 if (mAttachInfo != null) {
5978 try {
5979 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5980 } catch (RemoteException e) {
5981 return;
5982 }
5983 // XXX This is really broken, and probably all needs to be done
5984 // in the window manager, and we need to know more about whether
5985 // we want the area behind or in front of the IME.
5986 final Rect insets = mAttachInfo.mVisibleInsets;
5987 outRect.left += insets.left;
5988 outRect.top += insets.top;
5989 outRect.right -= insets.right;
5990 outRect.bottom -= insets.bottom;
5991 return;
5992 }
5993 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005994 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 }
5996
5997 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005998 * Dispatch a notification about a resource configuration change down
5999 * the view hierarchy.
6000 * ViewGroups should override to route to their children.
6001 *
6002 * @param newConfig The new resource configuration.
6003 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006004 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006005 */
6006 public void dispatchConfigurationChanged(Configuration newConfig) {
6007 onConfigurationChanged(newConfig);
6008 }
6009
6010 /**
6011 * Called when the current configuration of the resources being used
6012 * by the application have changed. You can use this to decide when
6013 * to reload resources that can changed based on orientation and other
6014 * configuration characterstics. You only need to use this if you are
6015 * not relying on the normal {@link android.app.Activity} mechanism of
6016 * recreating the activity instance upon a configuration change.
6017 *
6018 * @param newConfig The new resource configuration.
6019 */
6020 protected void onConfigurationChanged(Configuration newConfig) {
6021 }
6022
6023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 * Private function to aggregate all per-view attributes in to the view
6025 * root.
6026 */
6027 void dispatchCollectViewAttributes(int visibility) {
6028 performCollectViewAttributes(visibility);
6029 }
6030
6031 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006032 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006033 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6034 mAttachInfo.mKeepScreenOn = true;
6035 }
6036 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006037 ListenerInfo li = mListenerInfo;
6038 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006039 mAttachInfo.mHasSystemUiListeners = true;
6040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 }
6042 }
6043
6044 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006045 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006047 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6048 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 ai.mRecomputeGlobalAttributes = true;
6050 }
6051 }
6052 }
6053
6054 /**
6055 * Returns whether the device is currently in touch mode. Touch mode is entered
6056 * once the user begins interacting with the device by touch, and affects various
6057 * things like whether focus is always visible to the user.
6058 *
6059 * @return Whether the device is in touch mode.
6060 */
6061 @ViewDebug.ExportedProperty
6062 public boolean isInTouchMode() {
6063 if (mAttachInfo != null) {
6064 return mAttachInfo.mInTouchMode;
6065 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006066 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 }
6068 }
6069
6070 /**
6071 * Returns the context the view is running in, through which it can
6072 * access the current theme, resources, etc.
6073 *
6074 * @return The view's Context.
6075 */
6076 @ViewDebug.CapturedViewProperty
6077 public final Context getContext() {
6078 return mContext;
6079 }
6080
6081 /**
6082 * Handle a key event before it is processed by any input method
6083 * associated with the view hierarchy. This can be used to intercept
6084 * key events in special situations before the IME consumes them; a
6085 * typical example would be handling the BACK key to update the application's
6086 * UI instead of allowing the IME to see it and close itself.
6087 *
6088 * @param keyCode The value in event.getKeyCode().
6089 * @param event Description of the key event.
6090 * @return If you handled the event, return true. If you want to allow the
6091 * event to be handled by the next receiver, return false.
6092 */
6093 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6094 return false;
6095 }
6096
6097 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006098 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6099 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6101 * is released, if the view is enabled and clickable.
6102 *
6103 * @param keyCode A key code that represents the button pressed, from
6104 * {@link android.view.KeyEvent}.
6105 * @param event The KeyEvent object that defines the button action.
6106 */
6107 public boolean onKeyDown(int keyCode, KeyEvent event) {
6108 boolean result = false;
6109
6110 switch (keyCode) {
6111 case KeyEvent.KEYCODE_DPAD_CENTER:
6112 case KeyEvent.KEYCODE_ENTER: {
6113 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6114 return true;
6115 }
6116 // Long clickable items don't necessarily have to be clickable
6117 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6118 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6119 (event.getRepeatCount() == 0)) {
6120 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006121 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 return true;
6123 }
6124 break;
6125 }
6126 }
6127 return result;
6128 }
6129
6130 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006131 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6132 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6133 * the event).
6134 */
6135 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6136 return false;
6137 }
6138
6139 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006140 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6141 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006142 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6143 * {@link KeyEvent#KEYCODE_ENTER} is released.
6144 *
6145 * @param keyCode A key code that represents the button pressed, from
6146 * {@link android.view.KeyEvent}.
6147 * @param event The KeyEvent object that defines the button action.
6148 */
6149 public boolean onKeyUp(int keyCode, KeyEvent event) {
6150 boolean result = false;
6151
6152 switch (keyCode) {
6153 case KeyEvent.KEYCODE_DPAD_CENTER:
6154 case KeyEvent.KEYCODE_ENTER: {
6155 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6156 return true;
6157 }
6158 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6159 setPressed(false);
6160
6161 if (!mHasPerformedLongPress) {
6162 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006163 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164
6165 result = performClick();
6166 }
6167 }
6168 break;
6169 }
6170 }
6171 return result;
6172 }
6173
6174 /**
6175 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6176 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6177 * the event).
6178 *
6179 * @param keyCode A key code that represents the button pressed, from
6180 * {@link android.view.KeyEvent}.
6181 * @param repeatCount The number of times the action was made.
6182 * @param event The KeyEvent object that defines the button action.
6183 */
6184 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6185 return false;
6186 }
6187
6188 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006189 * Called on the focused view when a key shortcut event is not handled.
6190 * Override this method to implement local key shortcuts for the View.
6191 * Key shortcuts can also be implemented by setting the
6192 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006193 *
6194 * @param keyCode The value in event.getKeyCode().
6195 * @param event Description of the key event.
6196 * @return If you handled the event, return true. If you want to allow the
6197 * event to be handled by the next receiver, return false.
6198 */
6199 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6200 return false;
6201 }
6202
6203 /**
6204 * Check whether the called view is a text editor, in which case it
6205 * would make sense to automatically display a soft input window for
6206 * it. Subclasses should override this if they implement
6207 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006208 * a call on that method would return a non-null InputConnection, and
6209 * they are really a first-class editor that the user would normally
6210 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006211 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006212 * <p>The default implementation always returns false. This does
6213 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6214 * will not be called or the user can not otherwise perform edits on your
6215 * view; it is just a hint to the system that this is not the primary
6216 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006217 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 * @return Returns true if this view is a text editor, else false.
6219 */
6220 public boolean onCheckIsTextEditor() {
6221 return false;
6222 }
Romain Guy8506ab42009-06-11 17:35:47 -07006223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006224 /**
6225 * Create a new InputConnection for an InputMethod to interact
6226 * with the view. The default implementation returns null, since it doesn't
6227 * support input methods. You can override this to implement such support.
6228 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006229 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006230 * <p>When implementing this, you probably also want to implement
6231 * {@link #onCheckIsTextEditor()} to indicate you will return a
6232 * non-null InputConnection.
6233 *
6234 * @param outAttrs Fill in with attribute information about the connection.
6235 */
6236 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6237 return null;
6238 }
6239
6240 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006241 * Called by the {@link android.view.inputmethod.InputMethodManager}
6242 * when a view who is not the current
6243 * input connection target is trying to make a call on the manager. The
6244 * default implementation returns false; you can override this to return
6245 * true for certain views if you are performing InputConnection proxying
6246 * to them.
6247 * @param view The View that is making the InputMethodManager call.
6248 * @return Return true to allow the call, false to reject.
6249 */
6250 public boolean checkInputConnectionProxy(View view) {
6251 return false;
6252 }
Romain Guy8506ab42009-06-11 17:35:47 -07006253
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 * Show the context menu for this view. It is not safe to hold on to the
6256 * menu after returning from this method.
6257 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006258 * You should normally not overload this method. Overload
6259 * {@link #onCreateContextMenu(ContextMenu)} or define an
6260 * {@link OnCreateContextMenuListener} to add items to the context menu.
6261 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 * @param menu The context menu to populate
6263 */
6264 public void createContextMenu(ContextMenu menu) {
6265 ContextMenuInfo menuInfo = getContextMenuInfo();
6266
6267 // Sets the current menu info so all items added to menu will have
6268 // my extra info set.
6269 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6270
6271 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006272 ListenerInfo li = mListenerInfo;
6273 if (li != null && li.mOnCreateContextMenuListener != null) {
6274 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006275 }
6276
6277 // Clear the extra information so subsequent items that aren't mine don't
6278 // have my extra info.
6279 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6280
6281 if (mParent != null) {
6282 mParent.createContextMenu(menu);
6283 }
6284 }
6285
6286 /**
6287 * Views should implement this if they have extra information to associate
6288 * with the context menu. The return result is supplied as a parameter to
6289 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6290 * callback.
6291 *
6292 * @return Extra information about the item for which the context menu
6293 * should be shown. This information will vary across different
6294 * subclasses of View.
6295 */
6296 protected ContextMenuInfo getContextMenuInfo() {
6297 return null;
6298 }
6299
6300 /**
6301 * Views should implement this if the view itself is going to add items to
6302 * the context menu.
6303 *
6304 * @param menu the context menu to populate
6305 */
6306 protected void onCreateContextMenu(ContextMenu menu) {
6307 }
6308
6309 /**
6310 * Implement this method to handle trackball motion events. The
6311 * <em>relative</em> movement of the trackball since the last event
6312 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6313 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6314 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6315 * they will often be fractional values, representing the more fine-grained
6316 * movement information available from a trackball).
6317 *
6318 * @param event The motion event.
6319 * @return True if the event was handled, false otherwise.
6320 */
6321 public boolean onTrackballEvent(MotionEvent event) {
6322 return false;
6323 }
6324
6325 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006326 * Implement this method to handle generic motion events.
6327 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006328 * Generic motion events describe joystick movements, mouse hovers, track pad
6329 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006330 * {@link MotionEvent#getSource() source} of the motion event specifies
6331 * the class of input that was received. Implementations of this method
6332 * must examine the bits in the source before processing the event.
6333 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006334 * </p><p>
6335 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6336 * are delivered to the view under the pointer. All other generic motion events are
6337 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006338 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006339 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006340 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006341 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6342 * // process the joystick movement...
6343 * return true;
6344 * }
6345 * }
6346 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6347 * switch (event.getAction()) {
6348 * case MotionEvent.ACTION_HOVER_MOVE:
6349 * // process the mouse hover movement...
6350 * return true;
6351 * case MotionEvent.ACTION_SCROLL:
6352 * // process the scroll wheel movement...
6353 * return true;
6354 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006355 * }
6356 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006357 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006358 *
6359 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006360 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006361 */
6362 public boolean onGenericMotionEvent(MotionEvent event) {
6363 return false;
6364 }
6365
6366 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006367 * Implement this method to handle hover events.
6368 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006369 * This method is called whenever a pointer is hovering into, over, or out of the
6370 * bounds of a view and the view is not currently being touched.
6371 * Hover events are represented as pointer events with action
6372 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6373 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6374 * </p>
6375 * <ul>
6376 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6377 * when the pointer enters the bounds of the view.</li>
6378 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6379 * when the pointer has already entered the bounds of the view and has moved.</li>
6380 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6381 * when the pointer has exited the bounds of the view or when the pointer is
6382 * about to go down due to a button click, tap, or similar user action that
6383 * causes the view to be touched.</li>
6384 * </ul>
6385 * <p>
6386 * The view should implement this method to return true to indicate that it is
6387 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006388 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006389 * The default implementation calls {@link #setHovered} to update the hovered state
6390 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006391 * is enabled and is clickable. The default implementation also sends hover
6392 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006393 * </p>
6394 *
6395 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006396 * @return True if the view handled the hover event.
6397 *
6398 * @see #isHovered
6399 * @see #setHovered
6400 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006401 */
6402 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006403 // The root view may receive hover (or touch) events that are outside the bounds of
6404 // the window. This code ensures that we only send accessibility events for
6405 // hovers that are actually within the bounds of the root view.
6406 final int action = event.getAction();
6407 if (!mSendingHoverAccessibilityEvents) {
6408 if ((action == MotionEvent.ACTION_HOVER_ENTER
6409 || action == MotionEvent.ACTION_HOVER_MOVE)
6410 && !hasHoveredChild()
6411 && pointInView(event.getX(), event.getY())) {
6412 mSendingHoverAccessibilityEvents = true;
6413 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6414 }
6415 } else {
6416 if (action == MotionEvent.ACTION_HOVER_EXIT
6417 || (action == MotionEvent.ACTION_HOVER_MOVE
6418 && !pointInView(event.getX(), event.getY()))) {
6419 mSendingHoverAccessibilityEvents = false;
6420 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6421 }
Jeff Browna1b24182011-07-28 13:38:24 -07006422 }
6423
Jeff Brown87b7f802011-06-21 18:35:45 -07006424 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006425 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006426 case MotionEvent.ACTION_HOVER_ENTER:
6427 setHovered(true);
6428 break;
6429 case MotionEvent.ACTION_HOVER_EXIT:
6430 setHovered(false);
6431 break;
6432 }
Jeff Browna1b24182011-07-28 13:38:24 -07006433
6434 // Dispatch the event to onGenericMotionEvent before returning true.
6435 // This is to provide compatibility with existing applications that
6436 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6437 // break because of the new default handling for hoverable views
6438 // in onHoverEvent.
6439 // Note that onGenericMotionEvent will be called by default when
6440 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6441 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006442 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006443 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006444 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006445 }
6446
6447 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006448 * Returns true if the view should handle {@link #onHoverEvent}
6449 * by calling {@link #setHovered} to change its hovered state.
6450 *
6451 * @return True if the view is hoverable.
6452 */
6453 private boolean isHoverable() {
6454 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006455 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006456 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6457 return false;
6458 }
6459
6460 return (viewFlags & CLICKABLE) == CLICKABLE
6461 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6462 }
6463
6464 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006465 * Returns true if the view is currently hovered.
6466 *
6467 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006468 *
6469 * @see #setHovered
6470 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006471 */
Jeff Brown10b62902011-06-20 16:40:37 -07006472 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006473 public boolean isHovered() {
6474 return (mPrivateFlags & HOVERED) != 0;
6475 }
6476
6477 /**
6478 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006479 * <p>
6480 * Calling this method also changes the drawable state of the view. This
6481 * enables the view to react to hover by using different drawable resources
6482 * to change its appearance.
6483 * </p><p>
6484 * The {@link #onHoverChanged} method is called when the hovered state changes.
6485 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006486 *
6487 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006488 *
6489 * @see #isHovered
6490 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006491 */
6492 public void setHovered(boolean hovered) {
6493 if (hovered) {
6494 if ((mPrivateFlags & HOVERED) == 0) {
6495 mPrivateFlags |= HOVERED;
6496 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006497 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006498 }
6499 } else {
6500 if ((mPrivateFlags & HOVERED) != 0) {
6501 mPrivateFlags &= ~HOVERED;
6502 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006503 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006504 }
6505 }
6506 }
6507
6508 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006509 * Implement this method to handle hover state changes.
6510 * <p>
6511 * This method is called whenever the hover state changes as a result of a
6512 * call to {@link #setHovered}.
6513 * </p>
6514 *
6515 * @param hovered The current hover state, as returned by {@link #isHovered}.
6516 *
6517 * @see #isHovered
6518 * @see #setHovered
6519 */
6520 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006521 }
6522
6523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 * Implement this method to handle touch screen motion events.
6525 *
6526 * @param event The motion event.
6527 * @return True if the event was handled, false otherwise.
6528 */
6529 public boolean onTouchEvent(MotionEvent event) {
6530 final int viewFlags = mViewFlags;
6531
6532 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006533 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6534 mPrivateFlags &= ~PRESSED;
6535 refreshDrawableState();
6536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006537 // A disabled view that is clickable still consumes the touch
6538 // events, it just doesn't respond to them.
6539 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6540 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6541 }
6542
6543 if (mTouchDelegate != null) {
6544 if (mTouchDelegate.onTouchEvent(event)) {
6545 return true;
6546 }
6547 }
6548
6549 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6550 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6551 switch (event.getAction()) {
6552 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006553 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6554 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 // take focus if we don't have it already and we should in
6556 // touch mode.
6557 boolean focusTaken = false;
6558 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6559 focusTaken = requestFocus();
6560 }
6561
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006562 if (prepressed) {
6563 // The button is being released before we actually
6564 // showed it as pressed. Make it show the pressed
6565 // state now (before scheduling the click) to ensure
6566 // the user sees it.
6567 mPrivateFlags |= PRESSED;
6568 refreshDrawableState();
6569 }
Joe Malin32736f02011-01-19 16:14:20 -08006570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006571 if (!mHasPerformedLongPress) {
6572 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006573 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574
6575 // Only perform take click actions if we were in the pressed state
6576 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006577 // Use a Runnable and post this rather than calling
6578 // performClick directly. This lets other visual state
6579 // of the view update before click actions start.
6580 if (mPerformClick == null) {
6581 mPerformClick = new PerformClick();
6582 }
6583 if (!post(mPerformClick)) {
6584 performClick();
6585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 }
6587 }
6588
6589 if (mUnsetPressedState == null) {
6590 mUnsetPressedState = new UnsetPressedState();
6591 }
6592
Adam Powelle14579b2009-12-16 18:39:52 -08006593 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006594 postDelayed(mUnsetPressedState,
6595 ViewConfiguration.getPressedStateDuration());
6596 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 // If the post failed, unpress right now
6598 mUnsetPressedState.run();
6599 }
Adam Powelle14579b2009-12-16 18:39:52 -08006600 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 }
6602 break;
6603
6604 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006605 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006606
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006607 if (performButtonActionOnTouchDown(event)) {
6608 break;
6609 }
6610
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006611 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006612 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006613
6614 // For views inside a scrolling container, delay the pressed feedback for
6615 // a short period in case this is a scroll.
6616 if (isInScrollingContainer) {
6617 mPrivateFlags |= PREPRESSED;
6618 if (mPendingCheckForTap == null) {
6619 mPendingCheckForTap = new CheckForTap();
6620 }
6621 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6622 } else {
6623 // Not inside a scrolling container, so show the feedback right away
6624 mPrivateFlags |= PRESSED;
6625 refreshDrawableState();
6626 checkForLongClick(0);
6627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 break;
6629
6630 case MotionEvent.ACTION_CANCEL:
6631 mPrivateFlags &= ~PRESSED;
6632 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006633 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 break;
6635
6636 case MotionEvent.ACTION_MOVE:
6637 final int x = (int) event.getX();
6638 final int y = (int) event.getY();
6639
6640 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006641 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006642 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006643 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006645 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006646 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006647
6648 // Need to switch from pressed to not pressed
6649 mPrivateFlags &= ~PRESSED;
6650 refreshDrawableState();
6651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 }
6653 break;
6654 }
6655 return true;
6656 }
6657
6658 return false;
6659 }
6660
6661 /**
Adam Powell10298662011-08-14 18:26:30 -07006662 * @hide
6663 */
6664 public boolean isInScrollingContainer() {
6665 ViewParent p = getParent();
6666 while (p != null && p instanceof ViewGroup) {
6667 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6668 return true;
6669 }
6670 p = p.getParent();
6671 }
6672 return false;
6673 }
6674
6675 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006676 * Remove the longpress detection timer.
6677 */
6678 private void removeLongPressCallback() {
6679 if (mPendingCheckForLongPress != null) {
6680 removeCallbacks(mPendingCheckForLongPress);
6681 }
6682 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006683
6684 /**
6685 * Remove the pending click action
6686 */
6687 private void removePerformClickCallback() {
6688 if (mPerformClick != null) {
6689 removeCallbacks(mPerformClick);
6690 }
6691 }
6692
Adam Powelle14579b2009-12-16 18:39:52 -08006693 /**
Romain Guya440b002010-02-24 15:57:54 -08006694 * Remove the prepress detection timer.
6695 */
6696 private void removeUnsetPressCallback() {
6697 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6698 setPressed(false);
6699 removeCallbacks(mUnsetPressedState);
6700 }
6701 }
6702
6703 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006704 * Remove the tap detection timer.
6705 */
6706 private void removeTapCallback() {
6707 if (mPendingCheckForTap != null) {
6708 mPrivateFlags &= ~PREPRESSED;
6709 removeCallbacks(mPendingCheckForTap);
6710 }
6711 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006712
6713 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006714 * Cancels a pending long press. Your subclass can use this if you
6715 * want the context menu to come up if the user presses and holds
6716 * at the same place, but you don't want it to come up if they press
6717 * and then move around enough to cause scrolling.
6718 */
6719 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006720 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006721
6722 /*
6723 * The prepressed state handled by the tap callback is a display
6724 * construct, but the tap callback will post a long press callback
6725 * less its own timeout. Remove it here.
6726 */
6727 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 }
6729
6730 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006731 * Remove the pending callback for sending a
6732 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6733 */
6734 private void removeSendViewScrolledAccessibilityEventCallback() {
6735 if (mSendViewScrolledAccessibilityEvent != null) {
6736 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6737 }
6738 }
6739
6740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006741 * Sets the TouchDelegate for this View.
6742 */
6743 public void setTouchDelegate(TouchDelegate delegate) {
6744 mTouchDelegate = delegate;
6745 }
6746
6747 /**
6748 * Gets the TouchDelegate for this View.
6749 */
6750 public TouchDelegate getTouchDelegate() {
6751 return mTouchDelegate;
6752 }
6753
6754 /**
6755 * Set flags controlling behavior of this view.
6756 *
6757 * @param flags Constant indicating the value which should be set
6758 * @param mask Constant indicating the bit range that should be changed
6759 */
6760 void setFlags(int flags, int mask) {
6761 int old = mViewFlags;
6762 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6763
6764 int changed = mViewFlags ^ old;
6765 if (changed == 0) {
6766 return;
6767 }
6768 int privateFlags = mPrivateFlags;
6769
6770 /* Check if the FOCUSABLE bit has changed */
6771 if (((changed & FOCUSABLE_MASK) != 0) &&
6772 ((privateFlags & HAS_BOUNDS) !=0)) {
6773 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6774 && ((privateFlags & FOCUSED) != 0)) {
6775 /* Give up focus if we are no longer focusable */
6776 clearFocus();
6777 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6778 && ((privateFlags & FOCUSED) == 0)) {
6779 /*
6780 * Tell the view system that we are now available to take focus
6781 * if no one else already has it.
6782 */
6783 if (mParent != null) mParent.focusableViewAvailable(this);
6784 }
6785 }
6786
6787 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6788 if ((changed & VISIBILITY_MASK) != 0) {
6789 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006790 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006791 * it was not visible. Marking it drawn ensures that the invalidation will
6792 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006794 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006795 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796
6797 needGlobalAttributesUpdate(true);
6798
6799 // a view becoming visible is worth notifying the parent
6800 // about in case nothing has focus. even if this specific view
6801 // isn't focusable, it may contain something that is, so let
6802 // the root view try to give this focus if nothing else does.
6803 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6804 mParent.focusableViewAvailable(this);
6805 }
6806 }
6807 }
6808
6809 /* Check if the GONE bit has changed */
6810 if ((changed & GONE) != 0) {
6811 needGlobalAttributesUpdate(false);
6812 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813
Romain Guyecd80ee2009-12-03 17:13:02 -08006814 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6815 if (hasFocus()) clearFocus();
6816 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006817 if (mParent instanceof View) {
6818 // GONE views noop invalidation, so invalidate the parent
6819 ((View) mParent).invalidate(true);
6820 }
6821 // Mark the view drawn to ensure that it gets invalidated properly the next
6822 // time it is visible and gets invalidated
6823 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 }
6825 if (mAttachInfo != null) {
6826 mAttachInfo.mViewVisibilityChanged = true;
6827 }
6828 }
6829
6830 /* Check if the VISIBLE bit has changed */
6831 if ((changed & INVISIBLE) != 0) {
6832 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006833 /*
6834 * If this view is becoming invisible, set the DRAWN flag so that
6835 * the next invalidate() will not be skipped.
6836 */
6837 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006838
6839 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6840 // root view becoming invisible shouldn't clear focus
6841 if (getRootView() != this) {
6842 clearFocus();
6843 }
6844 }
6845 if (mAttachInfo != null) {
6846 mAttachInfo.mViewVisibilityChanged = true;
6847 }
6848 }
6849
Adam Powell326d8082009-12-09 15:10:07 -08006850 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006851 if (mParent instanceof ViewGroup) {
Chet Haase0d299362012-01-26 10:51:48 -08006852 ((ViewGroup) mParent).onChildVisibilityChanged(this, (changed & VISIBILITY_MASK),
6853 (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006854 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006855 } else if (mParent != null) {
6856 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006857 }
Adam Powell326d8082009-12-09 15:10:07 -08006858 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6859 }
6860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6862 destroyDrawingCache();
6863 }
6864
6865 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6866 destroyDrawingCache();
6867 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006868 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 }
6870
6871 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6872 destroyDrawingCache();
6873 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6874 }
6875
6876 if ((changed & DRAW_MASK) != 0) {
6877 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6878 if (mBGDrawable != null) {
6879 mPrivateFlags &= ~SKIP_DRAW;
6880 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6881 } else {
6882 mPrivateFlags |= SKIP_DRAW;
6883 }
6884 } else {
6885 mPrivateFlags &= ~SKIP_DRAW;
6886 }
6887 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006888 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006889 }
6890
6891 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006892 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 mParent.recomputeViewAttributes(this);
6894 }
6895 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006896
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006897 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006898 requestLayout();
6899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 }
6901
6902 /**
6903 * Change the view's z order in the tree, so it's on top of other sibling
6904 * views
6905 */
6906 public void bringToFront() {
6907 if (mParent != null) {
6908 mParent.bringChildToFront(this);
6909 }
6910 }
6911
6912 /**
6913 * This is called in response to an internal scroll in this view (i.e., the
6914 * view scrolled its own contents). This is typically as a result of
6915 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6916 * called.
6917 *
6918 * @param l Current horizontal scroll origin.
6919 * @param t Current vertical scroll origin.
6920 * @param oldl Previous horizontal scroll origin.
6921 * @param oldt Previous vertical scroll origin.
6922 */
6923 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006924 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6925 postSendViewScrolledAccessibilityEventCallback();
6926 }
6927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 mBackgroundSizeChanged = true;
6929
6930 final AttachInfo ai = mAttachInfo;
6931 if (ai != null) {
6932 ai.mViewScrollChanged = true;
6933 }
6934 }
6935
6936 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006937 * Interface definition for a callback to be invoked when the layout bounds of a view
6938 * changes due to layout processing.
6939 */
6940 public interface OnLayoutChangeListener {
6941 /**
6942 * Called when the focus state of a view has changed.
6943 *
6944 * @param v The view whose state has changed.
6945 * @param left The new value of the view's left property.
6946 * @param top The new value of the view's top property.
6947 * @param right The new value of the view's right property.
6948 * @param bottom The new value of the view's bottom property.
6949 * @param oldLeft The previous value of the view's left property.
6950 * @param oldTop The previous value of the view's top property.
6951 * @param oldRight The previous value of the view's right property.
6952 * @param oldBottom The previous value of the view's bottom property.
6953 */
6954 void onLayoutChange(View v, int left, int top, int right, int bottom,
6955 int oldLeft, int oldTop, int oldRight, int oldBottom);
6956 }
6957
6958 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 * This is called during layout when the size of this view has changed. If
6960 * you were just added to the view hierarchy, you're called with the old
6961 * values of 0.
6962 *
6963 * @param w Current width of this view.
6964 * @param h Current height of this view.
6965 * @param oldw Old width of this view.
6966 * @param oldh Old height of this view.
6967 */
6968 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6969 }
6970
6971 /**
6972 * Called by draw to draw the child views. This may be overridden
6973 * by derived classes to gain control just before its children are drawn
6974 * (but after its own view has been drawn).
6975 * @param canvas the canvas on which to draw the view
6976 */
6977 protected void dispatchDraw(Canvas canvas) {
6978 }
6979
6980 /**
6981 * Gets the parent of this view. Note that the parent is a
6982 * ViewParent and not necessarily a View.
6983 *
6984 * @return Parent of this view.
6985 */
6986 public final ViewParent getParent() {
6987 return mParent;
6988 }
6989
6990 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006991 * Set the horizontal scrolled position of your view. This will cause a call to
6992 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6993 * invalidated.
6994 * @param value the x position to scroll to
6995 */
6996 public void setScrollX(int value) {
6997 scrollTo(value, mScrollY);
6998 }
6999
7000 /**
7001 * Set the vertical scrolled position of your view. This will cause a call to
7002 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7003 * invalidated.
7004 * @param value the y position to scroll to
7005 */
7006 public void setScrollY(int value) {
7007 scrollTo(mScrollX, value);
7008 }
7009
7010 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 * Return the scrolled left position of this view. This is the left edge of
7012 * the displayed part of your view. You do not need to draw any pixels
7013 * farther left, since those are outside of the frame of your view on
7014 * screen.
7015 *
7016 * @return The left edge of the displayed part of your view, in pixels.
7017 */
7018 public final int getScrollX() {
7019 return mScrollX;
7020 }
7021
7022 /**
7023 * Return the scrolled top position of this view. This is the top edge of
7024 * the displayed part of your view. You do not need to draw any pixels above
7025 * it, since those are outside of the frame of your view on screen.
7026 *
7027 * @return The top edge of the displayed part of your view, in pixels.
7028 */
7029 public final int getScrollY() {
7030 return mScrollY;
7031 }
7032
7033 /**
7034 * Return the width of the your view.
7035 *
7036 * @return The width of your view, in pixels.
7037 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007038 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039 public final int getWidth() {
7040 return mRight - mLeft;
7041 }
7042
7043 /**
7044 * Return the height of your view.
7045 *
7046 * @return The height of your view, in pixels.
7047 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007048 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007049 public final int getHeight() {
7050 return mBottom - mTop;
7051 }
7052
7053 /**
7054 * Return the visible drawing bounds of your view. Fills in the output
7055 * rectangle with the values from getScrollX(), getScrollY(),
7056 * getWidth(), and getHeight().
7057 *
7058 * @param outRect The (scrolled) drawing bounds of the view.
7059 */
7060 public void getDrawingRect(Rect outRect) {
7061 outRect.left = mScrollX;
7062 outRect.top = mScrollY;
7063 outRect.right = mScrollX + (mRight - mLeft);
7064 outRect.bottom = mScrollY + (mBottom - mTop);
7065 }
7066
7067 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007068 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7069 * raw width component (that is the result is masked by
7070 * {@link #MEASURED_SIZE_MASK}).
7071 *
7072 * @return The raw measured width of this view.
7073 */
7074 public final int getMeasuredWidth() {
7075 return mMeasuredWidth & MEASURED_SIZE_MASK;
7076 }
7077
7078 /**
7079 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007080 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007081 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 * This should be used during measurement and layout calculations only. Use
7083 * {@link #getWidth()} to see how wide a view is after layout.
7084 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007085 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007087 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 return mMeasuredWidth;
7089 }
7090
7091 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007092 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7093 * raw width component (that is the result is masked by
7094 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007095 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007096 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097 */
7098 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007099 return mMeasuredHeight & MEASURED_SIZE_MASK;
7100 }
7101
7102 /**
7103 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007104 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007105 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7106 * This should be used during measurement and layout calculations only. Use
7107 * {@link #getHeight()} to see how wide a view is after layout.
7108 *
7109 * @return The measured width of this view as a bit mask.
7110 */
7111 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 return mMeasuredHeight;
7113 }
7114
7115 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007116 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7117 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7118 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7119 * and the height component is at the shifted bits
7120 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7121 */
7122 public final int getMeasuredState() {
7123 return (mMeasuredWidth&MEASURED_STATE_MASK)
7124 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7125 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7126 }
7127
7128 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007129 * The transform matrix of this view, which is calculated based on the current
7130 * roation, scale, and pivot properties.
7131 *
7132 * @see #getRotation()
7133 * @see #getScaleX()
7134 * @see #getScaleY()
7135 * @see #getPivotX()
7136 * @see #getPivotY()
7137 * @return The current transform matrix for the view
7138 */
7139 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007140 if (mTransformationInfo != null) {
7141 updateMatrix();
7142 return mTransformationInfo.mMatrix;
7143 }
7144 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007145 }
7146
7147 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007148 * Utility function to determine if the value is far enough away from zero to be
7149 * considered non-zero.
7150 * @param value A floating point value to check for zero-ness
7151 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7152 */
7153 private static boolean nonzero(float value) {
7154 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7155 }
7156
7157 /**
Jeff Brown86671742010-09-30 20:00:15 -07007158 * Returns true if the transform matrix is the identity matrix.
7159 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007160 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007161 * @return True if the transform matrix is the identity matrix, false otherwise.
7162 */
Jeff Brown86671742010-09-30 20:00:15 -07007163 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007164 if (mTransformationInfo != null) {
7165 updateMatrix();
7166 return mTransformationInfo.mMatrixIsIdentity;
7167 }
7168 return true;
7169 }
7170
7171 void ensureTransformationInfo() {
7172 if (mTransformationInfo == null) {
7173 mTransformationInfo = new TransformationInfo();
7174 }
Jeff Brown86671742010-09-30 20:00:15 -07007175 }
7176
7177 /**
7178 * Recomputes the transform matrix if necessary.
7179 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007180 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007181 final TransformationInfo info = mTransformationInfo;
7182 if (info == null) {
7183 return;
7184 }
7185 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007186 // transform-related properties have changed since the last time someone
7187 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007188
7189 // Figure out if we need to update the pivot point
7190 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007191 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7192 info.mPrevWidth = mRight - mLeft;
7193 info.mPrevHeight = mBottom - mTop;
7194 info.mPivotX = info.mPrevWidth / 2f;
7195 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007196 }
7197 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007198 info.mMatrix.reset();
7199 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7200 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7201 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7202 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007203 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007204 if (info.mCamera == null) {
7205 info.mCamera = new Camera();
7206 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007207 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007208 info.mCamera.save();
7209 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7210 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7211 info.mCamera.getMatrix(info.matrix3D);
7212 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7213 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7214 info.mPivotY + info.mTranslationY);
7215 info.mMatrix.postConcat(info.matrix3D);
7216 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007217 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007218 info.mMatrixDirty = false;
7219 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7220 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007221 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007222 }
7223
7224 /**
7225 * Utility method to retrieve the inverse of the current mMatrix property.
7226 * We cache the matrix to avoid recalculating it when transform properties
7227 * have not changed.
7228 *
7229 * @return The inverse of the current matrix of this view.
7230 */
Jeff Brown86671742010-09-30 20:00:15 -07007231 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007232 final TransformationInfo info = mTransformationInfo;
7233 if (info != null) {
7234 updateMatrix();
7235 if (info.mInverseMatrixDirty) {
7236 if (info.mInverseMatrix == null) {
7237 info.mInverseMatrix = new Matrix();
7238 }
7239 info.mMatrix.invert(info.mInverseMatrix);
7240 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007241 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007242 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007243 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007244 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007245 }
7246
7247 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007248 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7249 * views are drawn) from the camera to this view. The camera's distance
7250 * affects 3D transformations, for instance rotations around the X and Y
7251 * axis. If the rotationX or rotationY properties are changed and this view is
7252 * large (more than half the size of the screen), it is recommended to always
7253 * use a camera distance that's greater than the height (X axis rotation) or
7254 * the width (Y axis rotation) of this view.</p>
7255 *
7256 * <p>The distance of the camera from the view plane can have an affect on the
7257 * perspective distortion of the view when it is rotated around the x or y axis.
7258 * For example, a large distance will result in a large viewing angle, and there
7259 * will not be much perspective distortion of the view as it rotates. A short
7260 * distance may cause much more perspective distortion upon rotation, and can
7261 * also result in some drawing artifacts if the rotated view ends up partially
7262 * behind the camera (which is why the recommendation is to use a distance at
7263 * least as far as the size of the view, if the view is to be rotated.)</p>
7264 *
7265 * <p>The distance is expressed in "depth pixels." The default distance depends
7266 * on the screen density. For instance, on a medium density display, the
7267 * default distance is 1280. On a high density display, the default distance
7268 * is 1920.</p>
7269 *
7270 * <p>If you want to specify a distance that leads to visually consistent
7271 * results across various densities, use the following formula:</p>
7272 * <pre>
7273 * float scale = context.getResources().getDisplayMetrics().density;
7274 * view.setCameraDistance(distance * scale);
7275 * </pre>
7276 *
7277 * <p>The density scale factor of a high density display is 1.5,
7278 * and 1920 = 1280 * 1.5.</p>
7279 *
7280 * @param distance The distance in "depth pixels", if negative the opposite
7281 * value is used
7282 *
7283 * @see #setRotationX(float)
7284 * @see #setRotationY(float)
7285 */
7286 public void setCameraDistance(float distance) {
7287 invalidateParentCaches();
7288 invalidate(false);
7289
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007290 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007291 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007292 final TransformationInfo info = mTransformationInfo;
7293 if (info.mCamera == null) {
7294 info.mCamera = new Camera();
7295 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007296 }
7297
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007298 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7299 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007300
7301 invalidate(false);
7302 }
7303
7304 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007305 * The degrees that the view is rotated around the pivot point.
7306 *
Romain Guya5364ee2011-02-24 14:46:04 -08007307 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007308 * @see #getPivotX()
7309 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007310 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007311 * @return The degrees of rotation.
7312 */
Chet Haasea5531132012-02-02 13:41:44 -08007313 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007314 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007315 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007316 }
7317
7318 /**
Chet Haase897247b2010-09-09 14:54:47 -07007319 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7320 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007321 *
7322 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007323 *
7324 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007325 * @see #getPivotX()
7326 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007327 * @see #setRotationX(float)
7328 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007329 *
7330 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007331 */
7332 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007333 ensureTransformationInfo();
7334 final TransformationInfo info = mTransformationInfo;
7335 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007336 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007337 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007338 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007339 info.mRotation = rotation;
7340 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007341 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007342 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007343 }
7344 }
7345
7346 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007347 * The degrees that the view is rotated around the vertical axis through the pivot point.
7348 *
7349 * @see #getPivotX()
7350 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007351 * @see #setRotationY(float)
7352 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007353 * @return The degrees of Y rotation.
7354 */
Chet Haasea5531132012-02-02 13:41:44 -08007355 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007356 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007357 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007358 }
7359
7360 /**
Chet Haase897247b2010-09-09 14:54:47 -07007361 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7362 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7363 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007364 *
7365 * When rotating large views, it is recommended to adjust the camera distance
7366 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007367 *
7368 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007369 *
7370 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007371 * @see #getPivotX()
7372 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007373 * @see #setRotation(float)
7374 * @see #setRotationX(float)
7375 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007376 *
7377 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007378 */
7379 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007380 ensureTransformationInfo();
7381 final TransformationInfo info = mTransformationInfo;
7382 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007383 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007384 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007385 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007386 info.mRotationY = rotationY;
7387 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007388 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007389 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007390 }
7391 }
7392
7393 /**
7394 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7395 *
7396 * @see #getPivotX()
7397 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007398 * @see #setRotationX(float)
7399 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007400 * @return The degrees of X rotation.
7401 */
Chet Haasea5531132012-02-02 13:41:44 -08007402 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007403 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007404 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007405 }
7406
7407 /**
Chet Haase897247b2010-09-09 14:54:47 -07007408 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7409 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7410 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007411 *
7412 * When rotating large views, it is recommended to adjust the camera distance
7413 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007414 *
7415 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007416 *
7417 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007418 * @see #getPivotX()
7419 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007420 * @see #setRotation(float)
7421 * @see #setRotationY(float)
7422 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007423 *
7424 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007425 */
7426 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007427 ensureTransformationInfo();
7428 final TransformationInfo info = mTransformationInfo;
7429 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007430 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007431 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007432 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007433 info.mRotationX = rotationX;
7434 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007435 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007436 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007437 }
7438 }
7439
7440 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007441 * The amount that the view is scaled in x around the pivot point, as a proportion of
7442 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7443 *
Joe Onorato93162322010-09-16 15:42:01 -04007444 * <p>By default, this is 1.0f.
7445 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007446 * @see #getPivotX()
7447 * @see #getPivotY()
7448 * @return The scaling factor.
7449 */
Chet Haasea5531132012-02-02 13:41:44 -08007450 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007451 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007452 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007453 }
7454
7455 /**
7456 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7457 * the view's unscaled width. A value of 1 means that no scaling is applied.
7458 *
7459 * @param scaleX The scaling factor.
7460 * @see #getPivotX()
7461 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007462 *
7463 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007464 */
7465 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007466 ensureTransformationInfo();
7467 final TransformationInfo info = mTransformationInfo;
7468 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007469 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007470 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007471 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007472 info.mScaleX = scaleX;
7473 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007474 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007475 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007476 }
7477 }
7478
7479 /**
7480 * The amount that the view is scaled in y around the pivot point, as a proportion of
7481 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7482 *
Joe Onorato93162322010-09-16 15:42:01 -04007483 * <p>By default, this is 1.0f.
7484 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007485 * @see #getPivotX()
7486 * @see #getPivotY()
7487 * @return The scaling factor.
7488 */
Chet Haasea5531132012-02-02 13:41:44 -08007489 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007490 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007491 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007492 }
7493
7494 /**
7495 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7496 * the view's unscaled width. A value of 1 means that no scaling is applied.
7497 *
7498 * @param scaleY The scaling factor.
7499 * @see #getPivotX()
7500 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007501 *
7502 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007503 */
7504 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007505 ensureTransformationInfo();
7506 final TransformationInfo info = mTransformationInfo;
7507 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007508 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007509 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007510 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007511 info.mScaleY = scaleY;
7512 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007513 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007514 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007515 }
7516 }
7517
7518 /**
7519 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7520 * and {@link #setScaleX(float) scaled}.
7521 *
7522 * @see #getRotation()
7523 * @see #getScaleX()
7524 * @see #getScaleY()
7525 * @see #getPivotY()
7526 * @return The x location of the pivot point.
7527 */
Chet Haasea5531132012-02-02 13:41:44 -08007528 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007529 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007530 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007531 }
7532
7533 /**
7534 * Sets the x location of the point around which the view is
7535 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007536 * 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 pivotX The x 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_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007547 */
7548 public void setPivotX(float pivotX) {
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.mPivotX != pivotX) {
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.mPivotX = pivotX;
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 y location of the point around which the view is {@link #setRotation(float) rotated}
7565 * and {@link #setScaleY(float) scaled}.
7566 *
7567 * @see #getRotation()
7568 * @see #getScaleX()
7569 * @see #getScaleY()
7570 * @see #getPivotY()
7571 * @return The y location of the pivot point.
7572 */
Chet Haasea5531132012-02-02 13:41:44 -08007573 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007574 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007575 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007576 }
7577
7578 /**
7579 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007580 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7581 * Setting this property disables this behavior and causes the view to use only the
7582 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007583 *
7584 * @param pivotY The y location of the pivot point.
7585 * @see #getRotation()
7586 * @see #getScaleX()
7587 * @see #getScaleY()
7588 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007589 *
7590 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007591 */
7592 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007593 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007594 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007595 final TransformationInfo info = mTransformationInfo;
7596 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007597 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007598 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007599 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007600 info.mPivotY = pivotY;
7601 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007602 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007603 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007604 }
7605 }
7606
7607 /**
7608 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7609 * completely transparent and 1 means the view is completely opaque.
7610 *
Joe Onorato93162322010-09-16 15:42:01 -04007611 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007612 * @return The opacity of the view.
7613 */
Chet Haasea5531132012-02-02 13:41:44 -08007614 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007615 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007616 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007617 }
7618
7619 /**
Romain Guy171c5922011-01-06 10:04:23 -08007620 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7621 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007622 *
Romain Guy171c5922011-01-06 10:04:23 -08007623 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7624 * responsible for applying the opacity itself. Otherwise, calling this method is
7625 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007626 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007627 *
Chet Haasea5531132012-02-02 13:41:44 -08007628 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7629 * performance implications. It is generally best to use the alpha property sparingly and
7630 * transiently, as in the case of fading animations.</p>
7631 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007632 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007633 *
Joe Malin32736f02011-01-19 16:14:20 -08007634 * @see #setLayerType(int, android.graphics.Paint)
7635 *
Chet Haase73066682010-11-29 15:55:32 -08007636 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007637 */
7638 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007639 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007640 if (mTransformationInfo.mAlpha != alpha) {
7641 mTransformationInfo.mAlpha = alpha;
7642 invalidateParentCaches();
7643 if (onSetAlpha((int) (alpha * 255))) {
7644 mPrivateFlags |= ALPHA_SET;
7645 // subclass is handling alpha - don't optimize rendering cache invalidation
7646 invalidate(true);
7647 } else {
7648 mPrivateFlags &= ~ALPHA_SET;
7649 invalidate(false);
7650 }
Chet Haaseed032702010-10-01 14:05:54 -07007651 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007652 }
7653
7654 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007655 * Faster version of setAlpha() which performs the same steps except there are
7656 * no calls to invalidate(). The caller of this function should perform proper invalidation
7657 * on the parent and this object. The return value indicates whether the subclass handles
7658 * alpha (the return value for onSetAlpha()).
7659 *
7660 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007661 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7662 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007663 */
7664 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007665 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007666 if (mTransformationInfo.mAlpha != alpha) {
7667 mTransformationInfo.mAlpha = alpha;
7668 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7669 if (subclassHandlesAlpha) {
7670 mPrivateFlags |= ALPHA_SET;
7671 return true;
7672 } else {
7673 mPrivateFlags &= ~ALPHA_SET;
7674 }
Chet Haasea00f3862011-02-22 06:34:40 -08007675 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007676 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007677 }
7678
7679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680 * Top position of this view relative to its parent.
7681 *
7682 * @return The top of this view, in pixels.
7683 */
7684 @ViewDebug.CapturedViewProperty
7685 public final int getTop() {
7686 return mTop;
7687 }
7688
7689 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007690 * Sets the top position of this view relative to its parent. This method is meant to be called
7691 * by the layout system and should not generally be called otherwise, because the property
7692 * may be changed at any time by the layout.
7693 *
7694 * @param top The top of this view, in pixels.
7695 */
7696 public final void setTop(int top) {
7697 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007698 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007699 final boolean matrixIsIdentity = mTransformationInfo == null
7700 || mTransformationInfo.mMatrixIsIdentity;
7701 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007702 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007703 int minTop;
7704 int yLoc;
7705 if (top < mTop) {
7706 minTop = top;
7707 yLoc = top - mTop;
7708 } else {
7709 minTop = mTop;
7710 yLoc = 0;
7711 }
Chet Haasee9140a72011-02-16 16:23:29 -08007712 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007713 }
7714 } else {
7715 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007716 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007717 }
7718
Chet Haaseed032702010-10-01 14:05:54 -07007719 int width = mRight - mLeft;
7720 int oldHeight = mBottom - mTop;
7721
Chet Haase21cd1382010-09-01 17:42:29 -07007722 mTop = top;
7723
Chet Haaseed032702010-10-01 14:05:54 -07007724 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7725
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007726 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007727 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7728 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007729 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007730 }
Chet Haase21cd1382010-09-01 17:42:29 -07007731 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007732 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007733 }
Chet Haase55dbb652010-12-21 20:15:08 -08007734 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007735 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007736 }
7737 }
7738
7739 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007740 * Bottom position of this view relative to its parent.
7741 *
7742 * @return The bottom of this view, in pixels.
7743 */
7744 @ViewDebug.CapturedViewProperty
7745 public final int getBottom() {
7746 return mBottom;
7747 }
7748
7749 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007750 * True if this view has changed since the last time being drawn.
7751 *
7752 * @return The dirty state of this view.
7753 */
7754 public boolean isDirty() {
7755 return (mPrivateFlags & DIRTY_MASK) != 0;
7756 }
7757
7758 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007759 * Sets the bottom position of this view relative to its parent. This method is meant to be
7760 * called by the layout system and should not generally be called otherwise, because the
7761 * property may be changed at any time by the layout.
7762 *
7763 * @param bottom The bottom of this view, in pixels.
7764 */
7765 public final void setBottom(int bottom) {
7766 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007767 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007768 final boolean matrixIsIdentity = mTransformationInfo == null
7769 || mTransformationInfo.mMatrixIsIdentity;
7770 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007771 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007772 int maxBottom;
7773 if (bottom < mBottom) {
7774 maxBottom = mBottom;
7775 } else {
7776 maxBottom = bottom;
7777 }
Chet Haasee9140a72011-02-16 16:23:29 -08007778 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007779 }
7780 } else {
7781 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007782 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007783 }
7784
Chet Haaseed032702010-10-01 14:05:54 -07007785 int width = mRight - mLeft;
7786 int oldHeight = mBottom - mTop;
7787
Chet Haase21cd1382010-09-01 17:42:29 -07007788 mBottom = bottom;
7789
Chet Haaseed032702010-10-01 14:05:54 -07007790 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7791
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007792 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007793 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7794 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007795 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007796 }
Chet Haase21cd1382010-09-01 17:42:29 -07007797 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007798 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007799 }
Chet Haase55dbb652010-12-21 20:15:08 -08007800 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007801 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007802 }
7803 }
7804
7805 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007806 * Left position of this view relative to its parent.
7807 *
7808 * @return The left edge of this view, in pixels.
7809 */
7810 @ViewDebug.CapturedViewProperty
7811 public final int getLeft() {
7812 return mLeft;
7813 }
7814
7815 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007816 * Sets the left position of this view relative to its parent. This method is meant to be called
7817 * by the layout system and should not generally be called otherwise, because the property
7818 * may be changed at any time by the layout.
7819 *
7820 * @param left The bottom of this view, in pixels.
7821 */
7822 public final void setLeft(int left) {
7823 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007824 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007825 final boolean matrixIsIdentity = mTransformationInfo == null
7826 || mTransformationInfo.mMatrixIsIdentity;
7827 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007828 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007829 int minLeft;
7830 int xLoc;
7831 if (left < mLeft) {
7832 minLeft = left;
7833 xLoc = left - mLeft;
7834 } else {
7835 minLeft = mLeft;
7836 xLoc = 0;
7837 }
Chet Haasee9140a72011-02-16 16:23:29 -08007838 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007839 }
7840 } else {
7841 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007842 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007843 }
7844
Chet Haaseed032702010-10-01 14:05:54 -07007845 int oldWidth = mRight - mLeft;
7846 int height = mBottom - mTop;
7847
Chet Haase21cd1382010-09-01 17:42:29 -07007848 mLeft = left;
7849
Chet Haaseed032702010-10-01 14:05:54 -07007850 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7851
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007852 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007853 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7854 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007855 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007856 }
Chet Haase21cd1382010-09-01 17:42:29 -07007857 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007858 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007859 }
Chet Haase55dbb652010-12-21 20:15:08 -08007860 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007861 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007862 }
7863 }
7864
7865 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007866 * Right position of this view relative to its parent.
7867 *
7868 * @return The right edge of this view, in pixels.
7869 */
7870 @ViewDebug.CapturedViewProperty
7871 public final int getRight() {
7872 return mRight;
7873 }
7874
7875 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007876 * Sets the right position of this view relative to its parent. This method is meant to be called
7877 * by the layout system and should not generally be called otherwise, because the property
7878 * may be changed at any time by the layout.
7879 *
7880 * @param right The bottom of this view, in pixels.
7881 */
7882 public final void setRight(int right) {
7883 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007884 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007885 final boolean matrixIsIdentity = mTransformationInfo == null
7886 || mTransformationInfo.mMatrixIsIdentity;
7887 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007888 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007889 int maxRight;
7890 if (right < mRight) {
7891 maxRight = mRight;
7892 } else {
7893 maxRight = right;
7894 }
Chet Haasee9140a72011-02-16 16:23:29 -08007895 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007896 }
7897 } else {
7898 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007899 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007900 }
7901
Chet Haaseed032702010-10-01 14:05:54 -07007902 int oldWidth = mRight - mLeft;
7903 int height = mBottom - mTop;
7904
Chet Haase21cd1382010-09-01 17:42:29 -07007905 mRight = right;
7906
Chet Haaseed032702010-10-01 14:05:54 -07007907 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7908
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007909 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007910 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7911 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007912 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007913 }
Chet Haase21cd1382010-09-01 17:42:29 -07007914 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007915 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007916 }
Chet Haase55dbb652010-12-21 20:15:08 -08007917 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007918 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007919 }
7920 }
7921
7922 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007923 * The visual x position of this view, in pixels. This is equivalent to the
7924 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007925 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007926 *
Chet Haasedf030d22010-07-30 17:22:38 -07007927 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007928 */
Chet Haasea5531132012-02-02 13:41:44 -08007929 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007930 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007931 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007932 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007933
Chet Haasedf030d22010-07-30 17:22:38 -07007934 /**
7935 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7936 * {@link #setTranslationX(float) translationX} property to be the difference between
7937 * the x value passed in and the current {@link #getLeft() left} property.
7938 *
7939 * @param x The visual x position of this view, in pixels.
7940 */
7941 public void setX(float x) {
7942 setTranslationX(x - mLeft);
7943 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007944
Chet Haasedf030d22010-07-30 17:22:38 -07007945 /**
7946 * The visual y position of this view, in pixels. This is equivalent to the
7947 * {@link #setTranslationY(float) translationY} property plus the current
7948 * {@link #getTop() top} property.
7949 *
7950 * @return The visual y position of this view, in pixels.
7951 */
Chet Haasea5531132012-02-02 13:41:44 -08007952 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007953 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007954 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007955 }
7956
7957 /**
7958 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7959 * {@link #setTranslationY(float) translationY} property to be the difference between
7960 * the y value passed in and the current {@link #getTop() top} property.
7961 *
7962 * @param y The visual y position of this view, in pixels.
7963 */
7964 public void setY(float y) {
7965 setTranslationY(y - mTop);
7966 }
7967
7968
7969 /**
7970 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7971 * This position is post-layout, in addition to wherever the object's
7972 * layout placed it.
7973 *
7974 * @return The horizontal position of this view relative to its left position, in pixels.
7975 */
Chet Haasea5531132012-02-02 13:41:44 -08007976 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007977 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007978 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007979 }
7980
7981 /**
7982 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7983 * This effectively positions the object post-layout, in addition to wherever the object's
7984 * layout placed it.
7985 *
7986 * @param translationX The horizontal position of this view relative to its left position,
7987 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007988 *
7989 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007990 */
7991 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007992 ensureTransformationInfo();
7993 final TransformationInfo info = mTransformationInfo;
7994 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007995 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007996 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007997 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007998 info.mTranslationX = translationX;
7999 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008000 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008001 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008002 }
8003 }
8004
8005 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008006 * The horizontal location of this view relative to its {@link #getTop() top} position.
8007 * This position is post-layout, in addition to wherever the object's
8008 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008009 *
Chet Haasedf030d22010-07-30 17:22:38 -07008010 * @return The vertical position of this view relative to its top position,
8011 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008012 */
Chet Haasea5531132012-02-02 13:41:44 -08008013 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008014 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008015 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008016 }
8017
8018 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008019 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8020 * This effectively positions the object post-layout, in addition to wherever the object's
8021 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008022 *
Chet Haasedf030d22010-07-30 17:22:38 -07008023 * @param translationY The vertical position of this view relative to its top position,
8024 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008025 *
8026 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008027 */
Chet Haasedf030d22010-07-30 17:22:38 -07008028 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008029 ensureTransformationInfo();
8030 final TransformationInfo info = mTransformationInfo;
8031 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008032 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008033 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008034 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008035 info.mTranslationY = translationY;
8036 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008037 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008038 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07008039 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008040 }
8041
8042 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043 * Hit rectangle in parent's coordinates
8044 *
8045 * @param outRect The hit rectangle of the view.
8046 */
8047 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008048 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008049 final TransformationInfo info = mTransformationInfo;
8050 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008051 outRect.set(mLeft, mTop, mRight, mBottom);
8052 } else {
8053 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008054 tmpRect.set(-info.mPivotX, -info.mPivotY,
8055 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8056 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008057 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8058 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008059 }
8060 }
8061
8062 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008063 * Determines whether the given point, in local coordinates is inside the view.
8064 */
8065 /*package*/ final boolean pointInView(float localX, float localY) {
8066 return localX >= 0 && localX < (mRight - mLeft)
8067 && localY >= 0 && localY < (mBottom - mTop);
8068 }
8069
8070 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008071 * Utility method to determine whether the given point, in local coordinates,
8072 * is inside the view, where the area of the view is expanded by the slop factor.
8073 * This method is called while processing touch-move events to determine if the event
8074 * is still within the view.
8075 */
8076 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008077 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008078 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008079 }
8080
8081 /**
8082 * When a view has focus and the user navigates away from it, the next view is searched for
8083 * starting from the rectangle filled in by this method.
8084 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008085 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8086 * of the view. However, if your view maintains some idea of internal selection,
8087 * such as a cursor, or a selected row or column, you should override this method and
8088 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089 *
8090 * @param r The rectangle to fill in, in this view's coordinates.
8091 */
8092 public void getFocusedRect(Rect r) {
8093 getDrawingRect(r);
8094 }
8095
8096 /**
8097 * If some part of this view is not clipped by any of its parents, then
8098 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008099 * coordinates (without taking possible View rotations into account), offset
8100 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8101 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008102 *
8103 * @param r If true is returned, r holds the global coordinates of the
8104 * visible portion of this view.
8105 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8106 * between this view and its root. globalOffet may be null.
8107 * @return true if r is non-empty (i.e. part of the view is visible at the
8108 * root level.
8109 */
8110 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8111 int width = mRight - mLeft;
8112 int height = mBottom - mTop;
8113 if (width > 0 && height > 0) {
8114 r.set(0, 0, width, height);
8115 if (globalOffset != null) {
8116 globalOffset.set(-mScrollX, -mScrollY);
8117 }
8118 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8119 }
8120 return false;
8121 }
8122
8123 public final boolean getGlobalVisibleRect(Rect r) {
8124 return getGlobalVisibleRect(r, null);
8125 }
8126
8127 public final boolean getLocalVisibleRect(Rect r) {
8128 Point offset = new Point();
8129 if (getGlobalVisibleRect(r, offset)) {
8130 r.offset(-offset.x, -offset.y); // make r local
8131 return true;
8132 }
8133 return false;
8134 }
8135
8136 /**
8137 * Offset this view's vertical location by the specified number of pixels.
8138 *
8139 * @param offset the number of pixels to offset the view by
8140 */
8141 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008142 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008143 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008144 final boolean matrixIsIdentity = mTransformationInfo == null
8145 || mTransformationInfo.mMatrixIsIdentity;
8146 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008147 final ViewParent p = mParent;
8148 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008149 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008150 int minTop;
8151 int maxBottom;
8152 int yLoc;
8153 if (offset < 0) {
8154 minTop = mTop + offset;
8155 maxBottom = mBottom;
8156 yLoc = offset;
8157 } else {
8158 minTop = mTop;
8159 maxBottom = mBottom + offset;
8160 yLoc = 0;
8161 }
8162 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8163 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008164 }
8165 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008166 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008167 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008168
Chet Haasec3aa3612010-06-17 08:50:37 -07008169 mTop += offset;
8170 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008171
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008172 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008173 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008174 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008175 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008176 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 }
8179
8180 /**
8181 * Offset this view's horizontal location by the specified amount of pixels.
8182 *
8183 * @param offset the numer of pixels to offset the view by
8184 */
8185 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008186 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008187 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008188 final boolean matrixIsIdentity = mTransformationInfo == null
8189 || mTransformationInfo.mMatrixIsIdentity;
8190 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008191 final ViewParent p = mParent;
8192 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008193 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008194 int minLeft;
8195 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008196 if (offset < 0) {
8197 minLeft = mLeft + offset;
8198 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008199 } else {
8200 minLeft = mLeft;
8201 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008202 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008203 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008204 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008205 }
8206 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008207 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008208 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008209
Chet Haasec3aa3612010-06-17 08:50:37 -07008210 mLeft += offset;
8211 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008212
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008213 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008214 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008215 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008216 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008217 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008219 }
8220
8221 /**
8222 * Get the LayoutParams associated with this view. All views should have
8223 * layout parameters. These supply parameters to the <i>parent</i> of this
8224 * view specifying how it should be arranged. There are many subclasses of
8225 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8226 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008227 *
8228 * This method may return null if this View is not attached to a parent
8229 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8230 * was not invoked successfully. When a View is attached to a parent
8231 * ViewGroup, this method must not return null.
8232 *
8233 * @return The LayoutParams associated with this view, or null if no
8234 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008236 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008237 public ViewGroup.LayoutParams getLayoutParams() {
8238 return mLayoutParams;
8239 }
8240
8241 /**
8242 * Set the layout parameters associated with this view. These supply
8243 * parameters to the <i>parent</i> of this view specifying how it should be
8244 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8245 * correspond to the different subclasses of ViewGroup that are responsible
8246 * for arranging their children.
8247 *
Romain Guy01c174b2011-02-22 11:51:06 -08008248 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008249 */
8250 public void setLayoutParams(ViewGroup.LayoutParams params) {
8251 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008252 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253 }
8254 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008255 if (mParent instanceof ViewGroup) {
8256 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 requestLayout();
8259 }
8260
8261 /**
8262 * Set the scrolled position of your view. This will cause a call to
8263 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8264 * invalidated.
8265 * @param x the x position to scroll to
8266 * @param y the y position to scroll to
8267 */
8268 public void scrollTo(int x, int y) {
8269 if (mScrollX != x || mScrollY != y) {
8270 int oldX = mScrollX;
8271 int oldY = mScrollY;
8272 mScrollX = x;
8273 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008274 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008276 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008277 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279 }
8280 }
8281
8282 /**
8283 * Move the scrolled position of your view. This will cause a call to
8284 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8285 * invalidated.
8286 * @param x the amount of pixels to scroll by horizontally
8287 * @param y the amount of pixels to scroll by vertically
8288 */
8289 public void scrollBy(int x, int y) {
8290 scrollTo(mScrollX + x, mScrollY + y);
8291 }
8292
8293 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008294 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8295 * animation to fade the scrollbars out after a default delay. If a subclass
8296 * provides animated scrolling, the start delay should equal the duration
8297 * of the scrolling animation.</p>
8298 *
8299 * <p>The animation starts only if at least one of the scrollbars is
8300 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8301 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8302 * this method returns true, and false otherwise. If the animation is
8303 * started, this method calls {@link #invalidate()}; in that case the
8304 * caller should not call {@link #invalidate()}.</p>
8305 *
8306 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008307 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008308 *
8309 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8310 * and {@link #scrollTo(int, int)}.</p>
8311 *
8312 * @return true if the animation is played, false otherwise
8313 *
8314 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008315 * @see #scrollBy(int, int)
8316 * @see #scrollTo(int, int)
8317 * @see #isHorizontalScrollBarEnabled()
8318 * @see #isVerticalScrollBarEnabled()
8319 * @see #setHorizontalScrollBarEnabled(boolean)
8320 * @see #setVerticalScrollBarEnabled(boolean)
8321 */
8322 protected boolean awakenScrollBars() {
8323 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008324 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008325 }
8326
8327 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008328 * Trigger the scrollbars to draw.
8329 * This method differs from awakenScrollBars() only in its default duration.
8330 * initialAwakenScrollBars() will show the scroll bars for longer than
8331 * usual to give the user more of a chance to notice them.
8332 *
8333 * @return true if the animation is played, false otherwise.
8334 */
8335 private boolean initialAwakenScrollBars() {
8336 return mScrollCache != null &&
8337 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8338 }
8339
8340 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008341 * <p>
8342 * Trigger the scrollbars to draw. When invoked this method starts an
8343 * animation to fade the scrollbars out after a fixed delay. If a subclass
8344 * provides animated scrolling, the start delay should equal the duration of
8345 * the scrolling animation.
8346 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008347 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008348 * <p>
8349 * The animation starts only if at least one of the scrollbars is enabled,
8350 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8351 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8352 * this method returns true, and false otherwise. If the animation is
8353 * started, this method calls {@link #invalidate()}; in that case the caller
8354 * should not call {@link #invalidate()}.
8355 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008356 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008357 * <p>
8358 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008359 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008360 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008361 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008362 * @param startDelay the delay, in milliseconds, after which the animation
8363 * should start; when the delay is 0, the animation starts
8364 * immediately
8365 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008366 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008367 * @see #scrollBy(int, int)
8368 * @see #scrollTo(int, int)
8369 * @see #isHorizontalScrollBarEnabled()
8370 * @see #isVerticalScrollBarEnabled()
8371 * @see #setHorizontalScrollBarEnabled(boolean)
8372 * @see #setVerticalScrollBarEnabled(boolean)
8373 */
8374 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008375 return awakenScrollBars(startDelay, true);
8376 }
Joe Malin32736f02011-01-19 16:14:20 -08008377
Mike Cleron290947b2009-09-29 18:34:32 -07008378 /**
8379 * <p>
8380 * Trigger the scrollbars to draw. When invoked this method starts an
8381 * animation to fade the scrollbars out after a fixed delay. If a subclass
8382 * provides animated scrolling, the start delay should equal the duration of
8383 * the scrolling animation.
8384 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008385 *
Mike Cleron290947b2009-09-29 18:34:32 -07008386 * <p>
8387 * The animation starts only if at least one of the scrollbars is enabled,
8388 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8389 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8390 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008391 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008392 * is set to true; in that case the caller
8393 * should not call {@link #invalidate()}.
8394 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008395 *
Mike Cleron290947b2009-09-29 18:34:32 -07008396 * <p>
8397 * This method should be invoked everytime a subclass directly updates the
8398 * scroll parameters.
8399 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008400 *
Mike Cleron290947b2009-09-29 18:34:32 -07008401 * @param startDelay the delay, in milliseconds, after which the animation
8402 * should start; when the delay is 0, the animation starts
8403 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008404 *
Mike Cleron290947b2009-09-29 18:34:32 -07008405 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008406 *
Mike Cleron290947b2009-09-29 18:34:32 -07008407 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008408 *
Mike Cleron290947b2009-09-29 18:34:32 -07008409 * @see #scrollBy(int, int)
8410 * @see #scrollTo(int, int)
8411 * @see #isHorizontalScrollBarEnabled()
8412 * @see #isVerticalScrollBarEnabled()
8413 * @see #setHorizontalScrollBarEnabled(boolean)
8414 * @see #setVerticalScrollBarEnabled(boolean)
8415 */
8416 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008417 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008418
Mike Cleronf116bf82009-09-27 19:14:12 -07008419 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8420 return false;
8421 }
8422
8423 if (scrollCache.scrollBar == null) {
8424 scrollCache.scrollBar = new ScrollBarDrawable();
8425 }
8426
8427 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8428
Mike Cleron290947b2009-09-29 18:34:32 -07008429 if (invalidate) {
8430 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008431 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008432 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008433
8434 if (scrollCache.state == ScrollabilityCache.OFF) {
8435 // FIXME: this is copied from WindowManagerService.
8436 // We should get this value from the system when it
8437 // is possible to do so.
8438 final int KEY_REPEAT_FIRST_DELAY = 750;
8439 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8440 }
8441
8442 // Tell mScrollCache when we should start fading. This may
8443 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008444 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008445 scrollCache.fadeStartTime = fadeStartTime;
8446 scrollCache.state = ScrollabilityCache.ON;
8447
8448 // Schedule our fader to run, unscheduling any old ones first
8449 if (mAttachInfo != null) {
8450 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8451 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8452 }
8453
8454 return true;
8455 }
8456
8457 return false;
8458 }
8459
8460 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008461 * Do not invalidate views which are not visible and which are not running an animation. They
8462 * will not get drawn and they should not set dirty flags as if they will be drawn
8463 */
8464 private boolean skipInvalidate() {
8465 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8466 (!(mParent instanceof ViewGroup) ||
8467 !((ViewGroup) mParent).isViewTransitioning(this));
8468 }
8469 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008470 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008471 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8472 * in the future. This must be called from a UI thread. To call from a non-UI
8473 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008474 *
8475 * WARNING: This method is destructive to dirty.
8476 * @param dirty the rectangle representing the bounds of the dirty region
8477 */
8478 public void invalidate(Rect dirty) {
8479 if (ViewDebug.TRACE_HIERARCHY) {
8480 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8481 }
8482
Chet Haaseaceafe62011-08-26 15:44:33 -07008483 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008484 return;
8485 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008486 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008487 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8488 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008489 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008490 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008491 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 final ViewParent p = mParent;
8493 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008494 //noinspection PointlessBooleanExpression,ConstantConditions
8495 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8496 if (p != null && ai != null && ai.mHardwareAccelerated) {
8497 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008498 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008499 p.invalidateChild(this, null);
8500 return;
8501 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008503 if (p != null && ai != null) {
8504 final int scrollX = mScrollX;
8505 final int scrollY = mScrollY;
8506 final Rect r = ai.mTmpInvalRect;
8507 r.set(dirty.left - scrollX, dirty.top - scrollY,
8508 dirty.right - scrollX, dirty.bottom - scrollY);
8509 mParent.invalidateChild(this, r);
8510 }
8511 }
8512 }
8513
8514 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008515 * 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 -08008516 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008517 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8518 * will be called at some point in the future. This must be called from
8519 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 * @param l the left position of the dirty region
8521 * @param t the top position of the dirty region
8522 * @param r the right position of the dirty region
8523 * @param b the bottom position of the dirty region
8524 */
8525 public void invalidate(int l, int t, int r, int b) {
8526 if (ViewDebug.TRACE_HIERARCHY) {
8527 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8528 }
8529
Chet Haaseaceafe62011-08-26 15:44:33 -07008530 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008531 return;
8532 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008533 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008534 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8535 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008537 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008538 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008539 final ViewParent p = mParent;
8540 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008541 //noinspection PointlessBooleanExpression,ConstantConditions
8542 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8543 if (p != null && ai != null && ai.mHardwareAccelerated) {
8544 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008545 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008546 p.invalidateChild(this, null);
8547 return;
8548 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008550 if (p != null && ai != null && l < r && t < b) {
8551 final int scrollX = mScrollX;
8552 final int scrollY = mScrollY;
8553 final Rect tmpr = ai.mTmpInvalRect;
8554 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8555 p.invalidateChild(this, tmpr);
8556 }
8557 }
8558 }
8559
8560 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008561 * Invalidate the whole view. If the view is visible,
8562 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8563 * the future. This must be called from a UI thread. To call from a non-UI thread,
8564 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 */
8566 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008567 invalidate(true);
8568 }
Joe Malin32736f02011-01-19 16:14:20 -08008569
Chet Haaseed032702010-10-01 14:05:54 -07008570 /**
8571 * This is where the invalidate() work actually happens. A full invalidate()
8572 * causes the drawing cache to be invalidated, but this function can be called with
8573 * invalidateCache set to false to skip that invalidation step for cases that do not
8574 * need it (for example, a component that remains at the same dimensions with the same
8575 * content).
8576 *
8577 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8578 * well. This is usually true for a full invalidate, but may be set to false if the
8579 * View's contents or dimensions have not changed.
8580 */
Romain Guy849d0a32011-02-01 17:20:48 -08008581 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 if (ViewDebug.TRACE_HIERARCHY) {
8583 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8584 }
8585
Chet Haaseaceafe62011-08-26 15:44:33 -07008586 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008587 return;
8588 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008589 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008590 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008591 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8592 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008593 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008594 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008595 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008596 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008597 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008600 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008601 //noinspection PointlessBooleanExpression,ConstantConditions
8602 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8603 if (p != null && ai != null && ai.mHardwareAccelerated) {
8604 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008605 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008606 p.invalidateChild(this, null);
8607 return;
8608 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008609 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611 if (p != null && ai != null) {
8612 final Rect r = ai.mTmpInvalRect;
8613 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8614 // Don't call invalidate -- we don't want to internally scroll
8615 // our own bounds
8616 p.invalidateChild(this, r);
8617 }
8618 }
8619 }
8620
8621 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008622 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008623 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8624 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008625 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8626 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008627 *
8628 * @hide
8629 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008630 protected void invalidateParentCaches() {
8631 if (mParent instanceof View) {
8632 ((View) mParent).mPrivateFlags |= INVALIDATED;
8633 }
8634 }
Joe Malin32736f02011-01-19 16:14:20 -08008635
Romain Guy0fd89bf2011-01-26 15:41:30 -08008636 /**
8637 * Used to indicate that the parent of this view should be invalidated. This functionality
8638 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8639 * which is necessary when various parent-managed properties of the view change, such as
8640 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8641 * an invalidation event to the parent.
8642 *
8643 * @hide
8644 */
8645 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008646 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008647 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008648 }
8649 }
8650
8651 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008652 * Indicates whether this View is opaque. An opaque View guarantees that it will
8653 * draw all the pixels overlapping its bounds using a fully opaque color.
8654 *
8655 * Subclasses of View should override this method whenever possible to indicate
8656 * whether an instance is opaque. Opaque Views are treated in a special way by
8657 * the View hierarchy, possibly allowing it to perform optimizations during
8658 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008659 *
Romain Guy24443ea2009-05-11 11:56:30 -07008660 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008661 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008662 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008663 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008664 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008665 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8666 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008667 }
8668
Adam Powell20232d02010-12-08 21:08:53 -08008669 /**
8670 * @hide
8671 */
8672 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008673 // Opaque if:
8674 // - Has a background
8675 // - Background is opaque
8676 // - Doesn't have scrollbars or scrollbars are inside overlay
8677
8678 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8679 mPrivateFlags |= OPAQUE_BACKGROUND;
8680 } else {
8681 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8682 }
8683
8684 final int flags = mViewFlags;
8685 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8686 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8687 mPrivateFlags |= OPAQUE_SCROLLBARS;
8688 } else {
8689 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8690 }
8691 }
8692
8693 /**
8694 * @hide
8695 */
8696 protected boolean hasOpaqueScrollbars() {
8697 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008698 }
8699
8700 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008701 * @return A handler associated with the thread running the View. This
8702 * handler can be used to pump events in the UI events queue.
8703 */
8704 public Handler getHandler() {
8705 if (mAttachInfo != null) {
8706 return mAttachInfo.mHandler;
8707 }
8708 return null;
8709 }
8710
8711 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008712 * <p>Causes the Runnable to be added to the message queue.
8713 * The runnable will be run on the user interface thread.</p>
8714 *
8715 * <p>This method can be invoked from outside of the UI thread
8716 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 *
8718 * @param action The Runnable that will be executed.
8719 *
8720 * @return Returns true if the Runnable was successfully placed in to the
8721 * message queue. Returns false on failure, usually because the
8722 * looper processing the message queue is exiting.
8723 */
8724 public boolean post(Runnable action) {
8725 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008726 AttachInfo attachInfo = mAttachInfo;
8727 if (attachInfo != null) {
8728 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008729 } else {
8730 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008731 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008732 return true;
8733 }
8734
8735 return handler.post(action);
8736 }
8737
8738 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008739 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008741 * The runnable will be run on the user interface thread.</p>
8742 *
8743 * <p>This method can be invoked from outside of the UI thread
8744 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008745 *
8746 * @param action The Runnable that will be executed.
8747 * @param delayMillis The delay (in milliseconds) until the Runnable
8748 * will be executed.
8749 *
8750 * @return true if the Runnable was successfully placed in to the
8751 * message queue. Returns false on failure, usually because the
8752 * looper processing the message queue is exiting. Note that a
8753 * result of true does not mean the Runnable will be processed --
8754 * if the looper is quit before the delivery time of the message
8755 * occurs then the message will be dropped.
8756 */
8757 public boolean postDelayed(Runnable action, long delayMillis) {
8758 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008759 AttachInfo attachInfo = mAttachInfo;
8760 if (attachInfo != null) {
8761 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 } else {
8763 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008764 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 return true;
8766 }
8767
8768 return handler.postDelayed(action, delayMillis);
8769 }
8770
8771 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008772 * <p>Removes the specified Runnable from the message queue.</p>
8773 *
8774 * <p>This method can be invoked from outside of the UI thread
8775 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008776 *
8777 * @param action The Runnable to remove from the message handling queue
8778 *
8779 * @return true if this view could ask the Handler to remove the Runnable,
8780 * false otherwise. When the returned value is true, the Runnable
8781 * may or may not have been actually removed from the message queue
8782 * (for instance, if the Runnable was not in the queue already.)
8783 */
8784 public boolean removeCallbacks(Runnable action) {
8785 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008786 AttachInfo attachInfo = mAttachInfo;
8787 if (attachInfo != null) {
8788 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008789 } else {
8790 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008791 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008792 return true;
8793 }
8794
8795 handler.removeCallbacks(action);
8796 return true;
8797 }
8798
8799 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008800 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8801 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008802 *
Romain Guye63a4f32011-08-11 11:33:31 -07008803 * <p>This method can be invoked from outside of the UI thread
8804 * only when this View is attached to a window.</p>
8805 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008806 * @see #invalidate()
8807 */
8808 public void postInvalidate() {
8809 postInvalidateDelayed(0);
8810 }
8811
8812 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008813 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8814 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8815 *
8816 * <p>This method can be invoked from outside of the UI thread
8817 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008818 *
8819 * @param left The left coordinate of the rectangle to invalidate.
8820 * @param top The top coordinate of the rectangle to invalidate.
8821 * @param right The right coordinate of the rectangle to invalidate.
8822 * @param bottom The bottom coordinate of the rectangle to invalidate.
8823 *
8824 * @see #invalidate(int, int, int, int)
8825 * @see #invalidate(Rect)
8826 */
8827 public void postInvalidate(int left, int top, int right, int bottom) {
8828 postInvalidateDelayed(0, left, top, right, bottom);
8829 }
8830
8831 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008832 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8833 * loop. Waits for the specified amount of time.</p>
8834 *
8835 * <p>This method can be invoked from outside of the UI thread
8836 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008837 *
8838 * @param delayMilliseconds the duration in milliseconds to delay the
8839 * invalidation by
8840 */
8841 public void postInvalidateDelayed(long delayMilliseconds) {
8842 // We try only with the AttachInfo because there's no point in invalidating
8843 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008844 AttachInfo attachInfo = mAttachInfo;
8845 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008846 Message msg = Message.obtain();
8847 msg.what = AttachInfo.INVALIDATE_MSG;
8848 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008849 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 }
8851 }
8852
8853 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008854 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8855 * through the event loop. Waits for the specified amount of time.</p>
8856 *
8857 * <p>This method can be invoked from outside of the UI thread
8858 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008859 *
8860 * @param delayMilliseconds the duration in milliseconds to delay the
8861 * invalidation by
8862 * @param left The left coordinate of the rectangle to invalidate.
8863 * @param top The top coordinate of the rectangle to invalidate.
8864 * @param right The right coordinate of the rectangle to invalidate.
8865 * @param bottom The bottom coordinate of the rectangle to invalidate.
8866 */
8867 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8868 int right, int bottom) {
8869
8870 // We try only with the AttachInfo because there's no point in invalidating
8871 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008872 AttachInfo attachInfo = mAttachInfo;
8873 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008874 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8875 info.target = this;
8876 info.left = left;
8877 info.top = top;
8878 info.right = right;
8879 info.bottom = bottom;
8880
8881 final Message msg = Message.obtain();
8882 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8883 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008884 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008885 }
8886 }
8887
8888 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008889 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8890 * This event is sent at most once every
8891 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8892 */
8893 private void postSendViewScrolledAccessibilityEventCallback() {
8894 if (mSendViewScrolledAccessibilityEvent == null) {
8895 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8896 }
8897 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8898 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8899 postDelayed(mSendViewScrolledAccessibilityEvent,
8900 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8901 }
8902 }
8903
8904 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 * Called by a parent to request that a child update its values for mScrollX
8906 * and mScrollY if necessary. This will typically be done if the child is
8907 * animating a scroll using a {@link android.widget.Scroller Scroller}
8908 * object.
8909 */
8910 public void computeScroll() {
8911 }
8912
8913 /**
8914 * <p>Indicate whether the horizontal edges are faded when the view is
8915 * scrolled horizontally.</p>
8916 *
8917 * @return true if the horizontal edges should are faded on scroll, false
8918 * otherwise
8919 *
8920 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008921 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008922 */
8923 public boolean isHorizontalFadingEdgeEnabled() {
8924 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8925 }
8926
8927 /**
8928 * <p>Define whether the horizontal edges should be faded when this view
8929 * is scrolled horizontally.</p>
8930 *
8931 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8932 * be faded when the view is scrolled
8933 * horizontally
8934 *
8935 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008936 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 */
8938 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8939 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8940 if (horizontalFadingEdgeEnabled) {
8941 initScrollCache();
8942 }
8943
8944 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8945 }
8946 }
8947
8948 /**
8949 * <p>Indicate whether the vertical edges are faded when the view is
8950 * scrolled horizontally.</p>
8951 *
8952 * @return true if the vertical edges should are faded on scroll, false
8953 * otherwise
8954 *
8955 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008956 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008957 */
8958 public boolean isVerticalFadingEdgeEnabled() {
8959 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8960 }
8961
8962 /**
8963 * <p>Define whether the vertical edges should be faded when this view
8964 * is scrolled vertically.</p>
8965 *
8966 * @param verticalFadingEdgeEnabled true if the vertical edges should
8967 * be faded when the view is scrolled
8968 * vertically
8969 *
8970 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008971 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008972 */
8973 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8974 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8975 if (verticalFadingEdgeEnabled) {
8976 initScrollCache();
8977 }
8978
8979 mViewFlags ^= FADING_EDGE_VERTICAL;
8980 }
8981 }
8982
8983 /**
8984 * Returns the strength, or intensity, of the top faded edge. The strength is
8985 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8986 * returns 0.0 or 1.0 but no value in between.
8987 *
8988 * Subclasses should override this method to provide a smoother fade transition
8989 * when scrolling occurs.
8990 *
8991 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8992 */
8993 protected float getTopFadingEdgeStrength() {
8994 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8995 }
8996
8997 /**
8998 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8999 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9000 * returns 0.0 or 1.0 but no value in between.
9001 *
9002 * Subclasses should override this method to provide a smoother fade transition
9003 * when scrolling occurs.
9004 *
9005 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9006 */
9007 protected float getBottomFadingEdgeStrength() {
9008 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9009 computeVerticalScrollRange() ? 1.0f : 0.0f;
9010 }
9011
9012 /**
9013 * Returns the strength, or intensity, of the left faded edge. The strength is
9014 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9015 * returns 0.0 or 1.0 but no value in between.
9016 *
9017 * Subclasses should override this method to provide a smoother fade transition
9018 * when scrolling occurs.
9019 *
9020 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9021 */
9022 protected float getLeftFadingEdgeStrength() {
9023 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9024 }
9025
9026 /**
9027 * Returns the strength, or intensity, of the right faded edge. The strength is
9028 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9029 * returns 0.0 or 1.0 but no value in between.
9030 *
9031 * Subclasses should override this method to provide a smoother fade transition
9032 * when scrolling occurs.
9033 *
9034 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9035 */
9036 protected float getRightFadingEdgeStrength() {
9037 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9038 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9039 }
9040
9041 /**
9042 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9043 * scrollbar is not drawn by default.</p>
9044 *
9045 * @return true if the horizontal scrollbar should be painted, false
9046 * otherwise
9047 *
9048 * @see #setHorizontalScrollBarEnabled(boolean)
9049 */
9050 public boolean isHorizontalScrollBarEnabled() {
9051 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9052 }
9053
9054 /**
9055 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9056 * scrollbar is not drawn by default.</p>
9057 *
9058 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9059 * be painted
9060 *
9061 * @see #isHorizontalScrollBarEnabled()
9062 */
9063 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9064 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9065 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009066 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009067 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009068 }
9069 }
9070
9071 /**
9072 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9073 * scrollbar is not drawn by default.</p>
9074 *
9075 * @return true if the vertical scrollbar should be painted, false
9076 * otherwise
9077 *
9078 * @see #setVerticalScrollBarEnabled(boolean)
9079 */
9080 public boolean isVerticalScrollBarEnabled() {
9081 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9082 }
9083
9084 /**
9085 * <p>Define whether the vertical scrollbar should be drawn or not. The
9086 * scrollbar is not drawn by default.</p>
9087 *
9088 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9089 * be painted
9090 *
9091 * @see #isVerticalScrollBarEnabled()
9092 */
9093 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9094 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9095 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009096 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009097 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009098 }
9099 }
9100
Adam Powell20232d02010-12-08 21:08:53 -08009101 /**
9102 * @hide
9103 */
9104 protected void recomputePadding() {
9105 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009106 }
Joe Malin32736f02011-01-19 16:14:20 -08009107
Mike Cleronfe81d382009-09-28 14:22:16 -07009108 /**
9109 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009110 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009111 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009112 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009113 */
9114 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9115 initScrollCache();
9116 final ScrollabilityCache scrollabilityCache = mScrollCache;
9117 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009118 if (fadeScrollbars) {
9119 scrollabilityCache.state = ScrollabilityCache.OFF;
9120 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009121 scrollabilityCache.state = ScrollabilityCache.ON;
9122 }
9123 }
Joe Malin32736f02011-01-19 16:14:20 -08009124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009125 /**
Joe Malin32736f02011-01-19 16:14:20 -08009126 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009127 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009128 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009129 * @return true if scrollbar fading is enabled
9130 */
9131 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009132 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009133 }
Joe Malin32736f02011-01-19 16:14:20 -08009134
Mike Cleron52f0a642009-09-28 18:21:37 -07009135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9137 * inset. When inset, they add to the padding of the view. And the scrollbars
9138 * can be drawn inside the padding area or on the edge of the view. For example,
9139 * if a view has a background drawable and you want to draw the scrollbars
9140 * inside the padding specified by the drawable, you can use
9141 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9142 * appear at the edge of the view, ignoring the padding, then you can use
9143 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9144 * @param style the style of the scrollbars. Should be one of
9145 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9146 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9147 * @see #SCROLLBARS_INSIDE_OVERLAY
9148 * @see #SCROLLBARS_INSIDE_INSET
9149 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9150 * @see #SCROLLBARS_OUTSIDE_INSET
9151 */
9152 public void setScrollBarStyle(int style) {
9153 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9154 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009155 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009156 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157 }
9158 }
9159
9160 /**
9161 * <p>Returns the current scrollbar style.</p>
9162 * @return the current scrollbar style
9163 * @see #SCROLLBARS_INSIDE_OVERLAY
9164 * @see #SCROLLBARS_INSIDE_INSET
9165 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9166 * @see #SCROLLBARS_OUTSIDE_INSET
9167 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009168 @ViewDebug.ExportedProperty(mapping = {
9169 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9170 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9171 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9172 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9173 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009174 public int getScrollBarStyle() {
9175 return mViewFlags & SCROLLBARS_STYLE_MASK;
9176 }
9177
9178 /**
9179 * <p>Compute the horizontal range that the horizontal scrollbar
9180 * represents.</p>
9181 *
9182 * <p>The range is expressed in arbitrary units that must be the same as the
9183 * units used by {@link #computeHorizontalScrollExtent()} and
9184 * {@link #computeHorizontalScrollOffset()}.</p>
9185 *
9186 * <p>The default range is the drawing width of this view.</p>
9187 *
9188 * @return the total horizontal range represented by the horizontal
9189 * scrollbar
9190 *
9191 * @see #computeHorizontalScrollExtent()
9192 * @see #computeHorizontalScrollOffset()
9193 * @see android.widget.ScrollBarDrawable
9194 */
9195 protected int computeHorizontalScrollRange() {
9196 return getWidth();
9197 }
9198
9199 /**
9200 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9201 * within the horizontal range. This value is used to compute the position
9202 * of the thumb within the scrollbar's track.</p>
9203 *
9204 * <p>The range is expressed in arbitrary units that must be the same as the
9205 * units used by {@link #computeHorizontalScrollRange()} and
9206 * {@link #computeHorizontalScrollExtent()}.</p>
9207 *
9208 * <p>The default offset is the scroll offset of this view.</p>
9209 *
9210 * @return the horizontal offset of the scrollbar's thumb
9211 *
9212 * @see #computeHorizontalScrollRange()
9213 * @see #computeHorizontalScrollExtent()
9214 * @see android.widget.ScrollBarDrawable
9215 */
9216 protected int computeHorizontalScrollOffset() {
9217 return mScrollX;
9218 }
9219
9220 /**
9221 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9222 * within the horizontal range. This value is used to compute the length
9223 * of the thumb within the scrollbar's track.</p>
9224 *
9225 * <p>The range is expressed in arbitrary units that must be the same as the
9226 * units used by {@link #computeHorizontalScrollRange()} and
9227 * {@link #computeHorizontalScrollOffset()}.</p>
9228 *
9229 * <p>The default extent is the drawing width of this view.</p>
9230 *
9231 * @return the horizontal extent of the scrollbar's thumb
9232 *
9233 * @see #computeHorizontalScrollRange()
9234 * @see #computeHorizontalScrollOffset()
9235 * @see android.widget.ScrollBarDrawable
9236 */
9237 protected int computeHorizontalScrollExtent() {
9238 return getWidth();
9239 }
9240
9241 /**
9242 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9243 *
9244 * <p>The range is expressed in arbitrary units that must be the same as the
9245 * units used by {@link #computeVerticalScrollExtent()} and
9246 * {@link #computeVerticalScrollOffset()}.</p>
9247 *
9248 * @return the total vertical range represented by the vertical scrollbar
9249 *
9250 * <p>The default range is the drawing height of this view.</p>
9251 *
9252 * @see #computeVerticalScrollExtent()
9253 * @see #computeVerticalScrollOffset()
9254 * @see android.widget.ScrollBarDrawable
9255 */
9256 protected int computeVerticalScrollRange() {
9257 return getHeight();
9258 }
9259
9260 /**
9261 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9262 * within the horizontal range. This value is used to compute the position
9263 * of the thumb within the scrollbar's track.</p>
9264 *
9265 * <p>The range is expressed in arbitrary units that must be the same as the
9266 * units used by {@link #computeVerticalScrollRange()} and
9267 * {@link #computeVerticalScrollExtent()}.</p>
9268 *
9269 * <p>The default offset is the scroll offset of this view.</p>
9270 *
9271 * @return the vertical offset of the scrollbar's thumb
9272 *
9273 * @see #computeVerticalScrollRange()
9274 * @see #computeVerticalScrollExtent()
9275 * @see android.widget.ScrollBarDrawable
9276 */
9277 protected int computeVerticalScrollOffset() {
9278 return mScrollY;
9279 }
9280
9281 /**
9282 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9283 * within the vertical range. This value is used to compute the length
9284 * of the thumb within the scrollbar's track.</p>
9285 *
9286 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009287 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009288 * {@link #computeVerticalScrollOffset()}.</p>
9289 *
9290 * <p>The default extent is the drawing height of this view.</p>
9291 *
9292 * @return the vertical extent of the scrollbar's thumb
9293 *
9294 * @see #computeVerticalScrollRange()
9295 * @see #computeVerticalScrollOffset()
9296 * @see android.widget.ScrollBarDrawable
9297 */
9298 protected int computeVerticalScrollExtent() {
9299 return getHeight();
9300 }
9301
9302 /**
Adam Powell69159442011-06-13 17:53:06 -07009303 * Check if this view can be scrolled horizontally in a certain direction.
9304 *
9305 * @param direction Negative to check scrolling left, positive to check scrolling right.
9306 * @return true if this view can be scrolled in the specified direction, false otherwise.
9307 */
9308 public boolean canScrollHorizontally(int direction) {
9309 final int offset = computeHorizontalScrollOffset();
9310 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9311 if (range == 0) return false;
9312 if (direction < 0) {
9313 return offset > 0;
9314 } else {
9315 return offset < range - 1;
9316 }
9317 }
9318
9319 /**
9320 * Check if this view can be scrolled vertically in a certain direction.
9321 *
9322 * @param direction Negative to check scrolling up, positive to check scrolling down.
9323 * @return true if this view can be scrolled in the specified direction, false otherwise.
9324 */
9325 public boolean canScrollVertically(int direction) {
9326 final int offset = computeVerticalScrollOffset();
9327 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9328 if (range == 0) return false;
9329 if (direction < 0) {
9330 return offset > 0;
9331 } else {
9332 return offset < range - 1;
9333 }
9334 }
9335
9336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009337 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9338 * scrollbars are painted only if they have been awakened first.</p>
9339 *
9340 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009341 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009342 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009343 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009344 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 // scrollbars are drawn only when the animation is running
9346 final ScrollabilityCache cache = mScrollCache;
9347 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009348
Mike Cleronf116bf82009-09-27 19:14:12 -07009349 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009350
Mike Cleronf116bf82009-09-27 19:14:12 -07009351 if (state == ScrollabilityCache.OFF) {
9352 return;
9353 }
Joe Malin32736f02011-01-19 16:14:20 -08009354
Mike Cleronf116bf82009-09-27 19:14:12 -07009355 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009356
Mike Cleronf116bf82009-09-27 19:14:12 -07009357 if (state == ScrollabilityCache.FADING) {
9358 // We're fading -- get our fade interpolation
9359 if (cache.interpolatorValues == null) {
9360 cache.interpolatorValues = new float[1];
9361 }
Joe Malin32736f02011-01-19 16:14:20 -08009362
Mike Cleronf116bf82009-09-27 19:14:12 -07009363 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009364
Mike Cleronf116bf82009-09-27 19:14:12 -07009365 // Stops the animation if we're done
9366 if (cache.scrollBarInterpolator.timeToValues(values) ==
9367 Interpolator.Result.FREEZE_END) {
9368 cache.state = ScrollabilityCache.OFF;
9369 } else {
9370 cache.scrollBar.setAlpha(Math.round(values[0]));
9371 }
Joe Malin32736f02011-01-19 16:14:20 -08009372
9373 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009374 // drawing. We only want this when we're fading so that
9375 // we prevent excessive redraws
9376 invalidate = true;
9377 } else {
9378 // We're just on -- but we may have been fading before so
9379 // reset alpha
9380 cache.scrollBar.setAlpha(255);
9381 }
9382
Joe Malin32736f02011-01-19 16:14:20 -08009383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009384 final int viewFlags = mViewFlags;
9385
9386 final boolean drawHorizontalScrollBar =
9387 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9388 final boolean drawVerticalScrollBar =
9389 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9390 && !isVerticalScrollBarHidden();
9391
9392 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9393 final int width = mRight - mLeft;
9394 final int height = mBottom - mTop;
9395
9396 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397
Mike Reede8853fc2009-09-04 14:01:48 -04009398 final int scrollX = mScrollX;
9399 final int scrollY = mScrollY;
9400 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9401
Mike Cleronf116bf82009-09-27 19:14:12 -07009402 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009404 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009405 int size = scrollBar.getSize(false);
9406 if (size <= 0) {
9407 size = cache.scrollBarSize;
9408 }
9409
Mike Cleronf116bf82009-09-27 19:14:12 -07009410 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009411 computeHorizontalScrollOffset(),
9412 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009413 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009414 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009415 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009416 left = scrollX + (mPaddingLeft & inside);
9417 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9418 bottom = top + size;
9419 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9420 if (invalidate) {
9421 invalidate(left, top, right, bottom);
9422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009423 }
9424
9425 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009426 int size = scrollBar.getSize(true);
9427 if (size <= 0) {
9428 size = cache.scrollBarSize;
9429 }
9430
Mike Reede8853fc2009-09-04 14:01:48 -04009431 scrollBar.setParameters(computeVerticalScrollRange(),
9432 computeVerticalScrollOffset(),
9433 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009434 switch (mVerticalScrollbarPosition) {
9435 default:
9436 case SCROLLBAR_POSITION_DEFAULT:
9437 case SCROLLBAR_POSITION_RIGHT:
9438 left = scrollX + width - size - (mUserPaddingRight & inside);
9439 break;
9440 case SCROLLBAR_POSITION_LEFT:
9441 left = scrollX + (mUserPaddingLeft & inside);
9442 break;
9443 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009444 top = scrollY + (mPaddingTop & inside);
9445 right = left + size;
9446 bottom = scrollY + height - (mUserPaddingBottom & inside);
9447 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9448 if (invalidate) {
9449 invalidate(left, top, right, bottom);
9450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009451 }
9452 }
9453 }
9454 }
Romain Guy8506ab42009-06-11 17:35:47 -07009455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009456 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009457 * 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 -08009458 * FastScroller is visible.
9459 * @return whether to temporarily hide the vertical scrollbar
9460 * @hide
9461 */
9462 protected boolean isVerticalScrollBarHidden() {
9463 return false;
9464 }
9465
9466 /**
9467 * <p>Draw the horizontal scrollbar if
9468 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9469 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 * @param canvas the canvas on which to draw the scrollbar
9471 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009472 *
9473 * @see #isHorizontalScrollBarEnabled()
9474 * @see #computeHorizontalScrollRange()
9475 * @see #computeHorizontalScrollExtent()
9476 * @see #computeHorizontalScrollOffset()
9477 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009478 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009479 */
Romain Guy8fb95422010-08-17 18:38:51 -07009480 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9481 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009482 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009483 scrollBar.draw(canvas);
9484 }
Mike Reede8853fc2009-09-04 14:01:48 -04009485
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009486 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009487 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9488 * returns true.</p>
9489 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009490 * @param canvas the canvas on which to draw the scrollbar
9491 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 *
9493 * @see #isVerticalScrollBarEnabled()
9494 * @see #computeVerticalScrollRange()
9495 * @see #computeVerticalScrollExtent()
9496 * @see #computeVerticalScrollOffset()
9497 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009498 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499 */
Romain Guy8fb95422010-08-17 18:38:51 -07009500 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9501 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009502 scrollBar.setBounds(l, t, r, b);
9503 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009504 }
9505
9506 /**
9507 * Implement this to do your drawing.
9508 *
9509 * @param canvas the canvas on which the background will be drawn
9510 */
9511 protected void onDraw(Canvas canvas) {
9512 }
9513
9514 /*
9515 * Caller is responsible for calling requestLayout if necessary.
9516 * (This allows addViewInLayout to not request a new layout.)
9517 */
9518 void assignParent(ViewParent parent) {
9519 if (mParent == null) {
9520 mParent = parent;
9521 } else if (parent == null) {
9522 mParent = null;
9523 } else {
9524 throw new RuntimeException("view " + this + " being added, but"
9525 + " it already has a parent");
9526 }
9527 }
9528
9529 /**
9530 * This is called when the view is attached to a window. At this point it
9531 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009532 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9533 * however it may be called any time before the first onDraw -- including
9534 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 *
9536 * @see #onDetachedFromWindow()
9537 */
9538 protected void onAttachedToWindow() {
9539 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9540 mParent.requestTransparentRegion(this);
9541 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009542 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9543 initialAwakenScrollBars();
9544 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9545 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009546 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009547 // Order is important here: LayoutDirection MUST be resolved before Padding
9548 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009549 resolveLayoutDirectionIfNeeded();
9550 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009551 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009552 if (isFocused()) {
9553 InputMethodManager imm = InputMethodManager.peekInstance();
9554 imm.focusIn(this);
9555 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009556 }
Cibu Johny86666632010-02-22 13:01:02 -08009557
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009558 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009559 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9560 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009561 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009562 private void resolveLayoutDirectionIfNeeded() {
9563 // Do not resolve if it is not needed
9564 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9565
9566 // Clear any previous layout direction resolution
9567 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9568
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009569 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9570 // TextDirectionHeuristic
9571 resetResolvedTextDirection();
9572
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009573 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009574 switch (getLayoutDirection()) {
9575 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009576 // We cannot do the resolution if there is no parent
9577 if (mParent == null) return;
9578
Cibu Johny86666632010-02-22 13:01:02 -08009579 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009580 if (mParent instanceof ViewGroup) {
9581 ViewGroup viewGroup = ((ViewGroup) mParent);
9582
9583 // Check if the parent view group can resolve
9584 if (! viewGroup.canResolveLayoutDirection()) {
9585 return;
9586 }
9587 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9588 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9589 }
Cibu Johny86666632010-02-22 13:01:02 -08009590 }
9591 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009592 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009593 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009594 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009595 case LAYOUT_DIRECTION_LOCALE:
9596 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009597 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009598 }
9599 break;
9600 default:
9601 // Nothing to do, LTR by default
9602 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009603
9604 // Set to resolved
9605 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9606 }
9607
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009608 /**
9609 * @hide
9610 */
9611 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009612 // If the user specified the absolute padding (either with android:padding or
9613 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9614 // use the default padding or the padding from the background drawable
9615 // (stored at this point in mPadding*)
9616 switch (getResolvedLayoutDirection()) {
9617 case LAYOUT_DIRECTION_RTL:
9618 // Start user padding override Right user padding. Otherwise, if Right user
9619 // padding is not defined, use the default Right padding. If Right user padding
9620 // is defined, just use it.
9621 if (mUserPaddingStart >= 0) {
9622 mUserPaddingRight = mUserPaddingStart;
9623 } else if (mUserPaddingRight < 0) {
9624 mUserPaddingRight = mPaddingRight;
9625 }
9626 if (mUserPaddingEnd >= 0) {
9627 mUserPaddingLeft = mUserPaddingEnd;
9628 } else if (mUserPaddingLeft < 0) {
9629 mUserPaddingLeft = mPaddingLeft;
9630 }
9631 break;
9632 case LAYOUT_DIRECTION_LTR:
9633 default:
9634 // Start user padding override Left user padding. Otherwise, if Left user
9635 // padding is not defined, use the default left padding. If Left user padding
9636 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009637 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009638 mUserPaddingLeft = mUserPaddingStart;
9639 } else if (mUserPaddingLeft < 0) {
9640 mUserPaddingLeft = mPaddingLeft;
9641 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009642 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009643 mUserPaddingRight = mUserPaddingEnd;
9644 } else if (mUserPaddingRight < 0) {
9645 mUserPaddingRight = mPaddingRight;
9646 }
9647 }
9648
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009649 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9650
9651 recomputePadding();
9652 }
9653
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009654 /**
9655 * Return true if layout direction resolution can be done
9656 *
9657 * @hide
9658 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009659 protected boolean canResolveLayoutDirection() {
9660 switch (getLayoutDirection()) {
9661 case LAYOUT_DIRECTION_INHERIT:
9662 return (mParent != null);
9663 default:
9664 return true;
9665 }
9666 }
9667
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009668 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009669 * Reset the resolved layout direction.
9670 *
9671 * Subclasses need to override this method to clear cached information that depends on the
9672 * resolved layout direction, or to inform child views that inherit their layout direction.
9673 * Overrides must also call the superclass implementation at the start of their implementation.
9674 *
9675 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009676 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009677 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009678 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009679 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009680 }
9681
9682 /**
9683 * Check if a Locale is corresponding to a RTL script.
9684 *
9685 * @param locale Locale to check
9686 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009687 *
9688 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009689 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009690 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009691 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9692 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009693 }
9694
9695 /**
9696 * This is called when the view is detached from a window. At this point it
9697 * no longer has a surface for drawing.
9698 *
9699 * @see #onAttachedToWindow()
9700 */
9701 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009702 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009703
Romain Guya440b002010-02-24 15:57:54 -08009704 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009705 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009706 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009707 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009710
Romain Guy6d7475d2011-07-27 16:28:21 -07009711 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009712
Chet Haasedaf98e92011-01-10 14:10:36 -08009713 if (mDisplayList != null) {
9714 mDisplayList.invalidate();
9715 }
9716
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009717 if (mAttachInfo != null) {
9718 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009719 }
9720
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009721 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009722
9723 resetResolvedLayoutDirection();
9724 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009725 }
9726
9727 /**
9728 * @return The number of times this view has been attached to a window
9729 */
9730 protected int getWindowAttachCount() {
9731 return mWindowAttachCount;
9732 }
9733
9734 /**
9735 * Retrieve a unique token identifying the window this view is attached to.
9736 * @return Return the window's token for use in
9737 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9738 */
9739 public IBinder getWindowToken() {
9740 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9741 }
9742
9743 /**
9744 * Retrieve a unique token identifying the top-level "real" window of
9745 * the window that this view is attached to. That is, this is like
9746 * {@link #getWindowToken}, except if the window this view in is a panel
9747 * window (attached to another containing window), then the token of
9748 * the containing window is returned instead.
9749 *
9750 * @return Returns the associated window token, either
9751 * {@link #getWindowToken()} or the containing window's token.
9752 */
9753 public IBinder getApplicationWindowToken() {
9754 AttachInfo ai = mAttachInfo;
9755 if (ai != null) {
9756 IBinder appWindowToken = ai.mPanelParentWindowToken;
9757 if (appWindowToken == null) {
9758 appWindowToken = ai.mWindowToken;
9759 }
9760 return appWindowToken;
9761 }
9762 return null;
9763 }
9764
9765 /**
9766 * Retrieve private session object this view hierarchy is using to
9767 * communicate with the window manager.
9768 * @return the session object to communicate with the window manager
9769 */
9770 /*package*/ IWindowSession getWindowSession() {
9771 return mAttachInfo != null ? mAttachInfo.mSession : null;
9772 }
9773
9774 /**
9775 * @param info the {@link android.view.View.AttachInfo} to associated with
9776 * this view
9777 */
9778 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9779 //System.out.println("Attached! " + this);
9780 mAttachInfo = info;
9781 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009782 // We will need to evaluate the drawable state at least once.
9783 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 if (mFloatingTreeObserver != null) {
9785 info.mTreeObserver.merge(mFloatingTreeObserver);
9786 mFloatingTreeObserver = null;
9787 }
9788 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9789 mAttachInfo.mScrollContainers.add(this);
9790 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9791 }
9792 performCollectViewAttributes(visibility);
9793 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009794
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009795 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009796 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009797 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009798 if (listeners != null && listeners.size() > 0) {
9799 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9800 // perform the dispatching. The iterator is a safe guard against listeners that
9801 // could mutate the list by calling the various add/remove methods. This prevents
9802 // the array from being modified while we iterate it.
9803 for (OnAttachStateChangeListener listener : listeners) {
9804 listener.onViewAttachedToWindow(this);
9805 }
9806 }
9807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009808 int vis = info.mWindowVisibility;
9809 if (vis != GONE) {
9810 onWindowVisibilityChanged(vis);
9811 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009812 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9813 // If nobody has evaluated the drawable state yet, then do it now.
9814 refreshDrawableState();
9815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 }
9817
9818 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 AttachInfo info = mAttachInfo;
9820 if (info != null) {
9821 int vis = info.mWindowVisibility;
9822 if (vis != GONE) {
9823 onWindowVisibilityChanged(GONE);
9824 }
9825 }
9826
9827 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009828
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009829 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009830 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009831 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009832 if (listeners != null && listeners.size() > 0) {
9833 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9834 // perform the dispatching. The iterator is a safe guard against listeners that
9835 // could mutate the list by calling the various add/remove methods. This prevents
9836 // the array from being modified while we iterate it.
9837 for (OnAttachStateChangeListener listener : listeners) {
9838 listener.onViewDetachedFromWindow(this);
9839 }
9840 }
9841
Romain Guy01d5edc2011-01-28 11:28:53 -08009842 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009843 mAttachInfo.mScrollContainers.remove(this);
9844 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9845 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009847 mAttachInfo = null;
9848 }
9849
9850 /**
9851 * Store this view hierarchy's frozen state into the given container.
9852 *
9853 * @param container The SparseArray in which to save the view's state.
9854 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009855 * @see #restoreHierarchyState(android.util.SparseArray)
9856 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9857 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009858 */
9859 public void saveHierarchyState(SparseArray<Parcelable> container) {
9860 dispatchSaveInstanceState(container);
9861 }
9862
9863 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009864 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9865 * this view and its children. May be overridden to modify how freezing happens to a
9866 * 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 -08009867 *
9868 * @param container The SparseArray in which to save the view's state.
9869 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009870 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9871 * @see #saveHierarchyState(android.util.SparseArray)
9872 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 */
9874 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9875 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9876 mPrivateFlags &= ~SAVE_STATE_CALLED;
9877 Parcelable state = onSaveInstanceState();
9878 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9879 throw new IllegalStateException(
9880 "Derived class did not call super.onSaveInstanceState()");
9881 }
9882 if (state != null) {
9883 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9884 // + ": " + state);
9885 container.put(mID, state);
9886 }
9887 }
9888 }
9889
9890 /**
9891 * Hook allowing a view to generate a representation of its internal state
9892 * that can later be used to create a new instance with that same state.
9893 * This state should only contain information that is not persistent or can
9894 * not be reconstructed later. For example, you will never store your
9895 * current position on screen because that will be computed again when a
9896 * new instance of the view is placed in its view hierarchy.
9897 * <p>
9898 * Some examples of things you may store here: the current cursor position
9899 * in a text view (but usually not the text itself since that is stored in a
9900 * content provider or other persistent storage), the currently selected
9901 * item in a list view.
9902 *
9903 * @return Returns a Parcelable object containing the view's current dynamic
9904 * state, or null if there is nothing interesting to save. The
9905 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009906 * @see #onRestoreInstanceState(android.os.Parcelable)
9907 * @see #saveHierarchyState(android.util.SparseArray)
9908 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 * @see #setSaveEnabled(boolean)
9910 */
9911 protected Parcelable onSaveInstanceState() {
9912 mPrivateFlags |= SAVE_STATE_CALLED;
9913 return BaseSavedState.EMPTY_STATE;
9914 }
9915
9916 /**
9917 * Restore this view hierarchy's frozen state from the given container.
9918 *
9919 * @param container The SparseArray which holds previously frozen states.
9920 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009921 * @see #saveHierarchyState(android.util.SparseArray)
9922 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9923 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 */
9925 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9926 dispatchRestoreInstanceState(container);
9927 }
9928
9929 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009930 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9931 * state for this view and its children. May be overridden to modify how restoring
9932 * happens to a view's children; for example, some views may want to not store state
9933 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009934 *
9935 * @param container The SparseArray which holds previously saved state.
9936 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009937 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9938 * @see #restoreHierarchyState(android.util.SparseArray)
9939 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 */
9941 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9942 if (mID != NO_ID) {
9943 Parcelable state = container.get(mID);
9944 if (state != null) {
9945 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9946 // + ": " + state);
9947 mPrivateFlags &= ~SAVE_STATE_CALLED;
9948 onRestoreInstanceState(state);
9949 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9950 throw new IllegalStateException(
9951 "Derived class did not call super.onRestoreInstanceState()");
9952 }
9953 }
9954 }
9955 }
9956
9957 /**
9958 * Hook allowing a view to re-apply a representation of its internal state that had previously
9959 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9960 * null state.
9961 *
9962 * @param state The frozen state that had previously been returned by
9963 * {@link #onSaveInstanceState}.
9964 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009965 * @see #onSaveInstanceState()
9966 * @see #restoreHierarchyState(android.util.SparseArray)
9967 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 */
9969 protected void onRestoreInstanceState(Parcelable state) {
9970 mPrivateFlags |= SAVE_STATE_CALLED;
9971 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009972 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9973 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009974 + "when two views of different type have the same id in the same hierarchy. "
9975 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009976 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009977 }
9978 }
9979
9980 /**
9981 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9982 *
9983 * @return the drawing start time in milliseconds
9984 */
9985 public long getDrawingTime() {
9986 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9987 }
9988
9989 /**
9990 * <p>Enables or disables the duplication of the parent's state into this view. When
9991 * duplication is enabled, this view gets its drawable state from its parent rather
9992 * than from its own internal properties.</p>
9993 *
9994 * <p>Note: in the current implementation, setting this property to true after the
9995 * view was added to a ViewGroup might have no effect at all. This property should
9996 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9997 *
9998 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9999 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010000 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010001 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10002 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 *
10004 * @param enabled True to enable duplication of the parent's drawable state, false
10005 * to disable it.
10006 *
10007 * @see #getDrawableState()
10008 * @see #isDuplicateParentStateEnabled()
10009 */
10010 public void setDuplicateParentStateEnabled(boolean enabled) {
10011 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10012 }
10013
10014 /**
10015 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10016 *
10017 * @return True if this view's drawable state is duplicated from the parent,
10018 * false otherwise
10019 *
10020 * @see #getDrawableState()
10021 * @see #setDuplicateParentStateEnabled(boolean)
10022 */
10023 public boolean isDuplicateParentStateEnabled() {
10024 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10025 }
10026
10027 /**
Romain Guy171c5922011-01-06 10:04:23 -080010028 * <p>Specifies the type of layer backing this view. The layer can be
10029 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10030 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010031 *
Romain Guy171c5922011-01-06 10:04:23 -080010032 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10033 * instance that controls how the layer is composed on screen. The following
10034 * properties of the paint are taken into account when composing the layer:</p>
10035 * <ul>
10036 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10037 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10038 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10039 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010040 *
Romain Guy171c5922011-01-06 10:04:23 -080010041 * <p>If this view has an alpha value set to < 1.0 by calling
10042 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10043 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10044 * equivalent to setting a hardware layer on this view and providing a paint with
10045 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010046 *
Romain Guy171c5922011-01-06 10:04:23 -080010047 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10048 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10049 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010050 *
Romain Guy171c5922011-01-06 10:04:23 -080010051 * @param layerType The ype of layer to use with this view, must be one of
10052 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10053 * {@link #LAYER_TYPE_HARDWARE}
10054 * @param paint The paint used to compose the layer. This argument is optional
10055 * and can be null. It is ignored when the layer type is
10056 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010057 *
10058 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010059 * @see #LAYER_TYPE_NONE
10060 * @see #LAYER_TYPE_SOFTWARE
10061 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010062 * @see #setAlpha(float)
10063 *
Romain Guy171c5922011-01-06 10:04:23 -080010064 * @attr ref android.R.styleable#View_layerType
10065 */
10066 public void setLayerType(int layerType, Paint paint) {
10067 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010068 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010069 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10070 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010071
Romain Guyd6cd5722011-01-17 14:42:41 -080010072 if (layerType == mLayerType) {
10073 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10074 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010075 invalidateParentCaches();
10076 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010077 }
10078 return;
10079 }
Romain Guy171c5922011-01-06 10:04:23 -080010080
10081 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010082 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010083 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010084 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010085 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010086 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010087 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010088 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010089 default:
10090 break;
Romain Guy171c5922011-01-06 10:04:23 -080010091 }
10092
10093 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010094 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10095 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10096 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010097
Romain Guy0fd89bf2011-01-26 15:41:30 -080010098 invalidateParentCaches();
10099 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010100 }
10101
10102 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010103 * Indicates whether this view has a static layer. A view with layer type
10104 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10105 * dynamic.
10106 */
10107 boolean hasStaticLayer() {
10108 return mLayerType == LAYER_TYPE_NONE;
10109 }
10110
10111 /**
Romain Guy171c5922011-01-06 10:04:23 -080010112 * Indicates what type of layer is currently associated with this view. By default
10113 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10114 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10115 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010116 *
Romain Guy171c5922011-01-06 10:04:23 -080010117 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10118 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010119 *
10120 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010121 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010122 * @see #LAYER_TYPE_NONE
10123 * @see #LAYER_TYPE_SOFTWARE
10124 * @see #LAYER_TYPE_HARDWARE
10125 */
10126 public int getLayerType() {
10127 return mLayerType;
10128 }
Joe Malin32736f02011-01-19 16:14:20 -080010129
Romain Guy6c319ca2011-01-11 14:29:25 -080010130 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010131 * Forces this view's layer to be created and this view to be rendered
10132 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10133 * invoking this method will have no effect.
10134 *
10135 * This method can for instance be used to render a view into its layer before
10136 * starting an animation. If this view is complex, rendering into the layer
10137 * before starting the animation will avoid skipping frames.
10138 *
10139 * @throws IllegalStateException If this view is not attached to a window
10140 *
10141 * @see #setLayerType(int, android.graphics.Paint)
10142 */
10143 public void buildLayer() {
10144 if (mLayerType == LAYER_TYPE_NONE) return;
10145
10146 if (mAttachInfo == null) {
10147 throw new IllegalStateException("This view must be attached to a window first");
10148 }
10149
10150 switch (mLayerType) {
10151 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010152 if (mAttachInfo.mHardwareRenderer != null &&
10153 mAttachInfo.mHardwareRenderer.isEnabled() &&
10154 mAttachInfo.mHardwareRenderer.validate()) {
10155 getHardwareLayer();
10156 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010157 break;
10158 case LAYER_TYPE_SOFTWARE:
10159 buildDrawingCache(true);
10160 break;
10161 }
10162 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010163
10164 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10165 void flushLayer() {
10166 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10167 mHardwareLayer.flush();
10168 }
10169 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010170
10171 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010172 * <p>Returns a hardware layer that can be used to draw this view again
10173 * without executing its draw method.</p>
10174 *
10175 * @return A HardwareLayer ready to render, or null if an error occurred.
10176 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010177 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010178 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10179 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010180 return null;
10181 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010182
10183 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010184
10185 final int width = mRight - mLeft;
10186 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010187
Romain Guy6c319ca2011-01-11 14:29:25 -080010188 if (width == 0 || height == 0) {
10189 return null;
10190 }
10191
10192 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10193 if (mHardwareLayer == null) {
10194 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10195 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010196 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010197 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10198 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010199 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010200 }
10201
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010202 // The layer is not valid if the underlying GPU resources cannot be allocated
10203 if (!mHardwareLayer.isValid()) {
10204 return null;
10205 }
10206
Romain Guy59a12ca2011-06-09 17:48:21 -070010207 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010208 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010209
10210 // Make sure all the GPU resources have been properly allocated
10211 if (canvas == null) {
10212 mHardwareLayer.end(currentCanvas);
10213 return null;
10214 }
10215
Romain Guy5e7f7662011-01-24 22:35:56 -080010216 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010217 try {
10218 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010219 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010220 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010221
Chet Haase88172fe2011-03-07 17:36:33 -080010222 final int restoreCount = canvas.save();
10223
Romain Guy6c319ca2011-01-11 14:29:25 -080010224 computeScroll();
10225 canvas.translate(-mScrollX, -mScrollY);
10226
Romain Guy6c319ca2011-01-11 14:29:25 -080010227 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010228
Romain Guy6c319ca2011-01-11 14:29:25 -080010229 // Fast path for layouts with no backgrounds
10230 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10231 mPrivateFlags &= ~DIRTY_MASK;
10232 dispatchDraw(canvas);
10233 } else {
10234 draw(canvas);
10235 }
Joe Malin32736f02011-01-19 16:14:20 -080010236
Chet Haase88172fe2011-03-07 17:36:33 -080010237 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010238 } finally {
10239 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010240 mHardwareLayer.end(currentCanvas);
10241 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010242 }
10243 }
10244
10245 return mHardwareLayer;
10246 }
Romain Guy171c5922011-01-06 10:04:23 -080010247
Romain Guy589b0bb2011-10-10 13:57:47 -070010248 /**
10249 * Destroys this View's hardware layer if possible.
10250 *
10251 * @return True if the layer was destroyed, false otherwise.
10252 *
10253 * @see #setLayerType(int, android.graphics.Paint)
10254 * @see #LAYER_TYPE_HARDWARE
10255 */
Romain Guy65b345f2011-07-27 18:51:50 -070010256 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010257 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010258 AttachInfo info = mAttachInfo;
10259 if (info != null && info.mHardwareRenderer != null &&
10260 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10261 mHardwareLayer.destroy();
10262 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010263
Romain Guy9c4b79a2011-11-10 19:23:58 -080010264 invalidate(true);
10265 invalidateParentCaches();
10266 }
Romain Guy65b345f2011-07-27 18:51:50 -070010267 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010268 }
Romain Guy65b345f2011-07-27 18:51:50 -070010269 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010270 }
10271
Romain Guy171c5922011-01-06 10:04:23 -080010272 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010273 * Destroys all hardware rendering resources. This method is invoked
10274 * when the system needs to reclaim resources. Upon execution of this
10275 * method, you should free any OpenGL resources created by the view.
10276 *
10277 * Note: you <strong>must</strong> call
10278 * <code>super.destroyHardwareResources()</code> when overriding
10279 * this method.
10280 *
10281 * @hide
10282 */
10283 protected void destroyHardwareResources() {
10284 destroyLayer();
10285 }
10286
10287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010288 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10289 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10290 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10291 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10292 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10293 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010294 *
Romain Guy171c5922011-01-06 10:04:23 -080010295 * <p>Enabling the drawing cache is similar to
10296 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010297 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10298 * drawing cache has no effect on rendering because the system uses a different mechanism
10299 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10300 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10301 * for information on how to enable software and hardware layers.</p>
10302 *
10303 * <p>This API can be used to manually generate
10304 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10305 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010306 *
10307 * @param enabled true to enable the drawing cache, false otherwise
10308 *
10309 * @see #isDrawingCacheEnabled()
10310 * @see #getDrawingCache()
10311 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010312 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010313 */
10314 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010315 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010316 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10317 }
10318
10319 /**
10320 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10321 *
10322 * @return true if the drawing cache is enabled
10323 *
10324 * @see #setDrawingCacheEnabled(boolean)
10325 * @see #getDrawingCache()
10326 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010327 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 public boolean isDrawingCacheEnabled() {
10329 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10330 }
10331
10332 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010333 * Debugging utility which recursively outputs the dirty state of a view and its
10334 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010335 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010336 * @hide
10337 */
Romain Guy676b1732011-02-14 14:45:33 -080010338 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010339 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10340 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10341 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10342 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10343 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10344 if (clear) {
10345 mPrivateFlags &= clearMask;
10346 }
10347 if (this instanceof ViewGroup) {
10348 ViewGroup parent = (ViewGroup) this;
10349 final int count = parent.getChildCount();
10350 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010351 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010352 child.outputDirtyFlags(indent + " ", clear, clearMask);
10353 }
10354 }
10355 }
10356
10357 /**
10358 * This method is used by ViewGroup to cause its children to restore or recreate their
10359 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10360 * to recreate its own display list, which would happen if it went through the normal
10361 * draw/dispatchDraw mechanisms.
10362 *
10363 * @hide
10364 */
10365 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010366
10367 /**
10368 * A view that is not attached or hardware accelerated cannot create a display list.
10369 * This method checks these conditions and returns the appropriate result.
10370 *
10371 * @return true if view has the ability to create a display list, false otherwise.
10372 *
10373 * @hide
10374 */
10375 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010376 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010377 }
Joe Malin32736f02011-01-19 16:14:20 -080010378
Chet Haasedaf98e92011-01-10 14:10:36 -080010379 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010380 * @return The HardwareRenderer associated with that view or null if hardware rendering
10381 * is not supported or this this has not been attached to a window.
10382 *
10383 * @hide
10384 */
10385 public HardwareRenderer getHardwareRenderer() {
10386 if (mAttachInfo != null) {
10387 return mAttachInfo.mHardwareRenderer;
10388 }
10389 return null;
10390 }
10391
10392 /**
Romain Guyb051e892010-09-28 19:09:36 -070010393 * <p>Returns a display list that can be used to draw this view again
10394 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010395 *
Romain Guyb051e892010-09-28 19:09:36 -070010396 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010397 *
10398 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010399 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010400 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010401 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010402 return null;
10403 }
10404
Chet Haasedaf98e92011-01-10 14:10:36 -080010405 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10406 mDisplayList == null || !mDisplayList.isValid() ||
10407 mRecreateDisplayList)) {
10408 // Don't need to recreate the display list, just need to tell our
10409 // children to restore/recreate theirs
10410 if (mDisplayList != null && mDisplayList.isValid() &&
10411 !mRecreateDisplayList) {
10412 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10413 mPrivateFlags &= ~DIRTY_MASK;
10414 dispatchGetDisplayList();
10415
10416 return mDisplayList;
10417 }
10418
10419 // If we got here, we're recreating it. Mark it as such to ensure that
10420 // we copy in child display lists into ours in drawChild()
10421 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010422 if (mDisplayList == null) {
Romain Guy13631f32012-01-30 17:41:55 -080010423 final String name = getClass().getSimpleName();
10424 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
Chet Haasedaf98e92011-01-10 14:10:36 -080010425 // If we're creating a new display list, make sure our parent gets invalidated
10426 // since they will need to recreate their display list to account for this
10427 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010428 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010429 }
Romain Guyb051e892010-09-28 19:09:36 -070010430
10431 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010432 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010433 try {
10434 int width = mRight - mLeft;
10435 int height = mBottom - mTop;
10436
10437 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010438 // The dirty rect should always be null for a display list
10439 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010440
Chet Haasedaf98e92011-01-10 14:10:36 -080010441 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010442
10443 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010444 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010445 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010446 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010447
Romain Guyb051e892010-09-28 19:09:36 -070010448 // Fast path for layouts with no backgrounds
10449 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010450 dispatchDraw(canvas);
10451 } else {
10452 draw(canvas);
10453 }
Romain Guyb051e892010-09-28 19:09:36 -070010454 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010455 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010456 canvas.onPostDraw();
10457
10458 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010459 }
Chet Haase77785f92011-01-25 23:22:09 -080010460 } else {
10461 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10462 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010463 }
10464
10465 return mDisplayList;
10466 }
10467
10468 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010469 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010470 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010471 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010472 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010473 * @see #getDrawingCache(boolean)
10474 */
10475 public Bitmap getDrawingCache() {
10476 return getDrawingCache(false);
10477 }
10478
10479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10481 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10482 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10483 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10484 * request the drawing cache by calling this method and draw it on screen if the
10485 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010486 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010487 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10488 * this method will create a bitmap of the same size as this view. Because this bitmap
10489 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10490 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10491 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10492 * size than the view. This implies that your application must be able to handle this
10493 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010494 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010495 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10496 * the current density of the screen when the application is in compatibility
10497 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010499 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010500 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 * @see #setDrawingCacheEnabled(boolean)
10502 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010503 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010504 * @see #destroyDrawingCache()
10505 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010506 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10508 return null;
10509 }
10510 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010511 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010512 }
Romain Guy02890fd2010-08-06 17:58:44 -070010513 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010514 }
10515
10516 /**
10517 * <p>Frees the resources used by the drawing cache. If you call
10518 * {@link #buildDrawingCache()} manually without calling
10519 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10520 * should cleanup the cache with this method afterwards.</p>
10521 *
10522 * @see #setDrawingCacheEnabled(boolean)
10523 * @see #buildDrawingCache()
10524 * @see #getDrawingCache()
10525 */
10526 public void destroyDrawingCache() {
10527 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010528 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 mDrawingCache = null;
10530 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010531 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010532 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010533 mUnscaledDrawingCache = null;
10534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 }
10536
10537 /**
10538 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010539 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010540 * view will always be drawn on top of a solid color.
10541 *
10542 * @param color The background color to use for the drawing cache's bitmap
10543 *
10544 * @see #setDrawingCacheEnabled(boolean)
10545 * @see #buildDrawingCache()
10546 * @see #getDrawingCache()
10547 */
10548 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010549 if (color != mDrawingCacheBackgroundColor) {
10550 mDrawingCacheBackgroundColor = color;
10551 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 }
10554
10555 /**
10556 * @see #setDrawingCacheBackgroundColor(int)
10557 *
10558 * @return The background color to used for the drawing cache's bitmap
10559 */
10560 public int getDrawingCacheBackgroundColor() {
10561 return mDrawingCacheBackgroundColor;
10562 }
10563
10564 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010565 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010566 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010567 * @see #buildDrawingCache(boolean)
10568 */
10569 public void buildDrawingCache() {
10570 buildDrawingCache(false);
10571 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010572
Romain Guyfbd8f692009-06-26 14:51:58 -070010573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10575 *
10576 * <p>If you call {@link #buildDrawingCache()} manually without calling
10577 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10578 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010579 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010580 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10581 * this method will create a bitmap of the same size as this view. Because this bitmap
10582 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10583 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10584 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10585 * size than the view. This implies that your application must be able to handle this
10586 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010587 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010588 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10589 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010590 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010591 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 *
10593 * @see #getDrawingCache()
10594 * @see #destroyDrawingCache()
10595 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010596 public void buildDrawingCache(boolean autoScale) {
10597 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010598 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010599 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010600
10601 if (ViewDebug.TRACE_HIERARCHY) {
10602 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010604
Romain Guy8506ab42009-06-11 17:35:47 -070010605 int width = mRight - mLeft;
10606 int height = mBottom - mTop;
10607
10608 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010609 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010610
Romain Guye1123222009-06-29 14:24:56 -070010611 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010612 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10613 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010615
10616 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010617 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010618 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619
10620 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010621 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010622 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010623 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10624 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010625 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 return;
10627 }
10628
10629 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010630 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010631
10632 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633 Bitmap.Config quality;
10634 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010635 // Never pick ARGB_4444 because it looks awful
10636 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10638 case DRAWING_CACHE_QUALITY_AUTO:
10639 quality = Bitmap.Config.ARGB_8888;
10640 break;
10641 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010642 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 break;
10644 case DRAWING_CACHE_QUALITY_HIGH:
10645 quality = Bitmap.Config.ARGB_8888;
10646 break;
10647 default:
10648 quality = Bitmap.Config.ARGB_8888;
10649 break;
10650 }
10651 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010652 // Optimization for translucent windows
10653 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010654 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 }
10656
10657 // Try to cleanup memory
10658 if (bitmap != null) bitmap.recycle();
10659
10660 try {
10661 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010662 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010663 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010664 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010665 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010666 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010667 }
Adam Powell26153a32010-11-08 15:22:27 -080010668 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010669 } catch (OutOfMemoryError e) {
10670 // If there is not enough memory to create the bitmap cache, just
10671 // ignore the issue as bitmap caches are not required to draw the
10672 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010673 if (autoScale) {
10674 mDrawingCache = null;
10675 } else {
10676 mUnscaledDrawingCache = null;
10677 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010678 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 return;
10680 }
10681
10682 clear = drawingCacheBackgroundColor != 0;
10683 }
10684
10685 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 if (attachInfo != null) {
10687 canvas = attachInfo.mCanvas;
10688 if (canvas == null) {
10689 canvas = new Canvas();
10690 }
10691 canvas.setBitmap(bitmap);
10692 // Temporarily clobber the cached Canvas in case one of our children
10693 // is also using a drawing cache. Without this, the children would
10694 // steal the canvas by attaching their own bitmap to it and bad, bad
10695 // thing would happen (invisible views, corrupted drawings, etc.)
10696 attachInfo.mCanvas = null;
10697 } else {
10698 // This case should hopefully never or seldom happen
10699 canvas = new Canvas(bitmap);
10700 }
10701
10702 if (clear) {
10703 bitmap.eraseColor(drawingCacheBackgroundColor);
10704 }
10705
10706 computeScroll();
10707 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010708
Romain Guye1123222009-06-29 14:24:56 -070010709 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010710 final float scale = attachInfo.mApplicationScale;
10711 canvas.scale(scale, scale);
10712 }
Joe Malin32736f02011-01-19 16:14:20 -080010713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010714 canvas.translate(-mScrollX, -mScrollY);
10715
Romain Guy5bcdff42009-05-14 21:27:18 -070010716 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010717 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10718 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010719 mPrivateFlags |= DRAWING_CACHE_VALID;
10720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721
10722 // Fast path for layouts with no backgrounds
10723 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10724 if (ViewDebug.TRACE_HIERARCHY) {
10725 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10726 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010727 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010728 dispatchDraw(canvas);
10729 } else {
10730 draw(canvas);
10731 }
10732
10733 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010734 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735
10736 if (attachInfo != null) {
10737 // Restore the cached Canvas for our siblings
10738 attachInfo.mCanvas = canvas;
10739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 }
10741 }
10742
10743 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010744 * Create a snapshot of the view into a bitmap. We should probably make
10745 * some form of this public, but should think about the API.
10746 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010747 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010748 int width = mRight - mLeft;
10749 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010750
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010751 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010752 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010753 width = (int) ((width * scale) + 0.5f);
10754 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010755
Romain Guy8c11e312009-09-14 15:15:30 -070010756 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010757 if (bitmap == null) {
10758 throw new OutOfMemoryError();
10759 }
10760
Romain Guyc529d8d2011-09-06 15:01:39 -070010761 Resources resources = getResources();
10762 if (resources != null) {
10763 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10764 }
Joe Malin32736f02011-01-19 16:14:20 -080010765
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010766 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010767 if (attachInfo != null) {
10768 canvas = attachInfo.mCanvas;
10769 if (canvas == null) {
10770 canvas = new Canvas();
10771 }
10772 canvas.setBitmap(bitmap);
10773 // Temporarily clobber the cached Canvas in case one of our children
10774 // is also using a drawing cache. Without this, the children would
10775 // steal the canvas by attaching their own bitmap to it and bad, bad
10776 // things would happen (invisible views, corrupted drawings, etc.)
10777 attachInfo.mCanvas = null;
10778 } else {
10779 // This case should hopefully never or seldom happen
10780 canvas = new Canvas(bitmap);
10781 }
10782
Romain Guy5bcdff42009-05-14 21:27:18 -070010783 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010784 bitmap.eraseColor(backgroundColor);
10785 }
10786
10787 computeScroll();
10788 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010789 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010790 canvas.translate(-mScrollX, -mScrollY);
10791
Romain Guy5bcdff42009-05-14 21:27:18 -070010792 // Temporarily remove the dirty mask
10793 int flags = mPrivateFlags;
10794 mPrivateFlags &= ~DIRTY_MASK;
10795
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010796 // Fast path for layouts with no backgrounds
10797 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10798 dispatchDraw(canvas);
10799 } else {
10800 draw(canvas);
10801 }
10802
Romain Guy5bcdff42009-05-14 21:27:18 -070010803 mPrivateFlags = flags;
10804
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010805 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010806 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010807
10808 if (attachInfo != null) {
10809 // Restore the cached Canvas for our siblings
10810 attachInfo.mCanvas = canvas;
10811 }
Romain Guy8506ab42009-06-11 17:35:47 -070010812
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010813 return bitmap;
10814 }
10815
10816 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010817 * Indicates whether this View is currently in edit mode. A View is usually
10818 * in edit mode when displayed within a developer tool. For instance, if
10819 * this View is being drawn by a visual user interface builder, this method
10820 * should return true.
10821 *
10822 * Subclasses should check the return value of this method to provide
10823 * different behaviors if their normal behavior might interfere with the
10824 * host environment. For instance: the class spawns a thread in its
10825 * constructor, the drawing code relies on device-specific features, etc.
10826 *
10827 * This method is usually checked in the drawing code of custom widgets.
10828 *
10829 * @return True if this View is in edit mode, false otherwise.
10830 */
10831 public boolean isInEditMode() {
10832 return false;
10833 }
10834
10835 /**
10836 * If the View draws content inside its padding and enables fading edges,
10837 * it needs to support padding offsets. Padding offsets are added to the
10838 * fading edges to extend the length of the fade so that it covers pixels
10839 * drawn inside the padding.
10840 *
10841 * Subclasses of this class should override this method if they need
10842 * to draw content inside the padding.
10843 *
10844 * @return True if padding offset must be applied, false otherwise.
10845 *
10846 * @see #getLeftPaddingOffset()
10847 * @see #getRightPaddingOffset()
10848 * @see #getTopPaddingOffset()
10849 * @see #getBottomPaddingOffset()
10850 *
10851 * @since CURRENT
10852 */
10853 protected boolean isPaddingOffsetRequired() {
10854 return false;
10855 }
10856
10857 /**
10858 * Amount by which to extend the left fading region. Called only when
10859 * {@link #isPaddingOffsetRequired()} returns true.
10860 *
10861 * @return The left padding offset in pixels.
10862 *
10863 * @see #isPaddingOffsetRequired()
10864 *
10865 * @since CURRENT
10866 */
10867 protected int getLeftPaddingOffset() {
10868 return 0;
10869 }
10870
10871 /**
10872 * Amount by which to extend the right fading region. Called only when
10873 * {@link #isPaddingOffsetRequired()} returns true.
10874 *
10875 * @return The right padding offset in pixels.
10876 *
10877 * @see #isPaddingOffsetRequired()
10878 *
10879 * @since CURRENT
10880 */
10881 protected int getRightPaddingOffset() {
10882 return 0;
10883 }
10884
10885 /**
10886 * Amount by which to extend the top fading region. Called only when
10887 * {@link #isPaddingOffsetRequired()} returns true.
10888 *
10889 * @return The top padding offset in pixels.
10890 *
10891 * @see #isPaddingOffsetRequired()
10892 *
10893 * @since CURRENT
10894 */
10895 protected int getTopPaddingOffset() {
10896 return 0;
10897 }
10898
10899 /**
10900 * Amount by which to extend the bottom fading region. Called only when
10901 * {@link #isPaddingOffsetRequired()} returns true.
10902 *
10903 * @return The bottom padding offset in pixels.
10904 *
10905 * @see #isPaddingOffsetRequired()
10906 *
10907 * @since CURRENT
10908 */
10909 protected int getBottomPaddingOffset() {
10910 return 0;
10911 }
10912
10913 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010914 * @hide
10915 * @param offsetRequired
10916 */
10917 protected int getFadeTop(boolean offsetRequired) {
10918 int top = mPaddingTop;
10919 if (offsetRequired) top += getTopPaddingOffset();
10920 return top;
10921 }
10922
10923 /**
10924 * @hide
10925 * @param offsetRequired
10926 */
10927 protected int getFadeHeight(boolean offsetRequired) {
10928 int padding = mPaddingTop;
10929 if (offsetRequired) padding += getTopPaddingOffset();
10930 return mBottom - mTop - mPaddingBottom - padding;
10931 }
10932
10933 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010934 * <p>Indicates whether this view is attached to an hardware accelerated
10935 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010936 *
Romain Guy2bffd262010-09-12 17:40:02 -070010937 * <p>Even if this method returns true, it does not mean that every call
10938 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10939 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10940 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10941 * window is hardware accelerated,
10942 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10943 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010944 *
Romain Guy2bffd262010-09-12 17:40:02 -070010945 * @return True if the view is attached to a window and the window is
10946 * hardware accelerated; false in any other case.
10947 */
10948 public boolean isHardwareAccelerated() {
10949 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10950 }
Joe Malin32736f02011-01-19 16:14:20 -080010951
Romain Guy2bffd262010-09-12 17:40:02 -070010952 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010953 * Manually render this view (and all of its children) to the given Canvas.
10954 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010955 * called. When implementing a view, implement
10956 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10957 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958 *
10959 * @param canvas The Canvas to which the View is rendered.
10960 */
10961 public void draw(Canvas canvas) {
10962 if (ViewDebug.TRACE_HIERARCHY) {
10963 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10964 }
10965
Romain Guy5bcdff42009-05-14 21:27:18 -070010966 final int privateFlags = mPrivateFlags;
10967 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10968 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10969 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010971 /*
10972 * Draw traversal performs several drawing steps which must be executed
10973 * in the appropriate order:
10974 *
10975 * 1. Draw the background
10976 * 2. If necessary, save the canvas' layers to prepare for fading
10977 * 3. Draw view's content
10978 * 4. Draw children
10979 * 5. If necessary, draw the fading edges and restore layers
10980 * 6. Draw decorations (scrollbars for instance)
10981 */
10982
10983 // Step 1, draw the background, if needed
10984 int saveCount;
10985
Romain Guy24443ea2009-05-11 11:56:30 -070010986 if (!dirtyOpaque) {
10987 final Drawable background = mBGDrawable;
10988 if (background != null) {
10989 final int scrollX = mScrollX;
10990 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010991
Romain Guy24443ea2009-05-11 11:56:30 -070010992 if (mBackgroundSizeChanged) {
10993 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10994 mBackgroundSizeChanged = false;
10995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010996
Romain Guy24443ea2009-05-11 11:56:30 -070010997 if ((scrollX | scrollY) == 0) {
10998 background.draw(canvas);
10999 } else {
11000 canvas.translate(scrollX, scrollY);
11001 background.draw(canvas);
11002 canvas.translate(-scrollX, -scrollY);
11003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011004 }
11005 }
11006
11007 // skip step 2 & 5 if possible (common case)
11008 final int viewFlags = mViewFlags;
11009 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11010 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11011 if (!verticalEdges && !horizontalEdges) {
11012 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011013 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011014
11015 // Step 4, draw the children
11016 dispatchDraw(canvas);
11017
11018 // Step 6, draw decorations (scrollbars)
11019 onDrawScrollBars(canvas);
11020
11021 // we're done...
11022 return;
11023 }
11024
11025 /*
11026 * Here we do the full fledged routine...
11027 * (this is an uncommon case where speed matters less,
11028 * this is why we repeat some of the tests that have been
11029 * done above)
11030 */
11031
11032 boolean drawTop = false;
11033 boolean drawBottom = false;
11034 boolean drawLeft = false;
11035 boolean drawRight = false;
11036
11037 float topFadeStrength = 0.0f;
11038 float bottomFadeStrength = 0.0f;
11039 float leftFadeStrength = 0.0f;
11040 float rightFadeStrength = 0.0f;
11041
11042 // Step 2, save the canvas' layers
11043 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011044
11045 final boolean offsetRequired = isPaddingOffsetRequired();
11046 if (offsetRequired) {
11047 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011048 }
11049
11050 int left = mScrollX + paddingLeft;
11051 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011052 int top = mScrollY + getFadeTop(offsetRequired);
11053 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011054
11055 if (offsetRequired) {
11056 right += getRightPaddingOffset();
11057 bottom += getBottomPaddingOffset();
11058 }
11059
11060 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011061 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11062 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011063
11064 // clip the fade length if top and bottom fades overlap
11065 // overlapping fades produce odd-looking artifacts
11066 if (verticalEdges && (top + length > bottom - length)) {
11067 length = (bottom - top) / 2;
11068 }
11069
11070 // also clip horizontal fades if necessary
11071 if (horizontalEdges && (left + length > right - length)) {
11072 length = (right - left) / 2;
11073 }
11074
11075 if (verticalEdges) {
11076 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011077 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011078 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011079 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011080 }
11081
11082 if (horizontalEdges) {
11083 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011084 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011085 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011086 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011087 }
11088
11089 saveCount = canvas.getSaveCount();
11090
11091 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011092 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011093 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11094
11095 if (drawTop) {
11096 canvas.saveLayer(left, top, right, top + length, null, flags);
11097 }
11098
11099 if (drawBottom) {
11100 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11101 }
11102
11103 if (drawLeft) {
11104 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11105 }
11106
11107 if (drawRight) {
11108 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11109 }
11110 } else {
11111 scrollabilityCache.setFadeColor(solidColor);
11112 }
11113
11114 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011115 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011116
11117 // Step 4, draw the children
11118 dispatchDraw(canvas);
11119
11120 // Step 5, draw the fade effect and restore layers
11121 final Paint p = scrollabilityCache.paint;
11122 final Matrix matrix = scrollabilityCache.matrix;
11123 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011124
11125 if (drawTop) {
11126 matrix.setScale(1, fadeHeight * topFadeStrength);
11127 matrix.postTranslate(left, top);
11128 fade.setLocalMatrix(matrix);
11129 canvas.drawRect(left, top, right, top + length, p);
11130 }
11131
11132 if (drawBottom) {
11133 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11134 matrix.postRotate(180);
11135 matrix.postTranslate(left, bottom);
11136 fade.setLocalMatrix(matrix);
11137 canvas.drawRect(left, bottom - length, right, bottom, p);
11138 }
11139
11140 if (drawLeft) {
11141 matrix.setScale(1, fadeHeight * leftFadeStrength);
11142 matrix.postRotate(-90);
11143 matrix.postTranslate(left, top);
11144 fade.setLocalMatrix(matrix);
11145 canvas.drawRect(left, top, left + length, bottom, p);
11146 }
11147
11148 if (drawRight) {
11149 matrix.setScale(1, fadeHeight * rightFadeStrength);
11150 matrix.postRotate(90);
11151 matrix.postTranslate(right, top);
11152 fade.setLocalMatrix(matrix);
11153 canvas.drawRect(right - length, top, right, bottom, p);
11154 }
11155
11156 canvas.restoreToCount(saveCount);
11157
11158 // Step 6, draw decorations (scrollbars)
11159 onDrawScrollBars(canvas);
11160 }
11161
11162 /**
11163 * Override this if your view is known to always be drawn on top of a solid color background,
11164 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11165 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11166 * should be set to 0xFF.
11167 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011168 * @see #setVerticalFadingEdgeEnabled(boolean)
11169 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 *
11171 * @return The known solid color background for this view, or 0 if the color may vary
11172 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011173 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011174 public int getSolidColor() {
11175 return 0;
11176 }
11177
11178 /**
11179 * Build a human readable string representation of the specified view flags.
11180 *
11181 * @param flags the view flags to convert to a string
11182 * @return a String representing the supplied flags
11183 */
11184 private static String printFlags(int flags) {
11185 String output = "";
11186 int numFlags = 0;
11187 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11188 output += "TAKES_FOCUS";
11189 numFlags++;
11190 }
11191
11192 switch (flags & VISIBILITY_MASK) {
11193 case INVISIBLE:
11194 if (numFlags > 0) {
11195 output += " ";
11196 }
11197 output += "INVISIBLE";
11198 // USELESS HERE numFlags++;
11199 break;
11200 case GONE:
11201 if (numFlags > 0) {
11202 output += " ";
11203 }
11204 output += "GONE";
11205 // USELESS HERE numFlags++;
11206 break;
11207 default:
11208 break;
11209 }
11210 return output;
11211 }
11212
11213 /**
11214 * Build a human readable string representation of the specified private
11215 * view flags.
11216 *
11217 * @param privateFlags the private view flags to convert to a string
11218 * @return a String representing the supplied flags
11219 */
11220 private static String printPrivateFlags(int privateFlags) {
11221 String output = "";
11222 int numFlags = 0;
11223
11224 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11225 output += "WANTS_FOCUS";
11226 numFlags++;
11227 }
11228
11229 if ((privateFlags & FOCUSED) == FOCUSED) {
11230 if (numFlags > 0) {
11231 output += " ";
11232 }
11233 output += "FOCUSED";
11234 numFlags++;
11235 }
11236
11237 if ((privateFlags & SELECTED) == SELECTED) {
11238 if (numFlags > 0) {
11239 output += " ";
11240 }
11241 output += "SELECTED";
11242 numFlags++;
11243 }
11244
11245 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11246 if (numFlags > 0) {
11247 output += " ";
11248 }
11249 output += "IS_ROOT_NAMESPACE";
11250 numFlags++;
11251 }
11252
11253 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11254 if (numFlags > 0) {
11255 output += " ";
11256 }
11257 output += "HAS_BOUNDS";
11258 numFlags++;
11259 }
11260
11261 if ((privateFlags & DRAWN) == DRAWN) {
11262 if (numFlags > 0) {
11263 output += " ";
11264 }
11265 output += "DRAWN";
11266 // USELESS HERE numFlags++;
11267 }
11268 return output;
11269 }
11270
11271 /**
11272 * <p>Indicates whether or not this view's layout will be requested during
11273 * the next hierarchy layout pass.</p>
11274 *
11275 * @return true if the layout will be forced during next layout pass
11276 */
11277 public boolean isLayoutRequested() {
11278 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11279 }
11280
11281 /**
11282 * Assign a size and position to a view and all of its
11283 * descendants
11284 *
11285 * <p>This is the second phase of the layout mechanism.
11286 * (The first is measuring). In this phase, each parent calls
11287 * layout on all of its children to position them.
11288 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011289 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011290 *
Chet Haase9c087442011-01-12 16:20:16 -080011291 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011292 * Derived classes with children should override
11293 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011294 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011295 *
11296 * @param l Left position, relative to parent
11297 * @param t Top position, relative to parent
11298 * @param r Right position, relative to parent
11299 * @param b Bottom position, relative to parent
11300 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011301 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011302 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011303 int oldL = mLeft;
11304 int oldT = mTop;
11305 int oldB = mBottom;
11306 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011307 boolean changed = setFrame(l, t, r, b);
11308 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11309 if (ViewDebug.TRACE_HIERARCHY) {
11310 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11311 }
11312
11313 onLayout(changed, l, t, r, b);
11314 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011315
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011316 ListenerInfo li = mListenerInfo;
11317 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011318 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011319 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011320 int numListeners = listenersCopy.size();
11321 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011322 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011323 }
11324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011325 }
11326 mPrivateFlags &= ~FORCE_LAYOUT;
11327 }
11328
11329 /**
11330 * Called from layout when this view should
11331 * assign a size and position to each of its children.
11332 *
11333 * Derived classes with children should override
11334 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011335 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011336 * @param changed This is a new size or position for this view
11337 * @param left Left position, relative to parent
11338 * @param top Top position, relative to parent
11339 * @param right Right position, relative to parent
11340 * @param bottom Bottom position, relative to parent
11341 */
11342 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11343 }
11344
11345 /**
11346 * Assign a size and position to this view.
11347 *
11348 * This is called from layout.
11349 *
11350 * @param left Left position, relative to parent
11351 * @param top Top position, relative to parent
11352 * @param right Right position, relative to parent
11353 * @param bottom Bottom position, relative to parent
11354 * @return true if the new size and position are different than the
11355 * previous ones
11356 * {@hide}
11357 */
11358 protected boolean setFrame(int left, int top, int right, int bottom) {
11359 boolean changed = false;
11360
11361 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011362 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011363 + right + "," + bottom + ")");
11364 }
11365
11366 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11367 changed = true;
11368
11369 // Remember our drawn bit
11370 int drawn = mPrivateFlags & DRAWN;
11371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011372 int oldWidth = mRight - mLeft;
11373 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011374 int newWidth = right - left;
11375 int newHeight = bottom - top;
11376 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11377
11378 // Invalidate our old position
11379 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011380
11381 mLeft = left;
11382 mTop = top;
11383 mRight = right;
11384 mBottom = bottom;
11385
11386 mPrivateFlags |= HAS_BOUNDS;
11387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011388
Chet Haase75755e22011-07-18 17:48:25 -070011389 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011390 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11391 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011392 if (mTransformationInfo != null) {
11393 mTransformationInfo.mMatrixDirty = true;
11394 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011396 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11397 }
11398
11399 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11400 // If we are visible, force the DRAWN bit to on so that
11401 // this invalidate will go through (at least to our parent).
11402 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011403 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011404 // the DRAWN bit.
11405 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011406 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011407 // parent display list may need to be recreated based on a change in the bounds
11408 // of any child
11409 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011410 }
11411
11412 // Reset drawn bit to original value (invalidate turns it off)
11413 mPrivateFlags |= drawn;
11414
11415 mBackgroundSizeChanged = true;
11416 }
11417 return changed;
11418 }
11419
11420 /**
11421 * Finalize inflating a view from XML. This is called as the last phase
11422 * of inflation, after all child views have been added.
11423 *
11424 * <p>Even if the subclass overrides onFinishInflate, they should always be
11425 * sure to call the super method, so that we get called.
11426 */
11427 protected void onFinishInflate() {
11428 }
11429
11430 /**
11431 * Returns the resources associated with this view.
11432 *
11433 * @return Resources object.
11434 */
11435 public Resources getResources() {
11436 return mResources;
11437 }
11438
11439 /**
11440 * Invalidates the specified Drawable.
11441 *
11442 * @param drawable the drawable to invalidate
11443 */
11444 public void invalidateDrawable(Drawable drawable) {
11445 if (verifyDrawable(drawable)) {
11446 final Rect dirty = drawable.getBounds();
11447 final int scrollX = mScrollX;
11448 final int scrollY = mScrollY;
11449
11450 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11451 dirty.right + scrollX, dirty.bottom + scrollY);
11452 }
11453 }
11454
11455 /**
11456 * Schedules an action on a drawable to occur at a specified time.
11457 *
11458 * @param who the recipient of the action
11459 * @param what the action to run on the drawable
11460 * @param when the time at which the action must occur. Uses the
11461 * {@link SystemClock#uptimeMillis} timebase.
11462 */
11463 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011464 if (verifyDrawable(who) && what != null) {
11465 if (mAttachInfo != null) {
11466 mAttachInfo.mHandler.postAtTime(what, who, when);
11467 } else {
11468 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011470 }
11471 }
11472
11473 /**
11474 * Cancels a scheduled action on a drawable.
11475 *
11476 * @param who the recipient of the action
11477 * @param what the action to cancel
11478 */
11479 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011480 if (verifyDrawable(who) && what != null) {
11481 if (mAttachInfo != null) {
11482 mAttachInfo.mHandler.removeCallbacks(what, who);
11483 } else {
11484 ViewRootImpl.getRunQueue().removeCallbacks(what);
11485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011486 }
11487 }
11488
11489 /**
11490 * Unschedule any events associated with the given Drawable. This can be
11491 * used when selecting a new Drawable into a view, so that the previous
11492 * one is completely unscheduled.
11493 *
11494 * @param who The Drawable to unschedule.
11495 *
11496 * @see #drawableStateChanged
11497 */
11498 public void unscheduleDrawable(Drawable who) {
11499 if (mAttachInfo != null) {
11500 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11501 }
11502 }
11503
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011504 /**
11505 * Return the layout direction of a given Drawable.
11506 *
11507 * @param who the Drawable to query
11508 *
11509 * @hide
11510 */
11511 public int getResolvedLayoutDirection(Drawable who) {
11512 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011513 }
11514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011515 /**
11516 * If your view subclass is displaying its own Drawable objects, it should
11517 * override this function and return true for any Drawable it is
11518 * displaying. This allows animations for those drawables to be
11519 * scheduled.
11520 *
11521 * <p>Be sure to call through to the super class when overriding this
11522 * function.
11523 *
11524 * @param who The Drawable to verify. Return true if it is one you are
11525 * displaying, else return the result of calling through to the
11526 * super class.
11527 *
11528 * @return boolean If true than the Drawable is being displayed in the
11529 * view; else false and it is not allowed to animate.
11530 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011531 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11532 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011533 */
11534 protected boolean verifyDrawable(Drawable who) {
11535 return who == mBGDrawable;
11536 }
11537
11538 /**
11539 * This function is called whenever the state of the view changes in such
11540 * a way that it impacts the state of drawables being shown.
11541 *
11542 * <p>Be sure to call through to the superclass when overriding this
11543 * function.
11544 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011545 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011546 */
11547 protected void drawableStateChanged() {
11548 Drawable d = mBGDrawable;
11549 if (d != null && d.isStateful()) {
11550 d.setState(getDrawableState());
11551 }
11552 }
11553
11554 /**
11555 * Call this to force a view to update its drawable state. This will cause
11556 * drawableStateChanged to be called on this view. Views that are interested
11557 * in the new state should call getDrawableState.
11558 *
11559 * @see #drawableStateChanged
11560 * @see #getDrawableState
11561 */
11562 public void refreshDrawableState() {
11563 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11564 drawableStateChanged();
11565
11566 ViewParent parent = mParent;
11567 if (parent != null) {
11568 parent.childDrawableStateChanged(this);
11569 }
11570 }
11571
11572 /**
11573 * Return an array of resource IDs of the drawable states representing the
11574 * current state of the view.
11575 *
11576 * @return The current drawable state
11577 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011578 * @see Drawable#setState(int[])
11579 * @see #drawableStateChanged()
11580 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011581 */
11582 public final int[] getDrawableState() {
11583 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11584 return mDrawableState;
11585 } else {
11586 mDrawableState = onCreateDrawableState(0);
11587 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11588 return mDrawableState;
11589 }
11590 }
11591
11592 /**
11593 * Generate the new {@link android.graphics.drawable.Drawable} state for
11594 * this view. This is called by the view
11595 * system when the cached Drawable state is determined to be invalid. To
11596 * retrieve the current state, you should use {@link #getDrawableState}.
11597 *
11598 * @param extraSpace if non-zero, this is the number of extra entries you
11599 * would like in the returned array in which you can place your own
11600 * states.
11601 *
11602 * @return Returns an array holding the current {@link Drawable} state of
11603 * the view.
11604 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011605 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011606 */
11607 protected int[] onCreateDrawableState(int extraSpace) {
11608 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11609 mParent instanceof View) {
11610 return ((View) mParent).onCreateDrawableState(extraSpace);
11611 }
11612
11613 int[] drawableState;
11614
11615 int privateFlags = mPrivateFlags;
11616
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011617 int viewStateIndex = 0;
11618 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11619 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11620 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011621 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011622 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11623 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011624 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11625 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011626 // This is set if HW acceleration is requested, even if the current
11627 // process doesn't allow it. This is just to allow app preview
11628 // windows to better match their app.
11629 viewStateIndex |= VIEW_STATE_ACCELERATED;
11630 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011631 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011632
Christopher Tate3d4bf172011-03-28 16:16:46 -070011633 final int privateFlags2 = mPrivateFlags2;
11634 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11635 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011637 drawableState = VIEW_STATE_SETS[viewStateIndex];
11638
11639 //noinspection ConstantIfStatement
11640 if (false) {
11641 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11642 Log.i("View", toString()
11643 + " pressed=" + ((privateFlags & PRESSED) != 0)
11644 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11645 + " fo=" + hasFocus()
11646 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011647 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011648 + ": " + Arrays.toString(drawableState));
11649 }
11650
11651 if (extraSpace == 0) {
11652 return drawableState;
11653 }
11654
11655 final int[] fullState;
11656 if (drawableState != null) {
11657 fullState = new int[drawableState.length + extraSpace];
11658 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11659 } else {
11660 fullState = new int[extraSpace];
11661 }
11662
11663 return fullState;
11664 }
11665
11666 /**
11667 * Merge your own state values in <var>additionalState</var> into the base
11668 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011669 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011670 *
11671 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011672 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011673 * own additional state values.
11674 *
11675 * @param additionalState The additional state values you would like
11676 * added to <var>baseState</var>; this array is not modified.
11677 *
11678 * @return As a convenience, the <var>baseState</var> array you originally
11679 * passed into the function is returned.
11680 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011681 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011682 */
11683 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11684 final int N = baseState.length;
11685 int i = N - 1;
11686 while (i >= 0 && baseState[i] == 0) {
11687 i--;
11688 }
11689 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11690 return baseState;
11691 }
11692
11693 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011694 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11695 * on all Drawable objects associated with this view.
11696 */
11697 public void jumpDrawablesToCurrentState() {
11698 if (mBGDrawable != null) {
11699 mBGDrawable.jumpToCurrentState();
11700 }
11701 }
11702
11703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011704 * Sets the background color for this view.
11705 * @param color the color of the background
11706 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011707 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011708 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011709 if (mBGDrawable instanceof ColorDrawable) {
11710 ((ColorDrawable) mBGDrawable).setColor(color);
11711 } else {
11712 setBackgroundDrawable(new ColorDrawable(color));
11713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011714 }
11715
11716 /**
11717 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011718 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011719 * @param resid The identifier of the resource.
11720 * @attr ref android.R.styleable#View_background
11721 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011722 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011723 public void setBackgroundResource(int resid) {
11724 if (resid != 0 && resid == mBackgroundResource) {
11725 return;
11726 }
11727
11728 Drawable d= null;
11729 if (resid != 0) {
11730 d = mResources.getDrawable(resid);
11731 }
11732 setBackgroundDrawable(d);
11733
11734 mBackgroundResource = resid;
11735 }
11736
11737 /**
11738 * Set the background to a given Drawable, or remove the background. If the
11739 * background has padding, this View's padding is set to the background's
11740 * padding. However, when a background is removed, this View's padding isn't
11741 * touched. If setting the padding is desired, please use
11742 * {@link #setPadding(int, int, int, int)}.
11743 *
11744 * @param d The Drawable to use as the background, or null to remove the
11745 * background
11746 */
11747 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011748 if (d == mBGDrawable) {
11749 return;
11750 }
11751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011752 boolean requestLayout = false;
11753
11754 mBackgroundResource = 0;
11755
11756 /*
11757 * Regardless of whether we're setting a new background or not, we want
11758 * to clear the previous drawable.
11759 */
11760 if (mBGDrawable != null) {
11761 mBGDrawable.setCallback(null);
11762 unscheduleDrawable(mBGDrawable);
11763 }
11764
11765 if (d != null) {
11766 Rect padding = sThreadLocal.get();
11767 if (padding == null) {
11768 padding = new Rect();
11769 sThreadLocal.set(padding);
11770 }
11771 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011772 switch (d.getResolvedLayoutDirectionSelf()) {
11773 case LAYOUT_DIRECTION_RTL:
11774 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11775 break;
11776 case LAYOUT_DIRECTION_LTR:
11777 default:
11778 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011780 }
11781
11782 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11783 // if it has a different minimum size, we should layout again
11784 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11785 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11786 requestLayout = true;
11787 }
11788
11789 d.setCallback(this);
11790 if (d.isStateful()) {
11791 d.setState(getDrawableState());
11792 }
11793 d.setVisible(getVisibility() == VISIBLE, false);
11794 mBGDrawable = d;
11795
11796 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11797 mPrivateFlags &= ~SKIP_DRAW;
11798 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11799 requestLayout = true;
11800 }
11801 } else {
11802 /* Remove the background */
11803 mBGDrawable = null;
11804
11805 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11806 /*
11807 * This view ONLY drew the background before and we're removing
11808 * the background, so now it won't draw anything
11809 * (hence we SKIP_DRAW)
11810 */
11811 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11812 mPrivateFlags |= SKIP_DRAW;
11813 }
11814
11815 /*
11816 * When the background is set, we try to apply its padding to this
11817 * View. When the background is removed, we don't touch this View's
11818 * padding. This is noted in the Javadocs. Hence, we don't need to
11819 * requestLayout(), the invalidate() below is sufficient.
11820 */
11821
11822 // The old background's minimum size could have affected this
11823 // View's layout, so let's requestLayout
11824 requestLayout = true;
11825 }
11826
Romain Guy8f1344f52009-05-15 16:03:59 -070011827 computeOpaqueFlags();
11828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011829 if (requestLayout) {
11830 requestLayout();
11831 }
11832
11833 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011834 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011835 }
11836
11837 /**
11838 * Gets the background drawable
11839 * @return The drawable used as the background for this view, if any.
11840 */
11841 public Drawable getBackground() {
11842 return mBGDrawable;
11843 }
11844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011845 /**
11846 * Sets the padding. The view may add on the space required to display
11847 * the scrollbars, depending on the style and visibility of the scrollbars.
11848 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11849 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11850 * from the values set in this call.
11851 *
11852 * @attr ref android.R.styleable#View_padding
11853 * @attr ref android.R.styleable#View_paddingBottom
11854 * @attr ref android.R.styleable#View_paddingLeft
11855 * @attr ref android.R.styleable#View_paddingRight
11856 * @attr ref android.R.styleable#View_paddingTop
11857 * @param left the left padding in pixels
11858 * @param top the top padding in pixels
11859 * @param right the right padding in pixels
11860 * @param bottom the bottom padding in pixels
11861 */
11862 public void setPadding(int left, int top, int right, int bottom) {
11863 boolean changed = false;
11864
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011865 mUserPaddingRelative = false;
11866
Adam Powell20232d02010-12-08 21:08:53 -080011867 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011868 mUserPaddingRight = right;
11869 mUserPaddingBottom = bottom;
11870
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011871 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011872
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011873 // Common case is there are no scroll bars.
11874 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011875 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011876 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011877 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011878 switch (mVerticalScrollbarPosition) {
11879 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011880 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11881 left += offset;
11882 } else {
11883 right += offset;
11884 }
11885 break;
Adam Powell20232d02010-12-08 21:08:53 -080011886 case SCROLLBAR_POSITION_RIGHT:
11887 right += offset;
11888 break;
11889 case SCROLLBAR_POSITION_LEFT:
11890 left += offset;
11891 break;
11892 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011893 }
Adam Powell20232d02010-12-08 21:08:53 -080011894 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011895 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11896 ? 0 : getHorizontalScrollbarHeight();
11897 }
11898 }
Romain Guy8506ab42009-06-11 17:35:47 -070011899
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011900 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011901 changed = true;
11902 mPaddingLeft = left;
11903 }
11904 if (mPaddingTop != top) {
11905 changed = true;
11906 mPaddingTop = top;
11907 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011908 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011909 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011910 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011911 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011912 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011913 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011914 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011915 }
11916
11917 if (changed) {
11918 requestLayout();
11919 }
11920 }
11921
11922 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011923 * Sets the relative padding. The view may add on the space required to display
11924 * the scrollbars, depending on the style and visibility of the scrollbars.
11925 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11926 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11927 * from the values set in this call.
11928 *
11929 * @attr ref android.R.styleable#View_padding
11930 * @attr ref android.R.styleable#View_paddingBottom
11931 * @attr ref android.R.styleable#View_paddingStart
11932 * @attr ref android.R.styleable#View_paddingEnd
11933 * @attr ref android.R.styleable#View_paddingTop
11934 * @param start the start padding in pixels
11935 * @param top the top padding in pixels
11936 * @param end the end padding in pixels
11937 * @param bottom the bottom padding in pixels
11938 *
11939 * @hide
11940 */
11941 public void setPaddingRelative(int start, int top, int end, int bottom) {
11942 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011943
11944 mUserPaddingStart = start;
11945 mUserPaddingEnd = end;
11946
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011947 switch(getResolvedLayoutDirection()) {
11948 case LAYOUT_DIRECTION_RTL:
11949 setPadding(end, top, start, bottom);
11950 break;
11951 case LAYOUT_DIRECTION_LTR:
11952 default:
11953 setPadding(start, top, end, bottom);
11954 }
11955 }
11956
11957 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011958 * Returns the top padding of this view.
11959 *
11960 * @return the top padding in pixels
11961 */
11962 public int getPaddingTop() {
11963 return mPaddingTop;
11964 }
11965
11966 /**
11967 * Returns the bottom padding of this view. If there are inset and enabled
11968 * scrollbars, this value may include the space required to display the
11969 * scrollbars as well.
11970 *
11971 * @return the bottom padding in pixels
11972 */
11973 public int getPaddingBottom() {
11974 return mPaddingBottom;
11975 }
11976
11977 /**
11978 * Returns the left padding of this view. If there are inset and enabled
11979 * scrollbars, this value may include the space required to display the
11980 * scrollbars as well.
11981 *
11982 * @return the left padding in pixels
11983 */
11984 public int getPaddingLeft() {
11985 return mPaddingLeft;
11986 }
11987
11988 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011989 * Returns the start padding of this view. If there are inset and enabled
11990 * scrollbars, this value may include the space required to display the
11991 * scrollbars as well.
11992 *
11993 * @return the start padding in pixels
11994 *
11995 * @hide
11996 */
11997 public int getPaddingStart() {
11998 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11999 mPaddingRight : mPaddingLeft;
12000 }
12001
12002 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012003 * Returns the right padding of this view. If there are inset and enabled
12004 * scrollbars, this value may include the space required to display the
12005 * scrollbars as well.
12006 *
12007 * @return the right padding in pixels
12008 */
12009 public int getPaddingRight() {
12010 return mPaddingRight;
12011 }
12012
12013 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012014 * Returns the end padding of this view. If there are inset and enabled
12015 * scrollbars, this value may include the space required to display the
12016 * scrollbars as well.
12017 *
12018 * @return the end padding in pixels
12019 *
12020 * @hide
12021 */
12022 public int getPaddingEnd() {
12023 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12024 mPaddingLeft : mPaddingRight;
12025 }
12026
12027 /**
12028 * Return if the padding as been set thru relative values
12029 * {@link #setPaddingRelative(int, int, int, int)} or thru
12030 * @attr ref android.R.styleable#View_paddingStart or
12031 * @attr ref android.R.styleable#View_paddingEnd
12032 *
12033 * @return true if the padding is relative or false if it is not.
12034 *
12035 * @hide
12036 */
12037 public boolean isPaddingRelative() {
12038 return mUserPaddingRelative;
12039 }
12040
12041 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 * Changes the selection state of this view. A view can be selected or not.
12043 * Note that selection is not the same as focus. Views are typically
12044 * selected in the context of an AdapterView like ListView or GridView;
12045 * the selected view is the view that is highlighted.
12046 *
12047 * @param selected true if the view must be selected, false otherwise
12048 */
12049 public void setSelected(boolean selected) {
12050 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12051 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012052 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012053 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012054 refreshDrawableState();
12055 dispatchSetSelected(selected);
12056 }
12057 }
12058
12059 /**
12060 * Dispatch setSelected to all of this View's children.
12061 *
12062 * @see #setSelected(boolean)
12063 *
12064 * @param selected The new selected state
12065 */
12066 protected void dispatchSetSelected(boolean selected) {
12067 }
12068
12069 /**
12070 * Indicates the selection state of this view.
12071 *
12072 * @return true if the view is selected, false otherwise
12073 */
12074 @ViewDebug.ExportedProperty
12075 public boolean isSelected() {
12076 return (mPrivateFlags & SELECTED) != 0;
12077 }
12078
12079 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012080 * Changes the activated state of this view. A view can be activated or not.
12081 * Note that activation is not the same as selection. Selection is
12082 * a transient property, representing the view (hierarchy) the user is
12083 * currently interacting with. Activation is a longer-term state that the
12084 * user can move views in and out of. For example, in a list view with
12085 * single or multiple selection enabled, the views in the current selection
12086 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12087 * here.) The activated state is propagated down to children of the view it
12088 * is set on.
12089 *
12090 * @param activated true if the view must be activated, false otherwise
12091 */
12092 public void setActivated(boolean activated) {
12093 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12094 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012095 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012096 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012097 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012098 }
12099 }
12100
12101 /**
12102 * Dispatch setActivated to all of this View's children.
12103 *
12104 * @see #setActivated(boolean)
12105 *
12106 * @param activated The new activated state
12107 */
12108 protected void dispatchSetActivated(boolean activated) {
12109 }
12110
12111 /**
12112 * Indicates the activation state of this view.
12113 *
12114 * @return true if the view is activated, false otherwise
12115 */
12116 @ViewDebug.ExportedProperty
12117 public boolean isActivated() {
12118 return (mPrivateFlags & ACTIVATED) != 0;
12119 }
12120
12121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012122 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12123 * observer can be used to get notifications when global events, like
12124 * layout, happen.
12125 *
12126 * The returned ViewTreeObserver observer is not guaranteed to remain
12127 * valid for the lifetime of this View. If the caller of this method keeps
12128 * a long-lived reference to ViewTreeObserver, it should always check for
12129 * the return value of {@link ViewTreeObserver#isAlive()}.
12130 *
12131 * @return The ViewTreeObserver for this view's hierarchy.
12132 */
12133 public ViewTreeObserver getViewTreeObserver() {
12134 if (mAttachInfo != null) {
12135 return mAttachInfo.mTreeObserver;
12136 }
12137 if (mFloatingTreeObserver == null) {
12138 mFloatingTreeObserver = new ViewTreeObserver();
12139 }
12140 return mFloatingTreeObserver;
12141 }
12142
12143 /**
12144 * <p>Finds the topmost view in the current view hierarchy.</p>
12145 *
12146 * @return the topmost view containing this view
12147 */
12148 public View getRootView() {
12149 if (mAttachInfo != null) {
12150 final View v = mAttachInfo.mRootView;
12151 if (v != null) {
12152 return v;
12153 }
12154 }
Romain Guy8506ab42009-06-11 17:35:47 -070012155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012156 View parent = this;
12157
12158 while (parent.mParent != null && parent.mParent instanceof View) {
12159 parent = (View) parent.mParent;
12160 }
12161
12162 return parent;
12163 }
12164
12165 /**
12166 * <p>Computes the coordinates of this view on the screen. The argument
12167 * must be an array of two integers. After the method returns, the array
12168 * contains the x and y location in that order.</p>
12169 *
12170 * @param location an array of two integers in which to hold the coordinates
12171 */
12172 public void getLocationOnScreen(int[] location) {
12173 getLocationInWindow(location);
12174
12175 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012176 if (info != null) {
12177 location[0] += info.mWindowLeft;
12178 location[1] += info.mWindowTop;
12179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012180 }
12181
12182 /**
12183 * <p>Computes the coordinates of this view in its window. The argument
12184 * must be an array of two integers. After the method returns, the array
12185 * contains the x and y location in that order.</p>
12186 *
12187 * @param location an array of two integers in which to hold the coordinates
12188 */
12189 public void getLocationInWindow(int[] location) {
12190 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012191 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012192 }
12193
Gilles Debunne6583ce52011-12-06 18:09:02 -080012194 if (mAttachInfo == null) {
12195 // When the view is not attached to a window, this method does not make sense
12196 location[0] = location[1] = 0;
12197 return;
12198 }
12199
Gilles Debunnecea45132011-11-24 02:19:27 +010012200 float[] position = mAttachInfo.mTmpTransformLocation;
12201 position[0] = position[1] = 0.0f;
12202
12203 if (!hasIdentityMatrix()) {
12204 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012206
Gilles Debunnecea45132011-11-24 02:19:27 +010012207 position[0] += mLeft;
12208 position[1] += mTop;
12209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012210 ViewParent viewParent = mParent;
12211 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012212 final View view = (View) viewParent;
12213
12214 position[0] -= view.mScrollX;
12215 position[1] -= view.mScrollY;
12216
12217 if (!view.hasIdentityMatrix()) {
12218 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012219 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012220
12221 position[0] += view.mLeft;
12222 position[1] += view.mTop;
12223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012224 viewParent = view.mParent;
12225 }
Romain Guy8506ab42009-06-11 17:35:47 -070012226
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012227 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012229 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12230 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012231 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012232
12233 location[0] = (int) (position[0] + 0.5f);
12234 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012235 }
12236
12237 /**
12238 * {@hide}
12239 * @param id the id of the view to be found
12240 * @return the view of the specified id, null if cannot be found
12241 */
12242 protected View findViewTraversal(int id) {
12243 if (id == mID) {
12244 return this;
12245 }
12246 return null;
12247 }
12248
12249 /**
12250 * {@hide}
12251 * @param tag the tag of the view to be found
12252 * @return the view of specified tag, null if cannot be found
12253 */
12254 protected View findViewWithTagTraversal(Object tag) {
12255 if (tag != null && tag.equals(mTag)) {
12256 return this;
12257 }
12258 return null;
12259 }
12260
12261 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012262 * {@hide}
12263 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012264 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012265 * @return The first view that matches the predicate or null.
12266 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012267 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012268 if (predicate.apply(this)) {
12269 return this;
12270 }
12271 return null;
12272 }
12273
12274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012275 * Look for a child view with the given id. If this view has the given
12276 * id, return this view.
12277 *
12278 * @param id The id to search for.
12279 * @return The view that has the given id in the hierarchy or null
12280 */
12281 public final View findViewById(int id) {
12282 if (id < 0) {
12283 return null;
12284 }
12285 return findViewTraversal(id);
12286 }
12287
12288 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012289 * Finds a view by its unuque and stable accessibility id.
12290 *
12291 * @param accessibilityId The searched accessibility id.
12292 * @return The found view.
12293 */
12294 final View findViewByAccessibilityId(int accessibilityId) {
12295 if (accessibilityId < 0) {
12296 return null;
12297 }
12298 return findViewByAccessibilityIdTraversal(accessibilityId);
12299 }
12300
12301 /**
12302 * Performs the traversal to find a view by its unuque and stable accessibility id.
12303 *
12304 * <strong>Note:</strong>This method does not stop at the root namespace
12305 * boundary since the user can touch the screen at an arbitrary location
12306 * potentially crossing the root namespace bounday which will send an
12307 * accessibility event to accessibility services and they should be able
12308 * to obtain the event source. Also accessibility ids are guaranteed to be
12309 * unique in the window.
12310 *
12311 * @param accessibilityId The accessibility id.
12312 * @return The found view.
12313 */
12314 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12315 if (getAccessibilityViewId() == accessibilityId) {
12316 return this;
12317 }
12318 return null;
12319 }
12320
12321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012322 * Look for a child view with the given tag. If this view has the given
12323 * tag, return this view.
12324 *
12325 * @param tag The tag to search for, using "tag.equals(getTag())".
12326 * @return The View that has the given tag in the hierarchy or null
12327 */
12328 public final View findViewWithTag(Object tag) {
12329 if (tag == null) {
12330 return null;
12331 }
12332 return findViewWithTagTraversal(tag);
12333 }
12334
12335 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012336 * {@hide}
12337 * Look for a child view that matches the specified predicate.
12338 * If this view matches the predicate, return this view.
12339 *
12340 * @param predicate The predicate to evaluate.
12341 * @return The first view that matches the predicate or null.
12342 */
12343 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012344 return findViewByPredicateTraversal(predicate, null);
12345 }
12346
12347 /**
12348 * {@hide}
12349 * Look for a child view that matches the specified predicate,
12350 * starting with the specified view and its descendents and then
12351 * recusively searching the ancestors and siblings of that view
12352 * until this view is reached.
12353 *
12354 * This method is useful in cases where the predicate does not match
12355 * a single unique view (perhaps multiple views use the same id)
12356 * and we are trying to find the view that is "closest" in scope to the
12357 * starting view.
12358 *
12359 * @param start The view to start from.
12360 * @param predicate The predicate to evaluate.
12361 * @return The first view that matches the predicate or null.
12362 */
12363 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12364 View childToSkip = null;
12365 for (;;) {
12366 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12367 if (view != null || start == this) {
12368 return view;
12369 }
12370
12371 ViewParent parent = start.getParent();
12372 if (parent == null || !(parent instanceof View)) {
12373 return null;
12374 }
12375
12376 childToSkip = start;
12377 start = (View) parent;
12378 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012379 }
12380
12381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012382 * Sets the identifier for this view. The identifier does not have to be
12383 * unique in this view's hierarchy. The identifier should be a positive
12384 * number.
12385 *
12386 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012387 * @see #getId()
12388 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012389 *
12390 * @param id a number used to identify the view
12391 *
12392 * @attr ref android.R.styleable#View_id
12393 */
12394 public void setId(int id) {
12395 mID = id;
12396 }
12397
12398 /**
12399 * {@hide}
12400 *
12401 * @param isRoot true if the view belongs to the root namespace, false
12402 * otherwise
12403 */
12404 public void setIsRootNamespace(boolean isRoot) {
12405 if (isRoot) {
12406 mPrivateFlags |= IS_ROOT_NAMESPACE;
12407 } else {
12408 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12409 }
12410 }
12411
12412 /**
12413 * {@hide}
12414 *
12415 * @return true if the view belongs to the root namespace, false otherwise
12416 */
12417 public boolean isRootNamespace() {
12418 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12419 }
12420
12421 /**
12422 * Returns this view's identifier.
12423 *
12424 * @return a positive integer used to identify the view or {@link #NO_ID}
12425 * if the view has no ID
12426 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012427 * @see #setId(int)
12428 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012429 * @attr ref android.R.styleable#View_id
12430 */
12431 @ViewDebug.CapturedViewProperty
12432 public int getId() {
12433 return mID;
12434 }
12435
12436 /**
12437 * Returns this view's tag.
12438 *
12439 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012440 *
12441 * @see #setTag(Object)
12442 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012443 */
12444 @ViewDebug.ExportedProperty
12445 public Object getTag() {
12446 return mTag;
12447 }
12448
12449 /**
12450 * Sets the tag associated with this view. A tag can be used to mark
12451 * a view in its hierarchy and does not have to be unique within the
12452 * hierarchy. Tags can also be used to store data within a view without
12453 * resorting to another data structure.
12454 *
12455 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012456 *
12457 * @see #getTag()
12458 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012459 */
12460 public void setTag(final Object tag) {
12461 mTag = tag;
12462 }
12463
12464 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012465 * Returns the tag associated with this view and the specified key.
12466 *
12467 * @param key The key identifying the tag
12468 *
12469 * @return the Object stored in this view as a tag
12470 *
12471 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012472 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012473 */
12474 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012475 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012476 return null;
12477 }
12478
12479 /**
12480 * Sets a tag associated with this view and a key. A tag can be used
12481 * to mark a view in its hierarchy and does not have to be unique within
12482 * the hierarchy. Tags can also be used to store data within a view
12483 * without resorting to another data structure.
12484 *
12485 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012486 * application to ensure it is unique (see the <a
12487 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12488 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012489 * the Android framework or not associated with any package will cause
12490 * an {@link IllegalArgumentException} to be thrown.
12491 *
12492 * @param key The key identifying the tag
12493 * @param tag An Object to tag the view with
12494 *
12495 * @throws IllegalArgumentException If they specified key is not valid
12496 *
12497 * @see #setTag(Object)
12498 * @see #getTag(int)
12499 */
12500 public void setTag(int key, final Object tag) {
12501 // If the package id is 0x00 or 0x01, it's either an undefined package
12502 // or a framework id
12503 if ((key >>> 24) < 2) {
12504 throw new IllegalArgumentException("The key must be an application-specific "
12505 + "resource id.");
12506 }
12507
Adam Powell2b2f6d62011-09-23 11:15:39 -070012508 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012509 }
12510
12511 /**
12512 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12513 * framework id.
12514 *
12515 * @hide
12516 */
12517 public void setTagInternal(int key, Object tag) {
12518 if ((key >>> 24) != 0x1) {
12519 throw new IllegalArgumentException("The key must be a framework-specific "
12520 + "resource id.");
12521 }
12522
Adam Powell2b2f6d62011-09-23 11:15:39 -070012523 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012524 }
12525
Adam Powell2b2f6d62011-09-23 11:15:39 -070012526 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012527 if (mKeyedTags == null) {
12528 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012529 }
12530
Adam Powell7db82ac2011-09-22 19:44:04 -070012531 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012532 }
12533
12534 /**
Romain Guy13922e02009-05-12 17:56:14 -070012535 * @param consistency The type of consistency. See ViewDebug for more information.
12536 *
12537 * @hide
12538 */
12539 protected boolean dispatchConsistencyCheck(int consistency) {
12540 return onConsistencyCheck(consistency);
12541 }
12542
12543 /**
12544 * Method that subclasses should implement to check their consistency. The type of
12545 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012546 *
Romain Guy13922e02009-05-12 17:56:14 -070012547 * @param consistency The type of consistency. See ViewDebug for more information.
12548 *
12549 * @throws IllegalStateException if the view is in an inconsistent state.
12550 *
12551 * @hide
12552 */
12553 protected boolean onConsistencyCheck(int consistency) {
12554 boolean result = true;
12555
12556 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12557 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12558
12559 if (checkLayout) {
12560 if (getParent() == null) {
12561 result = false;
12562 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12563 "View " + this + " does not have a parent.");
12564 }
12565
12566 if (mAttachInfo == null) {
12567 result = false;
12568 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12569 "View " + this + " is not attached to a window.");
12570 }
12571 }
12572
12573 if (checkDrawing) {
12574 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12575 // from their draw() method
12576
12577 if ((mPrivateFlags & DRAWN) != DRAWN &&
12578 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12579 result = false;
12580 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12581 "View " + this + " was invalidated but its drawing cache is valid.");
12582 }
12583 }
12584
12585 return result;
12586 }
12587
12588 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012589 * Prints information about this view in the log output, with the tag
12590 * {@link #VIEW_LOG_TAG}.
12591 *
12592 * @hide
12593 */
12594 public void debug() {
12595 debug(0);
12596 }
12597
12598 /**
12599 * Prints information about this view in the log output, with the tag
12600 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12601 * indentation defined by the <code>depth</code>.
12602 *
12603 * @param depth the indentation level
12604 *
12605 * @hide
12606 */
12607 protected void debug(int depth) {
12608 String output = debugIndent(depth - 1);
12609
12610 output += "+ " + this;
12611 int id = getId();
12612 if (id != -1) {
12613 output += " (id=" + id + ")";
12614 }
12615 Object tag = getTag();
12616 if (tag != null) {
12617 output += " (tag=" + tag + ")";
12618 }
12619 Log.d(VIEW_LOG_TAG, output);
12620
12621 if ((mPrivateFlags & FOCUSED) != 0) {
12622 output = debugIndent(depth) + " FOCUSED";
12623 Log.d(VIEW_LOG_TAG, output);
12624 }
12625
12626 output = debugIndent(depth);
12627 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12628 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12629 + "} ";
12630 Log.d(VIEW_LOG_TAG, output);
12631
12632 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12633 || mPaddingBottom != 0) {
12634 output = debugIndent(depth);
12635 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12636 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12637 Log.d(VIEW_LOG_TAG, output);
12638 }
12639
12640 output = debugIndent(depth);
12641 output += "mMeasureWidth=" + mMeasuredWidth +
12642 " mMeasureHeight=" + mMeasuredHeight;
12643 Log.d(VIEW_LOG_TAG, output);
12644
12645 output = debugIndent(depth);
12646 if (mLayoutParams == null) {
12647 output += "BAD! no layout params";
12648 } else {
12649 output = mLayoutParams.debug(output);
12650 }
12651 Log.d(VIEW_LOG_TAG, output);
12652
12653 output = debugIndent(depth);
12654 output += "flags={";
12655 output += View.printFlags(mViewFlags);
12656 output += "}";
12657 Log.d(VIEW_LOG_TAG, output);
12658
12659 output = debugIndent(depth);
12660 output += "privateFlags={";
12661 output += View.printPrivateFlags(mPrivateFlags);
12662 output += "}";
12663 Log.d(VIEW_LOG_TAG, output);
12664 }
12665
12666 /**
12667 * Creates an string of whitespaces used for indentation.
12668 *
12669 * @param depth the indentation level
12670 * @return a String containing (depth * 2 + 3) * 2 white spaces
12671 *
12672 * @hide
12673 */
12674 protected static String debugIndent(int depth) {
12675 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12676 for (int i = 0; i < (depth * 2) + 3; i++) {
12677 spaces.append(' ').append(' ');
12678 }
12679 return spaces.toString();
12680 }
12681
12682 /**
12683 * <p>Return the offset of the widget's text baseline from the widget's top
12684 * boundary. If this widget does not support baseline alignment, this
12685 * method returns -1. </p>
12686 *
12687 * @return the offset of the baseline within the widget's bounds or -1
12688 * if baseline alignment is not supported
12689 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012690 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012691 public int getBaseline() {
12692 return -1;
12693 }
12694
12695 /**
12696 * Call this when something has changed which has invalidated the
12697 * layout of this view. This will schedule a layout pass of the view
12698 * tree.
12699 */
12700 public void requestLayout() {
12701 if (ViewDebug.TRACE_HIERARCHY) {
12702 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12703 }
12704
Svetoslav Ganov79311c42012-01-17 20:24:26 -080012705 if (getAccessibilityNodeProvider() != null) {
12706 throw new IllegalStateException("Views with AccessibilityNodeProvider"
12707 + " can't have children.");
12708 }
12709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012710 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012711 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012712
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012713 if (mParent != null) {
12714 if (mLayoutParams != null) {
12715 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12716 }
12717 if (!mParent.isLayoutRequested()) {
12718 mParent.requestLayout();
12719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012720 }
12721 }
12722
12723 /**
12724 * Forces this view to be laid out during the next layout pass.
12725 * This method does not call requestLayout() or forceLayout()
12726 * on the parent.
12727 */
12728 public void forceLayout() {
12729 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012730 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012731 }
12732
12733 /**
12734 * <p>
12735 * This is called to find out how big a view should be. The parent
12736 * supplies constraint information in the width and height parameters.
12737 * </p>
12738 *
12739 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080012740 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012741 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080012742 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012743 * </p>
12744 *
12745 *
12746 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12747 * parent
12748 * @param heightMeasureSpec Vertical space requirements as imposed by the
12749 * parent
12750 *
12751 * @see #onMeasure(int, int)
12752 */
12753 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12754 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12755 widthMeasureSpec != mOldWidthMeasureSpec ||
12756 heightMeasureSpec != mOldHeightMeasureSpec) {
12757
12758 // first clears the measured dimension flag
12759 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12760
12761 if (ViewDebug.TRACE_HIERARCHY) {
12762 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12763 }
12764
12765 // measure ourselves, this should set the measured dimension flag back
12766 onMeasure(widthMeasureSpec, heightMeasureSpec);
12767
12768 // flag not set, setMeasuredDimension() was not invoked, we raise
12769 // an exception to warn the developer
12770 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12771 throw new IllegalStateException("onMeasure() did not set the"
12772 + " measured dimension by calling"
12773 + " setMeasuredDimension()");
12774 }
12775
12776 mPrivateFlags |= LAYOUT_REQUIRED;
12777 }
12778
12779 mOldWidthMeasureSpec = widthMeasureSpec;
12780 mOldHeightMeasureSpec = heightMeasureSpec;
12781 }
12782
12783 /**
12784 * <p>
12785 * Measure the view and its content to determine the measured width and the
12786 * measured height. This method is invoked by {@link #measure(int, int)} and
12787 * should be overriden by subclasses to provide accurate and efficient
12788 * measurement of their contents.
12789 * </p>
12790 *
12791 * <p>
12792 * <strong>CONTRACT:</strong> When overriding this method, you
12793 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12794 * measured width and height of this view. Failure to do so will trigger an
12795 * <code>IllegalStateException</code>, thrown by
12796 * {@link #measure(int, int)}. Calling the superclass'
12797 * {@link #onMeasure(int, int)} is a valid use.
12798 * </p>
12799 *
12800 * <p>
12801 * The base class implementation of measure defaults to the background size,
12802 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12803 * override {@link #onMeasure(int, int)} to provide better measurements of
12804 * their content.
12805 * </p>
12806 *
12807 * <p>
12808 * If this method is overridden, it is the subclass's responsibility to make
12809 * sure the measured height and width are at least the view's minimum height
12810 * and width ({@link #getSuggestedMinimumHeight()} and
12811 * {@link #getSuggestedMinimumWidth()}).
12812 * </p>
12813 *
12814 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12815 * The requirements are encoded with
12816 * {@link android.view.View.MeasureSpec}.
12817 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12818 * The requirements are encoded with
12819 * {@link android.view.View.MeasureSpec}.
12820 *
12821 * @see #getMeasuredWidth()
12822 * @see #getMeasuredHeight()
12823 * @see #setMeasuredDimension(int, int)
12824 * @see #getSuggestedMinimumHeight()
12825 * @see #getSuggestedMinimumWidth()
12826 * @see android.view.View.MeasureSpec#getMode(int)
12827 * @see android.view.View.MeasureSpec#getSize(int)
12828 */
12829 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12830 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12831 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12832 }
12833
12834 /**
12835 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12836 * measured width and measured height. Failing to do so will trigger an
12837 * exception at measurement time.</p>
12838 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012839 * @param measuredWidth The measured width of this view. May be a complex
12840 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12841 * {@link #MEASURED_STATE_TOO_SMALL}.
12842 * @param measuredHeight The measured height of this view. May be a complex
12843 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12844 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012845 */
12846 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12847 mMeasuredWidth = measuredWidth;
12848 mMeasuredHeight = measuredHeight;
12849
12850 mPrivateFlags |= MEASURED_DIMENSION_SET;
12851 }
12852
12853 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012854 * Merge two states as returned by {@link #getMeasuredState()}.
12855 * @param curState The current state as returned from a view or the result
12856 * of combining multiple views.
12857 * @param newState The new view state to combine.
12858 * @return Returns a new integer reflecting the combination of the two
12859 * states.
12860 */
12861 public static int combineMeasuredStates(int curState, int newState) {
12862 return curState | newState;
12863 }
12864
12865 /**
12866 * Version of {@link #resolveSizeAndState(int, int, int)}
12867 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12868 */
12869 public static int resolveSize(int size, int measureSpec) {
12870 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12871 }
12872
12873 /**
12874 * Utility to reconcile a desired size and state, with constraints imposed
12875 * by a MeasureSpec. Will take the desired size, unless a different size
12876 * is imposed by the constraints. The returned value is a compound integer,
12877 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12878 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12879 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012880 *
12881 * @param size How big the view wants to be
12882 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012883 * @return Size information bit mask as defined by
12884 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012885 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012886 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012887 int result = size;
12888 int specMode = MeasureSpec.getMode(measureSpec);
12889 int specSize = MeasureSpec.getSize(measureSpec);
12890 switch (specMode) {
12891 case MeasureSpec.UNSPECIFIED:
12892 result = size;
12893 break;
12894 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012895 if (specSize < size) {
12896 result = specSize | MEASURED_STATE_TOO_SMALL;
12897 } else {
12898 result = size;
12899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012900 break;
12901 case MeasureSpec.EXACTLY:
12902 result = specSize;
12903 break;
12904 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012905 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012906 }
12907
12908 /**
12909 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012910 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012911 * by the MeasureSpec.
12912 *
12913 * @param size Default size for this view
12914 * @param measureSpec Constraints imposed by the parent
12915 * @return The size this view should be.
12916 */
12917 public static int getDefaultSize(int size, int measureSpec) {
12918 int result = size;
12919 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012920 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012921
12922 switch (specMode) {
12923 case MeasureSpec.UNSPECIFIED:
12924 result = size;
12925 break;
12926 case MeasureSpec.AT_MOST:
12927 case MeasureSpec.EXACTLY:
12928 result = specSize;
12929 break;
12930 }
12931 return result;
12932 }
12933
12934 /**
12935 * Returns the suggested minimum height that the view should use. This
12936 * returns the maximum of the view's minimum height
12937 * and the background's minimum height
12938 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12939 * <p>
12940 * When being used in {@link #onMeasure(int, int)}, the caller should still
12941 * ensure the returned height is within the requirements of the parent.
12942 *
12943 * @return The suggested minimum height of the view.
12944 */
12945 protected int getSuggestedMinimumHeight() {
12946 int suggestedMinHeight = mMinHeight;
12947
12948 if (mBGDrawable != null) {
12949 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12950 if (suggestedMinHeight < bgMinHeight) {
12951 suggestedMinHeight = bgMinHeight;
12952 }
12953 }
12954
12955 return suggestedMinHeight;
12956 }
12957
12958 /**
12959 * Returns the suggested minimum width that the view should use. This
12960 * returns the maximum of the view's minimum width)
12961 * and the background's minimum width
12962 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12963 * <p>
12964 * When being used in {@link #onMeasure(int, int)}, the caller should still
12965 * ensure the returned width is within the requirements of the parent.
12966 *
12967 * @return The suggested minimum width of the view.
12968 */
12969 protected int getSuggestedMinimumWidth() {
12970 int suggestedMinWidth = mMinWidth;
12971
12972 if (mBGDrawable != null) {
12973 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12974 if (suggestedMinWidth < bgMinWidth) {
12975 suggestedMinWidth = bgMinWidth;
12976 }
12977 }
12978
12979 return suggestedMinWidth;
12980 }
12981
12982 /**
12983 * Sets the minimum height of the view. It is not guaranteed the view will
12984 * be able to achieve this minimum height (for example, if its parent layout
12985 * constrains it with less available height).
12986 *
12987 * @param minHeight The minimum height the view will try to be.
12988 */
12989 public void setMinimumHeight(int minHeight) {
12990 mMinHeight = minHeight;
12991 }
12992
12993 /**
12994 * Sets the minimum width of the view. It is not guaranteed the view will
12995 * be able to achieve this minimum width (for example, if its parent layout
12996 * constrains it with less available width).
12997 *
12998 * @param minWidth The minimum width the view will try to be.
12999 */
13000 public void setMinimumWidth(int minWidth) {
13001 mMinWidth = minWidth;
13002 }
13003
13004 /**
13005 * Get the animation currently associated with this view.
13006 *
13007 * @return The animation that is currently playing or
13008 * scheduled to play for this view.
13009 */
13010 public Animation getAnimation() {
13011 return mCurrentAnimation;
13012 }
13013
13014 /**
13015 * Start the specified animation now.
13016 *
13017 * @param animation the animation to start now
13018 */
13019 public void startAnimation(Animation animation) {
13020 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13021 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013022 invalidateParentCaches();
13023 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013024 }
13025
13026 /**
13027 * Cancels any animations for this view.
13028 */
13029 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013030 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013031 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013033 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013034 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013035 }
13036
13037 /**
13038 * Sets the next animation to play for this view.
13039 * If you want the animation to play immediately, use
13040 * startAnimation. This method provides allows fine-grained
13041 * control over the start time and invalidation, but you
13042 * must make sure that 1) the animation has a start time set, and
13043 * 2) the view will be invalidated when the animation is supposed to
13044 * start.
13045 *
13046 * @param animation The next animation, or null.
13047 */
13048 public void setAnimation(Animation animation) {
13049 mCurrentAnimation = animation;
13050 if (animation != null) {
13051 animation.reset();
13052 }
13053 }
13054
13055 /**
13056 * Invoked by a parent ViewGroup to notify the start of the animation
13057 * currently associated with this view. If you override this method,
13058 * always call super.onAnimationStart();
13059 *
13060 * @see #setAnimation(android.view.animation.Animation)
13061 * @see #getAnimation()
13062 */
13063 protected void onAnimationStart() {
13064 mPrivateFlags |= ANIMATION_STARTED;
13065 }
13066
13067 /**
13068 * Invoked by a parent ViewGroup to notify the end of the animation
13069 * currently associated with this view. If you override this method,
13070 * always call super.onAnimationEnd();
13071 *
13072 * @see #setAnimation(android.view.animation.Animation)
13073 * @see #getAnimation()
13074 */
13075 protected void onAnimationEnd() {
13076 mPrivateFlags &= ~ANIMATION_STARTED;
13077 }
13078
13079 /**
13080 * Invoked if there is a Transform that involves alpha. Subclass that can
13081 * draw themselves with the specified alpha should return true, and then
13082 * respect that alpha when their onDraw() is called. If this returns false
13083 * then the view may be redirected to draw into an offscreen buffer to
13084 * fulfill the request, which will look fine, but may be slower than if the
13085 * subclass handles it internally. The default implementation returns false.
13086 *
13087 * @param alpha The alpha (0..255) to apply to the view's drawing
13088 * @return true if the view can draw with the specified alpha.
13089 */
13090 protected boolean onSetAlpha(int alpha) {
13091 return false;
13092 }
13093
13094 /**
13095 * This is used by the RootView to perform an optimization when
13096 * the view hierarchy contains one or several SurfaceView.
13097 * SurfaceView is always considered transparent, but its children are not,
13098 * therefore all View objects remove themselves from the global transparent
13099 * region (passed as a parameter to this function).
13100 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013101 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013102 *
13103 * @return Returns true if the effective visibility of the view at this
13104 * point is opaque, regardless of the transparent region; returns false
13105 * if it is possible for underlying windows to be seen behind the view.
13106 *
13107 * {@hide}
13108 */
13109 public boolean gatherTransparentRegion(Region region) {
13110 final AttachInfo attachInfo = mAttachInfo;
13111 if (region != null && attachInfo != null) {
13112 final int pflags = mPrivateFlags;
13113 if ((pflags & SKIP_DRAW) == 0) {
13114 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13115 // remove it from the transparent region.
13116 final int[] location = attachInfo.mTransparentLocation;
13117 getLocationInWindow(location);
13118 region.op(location[0], location[1], location[0] + mRight - mLeft,
13119 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13120 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13121 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13122 // exists, so we remove the background drawable's non-transparent
13123 // parts from this transparent region.
13124 applyDrawableToTransparentRegion(mBGDrawable, region);
13125 }
13126 }
13127 return true;
13128 }
13129
13130 /**
13131 * Play a sound effect for this view.
13132 *
13133 * <p>The framework will play sound effects for some built in actions, such as
13134 * clicking, but you may wish to play these effects in your widget,
13135 * for instance, for internal navigation.
13136 *
13137 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13138 * {@link #isSoundEffectsEnabled()} is true.
13139 *
13140 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13141 */
13142 public void playSoundEffect(int soundConstant) {
13143 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13144 return;
13145 }
13146 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13147 }
13148
13149 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013150 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013151 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013152 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013153 *
13154 * <p>The framework will provide haptic feedback for some built in actions,
13155 * such as long presses, but you may wish to provide feedback for your
13156 * own widget.
13157 *
13158 * <p>The feedback will only be performed if
13159 * {@link #isHapticFeedbackEnabled()} is true.
13160 *
13161 * @param feedbackConstant One of the constants defined in
13162 * {@link HapticFeedbackConstants}
13163 */
13164 public boolean performHapticFeedback(int feedbackConstant) {
13165 return performHapticFeedback(feedbackConstant, 0);
13166 }
13167
13168 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013169 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013170 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013171 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013172 *
13173 * @param feedbackConstant One of the constants defined in
13174 * {@link HapticFeedbackConstants}
13175 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13176 */
13177 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13178 if (mAttachInfo == null) {
13179 return false;
13180 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013181 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013182 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013183 && !isHapticFeedbackEnabled()) {
13184 return false;
13185 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013186 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13187 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013188 }
13189
13190 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013191 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013192 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13193 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013194 */
13195 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013196 if (visibility != mSystemUiVisibility) {
13197 mSystemUiVisibility = visibility;
13198 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13199 mParent.recomputeViewAttributes(this);
13200 }
Joe Onorato664644d2011-01-23 17:53:23 -080013201 }
13202 }
13203
13204 /**
13205 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013206 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13207 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013208 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013209 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013210 return mSystemUiVisibility;
13211 }
13212
Scott Mainec6331b2011-05-24 16:55:56 -070013213 /**
13214 * Set a listener to receive callbacks when the visibility of the system bar changes.
13215 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13216 */
Joe Onorato664644d2011-01-23 17:53:23 -080013217 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013218 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013219 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13220 mParent.recomputeViewAttributes(this);
13221 }
13222 }
13223
13224 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013225 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13226 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013227 */
13228 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013229 ListenerInfo li = mListenerInfo;
13230 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13231 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013232 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013233 }
13234 }
13235
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013236 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13237 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13238 if (val != mSystemUiVisibility) {
13239 setSystemUiVisibility(val);
13240 }
13241 }
13242
Joe Onorato664644d2011-01-23 17:53:23 -080013243 /**
Joe Malin32736f02011-01-19 16:14:20 -080013244 * Creates an image that the system displays during the drag and drop
13245 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13246 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13247 * appearance as the given View. The default also positions the center of the drag shadow
13248 * directly under the touch point. If no View is provided (the constructor with no parameters
13249 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13250 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13251 * default is an invisible drag shadow.
13252 * <p>
13253 * You are not required to use the View you provide to the constructor as the basis of the
13254 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13255 * anything you want as the drag shadow.
13256 * </p>
13257 * <p>
13258 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13259 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13260 * size and position of the drag shadow. It uses this data to construct a
13261 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13262 * so that your application can draw the shadow image in the Canvas.
13263 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013264 *
13265 * <div class="special reference">
13266 * <h3>Developer Guides</h3>
13267 * <p>For a guide to implementing drag and drop features, read the
13268 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13269 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013270 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013271 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013272 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013273
13274 /**
Joe Malin32736f02011-01-19 16:14:20 -080013275 * Constructs a shadow image builder based on a View. By default, the resulting drag
13276 * shadow will have the same appearance and dimensions as the View, with the touch point
13277 * over the center of the View.
13278 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013279 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013280 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013281 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013282 }
13283
Christopher Tate17ed60c2011-01-18 12:50:26 -080013284 /**
13285 * Construct a shadow builder object with no associated View. This
13286 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13287 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13288 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013289 * reference to any View object. If they are not overridden, then the result is an
13290 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013291 */
13292 public DragShadowBuilder() {
13293 mView = new WeakReference<View>(null);
13294 }
13295
13296 /**
13297 * Returns the View object that had been passed to the
13298 * {@link #View.DragShadowBuilder(View)}
13299 * constructor. If that View parameter was {@code null} or if the
13300 * {@link #View.DragShadowBuilder()}
13301 * constructor was used to instantiate the builder object, this method will return
13302 * null.
13303 *
13304 * @return The View object associate with this builder object.
13305 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013306 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013307 final public View getView() {
13308 return mView.get();
13309 }
13310
Christopher Tate2c095f32010-10-04 14:13:40 -070013311 /**
Joe Malin32736f02011-01-19 16:14:20 -080013312 * Provides the metrics for the shadow image. These include the dimensions of
13313 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013314 * be centered under the touch location while dragging.
13315 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013316 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013317 * same as the dimensions of the View itself and centers the shadow under
13318 * the touch point.
13319 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013320 *
Joe Malin32736f02011-01-19 16:14:20 -080013321 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13322 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13323 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13324 * image.
13325 *
13326 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13327 * shadow image that should be underneath the touch point during the drag and drop
13328 * operation. Your application must set {@link android.graphics.Point#x} to the
13329 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013330 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013331 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013332 final View view = mView.get();
13333 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013334 shadowSize.set(view.getWidth(), view.getHeight());
13335 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013336 } else {
13337 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13338 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013339 }
13340
13341 /**
Joe Malin32736f02011-01-19 16:14:20 -080013342 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13343 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013344 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013345 *
Joe Malin32736f02011-01-19 16:14:20 -080013346 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013347 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013348 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013349 final View view = mView.get();
13350 if (view != null) {
13351 view.draw(canvas);
13352 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013353 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013354 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013355 }
13356 }
13357
13358 /**
Joe Malin32736f02011-01-19 16:14:20 -080013359 * Starts a drag and drop operation. When your application calls this method, it passes a
13360 * {@link android.view.View.DragShadowBuilder} object to the system. The
13361 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13362 * to get metrics for the drag shadow, and then calls the object's
13363 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13364 * <p>
13365 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13366 * drag events to all the View objects in your application that are currently visible. It does
13367 * this either by calling the View object's drag listener (an implementation of
13368 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13369 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13370 * Both are passed a {@link android.view.DragEvent} object that has a
13371 * {@link android.view.DragEvent#getAction()} value of
13372 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13373 * </p>
13374 * <p>
13375 * Your application can invoke startDrag() on any attached View object. The View object does not
13376 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13377 * be related to the View the user selected for dragging.
13378 * </p>
13379 * @param data A {@link android.content.ClipData} object pointing to the data to be
13380 * transferred by the drag and drop operation.
13381 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13382 * drag shadow.
13383 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13384 * drop operation. This Object is put into every DragEvent object sent by the system during the
13385 * current drag.
13386 * <p>
13387 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13388 * to the target Views. For example, it can contain flags that differentiate between a
13389 * a copy operation and a move operation.
13390 * </p>
13391 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13392 * so the parameter should be set to 0.
13393 * @return {@code true} if the method completes successfully, or
13394 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13395 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013396 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013397 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013398 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013399 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013400 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013401 }
13402 boolean okay = false;
13403
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013404 Point shadowSize = new Point();
13405 Point shadowTouchPoint = new Point();
13406 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013407
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013408 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13409 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13410 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013411 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013412
Chris Tatea32dcf72010-10-14 12:13:50 -070013413 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013414 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13415 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013416 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013417 Surface surface = new Surface();
13418 try {
13419 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013420 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013421 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013422 + " surface=" + surface);
13423 if (token != null) {
13424 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013425 try {
Chris Tate6b391282010-10-14 15:48:59 -070013426 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013427 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013428 } finally {
13429 surface.unlockCanvasAndPost(canvas);
13430 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013431
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013432 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013433
13434 // Cache the local state object for delivery with DragEvents
13435 root.setLocalDragState(myLocalState);
13436
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013437 // repurpose 'shadowSize' for the last touch point
13438 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013439
Christopher Tatea53146c2010-09-07 11:57:52 -070013440 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013441 shadowSize.x, shadowSize.y,
13442 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013443 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013444
13445 // Off and running! Release our local surface instance; the drag
13446 // shadow surface is now managed by the system process.
13447 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013448 }
13449 } catch (Exception e) {
13450 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13451 surface.destroy();
13452 }
13453
13454 return okay;
13455 }
13456
Christopher Tatea53146c2010-09-07 11:57:52 -070013457 /**
Joe Malin32736f02011-01-19 16:14:20 -080013458 * Handles drag events sent by the system following a call to
13459 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13460 *<p>
13461 * When the system calls this method, it passes a
13462 * {@link android.view.DragEvent} object. A call to
13463 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13464 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13465 * operation.
13466 * @param event The {@link android.view.DragEvent} sent by the system.
13467 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13468 * in DragEvent, indicating the type of drag event represented by this object.
13469 * @return {@code true} if the method was successful, otherwise {@code false}.
13470 * <p>
13471 * The method should return {@code true} in response to an action type of
13472 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13473 * operation.
13474 * </p>
13475 * <p>
13476 * The method should also return {@code true} in response to an action type of
13477 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13478 * {@code false} if it didn't.
13479 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013480 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013481 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013482 return false;
13483 }
13484
13485 /**
Joe Malin32736f02011-01-19 16:14:20 -080013486 * Detects if this View is enabled and has a drag event listener.
13487 * If both are true, then it calls the drag event listener with the
13488 * {@link android.view.DragEvent} it received. If the drag event listener returns
13489 * {@code true}, then dispatchDragEvent() returns {@code true}.
13490 * <p>
13491 * For all other cases, the method calls the
13492 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13493 * method and returns its result.
13494 * </p>
13495 * <p>
13496 * This ensures that a drag event is always consumed, even if the View does not have a drag
13497 * event listener. However, if the View has a listener and the listener returns true, then
13498 * onDragEvent() is not called.
13499 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013500 */
13501 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013502 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013503 ListenerInfo li = mListenerInfo;
13504 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13505 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013506 return true;
13507 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013508 return onDragEvent(event);
13509 }
13510
Christopher Tate3d4bf172011-03-28 16:16:46 -070013511 boolean canAcceptDrag() {
13512 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13513 }
13514
Christopher Tatea53146c2010-09-07 11:57:52 -070013515 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013516 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13517 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013518 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013519 */
13520 public void onCloseSystemDialogs(String reason) {
13521 }
Joe Malin32736f02011-01-19 16:14:20 -080013522
Dianne Hackbornffa42482009-09-23 22:20:11 -070013523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013524 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013525 * update a Region being computed for
13526 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013527 * that any non-transparent parts of the Drawable are removed from the
13528 * given transparent region.
13529 *
13530 * @param dr The Drawable whose transparency is to be applied to the region.
13531 * @param region A Region holding the current transparency information,
13532 * where any parts of the region that are set are considered to be
13533 * transparent. On return, this region will be modified to have the
13534 * transparency information reduced by the corresponding parts of the
13535 * Drawable that are not transparent.
13536 * {@hide}
13537 */
13538 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13539 if (DBG) {
13540 Log.i("View", "Getting transparent region for: " + this);
13541 }
13542 final Region r = dr.getTransparentRegion();
13543 final Rect db = dr.getBounds();
13544 final AttachInfo attachInfo = mAttachInfo;
13545 if (r != null && attachInfo != null) {
13546 final int w = getRight()-getLeft();
13547 final int h = getBottom()-getTop();
13548 if (db.left > 0) {
13549 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13550 r.op(0, 0, db.left, h, Region.Op.UNION);
13551 }
13552 if (db.right < w) {
13553 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13554 r.op(db.right, 0, w, h, Region.Op.UNION);
13555 }
13556 if (db.top > 0) {
13557 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13558 r.op(0, 0, w, db.top, Region.Op.UNION);
13559 }
13560 if (db.bottom < h) {
13561 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13562 r.op(0, db.bottom, w, h, Region.Op.UNION);
13563 }
13564 final int[] location = attachInfo.mTransparentLocation;
13565 getLocationInWindow(location);
13566 r.translate(location[0], location[1]);
13567 region.op(r, Region.Op.INTERSECT);
13568 } else {
13569 region.op(db, Region.Op.DIFFERENCE);
13570 }
13571 }
13572
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013573 private void checkForLongClick(int delayOffset) {
13574 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13575 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013576
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013577 if (mPendingCheckForLongPress == null) {
13578 mPendingCheckForLongPress = new CheckForLongPress();
13579 }
13580 mPendingCheckForLongPress.rememberWindowAttachCount();
13581 postDelayed(mPendingCheckForLongPress,
13582 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013584 }
13585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586 /**
13587 * Inflate a view from an XML resource. This convenience method wraps the {@link
13588 * LayoutInflater} class, which provides a full range of options for view inflation.
13589 *
13590 * @param context The Context object for your activity or application.
13591 * @param resource The resource ID to inflate
13592 * @param root A view group that will be the parent. Used to properly inflate the
13593 * layout_* parameters.
13594 * @see LayoutInflater
13595 */
13596 public static View inflate(Context context, int resource, ViewGroup root) {
13597 LayoutInflater factory = LayoutInflater.from(context);
13598 return factory.inflate(resource, root);
13599 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013601 /**
Adam Powell637d3372010-08-25 14:37:03 -070013602 * Scroll the view with standard behavior for scrolling beyond the normal
13603 * content boundaries. Views that call this method should override
13604 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13605 * results of an over-scroll operation.
13606 *
13607 * Views can use this method to handle any touch or fling-based scrolling.
13608 *
13609 * @param deltaX Change in X in pixels
13610 * @param deltaY Change in Y in pixels
13611 * @param scrollX Current X scroll value in pixels before applying deltaX
13612 * @param scrollY Current Y scroll value in pixels before applying deltaY
13613 * @param scrollRangeX Maximum content scroll range along the X axis
13614 * @param scrollRangeY Maximum content scroll range along the Y axis
13615 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13616 * along the X axis.
13617 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13618 * along the Y axis.
13619 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13620 * @return true if scrolling was clamped to an over-scroll boundary along either
13621 * axis, false otherwise.
13622 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013623 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013624 protected boolean overScrollBy(int deltaX, int deltaY,
13625 int scrollX, int scrollY,
13626 int scrollRangeX, int scrollRangeY,
13627 int maxOverScrollX, int maxOverScrollY,
13628 boolean isTouchEvent) {
13629 final int overScrollMode = mOverScrollMode;
13630 final boolean canScrollHorizontal =
13631 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13632 final boolean canScrollVertical =
13633 computeVerticalScrollRange() > computeVerticalScrollExtent();
13634 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13635 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13636 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13637 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13638
13639 int newScrollX = scrollX + deltaX;
13640 if (!overScrollHorizontal) {
13641 maxOverScrollX = 0;
13642 }
13643
13644 int newScrollY = scrollY + deltaY;
13645 if (!overScrollVertical) {
13646 maxOverScrollY = 0;
13647 }
13648
13649 // Clamp values if at the limits and record
13650 final int left = -maxOverScrollX;
13651 final int right = maxOverScrollX + scrollRangeX;
13652 final int top = -maxOverScrollY;
13653 final int bottom = maxOverScrollY + scrollRangeY;
13654
13655 boolean clampedX = false;
13656 if (newScrollX > right) {
13657 newScrollX = right;
13658 clampedX = true;
13659 } else if (newScrollX < left) {
13660 newScrollX = left;
13661 clampedX = true;
13662 }
13663
13664 boolean clampedY = false;
13665 if (newScrollY > bottom) {
13666 newScrollY = bottom;
13667 clampedY = true;
13668 } else if (newScrollY < top) {
13669 newScrollY = top;
13670 clampedY = true;
13671 }
13672
13673 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13674
13675 return clampedX || clampedY;
13676 }
13677
13678 /**
13679 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13680 * respond to the results of an over-scroll operation.
13681 *
13682 * @param scrollX New X scroll value in pixels
13683 * @param scrollY New Y scroll value in pixels
13684 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13685 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13686 */
13687 protected void onOverScrolled(int scrollX, int scrollY,
13688 boolean clampedX, boolean clampedY) {
13689 // Intentionally empty.
13690 }
13691
13692 /**
13693 * Returns the over-scroll mode for this view. The result will be
13694 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13695 * (allow over-scrolling only if the view content is larger than the container),
13696 * or {@link #OVER_SCROLL_NEVER}.
13697 *
13698 * @return This view's over-scroll mode.
13699 */
13700 public int getOverScrollMode() {
13701 return mOverScrollMode;
13702 }
13703
13704 /**
13705 * Set the over-scroll mode for this view. Valid over-scroll modes are
13706 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13707 * (allow over-scrolling only if the view content is larger than the container),
13708 * or {@link #OVER_SCROLL_NEVER}.
13709 *
13710 * Setting the over-scroll mode of a view will have an effect only if the
13711 * view is capable of scrolling.
13712 *
13713 * @param overScrollMode The new over-scroll mode for this view.
13714 */
13715 public void setOverScrollMode(int overScrollMode) {
13716 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13717 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13718 overScrollMode != OVER_SCROLL_NEVER) {
13719 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13720 }
13721 mOverScrollMode = overScrollMode;
13722 }
13723
13724 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013725 * Gets a scale factor that determines the distance the view should scroll
13726 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13727 * @return The vertical scroll scale factor.
13728 * @hide
13729 */
13730 protected float getVerticalScrollFactor() {
13731 if (mVerticalScrollFactor == 0) {
13732 TypedValue outValue = new TypedValue();
13733 if (!mContext.getTheme().resolveAttribute(
13734 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13735 throw new IllegalStateException(
13736 "Expected theme to define listPreferredItemHeight.");
13737 }
13738 mVerticalScrollFactor = outValue.getDimension(
13739 mContext.getResources().getDisplayMetrics());
13740 }
13741 return mVerticalScrollFactor;
13742 }
13743
13744 /**
13745 * Gets a scale factor that determines the distance the view should scroll
13746 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13747 * @return The horizontal scroll scale factor.
13748 * @hide
13749 */
13750 protected float getHorizontalScrollFactor() {
13751 // TODO: Should use something else.
13752 return getVerticalScrollFactor();
13753 }
13754
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013755 /**
13756 * Return the value specifying the text direction or policy that was set with
13757 * {@link #setTextDirection(int)}.
13758 *
13759 * @return the defined text direction. It can be one of:
13760 *
13761 * {@link #TEXT_DIRECTION_INHERIT},
13762 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13763 * {@link #TEXT_DIRECTION_ANY_RTL},
13764 * {@link #TEXT_DIRECTION_LTR},
13765 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080013766 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013767 *
13768 * @hide
13769 */
13770 public int getTextDirection() {
13771 return mTextDirection;
13772 }
13773
13774 /**
13775 * Set the text direction.
13776 *
13777 * @param textDirection the direction to set. Should be one of:
13778 *
13779 * {@link #TEXT_DIRECTION_INHERIT},
13780 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13781 * {@link #TEXT_DIRECTION_ANY_RTL},
13782 * {@link #TEXT_DIRECTION_LTR},
13783 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080013784 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013785 *
13786 * @hide
13787 */
13788 public void setTextDirection(int textDirection) {
13789 if (textDirection != mTextDirection) {
13790 mTextDirection = textDirection;
13791 resetResolvedTextDirection();
13792 requestLayout();
13793 }
13794 }
13795
13796 /**
13797 * Return the resolved text direction.
13798 *
13799 * @return the resolved text direction. Return one of:
13800 *
Doug Feltcb3791202011-07-07 11:57:48 -070013801 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13802 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013803 * {@link #TEXT_DIRECTION_LTR},
13804 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080013805 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013806 *
13807 * @hide
13808 */
13809 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013810 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013811 resolveTextDirection();
13812 }
13813 return mResolvedTextDirection;
13814 }
13815
13816 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013817 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013818 *
13819 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013820 */
13821 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013822 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13823 mResolvedTextDirection = mTextDirection;
13824 return;
13825 }
13826 if (mParent != null && mParent instanceof ViewGroup) {
13827 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13828 return;
13829 }
13830 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013831 }
13832
13833 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013834 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013835 *
13836 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013837 */
13838 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013839 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013840 }
13841
Chet Haaseb39f0512011-05-24 14:36:40 -070013842 //
13843 // Properties
13844 //
13845 /**
13846 * A Property wrapper around the <code>alpha</code> functionality handled by the
13847 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13848 */
Chet Haased47f1532011-12-16 11:18:52 -080013849 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013850 @Override
13851 public void setValue(View object, float value) {
13852 object.setAlpha(value);
13853 }
13854
13855 @Override
13856 public Float get(View object) {
13857 return object.getAlpha();
13858 }
13859 };
13860
13861 /**
13862 * A Property wrapper around the <code>translationX</code> functionality handled by the
13863 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13864 */
Chet Haased47f1532011-12-16 11:18:52 -080013865 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013866 @Override
13867 public void setValue(View object, float value) {
13868 object.setTranslationX(value);
13869 }
13870
13871 @Override
13872 public Float get(View object) {
13873 return object.getTranslationX();
13874 }
13875 };
13876
13877 /**
13878 * A Property wrapper around the <code>translationY</code> functionality handled by the
13879 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13880 */
Chet Haased47f1532011-12-16 11:18:52 -080013881 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013882 @Override
13883 public void setValue(View object, float value) {
13884 object.setTranslationY(value);
13885 }
13886
13887 @Override
13888 public Float get(View object) {
13889 return object.getTranslationY();
13890 }
13891 };
13892
13893 /**
13894 * A Property wrapper around the <code>x</code> functionality handled by the
13895 * {@link View#setX(float)} and {@link View#getX()} methods.
13896 */
Chet Haased47f1532011-12-16 11:18:52 -080013897 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013898 @Override
13899 public void setValue(View object, float value) {
13900 object.setX(value);
13901 }
13902
13903 @Override
13904 public Float get(View object) {
13905 return object.getX();
13906 }
13907 };
13908
13909 /**
13910 * A Property wrapper around the <code>y</code> functionality handled by the
13911 * {@link View#setY(float)} and {@link View#getY()} methods.
13912 */
Chet Haased47f1532011-12-16 11:18:52 -080013913 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013914 @Override
13915 public void setValue(View object, float value) {
13916 object.setY(value);
13917 }
13918
13919 @Override
13920 public Float get(View object) {
13921 return object.getY();
13922 }
13923 };
13924
13925 /**
13926 * A Property wrapper around the <code>rotation</code> functionality handled by the
13927 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13928 */
Chet Haased47f1532011-12-16 11:18:52 -080013929 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013930 @Override
13931 public void setValue(View object, float value) {
13932 object.setRotation(value);
13933 }
13934
13935 @Override
13936 public Float get(View object) {
13937 return object.getRotation();
13938 }
13939 };
13940
13941 /**
13942 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13943 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13944 */
Chet Haased47f1532011-12-16 11:18:52 -080013945 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013946 @Override
13947 public void setValue(View object, float value) {
13948 object.setRotationX(value);
13949 }
13950
13951 @Override
13952 public Float get(View object) {
13953 return object.getRotationX();
13954 }
13955 };
13956
13957 /**
13958 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13959 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13960 */
Chet Haased47f1532011-12-16 11:18:52 -080013961 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013962 @Override
13963 public void setValue(View object, float value) {
13964 object.setRotationY(value);
13965 }
13966
13967 @Override
13968 public Float get(View object) {
13969 return object.getRotationY();
13970 }
13971 };
13972
13973 /**
13974 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13975 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13976 */
Chet Haased47f1532011-12-16 11:18:52 -080013977 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013978 @Override
13979 public void setValue(View object, float value) {
13980 object.setScaleX(value);
13981 }
13982
13983 @Override
13984 public Float get(View object) {
13985 return object.getScaleX();
13986 }
13987 };
13988
13989 /**
13990 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13991 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13992 */
Chet Haased47f1532011-12-16 11:18:52 -080013993 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013994 @Override
13995 public void setValue(View object, float value) {
13996 object.setScaleY(value);
13997 }
13998
13999 @Override
14000 public Float get(View object) {
14001 return object.getScaleY();
14002 }
14003 };
14004
Jeff Brown33bbfd22011-02-24 20:55:35 -080014005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014006 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14007 * Each MeasureSpec represents a requirement for either the width or the height.
14008 * A MeasureSpec is comprised of a size and a mode. There are three possible
14009 * modes:
14010 * <dl>
14011 * <dt>UNSPECIFIED</dt>
14012 * <dd>
14013 * The parent has not imposed any constraint on the child. It can be whatever size
14014 * it wants.
14015 * </dd>
14016 *
14017 * <dt>EXACTLY</dt>
14018 * <dd>
14019 * The parent has determined an exact size for the child. The child is going to be
14020 * given those bounds regardless of how big it wants to be.
14021 * </dd>
14022 *
14023 * <dt>AT_MOST</dt>
14024 * <dd>
14025 * The child can be as large as it wants up to the specified size.
14026 * </dd>
14027 * </dl>
14028 *
14029 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14030 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14031 */
14032 public static class MeasureSpec {
14033 private static final int MODE_SHIFT = 30;
14034 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14035
14036 /**
14037 * Measure specification mode: The parent has not imposed any constraint
14038 * on the child. It can be whatever size it wants.
14039 */
14040 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14041
14042 /**
14043 * Measure specification mode: The parent has determined an exact size
14044 * for the child. The child is going to be given those bounds regardless
14045 * of how big it wants to be.
14046 */
14047 public static final int EXACTLY = 1 << MODE_SHIFT;
14048
14049 /**
14050 * Measure specification mode: The child can be as large as it wants up
14051 * to the specified size.
14052 */
14053 public static final int AT_MOST = 2 << MODE_SHIFT;
14054
14055 /**
14056 * Creates a measure specification based on the supplied size and mode.
14057 *
14058 * The mode must always be one of the following:
14059 * <ul>
14060 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14061 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14062 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14063 * </ul>
14064 *
14065 * @param size the size of the measure specification
14066 * @param mode the mode of the measure specification
14067 * @return the measure specification based on size and mode
14068 */
14069 public static int makeMeasureSpec(int size, int mode) {
14070 return size + mode;
14071 }
14072
14073 /**
14074 * Extracts the mode from the supplied measure specification.
14075 *
14076 * @param measureSpec the measure specification to extract the mode from
14077 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14078 * {@link android.view.View.MeasureSpec#AT_MOST} or
14079 * {@link android.view.View.MeasureSpec#EXACTLY}
14080 */
14081 public static int getMode(int measureSpec) {
14082 return (measureSpec & MODE_MASK);
14083 }
14084
14085 /**
14086 * Extracts the size from the supplied measure specification.
14087 *
14088 * @param measureSpec the measure specification to extract the size from
14089 * @return the size in pixels defined in the supplied measure specification
14090 */
14091 public static int getSize(int measureSpec) {
14092 return (measureSpec & ~MODE_MASK);
14093 }
14094
14095 /**
14096 * Returns a String representation of the specified measure
14097 * specification.
14098 *
14099 * @param measureSpec the measure specification to convert to a String
14100 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14101 */
14102 public static String toString(int measureSpec) {
14103 int mode = getMode(measureSpec);
14104 int size = getSize(measureSpec);
14105
14106 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14107
14108 if (mode == UNSPECIFIED)
14109 sb.append("UNSPECIFIED ");
14110 else if (mode == EXACTLY)
14111 sb.append("EXACTLY ");
14112 else if (mode == AT_MOST)
14113 sb.append("AT_MOST ");
14114 else
14115 sb.append(mode).append(" ");
14116
14117 sb.append(size);
14118 return sb.toString();
14119 }
14120 }
14121
14122 class CheckForLongPress implements Runnable {
14123
14124 private int mOriginalWindowAttachCount;
14125
14126 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014127 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014128 && mOriginalWindowAttachCount == mWindowAttachCount) {
14129 if (performLongClick()) {
14130 mHasPerformedLongPress = true;
14131 }
14132 }
14133 }
14134
14135 public void rememberWindowAttachCount() {
14136 mOriginalWindowAttachCount = mWindowAttachCount;
14137 }
14138 }
Joe Malin32736f02011-01-19 16:14:20 -080014139
Adam Powelle14579b2009-12-16 18:39:52 -080014140 private final class CheckForTap implements Runnable {
14141 public void run() {
14142 mPrivateFlags &= ~PREPRESSED;
14143 mPrivateFlags |= PRESSED;
14144 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014145 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014146 }
14147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014148
Adam Powella35d7682010-03-12 14:48:13 -080014149 private final class PerformClick implements Runnable {
14150 public void run() {
14151 performClick();
14152 }
14153 }
14154
Dianne Hackborn63042d62011-01-26 18:56:29 -080014155 /** @hide */
14156 public void hackTurnOffWindowResizeAnim(boolean off) {
14157 mAttachInfo.mTurnOffWindowResizeAnim = off;
14158 }
Joe Malin32736f02011-01-19 16:14:20 -080014159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014160 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014161 * This method returns a ViewPropertyAnimator object, which can be used to animate
14162 * specific properties on this View.
14163 *
14164 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14165 */
14166 public ViewPropertyAnimator animate() {
14167 if (mAnimator == null) {
14168 mAnimator = new ViewPropertyAnimator(this);
14169 }
14170 return mAnimator;
14171 }
14172
14173 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014174 * Interface definition for a callback to be invoked when a key event is
14175 * dispatched to this view. The callback will be invoked before the key
14176 * event is given to the view.
14177 */
14178 public interface OnKeyListener {
14179 /**
14180 * Called when a key is dispatched to a view. This allows listeners to
14181 * get a chance to respond before the target view.
14182 *
14183 * @param v The view the key has been dispatched to.
14184 * @param keyCode The code for the physical key that was pressed
14185 * @param event The KeyEvent object containing full information about
14186 * the event.
14187 * @return True if the listener has consumed the event, false otherwise.
14188 */
14189 boolean onKey(View v, int keyCode, KeyEvent event);
14190 }
14191
14192 /**
14193 * Interface definition for a callback to be invoked when a touch event is
14194 * dispatched to this view. The callback will be invoked before the touch
14195 * event is given to the view.
14196 */
14197 public interface OnTouchListener {
14198 /**
14199 * Called when a touch event is dispatched to a view. This allows listeners to
14200 * get a chance to respond before the target view.
14201 *
14202 * @param v The view the touch event has been dispatched to.
14203 * @param event The MotionEvent object containing full information about
14204 * the event.
14205 * @return True if the listener has consumed the event, false otherwise.
14206 */
14207 boolean onTouch(View v, MotionEvent event);
14208 }
14209
14210 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014211 * Interface definition for a callback to be invoked when a hover event is
14212 * dispatched to this view. The callback will be invoked before the hover
14213 * event is given to the view.
14214 */
14215 public interface OnHoverListener {
14216 /**
14217 * Called when a hover event is dispatched to a view. This allows listeners to
14218 * get a chance to respond before the target view.
14219 *
14220 * @param v The view the hover event has been dispatched to.
14221 * @param event The MotionEvent object containing full information about
14222 * the event.
14223 * @return True if the listener has consumed the event, false otherwise.
14224 */
14225 boolean onHover(View v, MotionEvent event);
14226 }
14227
14228 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014229 * Interface definition for a callback to be invoked when a generic motion event is
14230 * dispatched to this view. The callback will be invoked before the generic motion
14231 * event is given to the view.
14232 */
14233 public interface OnGenericMotionListener {
14234 /**
14235 * Called when a generic motion event is dispatched to a view. This allows listeners to
14236 * get a chance to respond before the target view.
14237 *
14238 * @param v The view the generic motion event has been dispatched to.
14239 * @param event The MotionEvent object containing full information about
14240 * the event.
14241 * @return True if the listener has consumed the event, false otherwise.
14242 */
14243 boolean onGenericMotion(View v, MotionEvent event);
14244 }
14245
14246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014247 * Interface definition for a callback to be invoked when a view has been clicked and held.
14248 */
14249 public interface OnLongClickListener {
14250 /**
14251 * Called when a view has been clicked and held.
14252 *
14253 * @param v The view that was clicked and held.
14254 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014255 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 */
14257 boolean onLongClick(View v);
14258 }
14259
14260 /**
Chris Tate32affef2010-10-18 15:29:21 -070014261 * Interface definition for a callback to be invoked when a drag is being dispatched
14262 * to this view. The callback will be invoked before the hosting view's own
14263 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14264 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014265 *
14266 * <div class="special reference">
14267 * <h3>Developer Guides</h3>
14268 * <p>For a guide to implementing drag and drop features, read the
14269 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14270 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014271 */
14272 public interface OnDragListener {
14273 /**
14274 * Called when a drag event is dispatched to a view. This allows listeners
14275 * to get a chance to override base View behavior.
14276 *
Joe Malin32736f02011-01-19 16:14:20 -080014277 * @param v The View that received the drag event.
14278 * @param event The {@link android.view.DragEvent} object for the drag event.
14279 * @return {@code true} if the drag event was handled successfully, or {@code false}
14280 * if the drag event was not handled. Note that {@code false} will trigger the View
14281 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014282 */
14283 boolean onDrag(View v, DragEvent event);
14284 }
14285
14286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014287 * Interface definition for a callback to be invoked when the focus state of
14288 * a view changed.
14289 */
14290 public interface OnFocusChangeListener {
14291 /**
14292 * Called when the focus state of a view has changed.
14293 *
14294 * @param v The view whose state has changed.
14295 * @param hasFocus The new focus state of v.
14296 */
14297 void onFocusChange(View v, boolean hasFocus);
14298 }
14299
14300 /**
14301 * Interface definition for a callback to be invoked when a view is clicked.
14302 */
14303 public interface OnClickListener {
14304 /**
14305 * Called when a view has been clicked.
14306 *
14307 * @param v The view that was clicked.
14308 */
14309 void onClick(View v);
14310 }
14311
14312 /**
14313 * Interface definition for a callback to be invoked when the context menu
14314 * for this view is being built.
14315 */
14316 public interface OnCreateContextMenuListener {
14317 /**
14318 * Called when the context menu for this view is being built. It is not
14319 * safe to hold onto the menu after this method returns.
14320 *
14321 * @param menu The context menu that is being built
14322 * @param v The view for which the context menu is being built
14323 * @param menuInfo Extra information about the item for which the
14324 * context menu should be shown. This information will vary
14325 * depending on the class of v.
14326 */
14327 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14328 }
14329
Joe Onorato664644d2011-01-23 17:53:23 -080014330 /**
14331 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014332 * visibility. This reports <strong>global</strong> changes to the system UI
14333 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014334 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014335 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014336 */
14337 public interface OnSystemUiVisibilityChangeListener {
14338 /**
14339 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014340 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014341 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014342 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014343 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14344 * <strong>global</strong> state of the UI visibility flags, not what your
14345 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014346 */
14347 public void onSystemUiVisibilityChange(int visibility);
14348 }
14349
Adam Powell4afd62b2011-02-18 15:02:18 -080014350 /**
14351 * Interface definition for a callback to be invoked when this view is attached
14352 * or detached from its window.
14353 */
14354 public interface OnAttachStateChangeListener {
14355 /**
14356 * Called when the view is attached to a window.
14357 * @param v The view that was attached
14358 */
14359 public void onViewAttachedToWindow(View v);
14360 /**
14361 * Called when the view is detached from a window.
14362 * @param v The view that was detached
14363 */
14364 public void onViewDetachedFromWindow(View v);
14365 }
14366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014367 private final class UnsetPressedState implements Runnable {
14368 public void run() {
14369 setPressed(false);
14370 }
14371 }
14372
14373 /**
14374 * Base class for derived classes that want to save and restore their own
14375 * state in {@link android.view.View#onSaveInstanceState()}.
14376 */
14377 public static class BaseSavedState extends AbsSavedState {
14378 /**
14379 * Constructor used when reading from a parcel. Reads the state of the superclass.
14380 *
14381 * @param source
14382 */
14383 public BaseSavedState(Parcel source) {
14384 super(source);
14385 }
14386
14387 /**
14388 * Constructor called by derived classes when creating their SavedState objects
14389 *
14390 * @param superState The state of the superclass of this view
14391 */
14392 public BaseSavedState(Parcelable superState) {
14393 super(superState);
14394 }
14395
14396 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14397 new Parcelable.Creator<BaseSavedState>() {
14398 public BaseSavedState createFromParcel(Parcel in) {
14399 return new BaseSavedState(in);
14400 }
14401
14402 public BaseSavedState[] newArray(int size) {
14403 return new BaseSavedState[size];
14404 }
14405 };
14406 }
14407
14408 /**
14409 * A set of information given to a view when it is attached to its parent
14410 * window.
14411 */
14412 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014413 interface Callbacks {
14414 void playSoundEffect(int effectId);
14415 boolean performHapticFeedback(int effectId, boolean always);
14416 }
14417
14418 /**
14419 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14420 * to a Handler. This class contains the target (View) to invalidate and
14421 * the coordinates of the dirty rectangle.
14422 *
14423 * For performance purposes, this class also implements a pool of up to
14424 * POOL_LIMIT objects that get reused. This reduces memory allocations
14425 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014426 */
Romain Guyd928d682009-03-31 17:52:16 -070014427 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014428 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014429 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14430 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014431 public InvalidateInfo newInstance() {
14432 return new InvalidateInfo();
14433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014434
Romain Guyd928d682009-03-31 17:52:16 -070014435 public void onAcquired(InvalidateInfo element) {
14436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014437
Romain Guyd928d682009-03-31 17:52:16 -070014438 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014439 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014440 }
14441 }, POOL_LIMIT)
14442 );
14443
14444 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014445 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014446
14447 View target;
14448
14449 int left;
14450 int top;
14451 int right;
14452 int bottom;
14453
Romain Guyd928d682009-03-31 17:52:16 -070014454 public void setNextPoolable(InvalidateInfo element) {
14455 mNext = element;
14456 }
14457
14458 public InvalidateInfo getNextPoolable() {
14459 return mNext;
14460 }
14461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014462 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014463 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014464 }
14465
14466 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014467 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014468 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014469
14470 public boolean isPooled() {
14471 return mIsPooled;
14472 }
14473
14474 public void setPooled(boolean isPooled) {
14475 mIsPooled = isPooled;
14476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014477 }
14478
14479 final IWindowSession mSession;
14480
14481 final IWindow mWindow;
14482
14483 final IBinder mWindowToken;
14484
14485 final Callbacks mRootCallbacks;
14486
Romain Guy59a12ca2011-06-09 17:48:21 -070014487 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014489 /**
14490 * The top view of the hierarchy.
14491 */
14492 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014494 IBinder mPanelParentWindowToken;
14495 Surface mSurface;
14496
Romain Guyb051e892010-09-28 19:09:36 -070014497 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014498 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014499 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014501 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014502 * Scale factor used by the compatibility mode
14503 */
14504 float mApplicationScale;
14505
14506 /**
14507 * Indicates whether the application is in compatibility mode
14508 */
14509 boolean mScalingRequired;
14510
14511 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014512 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014513 */
14514 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014515
Dianne Hackborn63042d62011-01-26 18:56:29 -080014516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014517 * Left position of this view's window
14518 */
14519 int mWindowLeft;
14520
14521 /**
14522 * Top position of this view's window
14523 */
14524 int mWindowTop;
14525
14526 /**
Adam Powell26153a32010-11-08 15:22:27 -080014527 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014528 */
Adam Powell26153a32010-11-08 15:22:27 -080014529 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014530
14531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014532 * For windows that are full-screen but using insets to layout inside
14533 * of the screen decorations, these are the current insets for the
14534 * content of the window.
14535 */
14536 final Rect mContentInsets = new Rect();
14537
14538 /**
14539 * For windows that are full-screen but using insets to layout inside
14540 * of the screen decorations, these are the current insets for the
14541 * actual visible parts of the window.
14542 */
14543 final Rect mVisibleInsets = new Rect();
14544
14545 /**
14546 * The internal insets given by this window. This value is
14547 * supplied by the client (through
14548 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14549 * be given to the window manager when changed to be used in laying
14550 * out windows behind it.
14551 */
14552 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14553 = new ViewTreeObserver.InternalInsetsInfo();
14554
14555 /**
14556 * All views in the window's hierarchy that serve as scroll containers,
14557 * used to determine if the window can be resized or must be panned
14558 * to adjust for a soft input area.
14559 */
14560 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14561
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014562 final KeyEvent.DispatcherState mKeyDispatchState
14563 = new KeyEvent.DispatcherState();
14564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014565 /**
14566 * Indicates whether the view's window currently has the focus.
14567 */
14568 boolean mHasWindowFocus;
14569
14570 /**
14571 * The current visibility of the window.
14572 */
14573 int mWindowVisibility;
14574
14575 /**
14576 * Indicates the time at which drawing started to occur.
14577 */
14578 long mDrawingTime;
14579
14580 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014581 * Indicates whether or not ignoring the DIRTY_MASK flags.
14582 */
14583 boolean mIgnoreDirtyState;
14584
14585 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014586 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14587 * to avoid clearing that flag prematurely.
14588 */
14589 boolean mSetIgnoreDirtyState = false;
14590
14591 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014592 * Indicates whether the view's window is currently in touch mode.
14593 */
14594 boolean mInTouchMode;
14595
14596 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014597 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014598 * the next time it performs a traversal
14599 */
14600 boolean mRecomputeGlobalAttributes;
14601
14602 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014603 * Always report new attributes at next traversal.
14604 */
14605 boolean mForceReportNewAttributes;
14606
14607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014608 * Set during a traveral if any views want to keep the screen on.
14609 */
14610 boolean mKeepScreenOn;
14611
14612 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014613 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14614 */
14615 int mSystemUiVisibility;
14616
14617 /**
14618 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14619 * attached.
14620 */
14621 boolean mHasSystemUiListeners;
14622
14623 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014624 * Set if the visibility of any views has changed.
14625 */
14626 boolean mViewVisibilityChanged;
14627
14628 /**
14629 * Set to true if a view has been scrolled.
14630 */
14631 boolean mViewScrollChanged;
14632
14633 /**
14634 * Global to the view hierarchy used as a temporary for dealing with
14635 * x/y points in the transparent region computations.
14636 */
14637 final int[] mTransparentLocation = new int[2];
14638
14639 /**
14640 * Global to the view hierarchy used as a temporary for dealing with
14641 * x/y points in the ViewGroup.invalidateChild implementation.
14642 */
14643 final int[] mInvalidateChildLocation = new int[2];
14644
Chet Haasec3aa3612010-06-17 08:50:37 -070014645
14646 /**
14647 * Global to the view hierarchy used as a temporary for dealing with
14648 * x/y location when view is transformed.
14649 */
14650 final float[] mTmpTransformLocation = new float[2];
14651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014652 /**
14653 * The view tree observer used to dispatch global events like
14654 * layout, pre-draw, touch mode change, etc.
14655 */
14656 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14657
14658 /**
14659 * A Canvas used by the view hierarchy to perform bitmap caching.
14660 */
14661 Canvas mCanvas;
14662
14663 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014664 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014665 * handler can be used to pump events in the UI events queue.
14666 */
14667 final Handler mHandler;
14668
14669 /**
14670 * Identifier for messages requesting the view to be invalidated.
14671 * Such messages should be sent to {@link #mHandler}.
14672 */
14673 static final int INVALIDATE_MSG = 0x1;
14674
14675 /**
14676 * Identifier for messages requesting the view to invalidate a region.
14677 * Such messages should be sent to {@link #mHandler}.
14678 */
14679 static final int INVALIDATE_RECT_MSG = 0x2;
14680
14681 /**
14682 * Temporary for use in computing invalidate rectangles while
14683 * calling up the hierarchy.
14684 */
14685 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014686
14687 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014688 * Temporary for use in computing hit areas with transformed views
14689 */
14690 final RectF mTmpTransformRect = new RectF();
14691
14692 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014693 * Temporary list for use in collecting focusable descendents of a view.
14694 */
14695 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014697 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014698 * The id of the window for accessibility purposes.
14699 */
14700 int mAccessibilityWindowId = View.NO_ID;
14701
14702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014703 * Creates a new set of attachment information with the specified
14704 * events handler and thread.
14705 *
14706 * @param handler the events handler the view must use
14707 */
14708 AttachInfo(IWindowSession session, IWindow window,
14709 Handler handler, Callbacks effectPlayer) {
14710 mSession = session;
14711 mWindow = window;
14712 mWindowToken = window.asBinder();
14713 mHandler = handler;
14714 mRootCallbacks = effectPlayer;
14715 }
14716 }
14717
14718 /**
14719 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14720 * is supported. This avoids keeping too many unused fields in most
14721 * instances of View.</p>
14722 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014723 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014724
Mike Cleronf116bf82009-09-27 19:14:12 -070014725 /**
14726 * Scrollbars are not visible
14727 */
14728 public static final int OFF = 0;
14729
14730 /**
14731 * Scrollbars are visible
14732 */
14733 public static final int ON = 1;
14734
14735 /**
14736 * Scrollbars are fading away
14737 */
14738 public static final int FADING = 2;
14739
14740 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014742 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014743 public int scrollBarDefaultDelayBeforeFade;
14744 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014745
14746 public int scrollBarSize;
14747 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014748 public float[] interpolatorValues;
14749 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014750
14751 public final Paint paint;
14752 public final Matrix matrix;
14753 public Shader shader;
14754
Mike Cleronf116bf82009-09-27 19:14:12 -070014755 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14756
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014757 private static final float[] OPAQUE = { 255 };
14758 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014759
Mike Cleronf116bf82009-09-27 19:14:12 -070014760 /**
14761 * When fading should start. This time moves into the future every time
14762 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14763 */
14764 public long fadeStartTime;
14765
14766
14767 /**
14768 * The current state of the scrollbars: ON, OFF, or FADING
14769 */
14770 public int state = OFF;
14771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014772 private int mLastColor;
14773
Mike Cleronf116bf82009-09-27 19:14:12 -070014774 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014775 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14776 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014777 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14778 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014779
14780 paint = new Paint();
14781 matrix = new Matrix();
14782 // use use a height of 1, and then wack the matrix each time we
14783 // actually use it.
14784 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014786 paint.setShader(shader);
14787 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014788 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014789 }
Romain Guy8506ab42009-06-11 17:35:47 -070014790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014791 public void setFadeColor(int color) {
14792 if (color != 0 && color != mLastColor) {
14793 mLastColor = color;
14794 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014795
Romain Guye55e1a72009-08-27 10:42:26 -070014796 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14797 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014799 paint.setShader(shader);
14800 // Restore the default transfer mode (src_over)
14801 paint.setXfermode(null);
14802 }
14803 }
Joe Malin32736f02011-01-19 16:14:20 -080014804
Mike Cleronf116bf82009-09-27 19:14:12 -070014805 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014806 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014807 if (now >= fadeStartTime) {
14808
14809 // the animation fades the scrollbars out by changing
14810 // the opacity (alpha) from fully opaque to fully
14811 // transparent
14812 int nextFrame = (int) now;
14813 int framesCount = 0;
14814
14815 Interpolator interpolator = scrollBarInterpolator;
14816
14817 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014818 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014819
14820 // End transparent
14821 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014822 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014823
14824 state = FADING;
14825
14826 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014827 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014828 }
14829 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014830 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014831
Svetoslav Ganova0156172011-06-26 17:55:44 -070014832 /**
14833 * Resuable callback for sending
14834 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14835 */
14836 private class SendViewScrolledAccessibilityEvent implements Runnable {
14837 public volatile boolean mIsPending;
14838
14839 public void run() {
14840 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14841 mIsPending = false;
14842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014843 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014844
14845 /**
14846 * <p>
14847 * This class represents a delegate that can be registered in a {@link View}
14848 * to enhance accessibility support via composition rather via inheritance.
14849 * It is specifically targeted to widget developers that extend basic View
14850 * classes i.e. classes in package android.view, that would like their
14851 * applications to be backwards compatible.
14852 * </p>
14853 * <p>
14854 * A scenario in which a developer would like to use an accessibility delegate
14855 * is overriding a method introduced in a later API version then the minimal API
14856 * version supported by the application. For example, the method
14857 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14858 * in API version 4 when the accessibility APIs were first introduced. If a
14859 * developer would like his application to run on API version 4 devices (assuming
14860 * all other APIs used by the application are version 4 or lower) and take advantage
14861 * of this method, instead of overriding the method which would break the application's
14862 * backwards compatibility, he can override the corresponding method in this
14863 * delegate and register the delegate in the target View if the API version of
14864 * the system is high enough i.e. the API version is same or higher to the API
14865 * version that introduced
14866 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14867 * </p>
14868 * <p>
14869 * Here is an example implementation:
14870 * </p>
14871 * <code><pre><p>
14872 * if (Build.VERSION.SDK_INT >= 14) {
14873 * // If the API version is equal of higher than the version in
14874 * // which onInitializeAccessibilityNodeInfo was introduced we
14875 * // register a delegate with a customized implementation.
14876 * View view = findViewById(R.id.view_id);
14877 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14878 * public void onInitializeAccessibilityNodeInfo(View host,
14879 * AccessibilityNodeInfo info) {
14880 * // Let the default implementation populate the info.
14881 * super.onInitializeAccessibilityNodeInfo(host, info);
14882 * // Set some other information.
14883 * info.setEnabled(host.isEnabled());
14884 * }
14885 * });
14886 * }
14887 * </code></pre></p>
14888 * <p>
14889 * This delegate contains methods that correspond to the accessibility methods
14890 * in View. If a delegate has been specified the implementation in View hands
14891 * off handling to the corresponding method in this delegate. The default
14892 * implementation the delegate methods behaves exactly as the corresponding
14893 * method in View for the case of no accessibility delegate been set. Hence,
14894 * to customize the behavior of a View method, clients can override only the
14895 * corresponding delegate method without altering the behavior of the rest
14896 * accessibility related methods of the host view.
14897 * </p>
14898 */
14899 public static class AccessibilityDelegate {
14900
14901 /**
14902 * Sends an accessibility event of the given type. If accessibility is not
14903 * enabled this method has no effect.
14904 * <p>
14905 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14906 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14907 * been set.
14908 * </p>
14909 *
14910 * @param host The View hosting the delegate.
14911 * @param eventType The type of the event to send.
14912 *
14913 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14914 */
14915 public void sendAccessibilityEvent(View host, int eventType) {
14916 host.sendAccessibilityEventInternal(eventType);
14917 }
14918
14919 /**
14920 * Sends an accessibility event. This method behaves exactly as
14921 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14922 * empty {@link AccessibilityEvent} and does not perform a check whether
14923 * accessibility is enabled.
14924 * <p>
14925 * The default implementation behaves as
14926 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14927 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14928 * the case of no accessibility delegate been set.
14929 * </p>
14930 *
14931 * @param host The View hosting the delegate.
14932 * @param event The event to send.
14933 *
14934 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14935 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14936 */
14937 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14938 host.sendAccessibilityEventUncheckedInternal(event);
14939 }
14940
14941 /**
14942 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14943 * to its children for adding their text content to the event.
14944 * <p>
14945 * The default implementation behaves as
14946 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14947 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14948 * the case of no accessibility delegate been set.
14949 * </p>
14950 *
14951 * @param host The View hosting the delegate.
14952 * @param event The event.
14953 * @return True if the event population was completed.
14954 *
14955 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14956 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14957 */
14958 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14959 return host.dispatchPopulateAccessibilityEventInternal(event);
14960 }
14961
14962 /**
14963 * Gives a chance to the host View to populate the accessibility event with its
14964 * text content.
14965 * <p>
14966 * The default implementation behaves as
14967 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14968 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14969 * the case of no accessibility delegate been set.
14970 * </p>
14971 *
14972 * @param host The View hosting the delegate.
14973 * @param event The accessibility event which to populate.
14974 *
14975 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14976 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14977 */
14978 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14979 host.onPopulateAccessibilityEventInternal(event);
14980 }
14981
14982 /**
14983 * Initializes an {@link AccessibilityEvent} with information about the
14984 * the host View which is the event source.
14985 * <p>
14986 * The default implementation behaves as
14987 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14988 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14989 * the case of no accessibility delegate been set.
14990 * </p>
14991 *
14992 * @param host The View hosting the delegate.
14993 * @param event The event to initialize.
14994 *
14995 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14996 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14997 */
14998 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14999 host.onInitializeAccessibilityEventInternal(event);
15000 }
15001
15002 /**
15003 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15004 * <p>
15005 * The default implementation behaves as
15006 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15007 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15008 * the case of no accessibility delegate been set.
15009 * </p>
15010 *
15011 * @param host The View hosting the delegate.
15012 * @param info The instance to initialize.
15013 *
15014 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15015 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15016 */
15017 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15018 host.onInitializeAccessibilityNodeInfoInternal(info);
15019 }
15020
15021 /**
15022 * Called when a child of the host View has requested sending an
15023 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15024 * to augment the event.
15025 * <p>
15026 * The default implementation behaves as
15027 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15028 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15029 * the case of no accessibility delegate been set.
15030 * </p>
15031 *
15032 * @param host The View hosting the delegate.
15033 * @param child The child which requests sending the event.
15034 * @param event The event to be sent.
15035 * @return True if the event should be sent
15036 *
15037 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15038 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15039 */
15040 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15041 AccessibilityEvent event) {
15042 return host.onRequestSendAccessibilityEventInternal(child, event);
15043 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015044
15045 /**
15046 * Gets the provider for managing a virtual view hierarchy rooted at this View
15047 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15048 * that explore the window content.
15049 * <p>
15050 * The default implementation behaves as
15051 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15052 * the case of no accessibility delegate been set.
15053 * </p>
15054 *
15055 * @return The provider.
15056 *
15057 * @see AccessibilityNodeProvider
15058 */
15059 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15060 return null;
15061 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015063}