blob: 2ab27fc62b201e96c2dcc09865a72b4fb13f82c0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070027import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.LinearGradient;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070036import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.Region;
38import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.drawable.ColorDrawable;
40import android.graphics.drawable.Drawable;
41import android.os.Handler;
42import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Parcel;
44import android.os.Parcelable;
45import android.os.RemoteException;
46import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070047import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070049import android.util.FloatProperty;
50import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityEventSource;
62import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070063import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070064import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080067import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.inputmethod.InputConnection;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.widget.ScrollBarDrawable;
72
Romain Guy1ef3fdb2011-09-09 15:30:30 -070073import static android.os.Build.VERSION_CODES.*;
74
Doug Feltcb3791202011-07-07 11:57:48 -070075import com.android.internal.R;
76import com.android.internal.util.Predicate;
77import com.android.internal.view.menu.MenuBuilder;
78
Christopher Tatea0374192010-10-05 13:06:41 -070079import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070080import java.lang.reflect.InvocationTargetException;
81import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
83import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070084import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080085import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * <p>
89 * This class represents the basic building block for user interface components. A View
90 * occupies a rectangular area on the screen and is responsible for drawing and
91 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070092 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
94 * are invisible containers that hold other Views (or other ViewGroups) and define
95 * their layout properties.
96 * </p>
97 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070098 * <div class="special reference">
99 * <h3>Developer Guides</h3>
100 * <p>For information about using this class to develop your application's user interface,
101 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * <a name="Using"></a>
105 * <h3>Using Views</h3>
106 * <p>
107 * All of the views in a window are arranged in a single tree. You can add views
108 * either from code or by specifying a tree of views in one or more XML layout
109 * files. There are many specialized subclasses of views that act as controls or
110 * are capable of displaying text, images, or other content.
111 * </p>
112 * <p>
113 * Once you have created a tree of views, there are typically a few types of
114 * common operations you may wish to perform:
115 * <ul>
116 * <li><strong>Set properties:</strong> for example setting the text of a
117 * {@link android.widget.TextView}. The available properties and the methods
118 * that set them will vary among the different subclasses of views. Note that
119 * properties that are known at build time can be set in the XML layout
120 * files.</li>
121 * <li><strong>Set focus:</strong> The framework will handled moving focus in
122 * response to user input. To force focus to a specific view, call
123 * {@link #requestFocus}.</li>
124 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
125 * that will be notified when something interesting happens to the view. For
126 * example, all views will let you set a listener to be notified when the view
127 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700128 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
129 * Other view subclasses offer more specialized listeners. For example, a Button
130 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * </ul>
134 * </p>
135 * <p><em>
136 * Note: The Android framework is responsible for measuring, laying out and
137 * drawing views. You should not call methods that perform these actions on
138 * views yourself unless you are actually implementing a
139 * {@link android.view.ViewGroup}.
140 * </em></p>
141 *
142 * <a name="Lifecycle"></a>
143 * <h3>Implementing a Custom View</h3>
144 *
145 * <p>
146 * To implement a custom view, you will usually begin by providing overrides for
147 * some of the standard methods that the framework calls on all views. You do
148 * not need to override all of these methods. In fact, you can start by just
149 * overriding {@link #onDraw(android.graphics.Canvas)}.
150 * <table border="2" width="85%" align="center" cellpadding="5">
151 * <thead>
152 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
153 * </thead>
154 *
155 * <tbody>
156 * <tr>
157 * <td rowspan="2">Creation</td>
158 * <td>Constructors</td>
159 * <td>There is a form of the constructor that are called when the view
160 * is created from code and a form that is called when the view is
161 * inflated from a layout file. The second form should parse and apply
162 * any attributes defined in the layout file.
163 * </td>
164 * </tr>
165 * <tr>
166 * <td><code>{@link #onFinishInflate()}</code></td>
167 * <td>Called after a view and all of its children has been inflated
168 * from XML.</td>
169 * </tr>
170 *
171 * <tr>
172 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700173 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * <td>Called to determine the size requirements for this view and all
175 * of its children.
176 * </td>
177 * </tr>
178 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700179 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <td>Called when this view should assign a size and position to all
181 * of its children.
182 * </td>
183 * </tr>
184 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700185 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * <td>Called when the size of this view has changed.
187 * </td>
188 * </tr>
189 *
190 * <tr>
191 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the view should render its content.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when a new key event occurs.
201 * </td>
202 * </tr>
203 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700204 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * <td>Called when a key up event occurs.
206 * </td>
207 * </tr>
208 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700209 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <td>Called when a trackball motion event occurs.
211 * </td>
212 * </tr>
213 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * <td>Called when a touch screen motion event occurs.
216 * </td>
217 * </tr>
218 *
219 * <tr>
220 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when the view gains or loses focus.
223 * </td>
224 * </tr>
225 *
226 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700227 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <td>Called when the window containing the view gains or loses focus.
229 * </td>
230 * </tr>
231 *
232 * <tr>
233 * <td rowspan="3">Attaching</td>
234 * <td><code>{@link #onAttachedToWindow()}</code></td>
235 * <td>Called when the view is attached to a window.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td><code>{@link #onDetachedFromWindow}</code></td>
241 * <td>Called when the view is detached from its window.
242 * </td>
243 * </tr>
244 *
245 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700246 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <td>Called when the visibility of the window containing the view
248 * has changed.
249 * </td>
250 * </tr>
251 * </tbody>
252 *
253 * </table>
254 * </p>
255 *
256 * <a name="IDs"></a>
257 * <h3>IDs</h3>
258 * Views may have an integer id associated with them. These ids are typically
259 * assigned in the layout XML files, and are used to find specific views within
260 * the view tree. A common pattern is to:
261 * <ul>
262 * <li>Define a Button in the layout file and assign it a unique ID.
263 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700264 * &lt;Button
265 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * android:layout_width="wrap_content"
267 * android:layout_height="wrap_content"
268 * android:text="@string/my_button_text"/&gt;
269 * </pre></li>
270 * <li>From the onCreate method of an Activity, find the Button
271 * <pre class="prettyprint">
272 * Button myButton = (Button) findViewById(R.id.my_button);
273 * </pre></li>
274 * </ul>
275 * <p>
276 * View IDs need not be unique throughout the tree, but it is good practice to
277 * ensure that they are at least unique within the part of the tree you are
278 * searching.
279 * </p>
280 *
281 * <a name="Position"></a>
282 * <h3>Position</h3>
283 * <p>
284 * The geometry of a view is that of a rectangle. A view has a location,
285 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
286 * two dimensions, expressed as a width and a height. The unit for location
287 * and dimensions is the pixel.
288 * </p>
289 *
290 * <p>
291 * It is possible to retrieve the location of a view by invoking the methods
292 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
293 * coordinate of the rectangle representing the view. The latter returns the
294 * top, or Y, coordinate of the rectangle representing the view. These methods
295 * both return the location of the view relative to its parent. For instance,
296 * when getLeft() returns 20, that means the view is located 20 pixels to the
297 * right of the left edge of its direct parent.
298 * </p>
299 *
300 * <p>
301 * In addition, several convenience methods are offered to avoid unnecessary
302 * computations, namely {@link #getRight()} and {@link #getBottom()}.
303 * These methods return the coordinates of the right and bottom edges of the
304 * rectangle representing the view. For instance, calling {@link #getRight()}
305 * is similar to the following computation: <code>getLeft() + getWidth()</code>
306 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
307 * </p>
308 *
309 * <a name="SizePaddingMargins"></a>
310 * <h3>Size, padding and margins</h3>
311 * <p>
312 * The size of a view is expressed with a width and a height. A view actually
313 * possess two pairs of width and height values.
314 * </p>
315 *
316 * <p>
317 * The first pair is known as <em>measured width</em> and
318 * <em>measured height</em>. These dimensions define how big a view wants to be
319 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
320 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
321 * and {@link #getMeasuredHeight()}.
322 * </p>
323 *
324 * <p>
325 * The second pair is simply known as <em>width</em> and <em>height</em>, or
326 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
327 * dimensions define the actual size of the view on screen, at drawing time and
328 * after layout. These values may, but do not have to, be different from the
329 * measured width and height. The width and height can be obtained by calling
330 * {@link #getWidth()} and {@link #getHeight()}.
331 * </p>
332 *
333 * <p>
334 * To measure its dimensions, a view takes into account its padding. The padding
335 * is expressed in pixels for the left, top, right and bottom parts of the view.
336 * Padding can be used to offset the content of the view by a specific amount of
337 * pixels. For instance, a left padding of 2 will push the view's content by
338 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700339 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
340 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
341 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
342 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 * </p>
344 *
345 * <p>
346 * Even though a view can define a padding, it does not provide any support for
347 * margins. However, view groups provide such a support. Refer to
348 * {@link android.view.ViewGroup} and
349 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
350 * </p>
351 *
352 * <a name="Layout"></a>
353 * <h3>Layout</h3>
354 * <p>
355 * Layout is a two pass process: a measure pass and a layout pass. The measuring
356 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
357 * of the view tree. Each view pushes dimension specifications down the tree
358 * during the recursion. At the end of the measure pass, every view has stored
359 * its measurements. The second pass happens in
360 * {@link #layout(int,int,int,int)} and is also top-down. During
361 * this pass each parent is responsible for positioning all of its children
362 * using the sizes computed in the measure pass.
363 * </p>
364 *
365 * <p>
366 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
367 * {@link #getMeasuredHeight()} values must be set, along with those for all of
368 * that view's descendants. A view's measured width and measured height values
369 * must respect the constraints imposed by the view's parents. This guarantees
370 * that at the end of the measure pass, all parents accept all of their
371 * children's measurements. A parent view may call measure() more than once on
372 * its children. For example, the parent may measure each child once with
373 * unspecified dimensions to find out how big they want to be, then call
374 * measure() on them again with actual numbers if the sum of all the children's
375 * unconstrained sizes is too big or too small.
376 * </p>
377 *
378 * <p>
379 * The measure pass uses two classes to communicate dimensions. The
380 * {@link MeasureSpec} class is used by views to tell their parents how they
381 * want to be measured and positioned. The base LayoutParams class just
382 * describes how big the view wants to be for both width and height. For each
383 * dimension, it can specify one of:
384 * <ul>
385 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800386 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * (minus padding)
388 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
389 * enclose its content (plus padding).
390 * </ul>
391 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
392 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
393 * an X and Y value.
394 * </p>
395 *
396 * <p>
397 * MeasureSpecs are used to push requirements down the tree from parent to
398 * child. A MeasureSpec can be in one of three modes:
399 * <ul>
400 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
401 * of a child view. For example, a LinearLayout may call measure() on its child
402 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
403 * tall the child view wants to be given a width of 240 pixels.
404 * <li>EXACTLY: This is used by the parent to impose an exact size on the
405 * child. The child must use this size, and guarantee that all of its
406 * descendants will fit within this size.
407 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
408 * child. The child must gurantee that it and all of its descendants will fit
409 * within this size.
410 * </ul>
411 * </p>
412 *
413 * <p>
414 * To intiate a layout, call {@link #requestLayout}. This method is typically
415 * called by a view on itself when it believes that is can no longer fit within
416 * its current bounds.
417 * </p>
418 *
419 * <a name="Drawing"></a>
420 * <h3>Drawing</h3>
421 * <p>
422 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700423 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 * this means that parents will draw before (i.e., behind) their children, with
425 * siblings drawn in the order they appear in the tree.
426 * If you set a background drawable for a View, then the View will draw it for you
427 * before calling back to its <code>onDraw()</code> method.
428 * </p>
429 *
430 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700431 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 * </p>
433 *
434 * <p>
435 * To force a view to draw, call {@link #invalidate()}.
436 * </p>
437 *
438 * <a name="EventHandlingThreading"></a>
439 * <h3>Event Handling and Threading</h3>
440 * <p>
441 * The basic cycle of a view is as follows:
442 * <ol>
443 * <li>An event comes in and is dispatched to the appropriate view. The view
444 * handles the event and notifies any listeners.</li>
445 * <li>If in the course of processing the event, the view's bounds may need
446 * to be changed, the view will call {@link #requestLayout()}.</li>
447 * <li>Similarly, if in the course of processing the event the view's appearance
448 * may need to be changed, the view will call {@link #invalidate()}.</li>
449 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
450 * the framework will take care of measuring, laying out, and drawing the tree
451 * as appropriate.</li>
452 * </ol>
453 * </p>
454 *
455 * <p><em>Note: The entire view tree is single threaded. You must always be on
456 * the UI thread when calling any method on any view.</em>
457 * If you are doing work on other threads and want to update the state of a view
458 * from that thread, you should use a {@link Handler}.
459 * </p>
460 *
461 * <a name="FocusHandling"></a>
462 * <h3>Focus Handling</h3>
463 * <p>
464 * The framework will handle routine focus movement in response to user input.
465 * This includes changing the focus as views are removed or hidden, or as new
466 * views become available. Views indicate their willingness to take focus
467 * through the {@link #isFocusable} method. To change whether a view can take
468 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
469 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
470 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
471 * </p>
472 * <p>
473 * Focus movement is based on an algorithm which finds the nearest neighbor in a
474 * given direction. In rare cases, the default algorithm may not match the
475 * intended behavior of the developer. In these situations, you can provide
476 * explicit overrides by using these XML attributes in the layout file:
477 * <pre>
478 * nextFocusDown
479 * nextFocusLeft
480 * nextFocusRight
481 * nextFocusUp
482 * </pre>
483 * </p>
484 *
485 *
486 * <p>
487 * To get a particular view to take focus, call {@link #requestFocus()}.
488 * </p>
489 *
490 * <a name="TouchMode"></a>
491 * <h3>Touch Mode</h3>
492 * <p>
493 * When a user is navigating a user interface via directional keys such as a D-pad, it is
494 * necessary to give focus to actionable items such as buttons so the user can see
495 * what will take input. If the device has touch capabilities, however, and the user
496 * begins interacting with the interface by touching it, it is no longer necessary to
497 * always highlight, or give focus to, a particular view. This motivates a mode
498 * for interaction named 'touch mode'.
499 * </p>
500 * <p>
501 * For a touch capable device, once the user touches the screen, the device
502 * will enter touch mode. From this point onward, only views for which
503 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
504 * Other views that are touchable, like buttons, will not take focus when touched; they will
505 * only fire the on click listeners.
506 * </p>
507 * <p>
508 * Any time a user hits a directional key, such as a D-pad direction, the view device will
509 * exit touch mode, and find a view to take focus, so that the user may resume interacting
510 * with the user interface without touching the screen again.
511 * </p>
512 * <p>
513 * The touch mode state is maintained across {@link android.app.Activity}s. Call
514 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
515 * </p>
516 *
517 * <a name="Scrolling"></a>
518 * <h3>Scrolling</h3>
519 * <p>
520 * The framework provides basic support for views that wish to internally
521 * scroll their content. This includes keeping track of the X and Y scroll
522 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800523 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700524 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * </p>
526 *
527 * <a name="Tags"></a>
528 * <h3>Tags</h3>
529 * <p>
530 * Unlike IDs, tags are not used to identify views. Tags are essentially an
531 * extra piece of information that can be associated with a view. They are most
532 * often used as a convenience to store data related to views in the views
533 * themselves rather than by putting them in a separate structure.
534 * </p>
535 *
536 * <a name="Animation"></a>
537 * <h3>Animation</h3>
538 * <p>
539 * You can attach an {@link Animation} object to a view using
540 * {@link #setAnimation(Animation)} or
541 * {@link #startAnimation(Animation)}. The animation can alter the scale,
542 * rotation, translation and alpha of a view over time. If the animation is
543 * attached to a view that has children, the animation will affect the entire
544 * subtree rooted by that node. When an animation is started, the framework will
545 * take care of redrawing the appropriate views until the animation completes.
546 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800547 * <p>
548 * Starting with Android 3.0, the preferred way of animating views is to use the
549 * {@link android.animation} package APIs.
550 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 *
Jeff Brown85a31762010-09-01 17:01:00 -0700552 * <a name="Security"></a>
553 * <h3>Security</h3>
554 * <p>
555 * Sometimes it is essential that an application be able to verify that an action
556 * is being performed with the full knowledge and consent of the user, such as
557 * granting a permission request, making a purchase or clicking on an advertisement.
558 * Unfortunately, a malicious application could try to spoof the user into
559 * performing these actions, unaware, by concealing the intended purpose of the view.
560 * As a remedy, the framework offers a touch filtering mechanism that can be used to
561 * improve the security of views that provide access to sensitive functionality.
562 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700563 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800564 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700565 * will discard touches that are received whenever the view's window is obscured by
566 * another visible window. As a result, the view will not receive touches whenever a
567 * toast, dialog or other window appears above the view's window.
568 * </p><p>
569 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700570 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
571 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700572 * </p>
573 *
Romain Guy171c5922011-01-06 10:04:23 -0800574 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700575 * @attr ref android.R.styleable#View_background
576 * @attr ref android.R.styleable#View_clickable
577 * @attr ref android.R.styleable#View_contentDescription
578 * @attr ref android.R.styleable#View_drawingCacheQuality
579 * @attr ref android.R.styleable#View_duplicateParentState
580 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700581 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700582 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700583 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_isScrollContainer
586 * @attr ref android.R.styleable#View_focusable
587 * @attr ref android.R.styleable#View_focusableInTouchMode
588 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
589 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800590 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_longClickable
592 * @attr ref android.R.styleable#View_minHeight
593 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * @attr ref android.R.styleable#View_nextFocusDown
595 * @attr ref android.R.styleable#View_nextFocusLeft
596 * @attr ref android.R.styleable#View_nextFocusRight
597 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700598 * @attr ref android.R.styleable#View_onClick
599 * @attr ref android.R.styleable#View_padding
600 * @attr ref android.R.styleable#View_paddingBottom
601 * @attr ref android.R.styleable#View_paddingLeft
602 * @attr ref android.R.styleable#View_paddingRight
603 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800604 * @attr ref android.R.styleable#View_paddingStart
605 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700635public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
636 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 private static final boolean DBG = false;
638
639 /**
640 * The logging tag used by this class with android.util.Log.
641 */
642 protected static final String VIEW_LOG_TAG = "View";
643
644 /**
645 * Used to mark a View that has no ID.
646 */
647 public static final int NO_ID = -1;
648
649 /**
650 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
651 * calling setFlags.
652 */
653 private static final int NOT_FOCUSABLE = 0x00000000;
654
655 /**
656 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
657 * setFlags.
658 */
659 private static final int FOCUSABLE = 0x00000001;
660
661 /**
662 * Mask for use with setFlags indicating bits used for focus.
663 */
664 private static final int FOCUSABLE_MASK = 0x00000001;
665
666 /**
667 * This view will adjust its padding to fit sytem windows (e.g. status bar)
668 */
669 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
670
671 /**
Scott Main812634c22011-07-27 13:22:35 -0700672 * This view is visible.
673 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
674 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 */
676 public static final int VISIBLE = 0x00000000;
677
678 /**
679 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700680 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
681 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 public static final int INVISIBLE = 0x00000004;
684
685 /**
686 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700687 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
688 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 */
690 public static final int GONE = 0x00000008;
691
692 /**
693 * Mask for use with setFlags indicating bits used for visibility.
694 * {@hide}
695 */
696 static final int VISIBILITY_MASK = 0x0000000C;
697
698 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
699
700 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700701 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 * Use with ENABLED_MASK when calling setFlags.
703 * {@hide}
704 */
705 static final int ENABLED = 0x00000000;
706
707 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700708 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 * Use with ENABLED_MASK when calling setFlags.
710 * {@hide}
711 */
712 static final int DISABLED = 0x00000020;
713
714 /**
715 * Mask for use with setFlags indicating bits used for indicating whether
716 * this view is enabled
717 * {@hide}
718 */
719 static final int ENABLED_MASK = 0x00000020;
720
721 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700722 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
723 * called and further optimizations will be performed. It is okay to have
724 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 * {@hide}
726 */
727 static final int WILL_NOT_DRAW = 0x00000080;
728
729 /**
730 * Mask for use with setFlags indicating bits used for indicating whether
731 * this view is will draw
732 * {@hide}
733 */
734 static final int DRAW_MASK = 0x00000080;
735
736 /**
737 * <p>This view doesn't show scrollbars.</p>
738 * {@hide}
739 */
740 static final int SCROLLBARS_NONE = 0x00000000;
741
742 /**
743 * <p>This view shows horizontal scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
747
748 /**
749 * <p>This view shows vertical scrollbars.</p>
750 * {@hide}
751 */
752 static final int SCROLLBARS_VERTICAL = 0x00000200;
753
754 /**
755 * <p>Mask for use with setFlags indicating bits used for indicating which
756 * scrollbars are enabled.</p>
757 * {@hide}
758 */
759 static final int SCROLLBARS_MASK = 0x00000300;
760
Jeff Brown85a31762010-09-01 17:01:00 -0700761 /**
762 * Indicates that the view should filter touches when its window is obscured.
763 * Refer to the class comments for more information about this security feature.
764 * {@hide}
765 */
766 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
767
768 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
770 /**
771 * <p>This view doesn't show fading edges.</p>
772 * {@hide}
773 */
774 static final int FADING_EDGE_NONE = 0x00000000;
775
776 /**
777 * <p>This view shows horizontal fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
781
782 /**
783 * <p>This view shows vertical fading edges.</p>
784 * {@hide}
785 */
786 static final int FADING_EDGE_VERTICAL = 0x00002000;
787
788 /**
789 * <p>Mask for use with setFlags indicating bits used for indicating which
790 * fading edges are enabled.</p>
791 * {@hide}
792 */
793 static final int FADING_EDGE_MASK = 0x00003000;
794
795 /**
796 * <p>Indicates this view can be clicked. When clickable, a View reacts
797 * to clicks by notifying the OnClickListener.<p>
798 * {@hide}
799 */
800 static final int CLICKABLE = 0x00004000;
801
802 /**
803 * <p>Indicates this view is caching its drawing into a bitmap.</p>
804 * {@hide}
805 */
806 static final int DRAWING_CACHE_ENABLED = 0x00008000;
807
808 /**
809 * <p>Indicates that no icicle should be saved for this view.<p>
810 * {@hide}
811 */
812 static final int SAVE_DISABLED = 0x000010000;
813
814 /**
815 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
816 * property.</p>
817 * {@hide}
818 */
819 static final int SAVE_DISABLED_MASK = 0x000010000;
820
821 /**
822 * <p>Indicates that no drawing cache should ever be created for this view.<p>
823 * {@hide}
824 */
825 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
826
827 /**
828 * <p>Indicates this view can take / keep focus when int touch mode.</p>
829 * {@hide}
830 */
831 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
832
833 /**
834 * <p>Enables low quality mode for the drawing cache.</p>
835 */
836 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
837
838 /**
839 * <p>Enables high quality mode for the drawing cache.</p>
840 */
841 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
842
843 /**
844 * <p>Enables automatic quality mode for the drawing cache.</p>
845 */
846 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
847
848 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
849 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
850 };
851
852 /**
853 * <p>Mask for use with setFlags indicating bits used for the cache
854 * quality property.</p>
855 * {@hide}
856 */
857 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
858
859 /**
860 * <p>
861 * Indicates this view can be long clicked. When long clickable, a View
862 * reacts to long clicks by notifying the OnLongClickListener or showing a
863 * context menu.
864 * </p>
865 * {@hide}
866 */
867 static final int LONG_CLICKABLE = 0x00200000;
868
869 /**
870 * <p>Indicates that this view gets its drawable states from its direct parent
871 * and ignores its original internal states.</p>
872 *
873 * @hide
874 */
875 static final int DUPLICATE_PARENT_STATE = 0x00400000;
876
877 /**
878 * The scrollbar style to display the scrollbars inside the content area,
879 * without increasing the padding. The scrollbars will be overlaid with
880 * translucency on the view's content.
881 */
882 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
883
884 /**
885 * The scrollbar style to display the scrollbars inside the padded area,
886 * increasing the padding of the view. The scrollbars will not overlap the
887 * content area of the view.
888 */
889 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
890
891 /**
892 * The scrollbar style to display the scrollbars at the edge of the view,
893 * without increasing the padding. The scrollbars will be overlaid with
894 * translucency.
895 */
896 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
897
898 /**
899 * The scrollbar style to display the scrollbars at the edge of the view,
900 * increasing the padding of the view. The scrollbars will only overlap the
901 * background, if any.
902 */
903 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
904
905 /**
906 * Mask to check if the scrollbar style is overlay or inset.
907 * {@hide}
908 */
909 static final int SCROLLBARS_INSET_MASK = 0x01000000;
910
911 /**
912 * Mask to check if the scrollbar style is inside or outside.
913 * {@hide}
914 */
915 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
916
917 /**
918 * Mask for scrollbar style.
919 * {@hide}
920 */
921 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
922
923 /**
924 * View flag indicating that the screen should remain on while the
925 * window containing this view is visible to the user. This effectively
926 * takes care of automatically setting the WindowManager's
927 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
928 */
929 public static final int KEEP_SCREEN_ON = 0x04000000;
930
931 /**
932 * View flag indicating whether this view should have sound effects enabled
933 * for events such as clicking and touching.
934 */
935 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
936
937 /**
938 * View flag indicating whether this view should have haptic feedback
939 * enabled for events such as long presses.
940 */
941 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
942
943 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700944 * <p>Indicates that the view hierarchy should stop saving state when
945 * it reaches this view. If state saving is initiated immediately at
946 * the view, it will be allowed.
947 * {@hide}
948 */
949 static final int PARENT_SAVE_DISABLED = 0x20000000;
950
951 /**
952 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
953 * {@hide}
954 */
955 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
956
957 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700958 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
959 * should add all focusable Views regardless if they are focusable in touch mode.
960 */
961 public static final int FOCUSABLES_ALL = 0x00000000;
962
963 /**
964 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
965 * should add only Views focusable in touch mode.
966 */
967 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
968
969 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700970 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 * item.
972 */
973 public static final int FOCUS_BACKWARD = 0x00000001;
974
975 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700976 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 * item.
978 */
979 public static final int FOCUS_FORWARD = 0x00000002;
980
981 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700982 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 */
984 public static final int FOCUS_LEFT = 0x00000011;
985
986 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700987 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 */
989 public static final int FOCUS_UP = 0x00000021;
990
991 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700992 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 */
994 public static final int FOCUS_RIGHT = 0x00000042;
995
996 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700997 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 */
999 public static final int FOCUS_DOWN = 0x00000082;
1000
1001 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001002 * Bits of {@link #getMeasuredWidthAndState()} and
1003 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1004 */
1005 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1006
1007 /**
1008 * Bits of {@link #getMeasuredWidthAndState()} and
1009 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1010 */
1011 public static final int MEASURED_STATE_MASK = 0xff000000;
1012
1013 /**
1014 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1015 * for functions that combine both width and height into a single int,
1016 * such as {@link #getMeasuredState()} and the childState argument of
1017 * {@link #resolveSizeAndState(int, int, int)}.
1018 */
1019 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1020
1021 /**
1022 * Bit of {@link #getMeasuredWidthAndState()} and
1023 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1024 * is smaller that the space the view would like to have.
1025 */
1026 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1027
1028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 * Base View state sets
1030 */
1031 // Singles
1032 /**
1033 * Indicates the view has no states set. States are used with
1034 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1035 * view depending on its state.
1036 *
1037 * @see android.graphics.drawable.Drawable
1038 * @see #getDrawableState()
1039 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001040 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 /**
1042 * Indicates the view is enabled. States are used with
1043 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1044 * view depending on its state.
1045 *
1046 * @see android.graphics.drawable.Drawable
1047 * @see #getDrawableState()
1048 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001049 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 /**
1051 * Indicates the view is focused. States are used with
1052 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1053 * view depending on its state.
1054 *
1055 * @see android.graphics.drawable.Drawable
1056 * @see #getDrawableState()
1057 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001058 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 /**
1060 * Indicates the view is selected. States are used with
1061 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1062 * view depending on its state.
1063 *
1064 * @see android.graphics.drawable.Drawable
1065 * @see #getDrawableState()
1066 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001067 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 /**
1069 * Indicates the view is pressed. States are used with
1070 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1071 * view depending on its state.
1072 *
1073 * @see android.graphics.drawable.Drawable
1074 * @see #getDrawableState()
1075 * @hide
1076 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001077 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 /**
1079 * Indicates the view's window has focus. States are used with
1080 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1081 * view depending on its state.
1082 *
1083 * @see android.graphics.drawable.Drawable
1084 * @see #getDrawableState()
1085 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001086 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 // Doubles
1088 /**
1089 * Indicates the view is enabled and has the focus.
1090 *
1091 * @see #ENABLED_STATE_SET
1092 * @see #FOCUSED_STATE_SET
1093 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001094 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 /**
1096 * Indicates the view is enabled and selected.
1097 *
1098 * @see #ENABLED_STATE_SET
1099 * @see #SELECTED_STATE_SET
1100 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001101 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 /**
1103 * Indicates the view is enabled and that its window has focus.
1104 *
1105 * @see #ENABLED_STATE_SET
1106 * @see #WINDOW_FOCUSED_STATE_SET
1107 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001108 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 /**
1110 * Indicates the view is focused and selected.
1111 *
1112 * @see #FOCUSED_STATE_SET
1113 * @see #SELECTED_STATE_SET
1114 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001115 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 /**
1117 * Indicates the view has the focus and that its window has the focus.
1118 *
1119 * @see #FOCUSED_STATE_SET
1120 * @see #WINDOW_FOCUSED_STATE_SET
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is selected and that its window has the focus.
1125 *
1126 * @see #SELECTED_STATE_SET
1127 * @see #WINDOW_FOCUSED_STATE_SET
1128 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001129 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 // Triples
1131 /**
1132 * Indicates the view is enabled, focused and selected.
1133 *
1134 * @see #ENABLED_STATE_SET
1135 * @see #FOCUSED_STATE_SET
1136 * @see #SELECTED_STATE_SET
1137 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001138 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 /**
1140 * Indicates the view is enabled, focused and its window has the focus.
1141 *
1142 * @see #ENABLED_STATE_SET
1143 * @see #FOCUSED_STATE_SET
1144 * @see #WINDOW_FOCUSED_STATE_SET
1145 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001146 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 /**
1148 * Indicates the view is enabled, selected and its window has the focus.
1149 *
1150 * @see #ENABLED_STATE_SET
1151 * @see #SELECTED_STATE_SET
1152 * @see #WINDOW_FOCUSED_STATE_SET
1153 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001154 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 /**
1156 * Indicates the view is focused, selected and its window has the focus.
1157 *
1158 * @see #FOCUSED_STATE_SET
1159 * @see #SELECTED_STATE_SET
1160 * @see #WINDOW_FOCUSED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view is enabled, focused, selected and its window
1165 * has the focus.
1166 *
1167 * @see #ENABLED_STATE_SET
1168 * @see #FOCUSED_STATE_SET
1169 * @see #SELECTED_STATE_SET
1170 * @see #WINDOW_FOCUSED_STATE_SET
1171 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001172 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 /**
1174 * Indicates the view is pressed and its window has the focus.
1175 *
1176 * @see #PRESSED_STATE_SET
1177 * @see #WINDOW_FOCUSED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
1181 * Indicates the view is pressed and selected.
1182 *
1183 * @see #PRESSED_STATE_SET
1184 * @see #SELECTED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is pressed, selected and its window has the focus.
1189 *
1190 * @see #PRESSED_STATE_SET
1191 * @see #SELECTED_STATE_SET
1192 * @see #WINDOW_FOCUSED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is pressed and focused.
1197 *
1198 * @see #PRESSED_STATE_SET
1199 * @see #FOCUSED_STATE_SET
1200 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001201 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 /**
1203 * Indicates the view is pressed, focused and its window has the focus.
1204 *
1205 * @see #PRESSED_STATE_SET
1206 * @see #FOCUSED_STATE_SET
1207 * @see #WINDOW_FOCUSED_STATE_SET
1208 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001209 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 /**
1211 * Indicates the view is pressed, focused and selected.
1212 *
1213 * @see #PRESSED_STATE_SET
1214 * @see #SELECTED_STATE_SET
1215 * @see #FOCUSED_STATE_SET
1216 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001217 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 /**
1219 * Indicates the view is pressed, focused, selected and its window has the focus.
1220 *
1221 * @see #PRESSED_STATE_SET
1222 * @see #FOCUSED_STATE_SET
1223 * @see #SELECTED_STATE_SET
1224 * @see #WINDOW_FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is pressed and enabled.
1229 *
1230 * @see #PRESSED_STATE_SET
1231 * @see #ENABLED_STATE_SET
1232 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001233 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 /**
1235 * Indicates the view is pressed, enabled and its window has the focus.
1236 *
1237 * @see #PRESSED_STATE_SET
1238 * @see #ENABLED_STATE_SET
1239 * @see #WINDOW_FOCUSED_STATE_SET
1240 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001241 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 /**
1243 * Indicates the view is pressed, enabled and selected.
1244 *
1245 * @see #PRESSED_STATE_SET
1246 * @see #ENABLED_STATE_SET
1247 * @see #SELECTED_STATE_SET
1248 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001249 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 /**
1251 * Indicates the view is pressed, enabled, selected and its window has the
1252 * focus.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #ENABLED_STATE_SET
1256 * @see #SELECTED_STATE_SET
1257 * @see #WINDOW_FOCUSED_STATE_SET
1258 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001259 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 /**
1261 * Indicates the view is pressed, enabled and focused.
1262 *
1263 * @see #PRESSED_STATE_SET
1264 * @see #ENABLED_STATE_SET
1265 * @see #FOCUSED_STATE_SET
1266 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001267 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * Indicates the view is pressed, enabled, focused and its window has the
1270 * focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #ENABLED_STATE_SET
1274 * @see #FOCUSED_STATE_SET
1275 * @see #WINDOW_FOCUSED_STATE_SET
1276 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001277 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 /**
1279 * Indicates the view is pressed, enabled, focused and selected.
1280 *
1281 * @see #PRESSED_STATE_SET
1282 * @see #ENABLED_STATE_SET
1283 * @see #SELECTED_STATE_SET
1284 * @see #FOCUSED_STATE_SET
1285 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001286 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 /**
1288 * Indicates the view is pressed, enabled, focused, selected and its window
1289 * has the focus.
1290 *
1291 * @see #PRESSED_STATE_SET
1292 * @see #ENABLED_STATE_SET
1293 * @see #SELECTED_STATE_SET
1294 * @see #FOCUSED_STATE_SET
1295 * @see #WINDOW_FOCUSED_STATE_SET
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298
1299 /**
1300 * The order here is very important to {@link #getDrawableState()}
1301 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001302 private static final int[][] VIEW_STATE_SETS;
1303
Romain Guyb051e892010-09-28 19:09:36 -07001304 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1305 static final int VIEW_STATE_SELECTED = 1 << 1;
1306 static final int VIEW_STATE_FOCUSED = 1 << 2;
1307 static final int VIEW_STATE_ENABLED = 1 << 3;
1308 static final int VIEW_STATE_PRESSED = 1 << 4;
1309 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001310 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001311 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001312 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1313 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001314
1315 static final int[] VIEW_STATE_IDS = new int[] {
1316 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1317 R.attr.state_selected, VIEW_STATE_SELECTED,
1318 R.attr.state_focused, VIEW_STATE_FOCUSED,
1319 R.attr.state_enabled, VIEW_STATE_ENABLED,
1320 R.attr.state_pressed, VIEW_STATE_PRESSED,
1321 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001322 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001323 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001324 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1325 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 };
1327
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001329 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1330 throw new IllegalStateException(
1331 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1332 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001334 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001335 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001336 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001337 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001338 orderedIds[i * 2] = viewState;
1339 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 }
1341 }
1342 }
Romain Guyb051e892010-09-28 19:09:36 -07001343 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1344 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1345 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 int numBits = Integer.bitCount(i);
1347 int[] set = new int[numBits];
1348 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001349 for (int j = 0; j < orderedIds.length; j += 2) {
1350 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001351 set[pos++] = orderedIds[j];
1352 }
1353 }
1354 VIEW_STATE_SETS[i] = set;
1355 }
1356
1357 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1358 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1359 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1360 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1361 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1362 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1363 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1365 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1366 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1367 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1368 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1369 | VIEW_STATE_FOCUSED];
1370 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1371 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1372 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1373 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1374 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1375 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1376 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1377 | VIEW_STATE_ENABLED];
1378 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1379 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1380 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1381 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1382 | VIEW_STATE_ENABLED];
1383 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1385 | VIEW_STATE_ENABLED];
1386 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1387 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1388 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1389
1390 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1391 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1393 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1394 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1395 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1396 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1397 | VIEW_STATE_PRESSED];
1398 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1399 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1400 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1401 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1402 | VIEW_STATE_PRESSED];
1403 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1405 | VIEW_STATE_PRESSED];
1406 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1408 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1409 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1411 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1413 | VIEW_STATE_PRESSED];
1414 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1416 | VIEW_STATE_PRESSED];
1417 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1419 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1420 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1422 | VIEW_STATE_PRESSED];
1423 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1425 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1426 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1428 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1429 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1431 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1432 | VIEW_STATE_PRESSED];
1433 }
1434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001436 * Accessibility event types that are dispatched for text population.
1437 */
1438 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1439 AccessibilityEvent.TYPE_VIEW_CLICKED
1440 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1441 | AccessibilityEvent.TYPE_VIEW_SELECTED
1442 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1443 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1444 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001445 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001446 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1447 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001448
1449 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 * Temporary Rect currently for use in setBackground(). This will probably
1451 * be extended in the future to hold our own class with more than just
1452 * a Rect. :)
1453 */
1454 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001455
1456 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001457 * Temporary flag, used to enable processing of View properties in the native DisplayList
1458 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1459 * apps.
1460 * @hide
1461 */
Chet Haasead13c812012-03-22 08:33:09 -07001462 public static final boolean USE_DISPLAY_LIST_PROPERTIES = false;
Chet Haasea1cff502012-02-21 13:43:44 -08001463
1464 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001465 * Map used to store views' tags.
1466 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001467 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001470 * The next available accessiiblity id.
1471 */
1472 private static int sNextAccessibilityViewId;
1473
1474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 * The animation currently associated with this view.
1476 * @hide
1477 */
1478 protected Animation mCurrentAnimation = null;
1479
1480 /**
1481 * Width as measured during measure pass.
1482 * {@hide}
1483 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001484 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001485 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486
1487 /**
1488 * Height as measured during measure pass.
1489 * {@hide}
1490 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001491 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001492 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493
1494 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001495 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1496 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1497 * its display list. This flag, used only when hw accelerated, allows us to clear the
1498 * flag while retaining this information until it's needed (at getDisplayList() time and
1499 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1500 *
1501 * {@hide}
1502 */
1503 boolean mRecreateDisplayList = false;
1504
1505 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 * The view's identifier.
1507 * {@hide}
1508 *
1509 * @see #setId(int)
1510 * @see #getId()
1511 */
1512 @ViewDebug.ExportedProperty(resolveId = true)
1513 int mID = NO_ID;
1514
1515 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001516 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001517 */
1518 int mAccessibilityViewId = NO_ID;
1519
1520 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 * The view's tag.
1522 * {@hide}
1523 *
1524 * @see #setTag(Object)
1525 * @see #getTag()
1526 */
1527 protected Object mTag;
1528
1529 // for mPrivateFlags:
1530 /** {@hide} */
1531 static final int WANTS_FOCUS = 0x00000001;
1532 /** {@hide} */
1533 static final int FOCUSED = 0x00000002;
1534 /** {@hide} */
1535 static final int SELECTED = 0x00000004;
1536 /** {@hide} */
1537 static final int IS_ROOT_NAMESPACE = 0x00000008;
1538 /** {@hide} */
1539 static final int HAS_BOUNDS = 0x00000010;
1540 /** {@hide} */
1541 static final int DRAWN = 0x00000020;
1542 /**
1543 * When this flag is set, this view is running an animation on behalf of its
1544 * children and should therefore not cancel invalidate requests, even if they
1545 * lie outside of this view's bounds.
1546 *
1547 * {@hide}
1548 */
1549 static final int DRAW_ANIMATION = 0x00000040;
1550 /** {@hide} */
1551 static final int SKIP_DRAW = 0x00000080;
1552 /** {@hide} */
1553 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1554 /** {@hide} */
1555 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1556 /** {@hide} */
1557 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1558 /** {@hide} */
1559 static final int MEASURED_DIMENSION_SET = 0x00000800;
1560 /** {@hide} */
1561 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001562 /** {@hide} */
1563 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564
1565 private static final int PRESSED = 0x00004000;
1566
1567 /** {@hide} */
1568 static final int DRAWING_CACHE_VALID = 0x00008000;
1569 /**
1570 * Flag used to indicate that this view should be drawn once more (and only once
1571 * more) after its animation has completed.
1572 * {@hide}
1573 */
1574 static final int ANIMATION_STARTED = 0x00010000;
1575
1576 private static final int SAVE_STATE_CALLED = 0x00020000;
1577
1578 /**
1579 * Indicates that the View returned true when onSetAlpha() was called and that
1580 * the alpha must be restored.
1581 * {@hide}
1582 */
1583 static final int ALPHA_SET = 0x00040000;
1584
1585 /**
1586 * Set by {@link #setScrollContainer(boolean)}.
1587 */
1588 static final int SCROLL_CONTAINER = 0x00080000;
1589
1590 /**
1591 * Set by {@link #setScrollContainer(boolean)}.
1592 */
1593 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1594
1595 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001596 * View flag indicating whether this view was invalidated (fully or partially.)
1597 *
1598 * @hide
1599 */
1600 static final int DIRTY = 0x00200000;
1601
1602 /**
1603 * View flag indicating whether this view was invalidated by an opaque
1604 * invalidate request.
1605 *
1606 * @hide
1607 */
1608 static final int DIRTY_OPAQUE = 0x00400000;
1609
1610 /**
1611 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1612 *
1613 * @hide
1614 */
1615 static final int DIRTY_MASK = 0x00600000;
1616
1617 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001618 * Indicates whether the background is opaque.
1619 *
1620 * @hide
1621 */
1622 static final int OPAQUE_BACKGROUND = 0x00800000;
1623
1624 /**
1625 * Indicates whether the scrollbars are opaque.
1626 *
1627 * @hide
1628 */
1629 static final int OPAQUE_SCROLLBARS = 0x01000000;
1630
1631 /**
1632 * Indicates whether the view is opaque.
1633 *
1634 * @hide
1635 */
1636 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001637
Adam Powelle14579b2009-12-16 18:39:52 -08001638 /**
1639 * Indicates a prepressed state;
1640 * the short time between ACTION_DOWN and recognizing
1641 * a 'real' press. Prepressed is used to recognize quick taps
1642 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001643 *
Adam Powelle14579b2009-12-16 18:39:52 -08001644 * @hide
1645 */
1646 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001647
Adam Powellc9fbaab2010-02-16 17:16:19 -08001648 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001649 * Indicates whether the view is temporarily detached.
1650 *
1651 * @hide
1652 */
1653 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001654
Adam Powell8568c3a2010-04-19 14:26:11 -07001655 /**
1656 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001657 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001658 * @hide
1659 */
1660 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001661
1662 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001663 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1664 * @hide
1665 */
1666 private static final int HOVERED = 0x10000000;
1667
1668 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001669 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1670 * for transform operations
1671 *
1672 * @hide
1673 */
Adam Powellf37df072010-09-17 16:22:49 -07001674 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001675
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001676 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001677 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001678
Chet Haasefd2b0022010-08-06 13:08:56 -07001679 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001680 * Indicates that this view was specifically invalidated, not just dirtied because some
1681 * child view was invalidated. The flag is used to determine when we need to recreate
1682 * a view's display list (as opposed to just returning a reference to its existing
1683 * display list).
1684 *
1685 * @hide
1686 */
1687 static final int INVALIDATED = 0x80000000;
1688
Christopher Tate3d4bf172011-03-28 16:16:46 -07001689 /* Masks for mPrivateFlags2 */
1690
1691 /**
1692 * Indicates that this view has reported that it can accept the current drag's content.
1693 * Cleared when the drag operation concludes.
1694 * @hide
1695 */
1696 static final int DRAG_CAN_ACCEPT = 0x00000001;
1697
1698 /**
1699 * Indicates that this view is currently directly under the drag location in a
1700 * drag-and-drop operation involving content that it can accept. Cleared when
1701 * the drag exits the view, or when the drag operation concludes.
1702 * @hide
1703 */
1704 static final int DRAG_HOVERED = 0x00000002;
1705
Cibu Johny86666632010-02-22 13:01:02 -08001706 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001707 * Horizontal layout direction of this view is from Left to Right.
1708 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001709 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001710 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001711
1712 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001713 * Horizontal layout direction of this view is from Right to Left.
1714 * Use with {@link #setLayoutDirection}.
1715 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001716 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001717
1718 /**
1719 * Horizontal layout direction of this view is inherited from its parent.
1720 * Use with {@link #setLayoutDirection}.
1721 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001722 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001723
1724 /**
1725 * Horizontal layout direction of this view is from deduced from the default language
1726 * script for the locale. Use with {@link #setLayoutDirection}.
1727 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001728 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001729
1730 /**
1731 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001732 * @hide
1733 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001734 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1735
1736 /**
1737 * Mask for use with private flags indicating bits used for horizontal layout direction.
1738 * @hide
1739 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001740 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001741
1742 /**
1743 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1744 * right-to-left direction.
1745 * @hide
1746 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001747 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001748
1749 /**
1750 * Indicates whether the view horizontal layout direction has been resolved.
1751 * @hide
1752 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001753 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001754
1755 /**
1756 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1757 * @hide
1758 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001759 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001760
1761 /*
1762 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1763 * flag value.
1764 * @hide
1765 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001766 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1767 LAYOUT_DIRECTION_LTR,
1768 LAYOUT_DIRECTION_RTL,
1769 LAYOUT_DIRECTION_INHERIT,
1770 LAYOUT_DIRECTION_LOCALE
1771 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001772
1773 /**
1774 * Default horizontal layout direction.
1775 * @hide
1776 */
1777 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001778
Cibu Johny86666632010-02-22 13:01:02 -08001779
Adam Powell539ee872012-02-03 19:00:49 -08001780 /**
1781 * Indicates that the view is tracking some sort of transient state
1782 * that the app should not need to be aware of, but that the framework
1783 * should take special care to preserve.
1784 *
1785 * @hide
1786 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001787 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001788
1789
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001790 /**
1791 * Text direction is inherited thru {@link ViewGroup}
1792 */
1793 public static final int TEXT_DIRECTION_INHERIT = 0;
1794
1795 /**
1796 * Text direction is using "first strong algorithm". The first strong directional character
1797 * determines the paragraph direction. If there is no strong directional character, the
1798 * paragraph direction is the view's resolved layout direction.
1799 */
1800 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1801
1802 /**
1803 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1804 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1805 * If there are neither, the paragraph direction is the view's resolved layout direction.
1806 */
1807 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1808
1809 /**
1810 * Text direction is forced to LTR.
1811 */
1812 public static final int TEXT_DIRECTION_LTR = 3;
1813
1814 /**
1815 * Text direction is forced to RTL.
1816 */
1817 public static final int TEXT_DIRECTION_RTL = 4;
1818
1819 /**
1820 * Text direction is coming from the system Locale.
1821 */
1822 public static final int TEXT_DIRECTION_LOCALE = 5;
1823
1824 /**
1825 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1826 * @hide
1827 */
1828 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1829
1830 /**
1831 * Default text direction is inherited
1832 */
1833 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1834
1835 /**
1836 * Mask for use with private flags indicating bits used for text direction.
1837 * @hide
1838 */
1839 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1840
1841 /**
1842 * Array of text direction flags for mapping attribute "textDirection" to correct
1843 * flag value.
1844 * @hide
1845 */
1846 private static final int[] TEXT_DIRECTION_FLAGS = {
1847 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1848 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1849 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1850 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1851 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1852 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1853 };
1854
1855 /**
1856 * Indicates whether the view text direction has been resolved.
1857 * @hide
1858 */
1859 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1860
1861 /**
1862 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1863 * @hide
1864 */
1865 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1866
1867 /**
1868 * Mask for use with private flags indicating bits used for resolved text direction.
1869 * @hide
1870 */
1871 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1872
1873 /**
1874 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1875 * @hide
1876 */
1877 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1878 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1879
1880
Christopher Tate3d4bf172011-03-28 16:16:46 -07001881 /* End of masks for mPrivateFlags2 */
1882
1883 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1884
Chet Haasedaf98e92011-01-10 14:10:36 -08001885 /**
Adam Powell637d3372010-08-25 14:37:03 -07001886 * Always allow a user to over-scroll this view, provided it is a
1887 * view that can scroll.
1888 *
1889 * @see #getOverScrollMode()
1890 * @see #setOverScrollMode(int)
1891 */
1892 public static final int OVER_SCROLL_ALWAYS = 0;
1893
1894 /**
1895 * Allow a user to over-scroll this view only if the content is large
1896 * enough to meaningfully scroll, provided it is a view that can scroll.
1897 *
1898 * @see #getOverScrollMode()
1899 * @see #setOverScrollMode(int)
1900 */
1901 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1902
1903 /**
1904 * Never allow a user to over-scroll this view.
1905 *
1906 * @see #getOverScrollMode()
1907 * @see #setOverScrollMode(int)
1908 */
1909 public static final int OVER_SCROLL_NEVER = 2;
1910
1911 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001912 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001913 *
Joe Malin32736f02011-01-19 16:14:20 -08001914 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001915 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001916 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001917
1918 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001919 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1920 *
1921 * This is for use in games, book readers, video players, or any other "immersive" application
1922 * where the usual system chrome is deemed too distracting.
1923 *
1924 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001925 *
Joe Malin32736f02011-01-19 16:14:20 -08001926 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001927 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001928 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1929
1930 /**
1931 * View has requested that the system navigation be temporarily hidden.
1932 *
1933 * This is an even less obtrusive state than that called for by
1934 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1935 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1936 * those to disappear. This is useful (in conjunction with the
1937 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1938 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1939 * window flags) for displaying content using every last pixel on the display.
1940 *
1941 * There is a limitation: because navigation controls are so important, the least user
1942 * interaction will cause them to reappear immediately.
1943 *
1944 * @see #setSystemUiVisibility(int)
1945 */
1946 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1947
1948 /**
1949 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1950 */
1951 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1952
1953 /**
1954 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1955 */
1956 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001957
1958 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001959 * @hide
1960 *
1961 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1962 * out of the public fields to keep the undefined bits out of the developer's way.
1963 *
1964 * Flag to make the status bar not expandable. Unless you also
1965 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1966 */
1967 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1968
1969 /**
1970 * @hide
1971 *
1972 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1973 * out of the public fields to keep the undefined bits out of the developer's way.
1974 *
1975 * Flag to hide notification icons and scrolling ticker text.
1976 */
1977 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1978
1979 /**
1980 * @hide
1981 *
1982 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1983 * out of the public fields to keep the undefined bits out of the developer's way.
1984 *
1985 * Flag to disable incoming notification alerts. This will not block
1986 * icons, but it will block sound, vibrating and other visual or aural notifications.
1987 */
1988 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1989
1990 /**
1991 * @hide
1992 *
1993 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1994 * out of the public fields to keep the undefined bits out of the developer's way.
1995 *
1996 * Flag to hide only the scrolling ticker. Note that
1997 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1998 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1999 */
2000 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2001
2002 /**
2003 * @hide
2004 *
2005 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2006 * out of the public fields to keep the undefined bits out of the developer's way.
2007 *
2008 * Flag to hide the center system info area.
2009 */
2010 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2011
2012 /**
2013 * @hide
2014 *
2015 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2016 * out of the public fields to keep the undefined bits out of the developer's way.
2017 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002018 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002019 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2020 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002021 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002022
2023 /**
2024 * @hide
2025 *
2026 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2027 * out of the public fields to keep the undefined bits out of the developer's way.
2028 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002029 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002030 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2031 */
2032 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2033
2034 /**
2035 * @hide
2036 *
2037 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2038 * out of the public fields to keep the undefined bits out of the developer's way.
2039 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002040 * Flag to hide only the clock. You might use this if your activity has
2041 * its own clock making the status bar's clock redundant.
2042 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002043 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2044
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002045 /**
2046 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002047 *
2048 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2049 * out of the public fields to keep the undefined bits out of the developer's way.
2050 *
2051 * Flag to hide only the recent apps button. Don't use this
2052 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2053 */
2054 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2055
2056 /**
2057 * @hide
2058 *
2059 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
2060 *
2061 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
2062 */
2063 @Deprecated
2064 public static final int STATUS_BAR_DISABLE_NAVIGATION =
2065 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
2066
2067 /**
2068 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002069 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002070 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002071
2072 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002073 * These are the system UI flags that can be cleared by events outside
2074 * of an application. Currently this is just the ability to tap on the
2075 * screen while hiding the navigation bar to have it return.
2076 * @hide
2077 */
2078 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
2079 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
2080
2081 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002082 * Find views that render the specified text.
2083 *
2084 * @see #findViewsWithText(ArrayList, CharSequence, int)
2085 */
2086 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2087
2088 /**
2089 * Find find views that contain the specified content description.
2090 *
2091 * @see #findViewsWithText(ArrayList, CharSequence, int)
2092 */
2093 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2094
2095 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002096 * Find views that contain {@link AccessibilityNodeProvider}. Such
2097 * a View is a root of virtual view hierarchy and may contain the searched
2098 * text. If this flag is set Views with providers are automatically
2099 * added and it is a responsibility of the client to call the APIs of
2100 * the provider to determine whether the virtual tree rooted at this View
2101 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2102 * represeting the virtual views with this text.
2103 *
2104 * @see #findViewsWithText(ArrayList, CharSequence, int)
2105 *
2106 * @hide
2107 */
2108 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2109
2110 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002111 * Indicates that the screen has changed state and is now off.
2112 *
2113 * @see #onScreenStateChanged(int)
2114 */
2115 public static final int SCREEN_STATE_OFF = 0x0;
2116
2117 /**
2118 * Indicates that the screen has changed state and is now on.
2119 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002120 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002121 */
2122 public static final int SCREEN_STATE_ON = 0x1;
2123
2124 /**
Adam Powell637d3372010-08-25 14:37:03 -07002125 * Controls the over-scroll mode for this view.
2126 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2127 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2128 * and {@link #OVER_SCROLL_NEVER}.
2129 */
2130 private int mOverScrollMode;
2131
2132 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 * The parent this view is attached to.
2134 * {@hide}
2135 *
2136 * @see #getParent()
2137 */
2138 protected ViewParent mParent;
2139
2140 /**
2141 * {@hide}
2142 */
2143 AttachInfo mAttachInfo;
2144
2145 /**
2146 * {@hide}
2147 */
Romain Guy809a7f62009-05-14 15:44:42 -07002148 @ViewDebug.ExportedProperty(flagMapping = {
2149 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2150 name = "FORCE_LAYOUT"),
2151 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2152 name = "LAYOUT_REQUIRED"),
2153 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002154 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002155 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2156 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2157 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2158 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2159 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002161 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162
2163 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002164 * This view's request for the visibility of the status bar.
2165 * @hide
2166 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002167 @ViewDebug.ExportedProperty(flagMapping = {
2168 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2169 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2170 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2171 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2172 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2173 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2174 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2175 equals = SYSTEM_UI_FLAG_VISIBLE,
2176 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2177 })
Joe Onorato664644d2011-01-23 17:53:23 -08002178 int mSystemUiVisibility;
2179
2180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 * Count of how many windows this view has been attached to.
2182 */
2183 int mWindowAttachCount;
2184
2185 /**
2186 * The layout parameters associated with this view and used by the parent
2187 * {@link android.view.ViewGroup} to determine how this view should be
2188 * laid out.
2189 * {@hide}
2190 */
2191 protected ViewGroup.LayoutParams mLayoutParams;
2192
2193 /**
2194 * The view flags hold various views states.
2195 * {@hide}
2196 */
2197 @ViewDebug.ExportedProperty
2198 int mViewFlags;
2199
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002200 static class TransformationInfo {
2201 /**
2202 * The transform matrix for the View. This transform is calculated internally
2203 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2204 * is used by default. Do *not* use this variable directly; instead call
2205 * getMatrix(), which will automatically recalculate the matrix if necessary
2206 * to get the correct matrix based on the latest rotation and scale properties.
2207 */
2208 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002209
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002210 /**
2211 * The transform matrix for the View. This transform is calculated internally
2212 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2213 * is used by default. Do *not* use this variable directly; instead call
2214 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2215 * to get the correct matrix based on the latest rotation and scale properties.
2216 */
2217 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002218
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002219 /**
2220 * An internal variable that tracks whether we need to recalculate the
2221 * transform matrix, based on whether the rotation or scaleX/Y properties
2222 * have changed since the matrix was last calculated.
2223 */
2224 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002225
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002226 /**
2227 * An internal variable that tracks whether we need to recalculate the
2228 * transform matrix, based on whether the rotation or scaleX/Y properties
2229 * have changed since the matrix was last calculated.
2230 */
2231 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002232
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002233 /**
2234 * A variable that tracks whether we need to recalculate the
2235 * transform matrix, based on whether the rotation or scaleX/Y properties
2236 * have changed since the matrix was last calculated. This variable
2237 * is only valid after a call to updateMatrix() or to a function that
2238 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2239 */
2240 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002241
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002242 /**
2243 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2244 */
2245 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002246
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002247 /**
2248 * This matrix is used when computing the matrix for 3D rotations.
2249 */
2250 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002251
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002252 /**
2253 * These prev values are used to recalculate a centered pivot point when necessary. The
2254 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2255 * set), so thes values are only used then as well.
2256 */
2257 private int mPrevWidth = -1;
2258 private int mPrevHeight = -1;
2259
2260 /**
2261 * The degrees rotation around the vertical axis through the pivot point.
2262 */
2263 @ViewDebug.ExportedProperty
2264 float mRotationY = 0f;
2265
2266 /**
2267 * The degrees rotation around the horizontal axis through the pivot point.
2268 */
2269 @ViewDebug.ExportedProperty
2270 float mRotationX = 0f;
2271
2272 /**
2273 * The degrees rotation around the pivot point.
2274 */
2275 @ViewDebug.ExportedProperty
2276 float mRotation = 0f;
2277
2278 /**
2279 * The amount of translation of the object away from its left property (post-layout).
2280 */
2281 @ViewDebug.ExportedProperty
2282 float mTranslationX = 0f;
2283
2284 /**
2285 * The amount of translation of the object away from its top property (post-layout).
2286 */
2287 @ViewDebug.ExportedProperty
2288 float mTranslationY = 0f;
2289
2290 /**
2291 * The amount of scale in the x direction around the pivot point. A
2292 * value of 1 means no scaling is applied.
2293 */
2294 @ViewDebug.ExportedProperty
2295 float mScaleX = 1f;
2296
2297 /**
2298 * The amount of scale in the y direction around the pivot point. A
2299 * value of 1 means no scaling is applied.
2300 */
2301 @ViewDebug.ExportedProperty
2302 float mScaleY = 1f;
2303
2304 /**
Chet Haasea33de552012-02-03 16:28:24 -08002305 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002306 */
2307 @ViewDebug.ExportedProperty
2308 float mPivotX = 0f;
2309
2310 /**
Chet Haasea33de552012-02-03 16:28:24 -08002311 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002312 */
2313 @ViewDebug.ExportedProperty
2314 float mPivotY = 0f;
2315
2316 /**
2317 * The opacity of the View. This is a value from 0 to 1, where 0 means
2318 * completely transparent and 1 means completely opaque.
2319 */
2320 @ViewDebug.ExportedProperty
2321 float mAlpha = 1f;
2322 }
2323
2324 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002325
Joe Malin32736f02011-01-19 16:14:20 -08002326 private boolean mLastIsOpaque;
2327
Chet Haasefd2b0022010-08-06 13:08:56 -07002328 /**
2329 * Convenience value to check for float values that are close enough to zero to be considered
2330 * zero.
2331 */
Romain Guy2542d192010-08-18 11:47:12 -07002332 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002333
2334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 * The distance in pixels from the left edge of this view's parent
2336 * to the left edge of this view.
2337 * {@hide}
2338 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002339 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 protected int mLeft;
2341 /**
2342 * The distance in pixels from the left edge of this view's parent
2343 * to the right edge of this view.
2344 * {@hide}
2345 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002346 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 protected int mRight;
2348 /**
2349 * The distance in pixels from the top edge of this view's parent
2350 * to the top edge of this view.
2351 * {@hide}
2352 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002353 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 protected int mTop;
2355 /**
2356 * The distance in pixels from the top edge of this view's parent
2357 * to the bottom edge of this view.
2358 * {@hide}
2359 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002360 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 protected int mBottom;
2362
2363 /**
2364 * The offset, in pixels, by which the content of this view is scrolled
2365 * horizontally.
2366 * {@hide}
2367 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002368 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 protected int mScrollX;
2370 /**
2371 * The offset, in pixels, by which the content of this view is scrolled
2372 * vertically.
2373 * {@hide}
2374 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002375 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 protected int mScrollY;
2377
2378 /**
2379 * The left padding in pixels, that is the distance in pixels between the
2380 * left edge of this view and the left edge of its content.
2381 * {@hide}
2382 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002383 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 protected int mPaddingLeft;
2385 /**
2386 * The right padding in pixels, that is the distance in pixels between the
2387 * right edge of this view and the right edge of its content.
2388 * {@hide}
2389 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002390 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 protected int mPaddingRight;
2392 /**
2393 * The top padding in pixels, that is the distance in pixels between the
2394 * top edge of this view and the top edge of its content.
2395 * {@hide}
2396 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002397 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 protected int mPaddingTop;
2399 /**
2400 * The bottom padding in pixels, that is the distance in pixels between the
2401 * bottom edge of this view and the bottom edge of its content.
2402 * {@hide}
2403 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002404 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 protected int mPaddingBottom;
2406
2407 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002408 * Briefly describes the view and is primarily used for accessibility support.
2409 */
2410 private CharSequence mContentDescription;
2411
2412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002414 *
2415 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002417 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002418 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419
2420 /**
2421 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002422 *
2423 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002425 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002426 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002428 /**
Adam Powell20232d02010-12-08 21:08:53 -08002429 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002430 *
2431 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002432 */
2433 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002434 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002435
2436 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002437 * Cache if the user padding is relative.
2438 *
2439 */
2440 @ViewDebug.ExportedProperty(category = "padding")
2441 boolean mUserPaddingRelative;
2442
2443 /**
2444 * Cache the paddingStart set by the user to append to the scrollbar's size.
2445 *
2446 */
2447 @ViewDebug.ExportedProperty(category = "padding")
2448 int mUserPaddingStart;
2449
2450 /**
2451 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2452 *
2453 */
2454 @ViewDebug.ExportedProperty(category = "padding")
2455 int mUserPaddingEnd;
2456
2457 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002458 * @hide
2459 */
2460 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2461 /**
2462 * @hide
2463 */
2464 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 private Drawable mBGDrawable;
2467
2468 private int mBackgroundResource;
2469 private boolean mBackgroundSizeChanged;
2470
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002471 static class ListenerInfo {
2472 /**
2473 * Listener used to dispatch focus change events.
2474 * This field should be made private, so it is hidden from the SDK.
2475 * {@hide}
2476 */
2477 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002479 /**
2480 * Listeners for layout change events.
2481 */
2482 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002483
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002484 /**
2485 * Listeners for attach events.
2486 */
2487 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002488
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002489 /**
2490 * Listener used to dispatch click events.
2491 * This field should be made private, so it is hidden from the SDK.
2492 * {@hide}
2493 */
2494 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002496 /**
2497 * Listener used to dispatch long click events.
2498 * This field should be made private, so it is hidden from the SDK.
2499 * {@hide}
2500 */
2501 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002503 /**
2504 * Listener used to build the context menu.
2505 * This field should be made private, so it is hidden from the SDK.
2506 * {@hide}
2507 */
2508 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002510 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002512 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002514 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002515
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002516 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002517
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002518 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002519
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002520 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2521 }
2522
2523 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 /**
2526 * The application environment this view lives in.
2527 * This field should be made private, so it is hidden from the SDK.
2528 * {@hide}
2529 */
2530 protected Context mContext;
2531
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002532 private final Resources mResources;
2533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 private ScrollabilityCache mScrollCache;
2535
2536 private int[] mDrawableState = null;
2537
Romain Guy0211a0a2011-02-14 16:34:59 -08002538 /**
2539 * Set to true when drawing cache is enabled and cannot be created.
2540 *
2541 * @hide
2542 */
2543 public boolean mCachingFailed;
2544
Romain Guy02890fd2010-08-06 17:58:44 -07002545 private Bitmap mDrawingCache;
2546 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002547 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002548 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549
2550 /**
2551 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2552 * the user may specify which view to go to next.
2553 */
2554 private int mNextFocusLeftId = View.NO_ID;
2555
2556 /**
2557 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2558 * the user may specify which view to go to next.
2559 */
2560 private int mNextFocusRightId = View.NO_ID;
2561
2562 /**
2563 * When this view has focus and the next focus is {@link #FOCUS_UP},
2564 * the user may specify which view to go to next.
2565 */
2566 private int mNextFocusUpId = View.NO_ID;
2567
2568 /**
2569 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2570 * the user may specify which view to go to next.
2571 */
2572 private int mNextFocusDownId = View.NO_ID;
2573
Jeff Brown4e6319b2010-12-13 10:36:51 -08002574 /**
2575 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2576 * the user may specify which view to go to next.
2577 */
2578 int mNextFocusForwardId = View.NO_ID;
2579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002581 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002582 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002583 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 private UnsetPressedState mUnsetPressedState;
2586
2587 /**
2588 * Whether the long press's action has been invoked. The tap's action is invoked on the
2589 * up event while a long press is invoked as soon as the long press duration is reached, so
2590 * a long press could be performed before the tap is checked, in which case the tap's action
2591 * should not be invoked.
2592 */
2593 private boolean mHasPerformedLongPress;
2594
2595 /**
2596 * The minimum height of the view. We'll try our best to have the height
2597 * of this view to at least this amount.
2598 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002599 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 private int mMinHeight;
2601
2602 /**
2603 * The minimum width of the view. We'll try our best to have the width
2604 * of this view to at least this amount.
2605 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002606 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 private int mMinWidth;
2608
2609 /**
2610 * The delegate to handle touch events that are physically in this view
2611 * but should be handled by another view.
2612 */
2613 private TouchDelegate mTouchDelegate = null;
2614
2615 /**
2616 * Solid color to use as a background when creating the drawing cache. Enables
2617 * the cache to use 16 bit bitmaps instead of 32 bit.
2618 */
2619 private int mDrawingCacheBackgroundColor = 0;
2620
2621 /**
2622 * Special tree observer used when mAttachInfo is null.
2623 */
2624 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002625
Adam Powelle14579b2009-12-16 18:39:52 -08002626 /**
2627 * Cache the touch slop from the context that created the view.
2628 */
2629 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002632 * Object that handles automatic animation of view properties.
2633 */
2634 private ViewPropertyAnimator mAnimator = null;
2635
2636 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002637 * Flag indicating that a drag can cross window boundaries. When
2638 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2639 * with this flag set, all visible applications will be able to participate
2640 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002641 *
2642 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002643 */
2644 public static final int DRAG_FLAG_GLOBAL = 1;
2645
2646 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002647 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2648 */
2649 private float mVerticalScrollFactor;
2650
2651 /**
Adam Powell20232d02010-12-08 21:08:53 -08002652 * Position of the vertical scroll bar.
2653 */
2654 private int mVerticalScrollbarPosition;
2655
2656 /**
2657 * Position the scroll bar at the default position as determined by the system.
2658 */
2659 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2660
2661 /**
2662 * Position the scroll bar along the left edge.
2663 */
2664 public static final int SCROLLBAR_POSITION_LEFT = 1;
2665
2666 /**
2667 * Position the scroll bar along the right edge.
2668 */
2669 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2670
2671 /**
Romain Guy171c5922011-01-06 10:04:23 -08002672 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002673 *
2674 * @see #getLayerType()
2675 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002676 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002677 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002678 */
2679 public static final int LAYER_TYPE_NONE = 0;
2680
2681 /**
2682 * <p>Indicates that the view has a software layer. A software layer is backed
2683 * by a bitmap and causes the view to be rendered using Android's software
2684 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002685 *
Romain Guy171c5922011-01-06 10:04:23 -08002686 * <p>Software layers have various usages:</p>
2687 * <p>When the application is not using hardware acceleration, a software layer
2688 * is useful to apply a specific color filter and/or blending mode and/or
2689 * translucency to a view and all its children.</p>
2690 * <p>When the application is using hardware acceleration, a software layer
2691 * is useful to render drawing primitives not supported by the hardware
2692 * accelerated pipeline. It can also be used to cache a complex view tree
2693 * into a texture and reduce the complexity of drawing operations. For instance,
2694 * when animating a complex view tree with a translation, a software layer can
2695 * be used to render the view tree only once.</p>
2696 * <p>Software layers should be avoided when the affected view tree updates
2697 * often. Every update will require to re-render the software layer, which can
2698 * potentially be slow (particularly when hardware acceleration is turned on
2699 * since the layer will have to be uploaded into a hardware texture after every
2700 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002701 *
2702 * @see #getLayerType()
2703 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002704 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002705 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002706 */
2707 public static final int LAYER_TYPE_SOFTWARE = 1;
2708
2709 /**
2710 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2711 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2712 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2713 * rendering pipeline, but only if hardware acceleration is turned on for the
2714 * view hierarchy. When hardware acceleration is turned off, hardware layers
2715 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002716 *
Romain Guy171c5922011-01-06 10:04:23 -08002717 * <p>A hardware layer is useful to apply a specific color filter and/or
2718 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002719 * <p>A hardware layer can be used to cache a complex view tree into a
2720 * texture and reduce the complexity of drawing operations. For instance,
2721 * when animating a complex view tree with a translation, a hardware layer can
2722 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002723 * <p>A hardware layer can also be used to increase the rendering quality when
2724 * rotation transformations are applied on a view. It can also be used to
2725 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002726 *
2727 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002728 * @see #setLayerType(int, android.graphics.Paint)
2729 * @see #LAYER_TYPE_NONE
2730 * @see #LAYER_TYPE_SOFTWARE
2731 */
2732 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002733
Romain Guy3aaff3a2011-01-12 14:18:47 -08002734 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2735 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2736 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2737 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2738 })
Romain Guy171c5922011-01-06 10:04:23 -08002739 int mLayerType = LAYER_TYPE_NONE;
2740 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002741 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002742
2743 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002744 * Set to true when the view is sending hover accessibility events because it
2745 * is the innermost hovered view.
2746 */
2747 private boolean mSendingHoverAccessibilityEvents;
2748
2749 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002750 * Delegate for injecting accessiblity functionality.
2751 */
2752 AccessibilityDelegate mAccessibilityDelegate;
2753
2754 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002755 * Consistency verifier for debugging purposes.
2756 * @hide
2757 */
2758 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2759 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2760 new InputEventConsistencyVerifier(this, 0) : null;
2761
2762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 * Simple constructor to use when creating a view from code.
2764 *
2765 * @param context The Context the view is running in, through which it can
2766 * access the current theme, resources, etc.
2767 */
2768 public View(Context context) {
2769 mContext = context;
2770 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07002771 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002772 // Set layout and text direction defaults
2773 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
2774 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08002775 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002776 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002777 mUserPaddingStart = -1;
2778 mUserPaddingEnd = -1;
2779 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
2781
2782 /**
2783 * Constructor that is called when inflating a view from XML. This is called
2784 * when a view is being constructed from an XML file, supplying attributes
2785 * that were specified in the XML file. This version uses a default style of
2786 * 0, so the only attribute values applied are those in the Context's Theme
2787 * and the given AttributeSet.
2788 *
2789 * <p>
2790 * The method onFinishInflate() will be called after all children have been
2791 * added.
2792 *
2793 * @param context The Context the view is running in, through which it can
2794 * access the current theme, resources, etc.
2795 * @param attrs The attributes of the XML tag that is inflating the view.
2796 * @see #View(Context, AttributeSet, int)
2797 */
2798 public View(Context context, AttributeSet attrs) {
2799 this(context, attrs, 0);
2800 }
2801
2802 /**
2803 * Perform inflation from XML and apply a class-specific base style. This
2804 * constructor of View allows subclasses to use their own base style when
2805 * they are inflating. For example, a Button class's constructor would call
2806 * this version of the super class constructor and supply
2807 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2808 * the theme's button style to modify all of the base view attributes (in
2809 * particular its background) as well as the Button class's attributes.
2810 *
2811 * @param context The Context the view is running in, through which it can
2812 * access the current theme, resources, etc.
2813 * @param attrs The attributes of the XML tag that is inflating the view.
2814 * @param defStyle The default style to apply to this view. If 0, no style
2815 * will be applied (beyond what is included in the theme). This may
2816 * either be an attribute resource, whose value will be retrieved
2817 * from the current theme, or an explicit style resource.
2818 * @see #View(Context, AttributeSet)
2819 */
2820 public View(Context context, AttributeSet attrs, int defStyle) {
2821 this(context);
2822
2823 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2824 defStyle, 0);
2825
2826 Drawable background = null;
2827
2828 int leftPadding = -1;
2829 int topPadding = -1;
2830 int rightPadding = -1;
2831 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002832 int startPadding = -1;
2833 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834
2835 int padding = -1;
2836
2837 int viewFlagValues = 0;
2838 int viewFlagMasks = 0;
2839
2840 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 int x = 0;
2843 int y = 0;
2844
Chet Haase73066682010-11-29 15:55:32 -08002845 float tx = 0;
2846 float ty = 0;
2847 float rotation = 0;
2848 float rotationX = 0;
2849 float rotationY = 0;
2850 float sx = 1f;
2851 float sy = 1f;
2852 boolean transformSet = false;
2853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2855
Adam Powell637d3372010-08-25 14:37:03 -07002856 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 final int N = a.getIndexCount();
2858 for (int i = 0; i < N; i++) {
2859 int attr = a.getIndex(i);
2860 switch (attr) {
2861 case com.android.internal.R.styleable.View_background:
2862 background = a.getDrawable(attr);
2863 break;
2864 case com.android.internal.R.styleable.View_padding:
2865 padding = a.getDimensionPixelSize(attr, -1);
2866 break;
2867 case com.android.internal.R.styleable.View_paddingLeft:
2868 leftPadding = a.getDimensionPixelSize(attr, -1);
2869 break;
2870 case com.android.internal.R.styleable.View_paddingTop:
2871 topPadding = a.getDimensionPixelSize(attr, -1);
2872 break;
2873 case com.android.internal.R.styleable.View_paddingRight:
2874 rightPadding = a.getDimensionPixelSize(attr, -1);
2875 break;
2876 case com.android.internal.R.styleable.View_paddingBottom:
2877 bottomPadding = a.getDimensionPixelSize(attr, -1);
2878 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002879 case com.android.internal.R.styleable.View_paddingStart:
2880 startPadding = a.getDimensionPixelSize(attr, -1);
2881 break;
2882 case com.android.internal.R.styleable.View_paddingEnd:
2883 endPadding = a.getDimensionPixelSize(attr, -1);
2884 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 case com.android.internal.R.styleable.View_scrollX:
2886 x = a.getDimensionPixelOffset(attr, 0);
2887 break;
2888 case com.android.internal.R.styleable.View_scrollY:
2889 y = a.getDimensionPixelOffset(attr, 0);
2890 break;
Chet Haase73066682010-11-29 15:55:32 -08002891 case com.android.internal.R.styleable.View_alpha:
2892 setAlpha(a.getFloat(attr, 1f));
2893 break;
2894 case com.android.internal.R.styleable.View_transformPivotX:
2895 setPivotX(a.getDimensionPixelOffset(attr, 0));
2896 break;
2897 case com.android.internal.R.styleable.View_transformPivotY:
2898 setPivotY(a.getDimensionPixelOffset(attr, 0));
2899 break;
2900 case com.android.internal.R.styleable.View_translationX:
2901 tx = a.getDimensionPixelOffset(attr, 0);
2902 transformSet = true;
2903 break;
2904 case com.android.internal.R.styleable.View_translationY:
2905 ty = a.getDimensionPixelOffset(attr, 0);
2906 transformSet = true;
2907 break;
2908 case com.android.internal.R.styleable.View_rotation:
2909 rotation = a.getFloat(attr, 0);
2910 transformSet = true;
2911 break;
2912 case com.android.internal.R.styleable.View_rotationX:
2913 rotationX = a.getFloat(attr, 0);
2914 transformSet = true;
2915 break;
2916 case com.android.internal.R.styleable.View_rotationY:
2917 rotationY = a.getFloat(attr, 0);
2918 transformSet = true;
2919 break;
2920 case com.android.internal.R.styleable.View_scaleX:
2921 sx = a.getFloat(attr, 1f);
2922 transformSet = true;
2923 break;
2924 case com.android.internal.R.styleable.View_scaleY:
2925 sy = a.getFloat(attr, 1f);
2926 transformSet = true;
2927 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 case com.android.internal.R.styleable.View_id:
2929 mID = a.getResourceId(attr, NO_ID);
2930 break;
2931 case com.android.internal.R.styleable.View_tag:
2932 mTag = a.getText(attr);
2933 break;
2934 case com.android.internal.R.styleable.View_fitsSystemWindows:
2935 if (a.getBoolean(attr, false)) {
2936 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2937 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2938 }
2939 break;
2940 case com.android.internal.R.styleable.View_focusable:
2941 if (a.getBoolean(attr, false)) {
2942 viewFlagValues |= FOCUSABLE;
2943 viewFlagMasks |= FOCUSABLE_MASK;
2944 }
2945 break;
2946 case com.android.internal.R.styleable.View_focusableInTouchMode:
2947 if (a.getBoolean(attr, false)) {
2948 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2949 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2950 }
2951 break;
2952 case com.android.internal.R.styleable.View_clickable:
2953 if (a.getBoolean(attr, false)) {
2954 viewFlagValues |= CLICKABLE;
2955 viewFlagMasks |= CLICKABLE;
2956 }
2957 break;
2958 case com.android.internal.R.styleable.View_longClickable:
2959 if (a.getBoolean(attr, false)) {
2960 viewFlagValues |= LONG_CLICKABLE;
2961 viewFlagMasks |= LONG_CLICKABLE;
2962 }
2963 break;
2964 case com.android.internal.R.styleable.View_saveEnabled:
2965 if (!a.getBoolean(attr, true)) {
2966 viewFlagValues |= SAVE_DISABLED;
2967 viewFlagMasks |= SAVE_DISABLED_MASK;
2968 }
2969 break;
2970 case com.android.internal.R.styleable.View_duplicateParentState:
2971 if (a.getBoolean(attr, false)) {
2972 viewFlagValues |= DUPLICATE_PARENT_STATE;
2973 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2974 }
2975 break;
2976 case com.android.internal.R.styleable.View_visibility:
2977 final int visibility = a.getInt(attr, 0);
2978 if (visibility != 0) {
2979 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2980 viewFlagMasks |= VISIBILITY_MASK;
2981 }
2982 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002983 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07002984 // Clear any layout direction flags (included resolved bits) already set
2985 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
2986 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002987 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07002988 final int value = (layoutDirection != -1) ?
2989 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
2990 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002991 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 case com.android.internal.R.styleable.View_drawingCacheQuality:
2993 final int cacheQuality = a.getInt(attr, 0);
2994 if (cacheQuality != 0) {
2995 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2996 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2997 }
2998 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002999 case com.android.internal.R.styleable.View_contentDescription:
3000 mContentDescription = a.getString(attr);
3001 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3003 if (!a.getBoolean(attr, true)) {
3004 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3005 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3006 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003007 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3009 if (!a.getBoolean(attr, true)) {
3010 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3011 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3012 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003013 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 case R.styleable.View_scrollbars:
3015 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3016 if (scrollbars != SCROLLBARS_NONE) {
3017 viewFlagValues |= scrollbars;
3018 viewFlagMasks |= SCROLLBARS_MASK;
3019 initializeScrollbars(a);
3020 }
3021 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003022 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003024 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3025 // Ignore the attribute starting with ICS
3026 break;
3027 }
3028 // With builds < ICS, fall through and apply fading edges
3029 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3031 if (fadingEdge != FADING_EDGE_NONE) {
3032 viewFlagValues |= fadingEdge;
3033 viewFlagMasks |= FADING_EDGE_MASK;
3034 initializeFadingEdge(a);
3035 }
3036 break;
3037 case R.styleable.View_scrollbarStyle:
3038 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3039 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3040 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3041 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3042 }
3043 break;
3044 case R.styleable.View_isScrollContainer:
3045 setScrollContainer = true;
3046 if (a.getBoolean(attr, false)) {
3047 setScrollContainer(true);
3048 }
3049 break;
3050 case com.android.internal.R.styleable.View_keepScreenOn:
3051 if (a.getBoolean(attr, false)) {
3052 viewFlagValues |= KEEP_SCREEN_ON;
3053 viewFlagMasks |= KEEP_SCREEN_ON;
3054 }
3055 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003056 case R.styleable.View_filterTouchesWhenObscured:
3057 if (a.getBoolean(attr, false)) {
3058 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3059 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3060 }
3061 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 case R.styleable.View_nextFocusLeft:
3063 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3064 break;
3065 case R.styleable.View_nextFocusRight:
3066 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3067 break;
3068 case R.styleable.View_nextFocusUp:
3069 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3070 break;
3071 case R.styleable.View_nextFocusDown:
3072 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3073 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003074 case R.styleable.View_nextFocusForward:
3075 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3076 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 case R.styleable.View_minWidth:
3078 mMinWidth = a.getDimensionPixelSize(attr, 0);
3079 break;
3080 case R.styleable.View_minHeight:
3081 mMinHeight = a.getDimensionPixelSize(attr, 0);
3082 break;
Romain Guy9a817362009-05-01 10:57:14 -07003083 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003084 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003085 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003086 + "be used within a restricted context");
3087 }
3088
Romain Guy9a817362009-05-01 10:57:14 -07003089 final String handlerName = a.getString(attr);
3090 if (handlerName != null) {
3091 setOnClickListener(new OnClickListener() {
3092 private Method mHandler;
3093
3094 public void onClick(View v) {
3095 if (mHandler == null) {
3096 try {
3097 mHandler = getContext().getClass().getMethod(handlerName,
3098 View.class);
3099 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003100 int id = getId();
3101 String idText = id == NO_ID ? "" : " with id '"
3102 + getContext().getResources().getResourceEntryName(
3103 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003104 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003105 handlerName + "(View) in the activity "
3106 + getContext().getClass() + " for onClick handler"
3107 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003108 }
3109 }
3110
3111 try {
3112 mHandler.invoke(getContext(), View.this);
3113 } catch (IllegalAccessException e) {
3114 throw new IllegalStateException("Could not execute non "
3115 + "public method of the activity", e);
3116 } catch (InvocationTargetException e) {
3117 throw new IllegalStateException("Could not execute "
3118 + "method of the activity", e);
3119 }
3120 }
3121 });
3122 }
3123 break;
Adam Powell637d3372010-08-25 14:37:03 -07003124 case R.styleable.View_overScrollMode:
3125 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3126 break;
Adam Powell20232d02010-12-08 21:08:53 -08003127 case R.styleable.View_verticalScrollbarPosition:
3128 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3129 break;
Romain Guy171c5922011-01-06 10:04:23 -08003130 case R.styleable.View_layerType:
3131 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3132 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003133 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003134 // Clear any text direction flag already set
3135 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3136 // Set the text direction flags depending on the value of the attribute
3137 final int textDirection = a.getInt(attr, -1);
3138 if (textDirection != -1) {
3139 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3140 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003141 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 }
3143 }
3144
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003145 a.recycle();
3146
Adam Powell637d3372010-08-25 14:37:03 -07003147 setOverScrollMode(overScrollMode);
3148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 if (background != null) {
3150 setBackgroundDrawable(background);
3151 }
3152
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003153 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3154 // layout direction). Those cached values will be used later during padding resolution.
3155 mUserPaddingStart = startPadding;
3156 mUserPaddingEnd = endPadding;
3157
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003158 updateUserPaddingRelative();
3159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 if (padding >= 0) {
3161 leftPadding = padding;
3162 topPadding = padding;
3163 rightPadding = padding;
3164 bottomPadding = padding;
3165 }
3166
3167 // If the user specified the padding (either with android:padding or
3168 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3169 // use the default padding or the padding from the background drawable
3170 // (stored at this point in mPadding*)
3171 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3172 topPadding >= 0 ? topPadding : mPaddingTop,
3173 rightPadding >= 0 ? rightPadding : mPaddingRight,
3174 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3175
3176 if (viewFlagMasks != 0) {
3177 setFlags(viewFlagValues, viewFlagMasks);
3178 }
3179
3180 // Needs to be called after mViewFlags is set
3181 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3182 recomputePadding();
3183 }
3184
3185 if (x != 0 || y != 0) {
3186 scrollTo(x, y);
3187 }
3188
Chet Haase73066682010-11-29 15:55:32 -08003189 if (transformSet) {
3190 setTranslationX(tx);
3191 setTranslationY(ty);
3192 setRotation(rotation);
3193 setRotationX(rotationX);
3194 setRotationY(rotationY);
3195 setScaleX(sx);
3196 setScaleY(sy);
3197 }
3198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3200 setScrollContainer(true);
3201 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003202
3203 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 }
3205
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003206 private void updateUserPaddingRelative() {
3207 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3208 }
3209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 /**
3211 * Non-public constructor for use in testing
3212 */
3213 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003214 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 }
3216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 /**
3218 * <p>
3219 * Initializes the fading edges from a given set of styled attributes. This
3220 * method should be called by subclasses that need fading edges and when an
3221 * instance of these subclasses is created programmatically rather than
3222 * being inflated from XML. This method is automatically called when the XML
3223 * is inflated.
3224 * </p>
3225 *
3226 * @param a the styled attributes set to initialize the fading edges from
3227 */
3228 protected void initializeFadingEdge(TypedArray a) {
3229 initScrollCache();
3230
3231 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3232 R.styleable.View_fadingEdgeLength,
3233 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3234 }
3235
3236 /**
3237 * Returns the size of the vertical faded edges used to indicate that more
3238 * content in this view is visible.
3239 *
3240 * @return The size in pixels of the vertical faded edge or 0 if vertical
3241 * faded edges are not enabled for this view.
3242 * @attr ref android.R.styleable#View_fadingEdgeLength
3243 */
3244 public int getVerticalFadingEdgeLength() {
3245 if (isVerticalFadingEdgeEnabled()) {
3246 ScrollabilityCache cache = mScrollCache;
3247 if (cache != null) {
3248 return cache.fadingEdgeLength;
3249 }
3250 }
3251 return 0;
3252 }
3253
3254 /**
3255 * Set the size of the faded edge used to indicate that more content in this
3256 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003257 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3258 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3259 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 *
3261 * @param length The size in pixels of the faded edge used to indicate that more
3262 * content in this view is visible.
3263 */
3264 public void setFadingEdgeLength(int length) {
3265 initScrollCache();
3266 mScrollCache.fadingEdgeLength = length;
3267 }
3268
3269 /**
3270 * Returns the size of the horizontal faded edges used to indicate that more
3271 * content in this view is visible.
3272 *
3273 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3274 * faded edges are not enabled for this view.
3275 * @attr ref android.R.styleable#View_fadingEdgeLength
3276 */
3277 public int getHorizontalFadingEdgeLength() {
3278 if (isHorizontalFadingEdgeEnabled()) {
3279 ScrollabilityCache cache = mScrollCache;
3280 if (cache != null) {
3281 return cache.fadingEdgeLength;
3282 }
3283 }
3284 return 0;
3285 }
3286
3287 /**
3288 * Returns the width of the vertical scrollbar.
3289 *
3290 * @return The width in pixels of the vertical scrollbar or 0 if there
3291 * is no vertical scrollbar.
3292 */
3293 public int getVerticalScrollbarWidth() {
3294 ScrollabilityCache cache = mScrollCache;
3295 if (cache != null) {
3296 ScrollBarDrawable scrollBar = cache.scrollBar;
3297 if (scrollBar != null) {
3298 int size = scrollBar.getSize(true);
3299 if (size <= 0) {
3300 size = cache.scrollBarSize;
3301 }
3302 return size;
3303 }
3304 return 0;
3305 }
3306 return 0;
3307 }
3308
3309 /**
3310 * Returns the height of the horizontal scrollbar.
3311 *
3312 * @return The height in pixels of the horizontal scrollbar or 0 if
3313 * there is no horizontal scrollbar.
3314 */
3315 protected int getHorizontalScrollbarHeight() {
3316 ScrollabilityCache cache = mScrollCache;
3317 if (cache != null) {
3318 ScrollBarDrawable scrollBar = cache.scrollBar;
3319 if (scrollBar != null) {
3320 int size = scrollBar.getSize(false);
3321 if (size <= 0) {
3322 size = cache.scrollBarSize;
3323 }
3324 return size;
3325 }
3326 return 0;
3327 }
3328 return 0;
3329 }
3330
3331 /**
3332 * <p>
3333 * Initializes the scrollbars from a given set of styled attributes. This
3334 * method should be called by subclasses that need scrollbars and when an
3335 * instance of these subclasses is created programmatically rather than
3336 * being inflated from XML. This method is automatically called when the XML
3337 * is inflated.
3338 * </p>
3339 *
3340 * @param a the styled attributes set to initialize the scrollbars from
3341 */
3342 protected void initializeScrollbars(TypedArray a) {
3343 initScrollCache();
3344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003346
Mike Cleronf116bf82009-09-27 19:14:12 -07003347 if (scrollabilityCache.scrollBar == null) {
3348 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3349 }
Joe Malin32736f02011-01-19 16:14:20 -08003350
Romain Guy8bda2482010-03-02 11:42:11 -08003351 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352
Mike Cleronf116bf82009-09-27 19:14:12 -07003353 if (!fadeScrollbars) {
3354 scrollabilityCache.state = ScrollabilityCache.ON;
3355 }
3356 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003357
3358
Mike Cleronf116bf82009-09-27 19:14:12 -07003359 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3360 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3361 .getScrollBarFadeDuration());
3362 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3363 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3364 ViewConfiguration.getScrollDefaultDelay());
3365
Joe Malin32736f02011-01-19 16:14:20 -08003366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3368 com.android.internal.R.styleable.View_scrollbarSize,
3369 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3370
3371 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3372 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3373
3374 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3375 if (thumb != null) {
3376 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3377 }
3378
3379 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3380 false);
3381 if (alwaysDraw) {
3382 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3383 }
3384
3385 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3386 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3387
3388 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3389 if (thumb != null) {
3390 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3391 }
3392
3393 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3394 false);
3395 if (alwaysDraw) {
3396 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3397 }
3398
3399 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003400 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 }
3402
3403 /**
3404 * <p>
3405 * Initalizes the scrollability cache if necessary.
3406 * </p>
3407 */
3408 private void initScrollCache() {
3409 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003410 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 }
3412 }
3413
3414 /**
Adam Powell20232d02010-12-08 21:08:53 -08003415 * Set the position of the vertical scroll bar. Should be one of
3416 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3417 * {@link #SCROLLBAR_POSITION_RIGHT}.
3418 *
3419 * @param position Where the vertical scroll bar should be positioned.
3420 */
3421 public void setVerticalScrollbarPosition(int position) {
3422 if (mVerticalScrollbarPosition != position) {
3423 mVerticalScrollbarPosition = position;
3424 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003425 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003426 }
3427 }
3428
3429 /**
3430 * @return The position where the vertical scroll bar will show, if applicable.
3431 * @see #setVerticalScrollbarPosition(int)
3432 */
3433 public int getVerticalScrollbarPosition() {
3434 return mVerticalScrollbarPosition;
3435 }
3436
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003437 ListenerInfo getListenerInfo() {
3438 if (mListenerInfo != null) {
3439 return mListenerInfo;
3440 }
3441 mListenerInfo = new ListenerInfo();
3442 return mListenerInfo;
3443 }
3444
Adam Powell20232d02010-12-08 21:08:53 -08003445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 * Register a callback to be invoked when focus of this view changed.
3447 *
3448 * @param l The callback that will run.
3449 */
3450 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003451 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 }
3453
3454 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003455 * Add a listener that will be called when the bounds of the view change due to
3456 * layout processing.
3457 *
3458 * @param listener The listener that will be called when layout bounds change.
3459 */
3460 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003461 ListenerInfo li = getListenerInfo();
3462 if (li.mOnLayoutChangeListeners == null) {
3463 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003464 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003465 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3466 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003467 }
Chet Haase21cd1382010-09-01 17:42:29 -07003468 }
3469
3470 /**
3471 * Remove a listener for layout changes.
3472 *
3473 * @param listener The listener for layout bounds change.
3474 */
3475 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003476 ListenerInfo li = mListenerInfo;
3477 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003478 return;
3479 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003480 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003481 }
3482
3483 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003484 * Add a listener for attach state changes.
3485 *
3486 * This listener will be called whenever this view is attached or detached
3487 * from a window. Remove the listener using
3488 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3489 *
3490 * @param listener Listener to attach
3491 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3492 */
3493 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003494 ListenerInfo li = getListenerInfo();
3495 if (li.mOnAttachStateChangeListeners == null) {
3496 li.mOnAttachStateChangeListeners
3497 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003498 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003499 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003500 }
3501
3502 /**
3503 * Remove a listener for attach state changes. The listener will receive no further
3504 * notification of window attach/detach events.
3505 *
3506 * @param listener Listener to remove
3507 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3508 */
3509 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003510 ListenerInfo li = mListenerInfo;
3511 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003512 return;
3513 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003514 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003515 }
3516
3517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 * Returns the focus-change callback registered for this view.
3519 *
3520 * @return The callback, or null if one is not registered.
3521 */
3522 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003523 ListenerInfo li = mListenerInfo;
3524 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 }
3526
3527 /**
3528 * Register a callback to be invoked when this view is clicked. If this view is not
3529 * clickable, it becomes clickable.
3530 *
3531 * @param l The callback that will run
3532 *
3533 * @see #setClickable(boolean)
3534 */
3535 public void setOnClickListener(OnClickListener l) {
3536 if (!isClickable()) {
3537 setClickable(true);
3538 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003539 getListenerInfo().mOnClickListener = l;
3540 }
3541
3542 /**
3543 * Return whether this view has an attached OnClickListener. Returns
3544 * true if there is a listener, false if there is none.
3545 */
3546 public boolean hasOnClickListeners() {
3547 ListenerInfo li = mListenerInfo;
3548 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 }
3550
3551 /**
3552 * Register a callback to be invoked when this view is clicked and held. If this view is not
3553 * long clickable, it becomes long clickable.
3554 *
3555 * @param l The callback that will run
3556 *
3557 * @see #setLongClickable(boolean)
3558 */
3559 public void setOnLongClickListener(OnLongClickListener l) {
3560 if (!isLongClickable()) {
3561 setLongClickable(true);
3562 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003563 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 }
3565
3566 /**
3567 * Register a callback to be invoked when the context menu for this view is
3568 * being built. If this view is not long clickable, it becomes long clickable.
3569 *
3570 * @param l The callback that will run
3571 *
3572 */
3573 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3574 if (!isLongClickable()) {
3575 setLongClickable(true);
3576 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003577 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 }
3579
3580 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003581 * Call this view's OnClickListener, if it is defined. Performs all normal
3582 * actions associated with clicking: reporting accessibility event, playing
3583 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 *
3585 * @return True there was an assigned OnClickListener that was called, false
3586 * otherwise is returned.
3587 */
3588 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003589 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3590
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003591 ListenerInfo li = mListenerInfo;
3592 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003594 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 return true;
3596 }
3597
3598 return false;
3599 }
3600
3601 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003602 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3603 * this only calls the listener, and does not do any associated clicking
3604 * actions like reporting an accessibility event.
3605 *
3606 * @return True there was an assigned OnClickListener that was called, false
3607 * otherwise is returned.
3608 */
3609 public boolean callOnClick() {
3610 ListenerInfo li = mListenerInfo;
3611 if (li != null && li.mOnClickListener != null) {
3612 li.mOnClickListener.onClick(this);
3613 return true;
3614 }
3615 return false;
3616 }
3617
3618 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003619 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3620 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003622 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 */
3624 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003625 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003628 ListenerInfo li = mListenerInfo;
3629 if (li != null && li.mOnLongClickListener != null) {
3630 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 }
3632 if (!handled) {
3633 handled = showContextMenu();
3634 }
3635 if (handled) {
3636 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3637 }
3638 return handled;
3639 }
3640
3641 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003642 * Performs button-related actions during a touch down event.
3643 *
3644 * @param event The event.
3645 * @return True if the down was consumed.
3646 *
3647 * @hide
3648 */
3649 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3650 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3651 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3652 return true;
3653 }
3654 }
3655 return false;
3656 }
3657
3658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 * Bring up the context menu for this view.
3660 *
3661 * @return Whether a context menu was displayed.
3662 */
3663 public boolean showContextMenu() {
3664 return getParent().showContextMenuForChild(this);
3665 }
3666
3667 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003668 * Bring up the context menu for this view, referring to the item under the specified point.
3669 *
3670 * @param x The referenced x coordinate.
3671 * @param y The referenced y coordinate.
3672 * @param metaState The keyboard modifiers that were pressed.
3673 * @return Whether a context menu was displayed.
3674 *
3675 * @hide
3676 */
3677 public boolean showContextMenu(float x, float y, int metaState) {
3678 return showContextMenu();
3679 }
3680
3681 /**
Adam Powell6e346362010-07-23 10:18:23 -07003682 * Start an action mode.
3683 *
3684 * @param callback Callback that will control the lifecycle of the action mode
3685 * @return The new action mode if it is started, null otherwise
3686 *
3687 * @see ActionMode
3688 */
3689 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003690 ViewParent parent = getParent();
3691 if (parent == null) return null;
3692 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003693 }
3694
3695 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 * Register a callback to be invoked when a key is pressed in this view.
3697 * @param l the key listener to attach to this view
3698 */
3699 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003700 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 }
3702
3703 /**
3704 * Register a callback to be invoked when a touch event is sent to this view.
3705 * @param l the touch listener to attach to this view
3706 */
3707 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003708 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 }
3710
3711 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003712 * Register a callback to be invoked when a generic motion event is sent to this view.
3713 * @param l the generic motion listener to attach to this view
3714 */
3715 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003716 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003717 }
3718
3719 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003720 * Register a callback to be invoked when a hover event is sent to this view.
3721 * @param l the hover listener to attach to this view
3722 */
3723 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003724 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003725 }
3726
3727 /**
Joe Malin32736f02011-01-19 16:14:20 -08003728 * Register a drag event listener callback object for this View. The parameter is
3729 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3730 * View, the system calls the
3731 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3732 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003733 */
3734 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003735 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003736 }
3737
3738 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003739 * Give this view focus. This will cause
3740 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 *
3742 * Note: this does not check whether this {@link View} should get focus, it just
3743 * gives it focus no matter what. It should only be called internally by framework
3744 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3745 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003746 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3747 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 * focus moved when requestFocus() is called. It may not always
3749 * apply, in which case use the default View.FOCUS_DOWN.
3750 * @param previouslyFocusedRect The rectangle of the view that had focus
3751 * prior in this View's coordinate system.
3752 */
3753 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3754 if (DBG) {
3755 System.out.println(this + " requestFocus()");
3756 }
3757
3758 if ((mPrivateFlags & FOCUSED) == 0) {
3759 mPrivateFlags |= FOCUSED;
3760
3761 if (mParent != null) {
3762 mParent.requestChildFocus(this, this);
3763 }
3764
3765 onFocusChanged(true, direction, previouslyFocusedRect);
3766 refreshDrawableState();
3767 }
3768 }
3769
3770 /**
3771 * Request that a rectangle of this view be visible on the screen,
3772 * scrolling if necessary just enough.
3773 *
3774 * <p>A View should call this if it maintains some notion of which part
3775 * of its content is interesting. For example, a text editing view
3776 * should call this when its cursor moves.
3777 *
3778 * @param rectangle The rectangle.
3779 * @return Whether any parent scrolled.
3780 */
3781 public boolean requestRectangleOnScreen(Rect rectangle) {
3782 return requestRectangleOnScreen(rectangle, false);
3783 }
3784
3785 /**
3786 * Request that a rectangle of this view be visible on the screen,
3787 * scrolling if necessary just enough.
3788 *
3789 * <p>A View should call this if it maintains some notion of which part
3790 * of its content is interesting. For example, a text editing view
3791 * should call this when its cursor moves.
3792 *
3793 * <p>When <code>immediate</code> is set to true, scrolling will not be
3794 * animated.
3795 *
3796 * @param rectangle The rectangle.
3797 * @param immediate True to forbid animated scrolling, false otherwise
3798 * @return Whether any parent scrolled.
3799 */
3800 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3801 View child = this;
3802 ViewParent parent = mParent;
3803 boolean scrolled = false;
3804 while (parent != null) {
3805 scrolled |= parent.requestChildRectangleOnScreen(child,
3806 rectangle, immediate);
3807
3808 // offset rect so next call has the rectangle in the
3809 // coordinate system of its direct child.
3810 rectangle.offset(child.getLeft(), child.getTop());
3811 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3812
3813 if (!(parent instanceof View)) {
3814 break;
3815 }
Romain Guy8506ab42009-06-11 17:35:47 -07003816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 child = (View) parent;
3818 parent = child.getParent();
3819 }
3820 return scrolled;
3821 }
3822
3823 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003824 * Called when this view wants to give up focus. If focus is cleared
3825 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3826 * <p>
3827 * <strong>Note:</strong> When a View clears focus the framework is trying
3828 * to give focus to the first focusable View from the top. Hence, if this
3829 * View is the first from the top that can take focus, then its focus will
3830 * not be cleared nor will the focus change callback be invoked.
3831 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 */
3833 public void clearFocus() {
3834 if (DBG) {
3835 System.out.println(this + " clearFocus()");
3836 }
3837
3838 if ((mPrivateFlags & FOCUSED) != 0) {
3839 mPrivateFlags &= ~FOCUSED;
3840
3841 if (mParent != null) {
3842 mParent.clearChildFocus(this);
3843 }
3844
3845 onFocusChanged(false, 0, null);
3846 refreshDrawableState();
3847 }
3848 }
3849
3850 /**
3851 * Called to clear the focus of a view that is about to be removed.
3852 * Doesn't call clearChildFocus, which prevents this view from taking
3853 * focus again before it has been removed from the parent
3854 */
3855 void clearFocusForRemoval() {
3856 if ((mPrivateFlags & FOCUSED) != 0) {
3857 mPrivateFlags &= ~FOCUSED;
3858
3859 onFocusChanged(false, 0, null);
3860 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003861
3862 // The view cleared focus and invoked the callbacks, so now is the
3863 // time to give focus to the the first focusable from the top to
3864 // ensure that the gain focus is announced after clear focus.
3865 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 }
3867 }
3868
3869 /**
3870 * Called internally by the view system when a new view is getting focus.
3871 * This is what clears the old focus.
3872 */
3873 void unFocus() {
3874 if (DBG) {
3875 System.out.println(this + " unFocus()");
3876 }
3877
3878 if ((mPrivateFlags & FOCUSED) != 0) {
3879 mPrivateFlags &= ~FOCUSED;
3880
3881 onFocusChanged(false, 0, null);
3882 refreshDrawableState();
3883 }
3884 }
3885
3886 /**
3887 * Returns true if this view has focus iteself, or is the ancestor of the
3888 * view that has focus.
3889 *
3890 * @return True if this view has or contains focus, false otherwise.
3891 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003892 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 public boolean hasFocus() {
3894 return (mPrivateFlags & FOCUSED) != 0;
3895 }
3896
3897 /**
3898 * Returns true if this view is focusable or if it contains a reachable View
3899 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3900 * is a View whose parents do not block descendants focus.
3901 *
3902 * Only {@link #VISIBLE} views are considered focusable.
3903 *
3904 * @return True if the view is focusable or if the view contains a focusable
3905 * View, false otherwise.
3906 *
3907 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3908 */
3909 public boolean hasFocusable() {
3910 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3911 }
3912
3913 /**
3914 * Called by the view system when the focus state of this view changes.
3915 * When the focus change event is caused by directional navigation, direction
3916 * and previouslyFocusedRect provide insight into where the focus is coming from.
3917 * When overriding, be sure to call up through to the super class so that
3918 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003919 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 * @param gainFocus True if the View has focus; false otherwise.
3921 * @param direction The direction focus has moved when requestFocus()
3922 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003923 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3924 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3925 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3927 * system, of the previously focused view. If applicable, this will be
3928 * passed in as finer grained information about where the focus is coming
3929 * from (in addition to direction). Will be <code>null</code> otherwise.
3930 */
3931 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003932 if (gainFocus) {
3933 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3934 }
3935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 InputMethodManager imm = InputMethodManager.peekInstance();
3937 if (!gainFocus) {
3938 if (isPressed()) {
3939 setPressed(false);
3940 }
3941 if (imm != null && mAttachInfo != null
3942 && mAttachInfo.mHasWindowFocus) {
3943 imm.focusOut(this);
3944 }
Romain Guya2431d02009-04-30 16:30:00 -07003945 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 } else if (imm != null && mAttachInfo != null
3947 && mAttachInfo.mHasWindowFocus) {
3948 imm.focusIn(this);
3949 }
Romain Guy8506ab42009-06-11 17:35:47 -07003950
Romain Guy0fd89bf2011-01-26 15:41:30 -08003951 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003952 ListenerInfo li = mListenerInfo;
3953 if (li != null && li.mOnFocusChangeListener != null) {
3954 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 }
Joe Malin32736f02011-01-19 16:14:20 -08003956
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003957 if (mAttachInfo != null) {
3958 mAttachInfo.mKeyDispatchState.reset(this);
3959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 }
3961
3962 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003963 * Sends an accessibility event of the given type. If accessiiblity is
3964 * not enabled this method has no effect. The default implementation calls
3965 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3966 * to populate information about the event source (this View), then calls
3967 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3968 * populate the text content of the event source including its descendants,
3969 * and last calls
3970 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3971 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003972 * <p>
3973 * If an {@link AccessibilityDelegate} has been specified via calling
3974 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3975 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3976 * responsible for handling this call.
3977 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003978 *
Scott Mainb303d832011-10-12 16:45:18 -07003979 * @param eventType The type of the event to send, as defined by several types from
3980 * {@link android.view.accessibility.AccessibilityEvent}, such as
3981 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3982 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003983 *
3984 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3985 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3986 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003987 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003988 */
3989 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003990 if (mAccessibilityDelegate != null) {
3991 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3992 } else {
3993 sendAccessibilityEventInternal(eventType);
3994 }
3995 }
3996
3997 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08003998 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
3999 * {@link AccessibilityEvent} to make an announcement which is related to some
4000 * sort of a context change for which none of the events representing UI transitions
4001 * is a good fit. For example, announcing a new page in a book. If accessibility
4002 * is not enabled this method does nothing.
4003 *
4004 * @param text The announcement text.
4005 */
4006 public void announceForAccessibility(CharSequence text) {
4007 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4008 AccessibilityEvent event = AccessibilityEvent.obtain(
4009 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4010 event.getText().add(text);
4011 sendAccessibilityEventUnchecked(event);
4012 }
4013 }
4014
4015 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004016 * @see #sendAccessibilityEvent(int)
4017 *
4018 * Note: Called from the default {@link AccessibilityDelegate}.
4019 */
4020 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004021 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4022 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4023 }
4024 }
4025
4026 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004027 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4028 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004029 * perform a check whether accessibility is enabled.
4030 * <p>
4031 * If an {@link AccessibilityDelegate} has been specified via calling
4032 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4033 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4034 * is responsible for handling this call.
4035 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004036 *
4037 * @param event The event to send.
4038 *
4039 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004040 */
4041 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004042 if (mAccessibilityDelegate != null) {
4043 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4044 } else {
4045 sendAccessibilityEventUncheckedInternal(event);
4046 }
4047 }
4048
4049 /**
4050 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4051 *
4052 * Note: Called from the default {@link AccessibilityDelegate}.
4053 */
4054 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004055 if (!isShown()) {
4056 return;
4057 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004058 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004059 // Only a subset of accessibility events populates text content.
4060 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4061 dispatchPopulateAccessibilityEvent(event);
4062 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004063 // In the beginning we called #isShown(), so we know that getParent() is not null.
4064 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004065 }
4066
4067 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004068 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4069 * to its children for adding their text content to the event. Note that the
4070 * event text is populated in a separate dispatch path since we add to the
4071 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004072 * A typical implementation will call
4073 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4074 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4075 * on each child. Override this method if custom population of the event text
4076 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004077 * <p>
4078 * If an {@link AccessibilityDelegate} has been specified via calling
4079 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4080 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4081 * is responsible for handling this call.
4082 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004083 * <p>
4084 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4085 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4086 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004087 *
4088 * @param event The event.
4089 *
4090 * @return True if the event population was completed.
4091 */
4092 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004093 if (mAccessibilityDelegate != null) {
4094 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4095 } else {
4096 return dispatchPopulateAccessibilityEventInternal(event);
4097 }
4098 }
4099
4100 /**
4101 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4102 *
4103 * Note: Called from the default {@link AccessibilityDelegate}.
4104 */
4105 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004106 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004107 return false;
4108 }
4109
4110 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004111 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004112 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004113 * text content. While this method is free to modify event
4114 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004115 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4116 * <p>
4117 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004118 * to the text added by the super implementation:
4119 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004120 * super.onPopulateAccessibilityEvent(event);
4121 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4122 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4123 * mCurrentDate.getTimeInMillis(), flags);
4124 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004125 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004126 * <p>
4127 * If an {@link AccessibilityDelegate} has been specified via calling
4128 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4129 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4130 * is responsible for handling this call.
4131 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004132 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4133 * information to the event, in case the default implementation has basic information to add.
4134 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004135 *
4136 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004137 *
4138 * @see #sendAccessibilityEvent(int)
4139 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004140 */
4141 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004142 if (mAccessibilityDelegate != null) {
4143 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4144 } else {
4145 onPopulateAccessibilityEventInternal(event);
4146 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004147 }
4148
4149 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004150 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4151 *
4152 * Note: Called from the default {@link AccessibilityDelegate}.
4153 */
4154 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4155
4156 }
4157
4158 /**
4159 * Initializes an {@link AccessibilityEvent} with information about
4160 * this View which is the event source. In other words, the source of
4161 * an accessibility event is the view whose state change triggered firing
4162 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004163 * <p>
4164 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004165 * to properties set by the super implementation:
4166 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4167 * super.onInitializeAccessibilityEvent(event);
4168 * event.setPassword(true);
4169 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004170 * <p>
4171 * If an {@link AccessibilityDelegate} has been specified via calling
4172 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4173 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4174 * is responsible for handling this call.
4175 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004176 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4177 * information to the event, in case the default implementation has basic information to add.
4178 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004179 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004180 *
4181 * @see #sendAccessibilityEvent(int)
4182 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4183 */
4184 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004185 if (mAccessibilityDelegate != null) {
4186 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4187 } else {
4188 onInitializeAccessibilityEventInternal(event);
4189 }
4190 }
4191
4192 /**
4193 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4194 *
4195 * Note: Called from the default {@link AccessibilityDelegate}.
4196 */
4197 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004198 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004199 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004200 event.setPackageName(getContext().getPackageName());
4201 event.setEnabled(isEnabled());
4202 event.setContentDescription(mContentDescription);
4203
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004204 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4205 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4206 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4207 FOCUSABLES_ALL);
4208 event.setItemCount(focusablesTempList.size());
4209 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4210 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004211 }
4212 }
4213
4214 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004215 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4216 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4217 * This method is responsible for obtaining an accessibility node info from a
4218 * pool of reusable instances and calling
4219 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4220 * initialize the former.
4221 * <p>
4222 * Note: The client is responsible for recycling the obtained instance by calling
4223 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4224 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004225 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004226 * @return A populated {@link AccessibilityNodeInfo}.
4227 *
4228 * @see AccessibilityNodeInfo
4229 */
4230 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004231 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4232 if (provider != null) {
4233 return provider.createAccessibilityNodeInfo(View.NO_ID);
4234 } else {
4235 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4236 onInitializeAccessibilityNodeInfo(info);
4237 return info;
4238 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004239 }
4240
4241 /**
4242 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4243 * The base implementation sets:
4244 * <ul>
4245 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004246 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4247 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004248 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4249 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4250 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4251 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4252 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4253 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4254 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4255 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4256 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4257 * </ul>
4258 * <p>
4259 * Subclasses should override this method, call the super implementation,
4260 * and set additional attributes.
4261 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004262 * <p>
4263 * If an {@link AccessibilityDelegate} has been specified via calling
4264 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4265 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4266 * is responsible for handling this call.
4267 * </p>
4268 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004269 * @param info The instance to initialize.
4270 */
4271 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004272 if (mAccessibilityDelegate != null) {
4273 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4274 } else {
4275 onInitializeAccessibilityNodeInfoInternal(info);
4276 }
4277 }
4278
4279 /**
4280 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4281 *
4282 * Note: Called from the default {@link AccessibilityDelegate}.
4283 */
4284 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004285 Rect bounds = mAttachInfo.mTmpInvalRect;
4286 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004287 info.setBoundsInParent(bounds);
4288
4289 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4290 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004291 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004292 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4293 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004294
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004295 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4296 ViewParent parent = getParent();
4297 if (parent instanceof View) {
4298 View parentView = (View) parent;
4299 info.setParent(parentView);
4300 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004301 }
4302
4303 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004304 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004305 info.setContentDescription(getContentDescription());
4306
4307 info.setEnabled(isEnabled());
4308 info.setClickable(isClickable());
4309 info.setFocusable(isFocusable());
4310 info.setFocused(isFocused());
4311 info.setSelected(isSelected());
4312 info.setLongClickable(isLongClickable());
4313
4314 // TODO: These make sense only if we are in an AdapterView but all
4315 // views can be selected. Maybe from accessiiblity perspective
4316 // we should report as selectable view in an AdapterView.
4317 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4318 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4319
4320 if (isFocusable()) {
4321 if (isFocused()) {
4322 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4323 } else {
4324 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4325 }
4326 }
4327 }
4328
4329 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004330 * Sets a delegate for implementing accessibility support via compositon as
4331 * opposed to inheritance. The delegate's primary use is for implementing
4332 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4333 *
4334 * @param delegate The delegate instance.
4335 *
4336 * @see AccessibilityDelegate
4337 */
4338 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4339 mAccessibilityDelegate = delegate;
4340 }
4341
4342 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004343 * Gets the provider for managing a virtual view hierarchy rooted at this View
4344 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4345 * that explore the window content.
4346 * <p>
4347 * If this method returns an instance, this instance is responsible for managing
4348 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4349 * View including the one representing the View itself. Similarly the returned
4350 * instance is responsible for performing accessibility actions on any virtual
4351 * view or the root view itself.
4352 * </p>
4353 * <p>
4354 * If an {@link AccessibilityDelegate} has been specified via calling
4355 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4356 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4357 * is responsible for handling this call.
4358 * </p>
4359 *
4360 * @return The provider.
4361 *
4362 * @see AccessibilityNodeProvider
4363 */
4364 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4365 if (mAccessibilityDelegate != null) {
4366 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4367 } else {
4368 return null;
4369 }
4370 }
4371
4372 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004373 * Gets the unique identifier of this view on the screen for accessibility purposes.
4374 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4375 *
4376 * @return The view accessibility id.
4377 *
4378 * @hide
4379 */
4380 public int getAccessibilityViewId() {
4381 if (mAccessibilityViewId == NO_ID) {
4382 mAccessibilityViewId = sNextAccessibilityViewId++;
4383 }
4384 return mAccessibilityViewId;
4385 }
4386
4387 /**
4388 * Gets the unique identifier of the window in which this View reseides.
4389 *
4390 * @return The window accessibility id.
4391 *
4392 * @hide
4393 */
4394 public int getAccessibilityWindowId() {
4395 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4396 }
4397
4398 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004399 * Gets the {@link View} description. It briefly describes the view and is
4400 * primarily used for accessibility support. Set this property to enable
4401 * better accessibility support for your application. This is especially
4402 * true for views that do not have textual representation (For example,
4403 * ImageButton).
4404 *
4405 * @return The content descriptiopn.
4406 *
4407 * @attr ref android.R.styleable#View_contentDescription
4408 */
4409 public CharSequence getContentDescription() {
4410 return mContentDescription;
4411 }
4412
4413 /**
4414 * Sets the {@link View} description. It briefly describes the view and is
4415 * primarily used for accessibility support. Set this property to enable
4416 * better accessibility support for your application. This is especially
4417 * true for views that do not have textual representation (For example,
4418 * ImageButton).
4419 *
4420 * @param contentDescription The content description.
4421 *
4422 * @attr ref android.R.styleable#View_contentDescription
4423 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004424 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004425 public void setContentDescription(CharSequence contentDescription) {
4426 mContentDescription = contentDescription;
4427 }
4428
4429 /**
Romain Guya2431d02009-04-30 16:30:00 -07004430 * Invoked whenever this view loses focus, either by losing window focus or by losing
4431 * focus within its window. This method can be used to clear any state tied to the
4432 * focus. For instance, if a button is held pressed with the trackball and the window
4433 * loses focus, this method can be used to cancel the press.
4434 *
4435 * Subclasses of View overriding this method should always call super.onFocusLost().
4436 *
4437 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004438 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004439 *
4440 * @hide pending API council approval
4441 */
4442 protected void onFocusLost() {
4443 resetPressedState();
4444 }
4445
4446 private void resetPressedState() {
4447 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4448 return;
4449 }
4450
4451 if (isPressed()) {
4452 setPressed(false);
4453
4454 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004455 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004456 }
4457 }
4458 }
4459
4460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 * Returns true if this view has focus
4462 *
4463 * @return True if this view has focus, false otherwise.
4464 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004465 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 public boolean isFocused() {
4467 return (mPrivateFlags & FOCUSED) != 0;
4468 }
4469
4470 /**
4471 * Find the view in the hierarchy rooted at this view that currently has
4472 * focus.
4473 *
4474 * @return The view that currently has focus, or null if no focused view can
4475 * be found.
4476 */
4477 public View findFocus() {
4478 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4479 }
4480
4481 /**
4482 * Change whether this view is one of the set of scrollable containers in
4483 * its window. This will be used to determine whether the window can
4484 * resize or must pan when a soft input area is open -- scrollable
4485 * containers allow the window to use resize mode since the container
4486 * will appropriately shrink.
4487 */
4488 public void setScrollContainer(boolean isScrollContainer) {
4489 if (isScrollContainer) {
4490 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4491 mAttachInfo.mScrollContainers.add(this);
4492 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4493 }
4494 mPrivateFlags |= SCROLL_CONTAINER;
4495 } else {
4496 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4497 mAttachInfo.mScrollContainers.remove(this);
4498 }
4499 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4500 }
4501 }
4502
4503 /**
4504 * Returns the quality of the drawing cache.
4505 *
4506 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4507 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4508 *
4509 * @see #setDrawingCacheQuality(int)
4510 * @see #setDrawingCacheEnabled(boolean)
4511 * @see #isDrawingCacheEnabled()
4512 *
4513 * @attr ref android.R.styleable#View_drawingCacheQuality
4514 */
4515 public int getDrawingCacheQuality() {
4516 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4517 }
4518
4519 /**
4520 * Set the drawing cache quality of this view. This value is used only when the
4521 * drawing cache is enabled
4522 *
4523 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4524 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4525 *
4526 * @see #getDrawingCacheQuality()
4527 * @see #setDrawingCacheEnabled(boolean)
4528 * @see #isDrawingCacheEnabled()
4529 *
4530 * @attr ref android.R.styleable#View_drawingCacheQuality
4531 */
4532 public void setDrawingCacheQuality(int quality) {
4533 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4534 }
4535
4536 /**
4537 * Returns whether the screen should remain on, corresponding to the current
4538 * value of {@link #KEEP_SCREEN_ON}.
4539 *
4540 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4541 *
4542 * @see #setKeepScreenOn(boolean)
4543 *
4544 * @attr ref android.R.styleable#View_keepScreenOn
4545 */
4546 public boolean getKeepScreenOn() {
4547 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4548 }
4549
4550 /**
4551 * Controls whether the screen should remain on, modifying the
4552 * value of {@link #KEEP_SCREEN_ON}.
4553 *
4554 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4555 *
4556 * @see #getKeepScreenOn()
4557 *
4558 * @attr ref android.R.styleable#View_keepScreenOn
4559 */
4560 public void setKeepScreenOn(boolean keepScreenOn) {
4561 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4562 }
4563
4564 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004565 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4566 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 *
4568 * @attr ref android.R.styleable#View_nextFocusLeft
4569 */
4570 public int getNextFocusLeftId() {
4571 return mNextFocusLeftId;
4572 }
4573
4574 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004575 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4576 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4577 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 *
4579 * @attr ref android.R.styleable#View_nextFocusLeft
4580 */
4581 public void setNextFocusLeftId(int nextFocusLeftId) {
4582 mNextFocusLeftId = nextFocusLeftId;
4583 }
4584
4585 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004586 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4587 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 *
4589 * @attr ref android.R.styleable#View_nextFocusRight
4590 */
4591 public int getNextFocusRightId() {
4592 return mNextFocusRightId;
4593 }
4594
4595 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004596 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4597 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4598 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 *
4600 * @attr ref android.R.styleable#View_nextFocusRight
4601 */
4602 public void setNextFocusRightId(int nextFocusRightId) {
4603 mNextFocusRightId = nextFocusRightId;
4604 }
4605
4606 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004607 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4608 * @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 -08004609 *
4610 * @attr ref android.R.styleable#View_nextFocusUp
4611 */
4612 public int getNextFocusUpId() {
4613 return mNextFocusUpId;
4614 }
4615
4616 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004617 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4618 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4619 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004620 *
4621 * @attr ref android.R.styleable#View_nextFocusUp
4622 */
4623 public void setNextFocusUpId(int nextFocusUpId) {
4624 mNextFocusUpId = nextFocusUpId;
4625 }
4626
4627 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004628 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4629 * @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 -08004630 *
4631 * @attr ref android.R.styleable#View_nextFocusDown
4632 */
4633 public int getNextFocusDownId() {
4634 return mNextFocusDownId;
4635 }
4636
4637 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004638 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4639 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4640 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 *
4642 * @attr ref android.R.styleable#View_nextFocusDown
4643 */
4644 public void setNextFocusDownId(int nextFocusDownId) {
4645 mNextFocusDownId = nextFocusDownId;
4646 }
4647
4648 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004649 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4650 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4651 *
4652 * @attr ref android.R.styleable#View_nextFocusForward
4653 */
4654 public int getNextFocusForwardId() {
4655 return mNextFocusForwardId;
4656 }
4657
4658 /**
4659 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4660 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4661 * decide automatically.
4662 *
4663 * @attr ref android.R.styleable#View_nextFocusForward
4664 */
4665 public void setNextFocusForwardId(int nextFocusForwardId) {
4666 mNextFocusForwardId = nextFocusForwardId;
4667 }
4668
4669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 * Returns the visibility of this view and all of its ancestors
4671 *
4672 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4673 */
4674 public boolean isShown() {
4675 View current = this;
4676 //noinspection ConstantConditions
4677 do {
4678 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4679 return false;
4680 }
4681 ViewParent parent = current.mParent;
4682 if (parent == null) {
4683 return false; // We are not attached to the view root
4684 }
4685 if (!(parent instanceof View)) {
4686 return true;
4687 }
4688 current = (View) parent;
4689 } while (current != null);
4690
4691 return false;
4692 }
4693
4694 /**
4695 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4696 * is set
4697 *
4698 * @param insets Insets for system windows
4699 *
4700 * @return True if this view applied the insets, false otherwise
4701 */
4702 protected boolean fitSystemWindows(Rect insets) {
4703 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4704 mPaddingLeft = insets.left;
4705 mPaddingTop = insets.top;
4706 mPaddingRight = insets.right;
4707 mPaddingBottom = insets.bottom;
4708 requestLayout();
4709 return true;
4710 }
4711 return false;
4712 }
4713
4714 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004715 * Set whether or not this view should account for system screen decorations
4716 * such as the status bar and inset its content. This allows this view to be
4717 * positioned in absolute screen coordinates and remain visible to the user.
4718 *
4719 * <p>This should only be used by top-level window decor views.
4720 *
4721 * @param fitSystemWindows true to inset content for system screen decorations, false for
4722 * default behavior.
4723 *
4724 * @attr ref android.R.styleable#View_fitsSystemWindows
4725 */
4726 public void setFitsSystemWindows(boolean fitSystemWindows) {
4727 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4728 }
4729
4730 /**
4731 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4732 * will account for system screen decorations such as the status bar and inset its
4733 * content. This allows the view to be positioned in absolute screen coordinates
4734 * and remain visible to the user.
4735 *
4736 * @return true if this view will adjust its content bounds for system screen decorations.
4737 *
4738 * @attr ref android.R.styleable#View_fitsSystemWindows
4739 */
4740 public boolean fitsSystemWindows() {
4741 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4742 }
4743
4744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 * Returns the visibility status for this view.
4746 *
4747 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4748 * @attr ref android.R.styleable#View_visibility
4749 */
4750 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004751 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4752 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4753 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754 })
4755 public int getVisibility() {
4756 return mViewFlags & VISIBILITY_MASK;
4757 }
4758
4759 /**
4760 * Set the enabled state of this view.
4761 *
4762 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4763 * @attr ref android.R.styleable#View_visibility
4764 */
4765 @RemotableViewMethod
4766 public void setVisibility(int visibility) {
4767 setFlags(visibility, VISIBILITY_MASK);
4768 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4769 }
4770
4771 /**
4772 * Returns the enabled status for this view. The interpretation of the
4773 * enabled state varies by subclass.
4774 *
4775 * @return True if this view is enabled, false otherwise.
4776 */
4777 @ViewDebug.ExportedProperty
4778 public boolean isEnabled() {
4779 return (mViewFlags & ENABLED_MASK) == ENABLED;
4780 }
4781
4782 /**
4783 * Set the enabled state of this view. The interpretation of the enabled
4784 * state varies by subclass.
4785 *
4786 * @param enabled True if this view is enabled, false otherwise.
4787 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004788 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004789 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004790 if (enabled == isEnabled()) return;
4791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4793
4794 /*
4795 * The View most likely has to change its appearance, so refresh
4796 * the drawable state.
4797 */
4798 refreshDrawableState();
4799
4800 // Invalidate too, since the default behavior for views is to be
4801 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004802 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004803 }
4804
4805 /**
4806 * Set whether this view can receive the focus.
4807 *
4808 * Setting this to false will also ensure that this view is not focusable
4809 * in touch mode.
4810 *
4811 * @param focusable If true, this view can receive the focus.
4812 *
4813 * @see #setFocusableInTouchMode(boolean)
4814 * @attr ref android.R.styleable#View_focusable
4815 */
4816 public void setFocusable(boolean focusable) {
4817 if (!focusable) {
4818 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4819 }
4820 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4821 }
4822
4823 /**
4824 * Set whether this view can receive focus while in touch mode.
4825 *
4826 * Setting this to true will also ensure that this view is focusable.
4827 *
4828 * @param focusableInTouchMode If true, this view can receive the focus while
4829 * in touch mode.
4830 *
4831 * @see #setFocusable(boolean)
4832 * @attr ref android.R.styleable#View_focusableInTouchMode
4833 */
4834 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4835 // Focusable in touch mode should always be set before the focusable flag
4836 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4837 // which, in touch mode, will not successfully request focus on this view
4838 // because the focusable in touch mode flag is not set
4839 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4840 if (focusableInTouchMode) {
4841 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4842 }
4843 }
4844
4845 /**
4846 * Set whether this view should have sound effects enabled for events such as
4847 * clicking and touching.
4848 *
4849 * <p>You may wish to disable sound effects for a view if you already play sounds,
4850 * for instance, a dial key that plays dtmf tones.
4851 *
4852 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4853 * @see #isSoundEffectsEnabled()
4854 * @see #playSoundEffect(int)
4855 * @attr ref android.R.styleable#View_soundEffectsEnabled
4856 */
4857 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4858 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4859 }
4860
4861 /**
4862 * @return whether this view should have sound effects enabled for events such as
4863 * clicking and touching.
4864 *
4865 * @see #setSoundEffectsEnabled(boolean)
4866 * @see #playSoundEffect(int)
4867 * @attr ref android.R.styleable#View_soundEffectsEnabled
4868 */
4869 @ViewDebug.ExportedProperty
4870 public boolean isSoundEffectsEnabled() {
4871 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4872 }
4873
4874 /**
4875 * Set whether this view should have haptic feedback for events such as
4876 * long presses.
4877 *
4878 * <p>You may wish to disable haptic feedback if your view already controls
4879 * its own haptic feedback.
4880 *
4881 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4882 * @see #isHapticFeedbackEnabled()
4883 * @see #performHapticFeedback(int)
4884 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4885 */
4886 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4887 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4888 }
4889
4890 /**
4891 * @return whether this view should have haptic feedback enabled for events
4892 * long presses.
4893 *
4894 * @see #setHapticFeedbackEnabled(boolean)
4895 * @see #performHapticFeedback(int)
4896 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4897 */
4898 @ViewDebug.ExportedProperty
4899 public boolean isHapticFeedbackEnabled() {
4900 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4901 }
4902
4903 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004904 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004905 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004906 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4907 * {@link #LAYOUT_DIRECTION_RTL},
4908 * {@link #LAYOUT_DIRECTION_INHERIT} or
4909 * {@link #LAYOUT_DIRECTION_LOCALE}.
4910 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004911 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004912 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004913 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4914 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4915 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4916 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004917 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004918 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07004919 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08004920 }
4921
4922 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004923 * Set the layout direction for this view. This will propagate a reset of layout direction
4924 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004925 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004926 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4927 * {@link #LAYOUT_DIRECTION_RTL},
4928 * {@link #LAYOUT_DIRECTION_INHERIT} or
4929 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004930 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004931 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004932 */
4933 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004934 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004935 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07004936 // Reset the current layout direction
4937 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
4938 // Reset the current resolved layout direction
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004939 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07004940 // Set the new layout direction (filtered) and ask for a layout pass
4941 mPrivateFlags2 |=
4942 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
4943 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004944 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004945 }
4946
4947 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004948 * Returns the resolved layout direction for this view.
4949 *
4950 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07004951 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004952 */
4953 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07004954 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4955 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004956 })
4957 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004958 resolveLayoutDirectionIfNeeded();
4959 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004960 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4961 }
4962
4963 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07004964 * Indicates whether or not this view's layout is right-to-left. This is resolved from
4965 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004966 *
4967 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004968 */
4969 @ViewDebug.ExportedProperty(category = "layout")
4970 public boolean isLayoutRtl() {
4971 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4972 }
4973
4974 /**
Adam Powell539ee872012-02-03 19:00:49 -08004975 * Indicates whether the view is currently tracking transient state that the
4976 * app should not need to concern itself with saving and restoring, but that
4977 * the framework should take special note to preserve when possible.
4978 *
4979 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004980 */
4981 @ViewDebug.ExportedProperty(category = "layout")
4982 public boolean hasTransientState() {
4983 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4984 }
4985
4986 /**
4987 * Set whether this view is currently tracking transient state that the
4988 * framework should attempt to preserve when possible.
4989 *
4990 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004991 */
4992 public void setHasTransientState(boolean hasTransientState) {
4993 if (hasTransientState() == hasTransientState) return;
4994
4995 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4996 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4997 if (mParent != null) {
4998 try {
4999 mParent.childHasTransientStateChanged(this, hasTransientState);
5000 } catch (AbstractMethodError e) {
5001 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5002 " does not fully implement ViewParent", e);
5003 }
5004 }
5005 }
5006
5007 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 * If this view doesn't do any drawing on its own, set this flag to
5009 * allow further optimizations. By default, this flag is not set on
5010 * View, but could be set on some View subclasses such as ViewGroup.
5011 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005012 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5013 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 *
5015 * @param willNotDraw whether or not this View draw on its own
5016 */
5017 public void setWillNotDraw(boolean willNotDraw) {
5018 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5019 }
5020
5021 /**
5022 * Returns whether or not this View draws on its own.
5023 *
5024 * @return true if this view has nothing to draw, false otherwise
5025 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005026 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 public boolean willNotDraw() {
5028 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5029 }
5030
5031 /**
5032 * When a View's drawing cache is enabled, drawing is redirected to an
5033 * offscreen bitmap. Some views, like an ImageView, must be able to
5034 * bypass this mechanism if they already draw a single bitmap, to avoid
5035 * unnecessary usage of the memory.
5036 *
5037 * @param willNotCacheDrawing true if this view does not cache its
5038 * drawing, false otherwise
5039 */
5040 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5041 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5042 }
5043
5044 /**
5045 * Returns whether or not this View can cache its drawing or not.
5046 *
5047 * @return true if this view does not cache its drawing, false otherwise
5048 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005049 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 public boolean willNotCacheDrawing() {
5051 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5052 }
5053
5054 /**
5055 * Indicates whether this view reacts to click events or not.
5056 *
5057 * @return true if the view is clickable, false otherwise
5058 *
5059 * @see #setClickable(boolean)
5060 * @attr ref android.R.styleable#View_clickable
5061 */
5062 @ViewDebug.ExportedProperty
5063 public boolean isClickable() {
5064 return (mViewFlags & CLICKABLE) == CLICKABLE;
5065 }
5066
5067 /**
5068 * Enables or disables click events for this view. When a view
5069 * is clickable it will change its state to "pressed" on every click.
5070 * Subclasses should set the view clickable to visually react to
5071 * user's clicks.
5072 *
5073 * @param clickable true to make the view clickable, false otherwise
5074 *
5075 * @see #isClickable()
5076 * @attr ref android.R.styleable#View_clickable
5077 */
5078 public void setClickable(boolean clickable) {
5079 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5080 }
5081
5082 /**
5083 * Indicates whether this view reacts to long click events or not.
5084 *
5085 * @return true if the view is long clickable, false otherwise
5086 *
5087 * @see #setLongClickable(boolean)
5088 * @attr ref android.R.styleable#View_longClickable
5089 */
5090 public boolean isLongClickable() {
5091 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5092 }
5093
5094 /**
5095 * Enables or disables long click events for this view. When a view is long
5096 * clickable it reacts to the user holding down the button for a longer
5097 * duration than a tap. This event can either launch the listener or a
5098 * context menu.
5099 *
5100 * @param longClickable true to make the view long clickable, false otherwise
5101 * @see #isLongClickable()
5102 * @attr ref android.R.styleable#View_longClickable
5103 */
5104 public void setLongClickable(boolean longClickable) {
5105 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5106 }
5107
5108 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005109 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 *
5111 * @see #isClickable()
5112 * @see #setClickable(boolean)
5113 *
5114 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5115 * the View's internal state from a previously set "pressed" state.
5116 */
5117 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005118 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 if (pressed) {
5121 mPrivateFlags |= PRESSED;
5122 } else {
5123 mPrivateFlags &= ~PRESSED;
5124 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005125
5126 if (needsRefresh) {
5127 refreshDrawableState();
5128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 dispatchSetPressed(pressed);
5130 }
5131
5132 /**
5133 * Dispatch setPressed to all of this View's children.
5134 *
5135 * @see #setPressed(boolean)
5136 *
5137 * @param pressed The new pressed state
5138 */
5139 protected void dispatchSetPressed(boolean pressed) {
5140 }
5141
5142 /**
5143 * Indicates whether the view is currently in pressed state. Unless
5144 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5145 * the pressed state.
5146 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005147 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 * @see #isClickable()
5149 * @see #setClickable(boolean)
5150 *
5151 * @return true if the view is currently pressed, false otherwise
5152 */
5153 public boolean isPressed() {
5154 return (mPrivateFlags & PRESSED) == PRESSED;
5155 }
5156
5157 /**
5158 * Indicates whether this view will save its state (that is,
5159 * whether its {@link #onSaveInstanceState} method will be called).
5160 *
5161 * @return Returns true if the view state saving is enabled, else false.
5162 *
5163 * @see #setSaveEnabled(boolean)
5164 * @attr ref android.R.styleable#View_saveEnabled
5165 */
5166 public boolean isSaveEnabled() {
5167 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5168 }
5169
5170 /**
5171 * Controls whether the saving of this view's state is
5172 * enabled (that is, whether its {@link #onSaveInstanceState} method
5173 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005174 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 * for its state to be saved. This flag can only disable the
5176 * saving of this view; any child views may still have their state saved.
5177 *
5178 * @param enabled Set to false to <em>disable</em> state saving, or true
5179 * (the default) to allow it.
5180 *
5181 * @see #isSaveEnabled()
5182 * @see #setId(int)
5183 * @see #onSaveInstanceState()
5184 * @attr ref android.R.styleable#View_saveEnabled
5185 */
5186 public void setSaveEnabled(boolean enabled) {
5187 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5188 }
5189
Jeff Brown85a31762010-09-01 17:01:00 -07005190 /**
5191 * Gets whether the framework should discard touches when the view's
5192 * window is obscured by another visible window.
5193 * Refer to the {@link View} security documentation for more details.
5194 *
5195 * @return True if touch filtering is enabled.
5196 *
5197 * @see #setFilterTouchesWhenObscured(boolean)
5198 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5199 */
5200 @ViewDebug.ExportedProperty
5201 public boolean getFilterTouchesWhenObscured() {
5202 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5203 }
5204
5205 /**
5206 * Sets whether the framework should discard touches when the view's
5207 * window is obscured by another visible window.
5208 * Refer to the {@link View} security documentation for more details.
5209 *
5210 * @param enabled True if touch filtering should be enabled.
5211 *
5212 * @see #getFilterTouchesWhenObscured
5213 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5214 */
5215 public void setFilterTouchesWhenObscured(boolean enabled) {
5216 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5217 FILTER_TOUCHES_WHEN_OBSCURED);
5218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219
5220 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005221 * Indicates whether the entire hierarchy under this view will save its
5222 * state when a state saving traversal occurs from its parent. The default
5223 * is true; if false, these views will not be saved unless
5224 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5225 *
5226 * @return Returns true if the view state saving from parent is enabled, else false.
5227 *
5228 * @see #setSaveFromParentEnabled(boolean)
5229 */
5230 public boolean isSaveFromParentEnabled() {
5231 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5232 }
5233
5234 /**
5235 * Controls whether the entire hierarchy under this view will save its
5236 * state when a state saving traversal occurs from its parent. The default
5237 * is true; if false, these views will not be saved unless
5238 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5239 *
5240 * @param enabled Set to false to <em>disable</em> state saving, or true
5241 * (the default) to allow it.
5242 *
5243 * @see #isSaveFromParentEnabled()
5244 * @see #setId(int)
5245 * @see #onSaveInstanceState()
5246 */
5247 public void setSaveFromParentEnabled(boolean enabled) {
5248 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5249 }
5250
5251
5252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 * Returns whether this View is able to take focus.
5254 *
5255 * @return True if this view can take focus, or false otherwise.
5256 * @attr ref android.R.styleable#View_focusable
5257 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005258 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 public final boolean isFocusable() {
5260 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5261 }
5262
5263 /**
5264 * When a view is focusable, it may not want to take focus when in touch mode.
5265 * For example, a button would like focus when the user is navigating via a D-pad
5266 * so that the user can click on it, but once the user starts touching the screen,
5267 * the button shouldn't take focus
5268 * @return Whether the view is focusable in touch mode.
5269 * @attr ref android.R.styleable#View_focusableInTouchMode
5270 */
5271 @ViewDebug.ExportedProperty
5272 public final boolean isFocusableInTouchMode() {
5273 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5274 }
5275
5276 /**
5277 * Find the nearest view in the specified direction that can take focus.
5278 * This does not actually give focus to that view.
5279 *
5280 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5281 *
5282 * @return The nearest focusable in the specified direction, or null if none
5283 * can be found.
5284 */
5285 public View focusSearch(int direction) {
5286 if (mParent != null) {
5287 return mParent.focusSearch(this, direction);
5288 } else {
5289 return null;
5290 }
5291 }
5292
5293 /**
5294 * This method is the last chance for the focused view and its ancestors to
5295 * respond to an arrow key. This is called when the focused view did not
5296 * consume the key internally, nor could the view system find a new view in
5297 * the requested direction to give focus to.
5298 *
5299 * @param focused The currently focused view.
5300 * @param direction The direction focus wants to move. One of FOCUS_UP,
5301 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5302 * @return True if the this view consumed this unhandled move.
5303 */
5304 public boolean dispatchUnhandledMove(View focused, int direction) {
5305 return false;
5306 }
5307
5308 /**
5309 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005310 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005312 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5313 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 * @return The user specified next view, or null if there is none.
5315 */
5316 View findUserSetNextFocus(View root, int direction) {
5317 switch (direction) {
5318 case FOCUS_LEFT:
5319 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005320 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 case FOCUS_RIGHT:
5322 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005323 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 case FOCUS_UP:
5325 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005326 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005327 case FOCUS_DOWN:
5328 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005329 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005330 case FOCUS_FORWARD:
5331 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005332 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005333 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005334 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005335 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005336 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005337 @Override
5338 public boolean apply(View t) {
5339 return t.mNextFocusForwardId == id;
5340 }
5341 });
5342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 }
5344 return null;
5345 }
5346
Jeff Brown4dfbec22011-08-15 14:55:37 -07005347 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5348 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5349 @Override
5350 public boolean apply(View t) {
5351 return t.mID == childViewId;
5352 }
5353 });
5354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 if (result == null) {
5356 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5357 + "by user for id " + childViewId);
5358 }
5359 return result;
5360 }
5361
5362 /**
5363 * Find and return all focusable views that are descendants of this view,
5364 * possibly including this view if it is focusable itself.
5365 *
5366 * @param direction The direction of the focus
5367 * @return A list of focusable views
5368 */
5369 public ArrayList<View> getFocusables(int direction) {
5370 ArrayList<View> result = new ArrayList<View>(24);
5371 addFocusables(result, direction);
5372 return result;
5373 }
5374
5375 /**
5376 * Add any focusable views that are descendants of this view (possibly
5377 * including this view if it is focusable itself) to views. If we are in touch mode,
5378 * only add views that are also focusable in touch mode.
5379 *
5380 * @param views Focusable views found so far
5381 * @param direction The direction of the focus
5382 */
5383 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005384 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386
svetoslavganov75986cf2009-05-14 22:28:01 -07005387 /**
5388 * Adds any focusable views that are descendants of this view (possibly
5389 * including this view if it is focusable itself) to views. This method
5390 * adds all focusable views regardless if we are in touch mode or
5391 * only views focusable in touch mode if we are in touch mode depending on
5392 * the focusable mode paramater.
5393 *
5394 * @param views Focusable views found so far or null if all we are interested is
5395 * the number of focusables.
5396 * @param direction The direction of the focus.
5397 * @param focusableMode The type of focusables to be added.
5398 *
5399 * @see #FOCUSABLES_ALL
5400 * @see #FOCUSABLES_TOUCH_MODE
5401 */
5402 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5403 if (!isFocusable()) {
5404 return;
5405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406
svetoslavganov75986cf2009-05-14 22:28:01 -07005407 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5408 isInTouchMode() && !isFocusableInTouchMode()) {
5409 return;
5410 }
5411
5412 if (views != null) {
5413 views.add(this);
5414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 }
5416
5417 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005418 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005419 * The search is performed by either the text that the View renders or the content
5420 * description that describes the view for accessibility purposes and the view does
5421 * not render or both. Clients can specify how the search is to be performed via
5422 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5423 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005424 *
5425 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005426 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005427 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005428 * @see #FIND_VIEWS_WITH_TEXT
5429 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5430 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005431 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005432 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005433 if (getAccessibilityNodeProvider() != null) {
5434 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5435 outViews.add(this);
5436 }
5437 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5438 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005439 String searchedLowerCase = searched.toString().toLowerCase();
5440 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5441 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5442 outViews.add(this);
5443 }
5444 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005445 }
5446
5447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 * Find and return all touchable views that are descendants of this view,
5449 * possibly including this view if it is touchable itself.
5450 *
5451 * @return A list of touchable views
5452 */
5453 public ArrayList<View> getTouchables() {
5454 ArrayList<View> result = new ArrayList<View>();
5455 addTouchables(result);
5456 return result;
5457 }
5458
5459 /**
5460 * Add any touchable views that are descendants of this view (possibly
5461 * including this view if it is touchable itself) to views.
5462 *
5463 * @param views Touchable views found so far
5464 */
5465 public void addTouchables(ArrayList<View> views) {
5466 final int viewFlags = mViewFlags;
5467
5468 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5469 && (viewFlags & ENABLED_MASK) == ENABLED) {
5470 views.add(this);
5471 }
5472 }
5473
5474 /**
5475 * Call this to try to give focus to a specific view or to one of its
5476 * descendants.
5477 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005478 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5479 * false), or if it is focusable and it is not focusable in touch mode
5480 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005482 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 * have focus, and you want your parent to look for the next one.
5484 *
5485 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5486 * {@link #FOCUS_DOWN} and <code>null</code>.
5487 *
5488 * @return Whether this view or one of its descendants actually took focus.
5489 */
5490 public final boolean requestFocus() {
5491 return requestFocus(View.FOCUS_DOWN);
5492 }
5493
5494
5495 /**
5496 * Call this to try to give focus to a specific view or to one of its
5497 * descendants and give it a hint about what direction focus is heading.
5498 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005499 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5500 * false), or if it is focusable and it is not focusable in touch mode
5501 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005503 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 * have focus, and you want your parent to look for the next one.
5505 *
5506 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5507 * <code>null</code> set for the previously focused rectangle.
5508 *
5509 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5510 * @return Whether this view or one of its descendants actually took focus.
5511 */
5512 public final boolean requestFocus(int direction) {
5513 return requestFocus(direction, null);
5514 }
5515
5516 /**
5517 * Call this to try to give focus to a specific view or to one of its descendants
5518 * and give it hints about the direction and a specific rectangle that the focus
5519 * is coming from. The rectangle can help give larger views a finer grained hint
5520 * about where focus is coming from, and therefore, where to show selection, or
5521 * forward focus change internally.
5522 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005523 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5524 * false), or if it is focusable and it is not focusable in touch mode
5525 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005526 *
5527 * A View will not take focus if it is not visible.
5528 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005529 * A View will not take focus if one of its parents has
5530 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5531 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005533 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 * have focus, and you want your parent to look for the next one.
5535 *
5536 * You may wish to override this method if your custom {@link View} has an internal
5537 * {@link View} that it wishes to forward the request to.
5538 *
5539 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5540 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5541 * to give a finer grained hint about where focus is coming from. May be null
5542 * if there is no hint.
5543 * @return Whether this view or one of its descendants actually took focus.
5544 */
5545 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5546 // need to be focusable
5547 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5548 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5549 return false;
5550 }
5551
5552 // need to be focusable in touch mode if in touch mode
5553 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005554 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5555 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 }
5557
5558 // need to not have any parents blocking us
5559 if (hasAncestorThatBlocksDescendantFocus()) {
5560 return false;
5561 }
5562
5563 handleFocusGainInternal(direction, previouslyFocusedRect);
5564 return true;
5565 }
5566
5567 /**
5568 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5569 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5570 * touch mode to request focus when they are touched.
5571 *
5572 * @return Whether this view or one of its descendants actually took focus.
5573 *
5574 * @see #isInTouchMode()
5575 *
5576 */
5577 public final boolean requestFocusFromTouch() {
5578 // Leave touch mode if we need to
5579 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005580 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005581 if (viewRoot != null) {
5582 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 }
5584 }
5585 return requestFocus(View.FOCUS_DOWN);
5586 }
5587
5588 /**
5589 * @return Whether any ancestor of this view blocks descendant focus.
5590 */
5591 private boolean hasAncestorThatBlocksDescendantFocus() {
5592 ViewParent ancestor = mParent;
5593 while (ancestor instanceof ViewGroup) {
5594 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5595 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5596 return true;
5597 } else {
5598 ancestor = vgAncestor.getParent();
5599 }
5600 }
5601 return false;
5602 }
5603
5604 /**
Romain Guya440b002010-02-24 15:57:54 -08005605 * @hide
5606 */
5607 public void dispatchStartTemporaryDetach() {
5608 onStartTemporaryDetach();
5609 }
5610
5611 /**
5612 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5614 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005615 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 */
5617 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005618 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005619 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005620 }
5621
5622 /**
5623 * @hide
5624 */
5625 public void dispatchFinishTemporaryDetach() {
5626 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 }
Romain Guy8506ab42009-06-11 17:35:47 -07005628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 /**
5630 * Called after {@link #onStartTemporaryDetach} when the container is done
5631 * changing the view.
5632 */
5633 public void onFinishTemporaryDetach() {
5634 }
Romain Guy8506ab42009-06-11 17:35:47 -07005635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005637 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5638 * for this view's window. Returns null if the view is not currently attached
5639 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005640 * just use the standard high-level event callbacks like
5641 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005642 */
5643 public KeyEvent.DispatcherState getKeyDispatcherState() {
5644 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5645 }
Joe Malin32736f02011-01-19 16:14:20 -08005646
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 * Dispatch a key event before it is processed by any input method
5649 * associated with the view hierarchy. This can be used to intercept
5650 * key events in special situations before the IME consumes them; a
5651 * typical example would be handling the BACK key to update the application's
5652 * UI instead of allowing the IME to see it and close itself.
5653 *
5654 * @param event The key event to be dispatched.
5655 * @return True if the event was handled, false otherwise.
5656 */
5657 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5658 return onKeyPreIme(event.getKeyCode(), event);
5659 }
5660
5661 /**
5662 * Dispatch a key event to the next view on the focus path. This path runs
5663 * from the top of the view tree down to the currently focused view. If this
5664 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5665 * the next node down the focus path. This method also fires any key
5666 * listeners.
5667 *
5668 * @param event The key event to be dispatched.
5669 * @return True if the event was handled, false otherwise.
5670 */
5671 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005672 if (mInputEventConsistencyVerifier != null) {
5673 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675
Jeff Brown21bc5c92011-02-28 18:27:14 -08005676 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005677 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005678 ListenerInfo li = mListenerInfo;
5679 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5680 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 return true;
5682 }
5683
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005684 if (event.dispatch(this, mAttachInfo != null
5685 ? mAttachInfo.mKeyDispatchState : null, this)) {
5686 return true;
5687 }
5688
5689 if (mInputEventConsistencyVerifier != null) {
5690 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5691 }
5692 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 }
5694
5695 /**
5696 * Dispatches a key shortcut event.
5697 *
5698 * @param event The key event to be dispatched.
5699 * @return True if the event was handled by the view, false otherwise.
5700 */
5701 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5702 return onKeyShortcut(event.getKeyCode(), event);
5703 }
5704
5705 /**
5706 * Pass the touch screen motion event down to the target view, or this
5707 * view if it is the target.
5708 *
5709 * @param event The motion event to be dispatched.
5710 * @return True if the event was handled by the view, false otherwise.
5711 */
5712 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005713 if (mInputEventConsistencyVerifier != null) {
5714 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5715 }
5716
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005717 if (onFilterTouchEventForSecurity(event)) {
5718 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005719 ListenerInfo li = mListenerInfo;
5720 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5721 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005722 return true;
5723 }
5724
5725 if (onTouchEvent(event)) {
5726 return true;
5727 }
Jeff Brown85a31762010-09-01 17:01:00 -07005728 }
5729
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005730 if (mInputEventConsistencyVerifier != null) {
5731 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005733 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 }
5735
5736 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005737 * Filter the touch event to apply security policies.
5738 *
5739 * @param event The motion event to be filtered.
5740 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005741 *
Jeff Brown85a31762010-09-01 17:01:00 -07005742 * @see #getFilterTouchesWhenObscured
5743 */
5744 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005745 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005746 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5747 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5748 // Window is obscured, drop this touch.
5749 return false;
5750 }
5751 return true;
5752 }
5753
5754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 * Pass a trackball motion event down to the focused view.
5756 *
5757 * @param event The motion event to be dispatched.
5758 * @return True if the event was handled by the view, false otherwise.
5759 */
5760 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005761 if (mInputEventConsistencyVerifier != null) {
5762 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5763 }
5764
Romain Guy02ccac62011-06-24 13:20:23 -07005765 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 }
5767
5768 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005769 * Dispatch a generic motion event.
5770 * <p>
5771 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5772 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005773 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005774 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005775 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005776 *
5777 * @param event The motion event to be dispatched.
5778 * @return True if the event was handled by the view, false otherwise.
5779 */
5780 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005781 if (mInputEventConsistencyVerifier != null) {
5782 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5783 }
5784
Jeff Browna032cc02011-03-07 16:56:21 -08005785 final int source = event.getSource();
5786 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5787 final int action = event.getAction();
5788 if (action == MotionEvent.ACTION_HOVER_ENTER
5789 || action == MotionEvent.ACTION_HOVER_MOVE
5790 || action == MotionEvent.ACTION_HOVER_EXIT) {
5791 if (dispatchHoverEvent(event)) {
5792 return true;
5793 }
5794 } else if (dispatchGenericPointerEvent(event)) {
5795 return true;
5796 }
5797 } else if (dispatchGenericFocusedEvent(event)) {
5798 return true;
5799 }
5800
Jeff Brown10b62902011-06-20 16:40:37 -07005801 if (dispatchGenericMotionEventInternal(event)) {
5802 return true;
5803 }
5804
5805 if (mInputEventConsistencyVerifier != null) {
5806 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5807 }
5808 return false;
5809 }
5810
5811 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005812 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005813 ListenerInfo li = mListenerInfo;
5814 if (li != null && li.mOnGenericMotionListener != null
5815 && (mViewFlags & ENABLED_MASK) == ENABLED
5816 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005817 return true;
5818 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005819
5820 if (onGenericMotionEvent(event)) {
5821 return true;
5822 }
5823
5824 if (mInputEventConsistencyVerifier != null) {
5825 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5826 }
5827 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005828 }
5829
5830 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005831 * Dispatch a hover event.
5832 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005833 * Do not call this method directly.
5834 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005835 * </p>
5836 *
5837 * @param event The motion event to be dispatched.
5838 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005839 */
5840 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005841 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005842 ListenerInfo li = mListenerInfo;
5843 if (li != null && li.mOnHoverListener != null
5844 && (mViewFlags & ENABLED_MASK) == ENABLED
5845 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005846 return true;
5847 }
5848
Jeff Browna032cc02011-03-07 16:56:21 -08005849 return onHoverEvent(event);
5850 }
5851
5852 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005853 * Returns true if the view has a child to which it has recently sent
5854 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5855 * it does not have a hovered child, then it must be the innermost hovered view.
5856 * @hide
5857 */
5858 protected boolean hasHoveredChild() {
5859 return false;
5860 }
5861
5862 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005863 * Dispatch a generic motion event to the view under the first pointer.
5864 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005865 * Do not call this method directly.
5866 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005867 * </p>
5868 *
5869 * @param event The motion event to be dispatched.
5870 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005871 */
5872 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5873 return false;
5874 }
5875
5876 /**
5877 * Dispatch a generic motion event to the currently focused view.
5878 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005879 * Do not call this method directly.
5880 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005881 * </p>
5882 *
5883 * @param event The motion event to be dispatched.
5884 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005885 */
5886 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5887 return false;
5888 }
5889
5890 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005891 * Dispatch a pointer event.
5892 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005893 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5894 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5895 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005896 * and should not be expected to handle other pointing device features.
5897 * </p>
5898 *
5899 * @param event The motion event to be dispatched.
5900 * @return True if the event was handled by the view, false otherwise.
5901 * @hide
5902 */
5903 public final boolean dispatchPointerEvent(MotionEvent event) {
5904 if (event.isTouchEvent()) {
5905 return dispatchTouchEvent(event);
5906 } else {
5907 return dispatchGenericMotionEvent(event);
5908 }
5909 }
5910
5911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 * Called when the window containing this view gains or loses window focus.
5913 * ViewGroups should override to route to their children.
5914 *
5915 * @param hasFocus True if the window containing this view now has focus,
5916 * false otherwise.
5917 */
5918 public void dispatchWindowFocusChanged(boolean hasFocus) {
5919 onWindowFocusChanged(hasFocus);
5920 }
5921
5922 /**
5923 * Called when the window containing this view gains or loses focus. Note
5924 * that this is separate from view focus: to receive key events, both
5925 * your view and its window must have focus. If a window is displayed
5926 * on top of yours that takes input focus, then your own window will lose
5927 * focus but the view focus will remain unchanged.
5928 *
5929 * @param hasWindowFocus True if the window containing this view now has
5930 * focus, false otherwise.
5931 */
5932 public void onWindowFocusChanged(boolean hasWindowFocus) {
5933 InputMethodManager imm = InputMethodManager.peekInstance();
5934 if (!hasWindowFocus) {
5935 if (isPressed()) {
5936 setPressed(false);
5937 }
5938 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5939 imm.focusOut(this);
5940 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005941 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005942 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005943 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5945 imm.focusIn(this);
5946 }
5947 refreshDrawableState();
5948 }
5949
5950 /**
5951 * Returns true if this view is in a window that currently has window focus.
5952 * Note that this is not the same as the view itself having focus.
5953 *
5954 * @return True if this view is in a window that currently has window focus.
5955 */
5956 public boolean hasWindowFocus() {
5957 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5958 }
5959
5960 /**
Adam Powell326d8082009-12-09 15:10:07 -08005961 * Dispatch a view visibility change down the view hierarchy.
5962 * ViewGroups should override to route to their children.
5963 * @param changedView The view whose visibility changed. Could be 'this' or
5964 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005965 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5966 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005967 */
5968 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5969 onVisibilityChanged(changedView, visibility);
5970 }
5971
5972 /**
5973 * Called when the visibility of the view or an ancestor of the view is changed.
5974 * @param changedView The view whose visibility changed. Could be 'this' or
5975 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005976 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5977 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005978 */
5979 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005980 if (visibility == VISIBLE) {
5981 if (mAttachInfo != null) {
5982 initialAwakenScrollBars();
5983 } else {
5984 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5985 }
5986 }
Adam Powell326d8082009-12-09 15:10:07 -08005987 }
5988
5989 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005990 * Dispatch a hint about whether this view is displayed. For instance, when
5991 * a View moves out of the screen, it might receives a display hint indicating
5992 * the view is not displayed. Applications should not <em>rely</em> on this hint
5993 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005994 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005995 * @param hint A hint about whether or not this view is displayed:
5996 * {@link #VISIBLE} or {@link #INVISIBLE}.
5997 */
5998 public void dispatchDisplayHint(int hint) {
5999 onDisplayHint(hint);
6000 }
6001
6002 /**
6003 * Gives this view a hint about whether is displayed or not. For instance, when
6004 * a View moves out of the screen, it might receives a display hint indicating
6005 * the view is not displayed. Applications should not <em>rely</em> on this hint
6006 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006007 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006008 * @param hint A hint about whether or not this view is displayed:
6009 * {@link #VISIBLE} or {@link #INVISIBLE}.
6010 */
6011 protected void onDisplayHint(int hint) {
6012 }
6013
6014 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006015 * Dispatch a window visibility change down the view hierarchy.
6016 * ViewGroups should override to route to their children.
6017 *
6018 * @param visibility The new visibility of the window.
6019 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006020 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 */
6022 public void dispatchWindowVisibilityChanged(int visibility) {
6023 onWindowVisibilityChanged(visibility);
6024 }
6025
6026 /**
6027 * Called when the window containing has change its visibility
6028 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6029 * that this tells you whether or not your window is being made visible
6030 * to the window manager; this does <em>not</em> tell you whether or not
6031 * your window is obscured by other windows on the screen, even if it
6032 * is itself visible.
6033 *
6034 * @param visibility The new visibility of the window.
6035 */
6036 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006037 if (visibility == VISIBLE) {
6038 initialAwakenScrollBars();
6039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006040 }
6041
6042 /**
6043 * Returns the current visibility of the window this view is attached to
6044 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6045 *
6046 * @return Returns the current visibility of the view's window.
6047 */
6048 public int getWindowVisibility() {
6049 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6050 }
6051
6052 /**
6053 * Retrieve the overall visible display size in which the window this view is
6054 * attached to has been positioned in. This takes into account screen
6055 * decorations above the window, for both cases where the window itself
6056 * is being position inside of them or the window is being placed under
6057 * then and covered insets are used for the window to position its content
6058 * inside. In effect, this tells you the available area where content can
6059 * be placed and remain visible to users.
6060 *
6061 * <p>This function requires an IPC back to the window manager to retrieve
6062 * the requested information, so should not be used in performance critical
6063 * code like drawing.
6064 *
6065 * @param outRect Filled in with the visible display frame. If the view
6066 * is not attached to a window, this is simply the raw display size.
6067 */
6068 public void getWindowVisibleDisplayFrame(Rect outRect) {
6069 if (mAttachInfo != null) {
6070 try {
6071 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6072 } catch (RemoteException e) {
6073 return;
6074 }
6075 // XXX This is really broken, and probably all needs to be done
6076 // in the window manager, and we need to know more about whether
6077 // we want the area behind or in front of the IME.
6078 final Rect insets = mAttachInfo.mVisibleInsets;
6079 outRect.left += insets.left;
6080 outRect.top += insets.top;
6081 outRect.right -= insets.right;
6082 outRect.bottom -= insets.bottom;
6083 return;
6084 }
6085 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006086 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006087 }
6088
6089 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006090 * Dispatch a notification about a resource configuration change down
6091 * the view hierarchy.
6092 * ViewGroups should override to route to their children.
6093 *
6094 * @param newConfig The new resource configuration.
6095 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006096 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006097 */
6098 public void dispatchConfigurationChanged(Configuration newConfig) {
6099 onConfigurationChanged(newConfig);
6100 }
6101
6102 /**
6103 * Called when the current configuration of the resources being used
6104 * by the application have changed. You can use this to decide when
6105 * to reload resources that can changed based on orientation and other
6106 * configuration characterstics. You only need to use this if you are
6107 * not relying on the normal {@link android.app.Activity} mechanism of
6108 * recreating the activity instance upon a configuration change.
6109 *
6110 * @param newConfig The new resource configuration.
6111 */
6112 protected void onConfigurationChanged(Configuration newConfig) {
6113 }
6114
6115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 * Private function to aggregate all per-view attributes in to the view
6117 * root.
6118 */
6119 void dispatchCollectViewAttributes(int visibility) {
6120 performCollectViewAttributes(visibility);
6121 }
6122
6123 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006124 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006125 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6126 mAttachInfo.mKeepScreenOn = true;
6127 }
6128 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006129 ListenerInfo li = mListenerInfo;
6130 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006131 mAttachInfo.mHasSystemUiListeners = true;
6132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 }
6134 }
6135
6136 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006137 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006138 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006139 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6140 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 ai.mRecomputeGlobalAttributes = true;
6142 }
6143 }
6144 }
6145
6146 /**
6147 * Returns whether the device is currently in touch mode. Touch mode is entered
6148 * once the user begins interacting with the device by touch, and affects various
6149 * things like whether focus is always visible to the user.
6150 *
6151 * @return Whether the device is in touch mode.
6152 */
6153 @ViewDebug.ExportedProperty
6154 public boolean isInTouchMode() {
6155 if (mAttachInfo != null) {
6156 return mAttachInfo.mInTouchMode;
6157 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006158 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 }
6160 }
6161
6162 /**
6163 * Returns the context the view is running in, through which it can
6164 * access the current theme, resources, etc.
6165 *
6166 * @return The view's Context.
6167 */
6168 @ViewDebug.CapturedViewProperty
6169 public final Context getContext() {
6170 return mContext;
6171 }
6172
6173 /**
6174 * Handle a key event before it is processed by any input method
6175 * associated with the view hierarchy. This can be used to intercept
6176 * key events in special situations before the IME consumes them; a
6177 * typical example would be handling the BACK key to update the application's
6178 * UI instead of allowing the IME to see it and close itself.
6179 *
6180 * @param keyCode The value in event.getKeyCode().
6181 * @param event Description of the key event.
6182 * @return If you handled the event, return true. If you want to allow the
6183 * event to be handled by the next receiver, return false.
6184 */
6185 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6186 return false;
6187 }
6188
6189 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006190 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6191 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6193 * is released, if the view is enabled and clickable.
6194 *
6195 * @param keyCode A key code that represents the button pressed, from
6196 * {@link android.view.KeyEvent}.
6197 * @param event The KeyEvent object that defines the button action.
6198 */
6199 public boolean onKeyDown(int keyCode, KeyEvent event) {
6200 boolean result = false;
6201
6202 switch (keyCode) {
6203 case KeyEvent.KEYCODE_DPAD_CENTER:
6204 case KeyEvent.KEYCODE_ENTER: {
6205 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6206 return true;
6207 }
6208 // Long clickable items don't necessarily have to be clickable
6209 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6210 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6211 (event.getRepeatCount() == 0)) {
6212 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006213 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 return true;
6215 }
6216 break;
6217 }
6218 }
6219 return result;
6220 }
6221
6222 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006223 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6224 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6225 * the event).
6226 */
6227 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6228 return false;
6229 }
6230
6231 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006232 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6233 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6235 * {@link KeyEvent#KEYCODE_ENTER} is released.
6236 *
6237 * @param keyCode A key code that represents the button pressed, from
6238 * {@link android.view.KeyEvent}.
6239 * @param event The KeyEvent object that defines the button action.
6240 */
6241 public boolean onKeyUp(int keyCode, KeyEvent event) {
6242 boolean result = false;
6243
6244 switch (keyCode) {
6245 case KeyEvent.KEYCODE_DPAD_CENTER:
6246 case KeyEvent.KEYCODE_ENTER: {
6247 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6248 return true;
6249 }
6250 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6251 setPressed(false);
6252
6253 if (!mHasPerformedLongPress) {
6254 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006255 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256
6257 result = performClick();
6258 }
6259 }
6260 break;
6261 }
6262 }
6263 return result;
6264 }
6265
6266 /**
6267 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6268 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6269 * the event).
6270 *
6271 * @param keyCode A key code that represents the button pressed, from
6272 * {@link android.view.KeyEvent}.
6273 * @param repeatCount The number of times the action was made.
6274 * @param event The KeyEvent object that defines the button action.
6275 */
6276 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6277 return false;
6278 }
6279
6280 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006281 * Called on the focused view when a key shortcut event is not handled.
6282 * Override this method to implement local key shortcuts for the View.
6283 * Key shortcuts can also be implemented by setting the
6284 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 *
6286 * @param keyCode The value in event.getKeyCode().
6287 * @param event Description of the key event.
6288 * @return If you handled the event, return true. If you want to allow the
6289 * event to be handled by the next receiver, return false.
6290 */
6291 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6292 return false;
6293 }
6294
6295 /**
6296 * Check whether the called view is a text editor, in which case it
6297 * would make sense to automatically display a soft input window for
6298 * it. Subclasses should override this if they implement
6299 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006300 * a call on that method would return a non-null InputConnection, and
6301 * they are really a first-class editor that the user would normally
6302 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006303 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006304 * <p>The default implementation always returns false. This does
6305 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6306 * will not be called or the user can not otherwise perform edits on your
6307 * view; it is just a hint to the system that this is not the primary
6308 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 * @return Returns true if this view is a text editor, else false.
6311 */
6312 public boolean onCheckIsTextEditor() {
6313 return false;
6314 }
Romain Guy8506ab42009-06-11 17:35:47 -07006315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 /**
6317 * Create a new InputConnection for an InputMethod to interact
6318 * with the view. The default implementation returns null, since it doesn't
6319 * support input methods. You can override this to implement such support.
6320 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006321 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 * <p>When implementing this, you probably also want to implement
6323 * {@link #onCheckIsTextEditor()} to indicate you will return a
6324 * non-null InputConnection.
6325 *
6326 * @param outAttrs Fill in with attribute information about the connection.
6327 */
6328 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6329 return null;
6330 }
6331
6332 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006333 * Called by the {@link android.view.inputmethod.InputMethodManager}
6334 * when a view who is not the current
6335 * input connection target is trying to make a call on the manager. The
6336 * default implementation returns false; you can override this to return
6337 * true for certain views if you are performing InputConnection proxying
6338 * to them.
6339 * @param view The View that is making the InputMethodManager call.
6340 * @return Return true to allow the call, false to reject.
6341 */
6342 public boolean checkInputConnectionProxy(View view) {
6343 return false;
6344 }
Romain Guy8506ab42009-06-11 17:35:47 -07006345
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 * Show the context menu for this view. It is not safe to hold on to the
6348 * menu after returning from this method.
6349 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006350 * You should normally not overload this method. Overload
6351 * {@link #onCreateContextMenu(ContextMenu)} or define an
6352 * {@link OnCreateContextMenuListener} to add items to the context menu.
6353 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354 * @param menu The context menu to populate
6355 */
6356 public void createContextMenu(ContextMenu menu) {
6357 ContextMenuInfo menuInfo = getContextMenuInfo();
6358
6359 // Sets the current menu info so all items added to menu will have
6360 // my extra info set.
6361 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6362
6363 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006364 ListenerInfo li = mListenerInfo;
6365 if (li != null && li.mOnCreateContextMenuListener != null) {
6366 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 }
6368
6369 // Clear the extra information so subsequent items that aren't mine don't
6370 // have my extra info.
6371 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6372
6373 if (mParent != null) {
6374 mParent.createContextMenu(menu);
6375 }
6376 }
6377
6378 /**
6379 * Views should implement this if they have extra information to associate
6380 * with the context menu. The return result is supplied as a parameter to
6381 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6382 * callback.
6383 *
6384 * @return Extra information about the item for which the context menu
6385 * should be shown. This information will vary across different
6386 * subclasses of View.
6387 */
6388 protected ContextMenuInfo getContextMenuInfo() {
6389 return null;
6390 }
6391
6392 /**
6393 * Views should implement this if the view itself is going to add items to
6394 * the context menu.
6395 *
6396 * @param menu the context menu to populate
6397 */
6398 protected void onCreateContextMenu(ContextMenu menu) {
6399 }
6400
6401 /**
6402 * Implement this method to handle trackball motion events. The
6403 * <em>relative</em> movement of the trackball since the last event
6404 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6405 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6406 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6407 * they will often be fractional values, representing the more fine-grained
6408 * movement information available from a trackball).
6409 *
6410 * @param event The motion event.
6411 * @return True if the event was handled, false otherwise.
6412 */
6413 public boolean onTrackballEvent(MotionEvent event) {
6414 return false;
6415 }
6416
6417 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006418 * Implement this method to handle generic motion events.
6419 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006420 * Generic motion events describe joystick movements, mouse hovers, track pad
6421 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006422 * {@link MotionEvent#getSource() source} of the motion event specifies
6423 * the class of input that was received. Implementations of this method
6424 * must examine the bits in the source before processing the event.
6425 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006426 * </p><p>
6427 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6428 * are delivered to the view under the pointer. All other generic motion events are
6429 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006430 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006431 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006432 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006433 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6434 * // process the joystick movement...
6435 * return true;
6436 * }
6437 * }
6438 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6439 * switch (event.getAction()) {
6440 * case MotionEvent.ACTION_HOVER_MOVE:
6441 * // process the mouse hover movement...
6442 * return true;
6443 * case MotionEvent.ACTION_SCROLL:
6444 * // process the scroll wheel movement...
6445 * return true;
6446 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006447 * }
6448 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006449 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006450 *
6451 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006452 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006453 */
6454 public boolean onGenericMotionEvent(MotionEvent event) {
6455 return false;
6456 }
6457
6458 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006459 * Implement this method to handle hover events.
6460 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006461 * This method is called whenever a pointer is hovering into, over, or out of the
6462 * bounds of a view and the view is not currently being touched.
6463 * Hover events are represented as pointer events with action
6464 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6465 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6466 * </p>
6467 * <ul>
6468 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6469 * when the pointer enters the bounds of the view.</li>
6470 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6471 * when the pointer has already entered the bounds of the view and has moved.</li>
6472 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6473 * when the pointer has exited the bounds of the view or when the pointer is
6474 * about to go down due to a button click, tap, or similar user action that
6475 * causes the view to be touched.</li>
6476 * </ul>
6477 * <p>
6478 * The view should implement this method to return true to indicate that it is
6479 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006480 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006481 * The default implementation calls {@link #setHovered} to update the hovered state
6482 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006483 * is enabled and is clickable. The default implementation also sends hover
6484 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006485 * </p>
6486 *
6487 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006488 * @return True if the view handled the hover event.
6489 *
6490 * @see #isHovered
6491 * @see #setHovered
6492 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006493 */
6494 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006495 // The root view may receive hover (or touch) events that are outside the bounds of
6496 // the window. This code ensures that we only send accessibility events for
6497 // hovers that are actually within the bounds of the root view.
6498 final int action = event.getAction();
6499 if (!mSendingHoverAccessibilityEvents) {
6500 if ((action == MotionEvent.ACTION_HOVER_ENTER
6501 || action == MotionEvent.ACTION_HOVER_MOVE)
6502 && !hasHoveredChild()
6503 && pointInView(event.getX(), event.getY())) {
6504 mSendingHoverAccessibilityEvents = true;
6505 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6506 }
6507 } else {
6508 if (action == MotionEvent.ACTION_HOVER_EXIT
6509 || (action == MotionEvent.ACTION_HOVER_MOVE
6510 && !pointInView(event.getX(), event.getY()))) {
6511 mSendingHoverAccessibilityEvents = false;
6512 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6513 }
Jeff Browna1b24182011-07-28 13:38:24 -07006514 }
6515
Jeff Brown87b7f802011-06-21 18:35:45 -07006516 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006517 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006518 case MotionEvent.ACTION_HOVER_ENTER:
6519 setHovered(true);
6520 break;
6521 case MotionEvent.ACTION_HOVER_EXIT:
6522 setHovered(false);
6523 break;
6524 }
Jeff Browna1b24182011-07-28 13:38:24 -07006525
6526 // Dispatch the event to onGenericMotionEvent before returning true.
6527 // This is to provide compatibility with existing applications that
6528 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6529 // break because of the new default handling for hoverable views
6530 // in onHoverEvent.
6531 // Note that onGenericMotionEvent will be called by default when
6532 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6533 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006534 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006535 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006536 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006537 }
6538
6539 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006540 * Returns true if the view should handle {@link #onHoverEvent}
6541 * by calling {@link #setHovered} to change its hovered state.
6542 *
6543 * @return True if the view is hoverable.
6544 */
6545 private boolean isHoverable() {
6546 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006547 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006548 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6549 return false;
6550 }
6551
6552 return (viewFlags & CLICKABLE) == CLICKABLE
6553 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6554 }
6555
6556 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006557 * Returns true if the view is currently hovered.
6558 *
6559 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006560 *
6561 * @see #setHovered
6562 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006563 */
Jeff Brown10b62902011-06-20 16:40:37 -07006564 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006565 public boolean isHovered() {
6566 return (mPrivateFlags & HOVERED) != 0;
6567 }
6568
6569 /**
6570 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006571 * <p>
6572 * Calling this method also changes the drawable state of the view. This
6573 * enables the view to react to hover by using different drawable resources
6574 * to change its appearance.
6575 * </p><p>
6576 * The {@link #onHoverChanged} method is called when the hovered state changes.
6577 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006578 *
6579 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006580 *
6581 * @see #isHovered
6582 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006583 */
6584 public void setHovered(boolean hovered) {
6585 if (hovered) {
6586 if ((mPrivateFlags & HOVERED) == 0) {
6587 mPrivateFlags |= HOVERED;
6588 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006589 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006590 }
6591 } else {
6592 if ((mPrivateFlags & HOVERED) != 0) {
6593 mPrivateFlags &= ~HOVERED;
6594 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006595 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006596 }
6597 }
6598 }
6599
6600 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006601 * Implement this method to handle hover state changes.
6602 * <p>
6603 * This method is called whenever the hover state changes as a result of a
6604 * call to {@link #setHovered}.
6605 * </p>
6606 *
6607 * @param hovered The current hover state, as returned by {@link #isHovered}.
6608 *
6609 * @see #isHovered
6610 * @see #setHovered
6611 */
6612 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006613 }
6614
6615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 * Implement this method to handle touch screen motion events.
6617 *
6618 * @param event The motion event.
6619 * @return True if the event was handled, false otherwise.
6620 */
6621 public boolean onTouchEvent(MotionEvent event) {
6622 final int viewFlags = mViewFlags;
6623
6624 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006625 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006626 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 // A disabled view that is clickable still consumes the touch
6629 // events, it just doesn't respond to them.
6630 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6631 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6632 }
6633
6634 if (mTouchDelegate != null) {
6635 if (mTouchDelegate.onTouchEvent(event)) {
6636 return true;
6637 }
6638 }
6639
6640 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6641 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6642 switch (event.getAction()) {
6643 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006644 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6645 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006646 // take focus if we don't have it already and we should in
6647 // touch mode.
6648 boolean focusTaken = false;
6649 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6650 focusTaken = requestFocus();
6651 }
6652
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006653 if (prepressed) {
6654 // The button is being released before we actually
6655 // showed it as pressed. Make it show the pressed
6656 // state now (before scheduling the click) to ensure
6657 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006658 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006659 }
Joe Malin32736f02011-01-19 16:14:20 -08006660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 if (!mHasPerformedLongPress) {
6662 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006663 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664
6665 // Only perform take click actions if we were in the pressed state
6666 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006667 // Use a Runnable and post this rather than calling
6668 // performClick directly. This lets other visual state
6669 // of the view update before click actions start.
6670 if (mPerformClick == null) {
6671 mPerformClick = new PerformClick();
6672 }
6673 if (!post(mPerformClick)) {
6674 performClick();
6675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 }
6677 }
6678
6679 if (mUnsetPressedState == null) {
6680 mUnsetPressedState = new UnsetPressedState();
6681 }
6682
Adam Powelle14579b2009-12-16 18:39:52 -08006683 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006684 postDelayed(mUnsetPressedState,
6685 ViewConfiguration.getPressedStateDuration());
6686 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006687 // If the post failed, unpress right now
6688 mUnsetPressedState.run();
6689 }
Adam Powelle14579b2009-12-16 18:39:52 -08006690 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006691 }
6692 break;
6693
6694 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006695 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006696
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006697 if (performButtonActionOnTouchDown(event)) {
6698 break;
6699 }
6700
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006701 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006702 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006703
6704 // For views inside a scrolling container, delay the pressed feedback for
6705 // a short period in case this is a scroll.
6706 if (isInScrollingContainer) {
6707 mPrivateFlags |= PREPRESSED;
6708 if (mPendingCheckForTap == null) {
6709 mPendingCheckForTap = new CheckForTap();
6710 }
6711 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6712 } else {
6713 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006714 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006715 checkForLongClick(0);
6716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006717 break;
6718
6719 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006720 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006721 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006722 break;
6723
6724 case MotionEvent.ACTION_MOVE:
6725 final int x = (int) event.getX();
6726 final int y = (int) event.getY();
6727
6728 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006729 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006730 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006731 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006732 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006733 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006734 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006735
Adam Powell4d6f0662012-02-21 15:11:11 -08006736 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006738 }
6739 break;
6740 }
6741 return true;
6742 }
6743
6744 return false;
6745 }
6746
6747 /**
Adam Powell10298662011-08-14 18:26:30 -07006748 * @hide
6749 */
6750 public boolean isInScrollingContainer() {
6751 ViewParent p = getParent();
6752 while (p != null && p instanceof ViewGroup) {
6753 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6754 return true;
6755 }
6756 p = p.getParent();
6757 }
6758 return false;
6759 }
6760
6761 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006762 * Remove the longpress detection timer.
6763 */
6764 private void removeLongPressCallback() {
6765 if (mPendingCheckForLongPress != null) {
6766 removeCallbacks(mPendingCheckForLongPress);
6767 }
6768 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006769
6770 /**
6771 * Remove the pending click action
6772 */
6773 private void removePerformClickCallback() {
6774 if (mPerformClick != null) {
6775 removeCallbacks(mPerformClick);
6776 }
6777 }
6778
Adam Powelle14579b2009-12-16 18:39:52 -08006779 /**
Romain Guya440b002010-02-24 15:57:54 -08006780 * Remove the prepress detection timer.
6781 */
6782 private void removeUnsetPressCallback() {
6783 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6784 setPressed(false);
6785 removeCallbacks(mUnsetPressedState);
6786 }
6787 }
6788
6789 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006790 * Remove the tap detection timer.
6791 */
6792 private void removeTapCallback() {
6793 if (mPendingCheckForTap != null) {
6794 mPrivateFlags &= ~PREPRESSED;
6795 removeCallbacks(mPendingCheckForTap);
6796 }
6797 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006798
6799 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800 * Cancels a pending long press. Your subclass can use this if you
6801 * want the context menu to come up if the user presses and holds
6802 * at the same place, but you don't want it to come up if they press
6803 * and then move around enough to cause scrolling.
6804 */
6805 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006806 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006807
6808 /*
6809 * The prepressed state handled by the tap callback is a display
6810 * construct, but the tap callback will post a long press callback
6811 * less its own timeout. Remove it here.
6812 */
6813 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814 }
6815
6816 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006817 * Remove the pending callback for sending a
6818 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6819 */
6820 private void removeSendViewScrolledAccessibilityEventCallback() {
6821 if (mSendViewScrolledAccessibilityEvent != null) {
6822 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6823 }
6824 }
6825
6826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006827 * Sets the TouchDelegate for this View.
6828 */
6829 public void setTouchDelegate(TouchDelegate delegate) {
6830 mTouchDelegate = delegate;
6831 }
6832
6833 /**
6834 * Gets the TouchDelegate for this View.
6835 */
6836 public TouchDelegate getTouchDelegate() {
6837 return mTouchDelegate;
6838 }
6839
6840 /**
6841 * Set flags controlling behavior of this view.
6842 *
6843 * @param flags Constant indicating the value which should be set
6844 * @param mask Constant indicating the bit range that should be changed
6845 */
6846 void setFlags(int flags, int mask) {
6847 int old = mViewFlags;
6848 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6849
6850 int changed = mViewFlags ^ old;
6851 if (changed == 0) {
6852 return;
6853 }
6854 int privateFlags = mPrivateFlags;
6855
6856 /* Check if the FOCUSABLE bit has changed */
6857 if (((changed & FOCUSABLE_MASK) != 0) &&
6858 ((privateFlags & HAS_BOUNDS) !=0)) {
6859 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6860 && ((privateFlags & FOCUSED) != 0)) {
6861 /* Give up focus if we are no longer focusable */
6862 clearFocus();
6863 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6864 && ((privateFlags & FOCUSED) == 0)) {
6865 /*
6866 * Tell the view system that we are now available to take focus
6867 * if no one else already has it.
6868 */
6869 if (mParent != null) mParent.focusableViewAvailable(this);
6870 }
6871 }
6872
6873 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6874 if ((changed & VISIBILITY_MASK) != 0) {
6875 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006876 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006877 * it was not visible. Marking it drawn ensures that the invalidation will
6878 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006880 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006881 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006882
6883 needGlobalAttributesUpdate(true);
6884
6885 // a view becoming visible is worth notifying the parent
6886 // about in case nothing has focus. even if this specific view
6887 // isn't focusable, it may contain something that is, so let
6888 // the root view try to give this focus if nothing else does.
6889 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6890 mParent.focusableViewAvailable(this);
6891 }
6892 }
6893 }
6894
6895 /* Check if the GONE bit has changed */
6896 if ((changed & GONE) != 0) {
6897 needGlobalAttributesUpdate(false);
6898 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899
Romain Guyecd80ee2009-12-03 17:13:02 -08006900 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6901 if (hasFocus()) clearFocus();
6902 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006903 if (mParent instanceof View) {
6904 // GONE views noop invalidation, so invalidate the parent
6905 ((View) mParent).invalidate(true);
6906 }
6907 // Mark the view drawn to ensure that it gets invalidated properly the next
6908 // time it is visible and gets invalidated
6909 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 }
6911 if (mAttachInfo != null) {
6912 mAttachInfo.mViewVisibilityChanged = true;
6913 }
6914 }
6915
6916 /* Check if the VISIBLE bit has changed */
6917 if ((changed & INVISIBLE) != 0) {
6918 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006919 /*
6920 * If this view is becoming invisible, set the DRAWN flag so that
6921 * the next invalidate() will not be skipped.
6922 */
6923 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924
6925 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6926 // root view becoming invisible shouldn't clear focus
6927 if (getRootView() != this) {
6928 clearFocus();
6929 }
6930 }
6931 if (mAttachInfo != null) {
6932 mAttachInfo.mViewVisibilityChanged = true;
6933 }
6934 }
6935
Adam Powell326d8082009-12-09 15:10:07 -08006936 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006937 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006938 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6939 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006940 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006941 } else if (mParent != null) {
6942 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006943 }
Adam Powell326d8082009-12-09 15:10:07 -08006944 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6945 }
6946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6948 destroyDrawingCache();
6949 }
6950
6951 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6952 destroyDrawingCache();
6953 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006954 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 }
6956
6957 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6958 destroyDrawingCache();
6959 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6960 }
6961
6962 if ((changed & DRAW_MASK) != 0) {
6963 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6964 if (mBGDrawable != null) {
6965 mPrivateFlags &= ~SKIP_DRAW;
6966 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6967 } else {
6968 mPrivateFlags |= SKIP_DRAW;
6969 }
6970 } else {
6971 mPrivateFlags &= ~SKIP_DRAW;
6972 }
6973 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006974 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 }
6976
6977 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006978 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 mParent.recomputeViewAttributes(this);
6980 }
6981 }
6982 }
6983
6984 /**
6985 * Change the view's z order in the tree, so it's on top of other sibling
6986 * views
6987 */
6988 public void bringToFront() {
6989 if (mParent != null) {
6990 mParent.bringChildToFront(this);
6991 }
6992 }
6993
6994 /**
6995 * This is called in response to an internal scroll in this view (i.e., the
6996 * view scrolled its own contents). This is typically as a result of
6997 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6998 * called.
6999 *
7000 * @param l Current horizontal scroll origin.
7001 * @param t Current vertical scroll origin.
7002 * @param oldl Previous horizontal scroll origin.
7003 * @param oldt Previous vertical scroll origin.
7004 */
7005 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007006 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7007 postSendViewScrolledAccessibilityEventCallback();
7008 }
7009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007010 mBackgroundSizeChanged = true;
7011
7012 final AttachInfo ai = mAttachInfo;
7013 if (ai != null) {
7014 ai.mViewScrollChanged = true;
7015 }
7016 }
7017
7018 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007019 * Interface definition for a callback to be invoked when the layout bounds of a view
7020 * changes due to layout processing.
7021 */
7022 public interface OnLayoutChangeListener {
7023 /**
7024 * Called when the focus state of a view has changed.
7025 *
7026 * @param v The view whose state has changed.
7027 * @param left The new value of the view's left property.
7028 * @param top The new value of the view's top property.
7029 * @param right The new value of the view's right property.
7030 * @param bottom The new value of the view's bottom property.
7031 * @param oldLeft The previous value of the view's left property.
7032 * @param oldTop The previous value of the view's top property.
7033 * @param oldRight The previous value of the view's right property.
7034 * @param oldBottom The previous value of the view's bottom property.
7035 */
7036 void onLayoutChange(View v, int left, int top, int right, int bottom,
7037 int oldLeft, int oldTop, int oldRight, int oldBottom);
7038 }
7039
7040 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041 * This is called during layout when the size of this view has changed. If
7042 * you were just added to the view hierarchy, you're called with the old
7043 * values of 0.
7044 *
7045 * @param w Current width of this view.
7046 * @param h Current height of this view.
7047 * @param oldw Old width of this view.
7048 * @param oldh Old height of this view.
7049 */
7050 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7051 }
7052
7053 /**
7054 * Called by draw to draw the child views. This may be overridden
7055 * by derived classes to gain control just before its children are drawn
7056 * (but after its own view has been drawn).
7057 * @param canvas the canvas on which to draw the view
7058 */
7059 protected void dispatchDraw(Canvas canvas) {
7060 }
7061
7062 /**
7063 * Gets the parent of this view. Note that the parent is a
7064 * ViewParent and not necessarily a View.
7065 *
7066 * @return Parent of this view.
7067 */
7068 public final ViewParent getParent() {
7069 return mParent;
7070 }
7071
7072 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007073 * Set the horizontal scrolled position of your view. This will cause a call to
7074 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7075 * invalidated.
7076 * @param value the x position to scroll to
7077 */
7078 public void setScrollX(int value) {
7079 scrollTo(value, mScrollY);
7080 }
7081
7082 /**
7083 * Set the vertical scrolled position of your view. This will cause a call to
7084 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7085 * invalidated.
7086 * @param value the y position to scroll to
7087 */
7088 public void setScrollY(int value) {
7089 scrollTo(mScrollX, value);
7090 }
7091
7092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 * Return the scrolled left position of this view. This is the left edge of
7094 * the displayed part of your view. You do not need to draw any pixels
7095 * farther left, since those are outside of the frame of your view on
7096 * screen.
7097 *
7098 * @return The left edge of the displayed part of your view, in pixels.
7099 */
7100 public final int getScrollX() {
7101 return mScrollX;
7102 }
7103
7104 /**
7105 * Return the scrolled top position of this view. This is the top edge of
7106 * the displayed part of your view. You do not need to draw any pixels above
7107 * it, since those are outside of the frame of your view on screen.
7108 *
7109 * @return The top edge of the displayed part of your view, in pixels.
7110 */
7111 public final int getScrollY() {
7112 return mScrollY;
7113 }
7114
7115 /**
7116 * Return the width of the your view.
7117 *
7118 * @return The width of your view, in pixels.
7119 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007120 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 public final int getWidth() {
7122 return mRight - mLeft;
7123 }
7124
7125 /**
7126 * Return the height of your view.
7127 *
7128 * @return The height of your view, in pixels.
7129 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007130 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 public final int getHeight() {
7132 return mBottom - mTop;
7133 }
7134
7135 /**
7136 * Return the visible drawing bounds of your view. Fills in the output
7137 * rectangle with the values from getScrollX(), getScrollY(),
7138 * getWidth(), and getHeight().
7139 *
7140 * @param outRect The (scrolled) drawing bounds of the view.
7141 */
7142 public void getDrawingRect(Rect outRect) {
7143 outRect.left = mScrollX;
7144 outRect.top = mScrollY;
7145 outRect.right = mScrollX + (mRight - mLeft);
7146 outRect.bottom = mScrollY + (mBottom - mTop);
7147 }
7148
7149 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007150 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7151 * raw width component (that is the result is masked by
7152 * {@link #MEASURED_SIZE_MASK}).
7153 *
7154 * @return The raw measured width of this view.
7155 */
7156 public final int getMeasuredWidth() {
7157 return mMeasuredWidth & MEASURED_SIZE_MASK;
7158 }
7159
7160 /**
7161 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007162 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007163 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 * This should be used during measurement and layout calculations only. Use
7165 * {@link #getWidth()} to see how wide a view is after layout.
7166 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007167 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007169 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170 return mMeasuredWidth;
7171 }
7172
7173 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007174 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7175 * raw width component (that is the result is masked by
7176 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007178 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 */
7180 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007181 return mMeasuredHeight & MEASURED_SIZE_MASK;
7182 }
7183
7184 /**
7185 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007186 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007187 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7188 * This should be used during measurement and layout calculations only. Use
7189 * {@link #getHeight()} to see how wide a view is after layout.
7190 *
7191 * @return The measured width of this view as a bit mask.
7192 */
7193 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194 return mMeasuredHeight;
7195 }
7196
7197 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007198 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7199 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7200 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7201 * and the height component is at the shifted bits
7202 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7203 */
7204 public final int getMeasuredState() {
7205 return (mMeasuredWidth&MEASURED_STATE_MASK)
7206 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7207 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7208 }
7209
7210 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007211 * The transform matrix of this view, which is calculated based on the current
7212 * roation, scale, and pivot properties.
7213 *
7214 * @see #getRotation()
7215 * @see #getScaleX()
7216 * @see #getScaleY()
7217 * @see #getPivotX()
7218 * @see #getPivotY()
7219 * @return The current transform matrix for the view
7220 */
7221 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007222 if (mTransformationInfo != null) {
7223 updateMatrix();
7224 return mTransformationInfo.mMatrix;
7225 }
7226 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007227 }
7228
7229 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007230 * Utility function to determine if the value is far enough away from zero to be
7231 * considered non-zero.
7232 * @param value A floating point value to check for zero-ness
7233 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7234 */
7235 private static boolean nonzero(float value) {
7236 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7237 }
7238
7239 /**
Jeff Brown86671742010-09-30 20:00:15 -07007240 * Returns true if the transform matrix is the identity matrix.
7241 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007242 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007243 * @return True if the transform matrix is the identity matrix, false otherwise.
7244 */
Jeff Brown86671742010-09-30 20:00:15 -07007245 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007246 if (mTransformationInfo != null) {
7247 updateMatrix();
7248 return mTransformationInfo.mMatrixIsIdentity;
7249 }
7250 return true;
7251 }
7252
7253 void ensureTransformationInfo() {
7254 if (mTransformationInfo == null) {
7255 mTransformationInfo = new TransformationInfo();
7256 }
Jeff Brown86671742010-09-30 20:00:15 -07007257 }
7258
7259 /**
7260 * Recomputes the transform matrix if necessary.
7261 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007262 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007263 final TransformationInfo info = mTransformationInfo;
7264 if (info == null) {
7265 return;
7266 }
7267 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007268 // transform-related properties have changed since the last time someone
7269 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007270
7271 // Figure out if we need to update the pivot point
7272 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007273 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7274 info.mPrevWidth = mRight - mLeft;
7275 info.mPrevHeight = mBottom - mTop;
7276 info.mPivotX = info.mPrevWidth / 2f;
7277 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007278 }
7279 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007280 info.mMatrix.reset();
7281 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7282 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7283 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7284 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007285 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007286 if (info.mCamera == null) {
7287 info.mCamera = new Camera();
7288 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007289 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007290 info.mCamera.save();
7291 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7292 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7293 info.mCamera.getMatrix(info.matrix3D);
7294 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7295 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7296 info.mPivotY + info.mTranslationY);
7297 info.mMatrix.postConcat(info.matrix3D);
7298 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007299 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007300 info.mMatrixDirty = false;
7301 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7302 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007303 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007304 }
7305
7306 /**
7307 * Utility method to retrieve the inverse of the current mMatrix property.
7308 * We cache the matrix to avoid recalculating it when transform properties
7309 * have not changed.
7310 *
7311 * @return The inverse of the current matrix of this view.
7312 */
Jeff Brown86671742010-09-30 20:00:15 -07007313 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007314 final TransformationInfo info = mTransformationInfo;
7315 if (info != null) {
7316 updateMatrix();
7317 if (info.mInverseMatrixDirty) {
7318 if (info.mInverseMatrix == null) {
7319 info.mInverseMatrix = new Matrix();
7320 }
7321 info.mMatrix.invert(info.mInverseMatrix);
7322 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007323 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007324 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007325 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007326 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007327 }
7328
7329 /**
Chet Haasea1cff502012-02-21 13:43:44 -08007330 * Gets the distance along the Z axis from the camera to this view.
7331 *
7332 * @see #setCameraDistance(float)
7333 *
7334 * @return The distance along the Z axis.
7335 */
7336 public float getCameraDistance() {
7337 ensureTransformationInfo();
7338 final float dpi = mResources.getDisplayMetrics().densityDpi;
7339 final TransformationInfo info = mTransformationInfo;
7340 if (info.mCamera == null) {
7341 info.mCamera = new Camera();
7342 info.matrix3D = new Matrix();
7343 }
7344 return -(info.mCamera.getLocationZ() * dpi);
7345 }
7346
7347 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007348 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7349 * views are drawn) from the camera to this view. The camera's distance
7350 * affects 3D transformations, for instance rotations around the X and Y
7351 * axis. If the rotationX or rotationY properties are changed and this view is
7352 * large (more than half the size of the screen), it is recommended to always
7353 * use a camera distance that's greater than the height (X axis rotation) or
7354 * the width (Y axis rotation) of this view.</p>
7355 *
7356 * <p>The distance of the camera from the view plane can have an affect on the
7357 * perspective distortion of the view when it is rotated around the x or y axis.
7358 * For example, a large distance will result in a large viewing angle, and there
7359 * will not be much perspective distortion of the view as it rotates. A short
7360 * distance may cause much more perspective distortion upon rotation, and can
7361 * also result in some drawing artifacts if the rotated view ends up partially
7362 * behind the camera (which is why the recommendation is to use a distance at
7363 * least as far as the size of the view, if the view is to be rotated.)</p>
7364 *
7365 * <p>The distance is expressed in "depth pixels." The default distance depends
7366 * on the screen density. For instance, on a medium density display, the
7367 * default distance is 1280. On a high density display, the default distance
7368 * is 1920.</p>
7369 *
7370 * <p>If you want to specify a distance that leads to visually consistent
7371 * results across various densities, use the following formula:</p>
7372 * <pre>
7373 * float scale = context.getResources().getDisplayMetrics().density;
7374 * view.setCameraDistance(distance * scale);
7375 * </pre>
7376 *
7377 * <p>The density scale factor of a high density display is 1.5,
7378 * and 1920 = 1280 * 1.5.</p>
7379 *
7380 * @param distance The distance in "depth pixels", if negative the opposite
7381 * value is used
7382 *
7383 * @see #setRotationX(float)
7384 * @see #setRotationY(float)
7385 */
7386 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007387 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08007388
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007389 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007390 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007391 final TransformationInfo info = mTransformationInfo;
7392 if (info.mCamera == null) {
7393 info.mCamera = new Camera();
7394 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007395 }
7396
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007397 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7398 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007399
Chet Haase9d1992d2012-03-13 11:03:25 -07007400 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007401 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7402 mDisplayList.setCameraDistance(distance);
7403 }
Romain Guya5364ee2011-02-24 14:46:04 -08007404 }
7405
7406 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007407 * The degrees that the view is rotated around the pivot point.
7408 *
Romain Guya5364ee2011-02-24 14:46:04 -08007409 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007410 * @see #getPivotX()
7411 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007412 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007413 * @return The degrees of rotation.
7414 */
Chet Haasea5531132012-02-02 13:41:44 -08007415 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007416 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007417 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007418 }
7419
7420 /**
Chet Haase897247b2010-09-09 14:54:47 -07007421 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7422 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007423 *
7424 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007425 *
7426 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007427 * @see #getPivotX()
7428 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007429 * @see #setRotationX(float)
7430 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007431 *
7432 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007433 */
7434 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007435 ensureTransformationInfo();
7436 final TransformationInfo info = mTransformationInfo;
7437 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007438 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07007439 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007440 info.mRotation = rotation;
7441 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007442 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007443 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7444 mDisplayList.setRotation(rotation);
7445 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007446 }
7447 }
7448
7449 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007450 * The degrees that the view is rotated around the vertical axis through the pivot point.
7451 *
7452 * @see #getPivotX()
7453 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007454 * @see #setRotationY(float)
7455 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007456 * @return The degrees of Y rotation.
7457 */
Chet Haasea5531132012-02-02 13:41:44 -08007458 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007459 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007460 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007461 }
7462
7463 /**
Chet Haase897247b2010-09-09 14:54:47 -07007464 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7465 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7466 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007467 *
7468 * When rotating large views, it is recommended to adjust the camera distance
7469 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007470 *
7471 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007472 *
7473 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007474 * @see #getPivotX()
7475 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007476 * @see #setRotation(float)
7477 * @see #setRotationX(float)
7478 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007479 *
7480 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007481 */
7482 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007483 ensureTransformationInfo();
7484 final TransformationInfo info = mTransformationInfo;
7485 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007486 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007487 info.mRotationY = rotationY;
7488 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007489 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007490 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7491 mDisplayList.setRotationY(rotationY);
7492 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007493 }
7494 }
7495
7496 /**
7497 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7498 *
7499 * @see #getPivotX()
7500 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007501 * @see #setRotationX(float)
7502 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007503 * @return The degrees of X rotation.
7504 */
Chet Haasea5531132012-02-02 13:41:44 -08007505 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007506 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007507 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007508 }
7509
7510 /**
Chet Haase897247b2010-09-09 14:54:47 -07007511 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7512 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7513 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007514 *
7515 * When rotating large views, it is recommended to adjust the camera distance
7516 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007517 *
7518 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007519 *
7520 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007521 * @see #getPivotX()
7522 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007523 * @see #setRotation(float)
7524 * @see #setRotationY(float)
7525 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007526 *
7527 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007528 */
7529 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007530 ensureTransformationInfo();
7531 final TransformationInfo info = mTransformationInfo;
7532 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007533 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007534 info.mRotationX = rotationX;
7535 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007536 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007537 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7538 mDisplayList.setRotationX(rotationX);
7539 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007540 }
7541 }
7542
7543 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007544 * The amount that the view is scaled in x around the pivot point, as a proportion of
7545 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7546 *
Joe Onorato93162322010-09-16 15:42:01 -04007547 * <p>By default, this is 1.0f.
7548 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007549 * @see #getPivotX()
7550 * @see #getPivotY()
7551 * @return The scaling factor.
7552 */
Chet Haasea5531132012-02-02 13:41:44 -08007553 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007554 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007555 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007556 }
7557
7558 /**
7559 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7560 * the view's unscaled width. A value of 1 means that no scaling is applied.
7561 *
7562 * @param scaleX The scaling factor.
7563 * @see #getPivotX()
7564 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007565 *
7566 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007567 */
7568 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007569 ensureTransformationInfo();
7570 final TransformationInfo info = mTransformationInfo;
7571 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007572 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007573 info.mScaleX = scaleX;
7574 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007575 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007576 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7577 mDisplayList.setScaleX(scaleX);
7578 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007579 }
7580 }
7581
7582 /**
7583 * The amount that the view is scaled in y around the pivot point, as a proportion of
7584 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7585 *
Joe Onorato93162322010-09-16 15:42:01 -04007586 * <p>By default, this is 1.0f.
7587 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007588 * @see #getPivotX()
7589 * @see #getPivotY()
7590 * @return The scaling factor.
7591 */
Chet Haasea5531132012-02-02 13:41:44 -08007592 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007593 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007594 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007595 }
7596
7597 /**
7598 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7599 * the view's unscaled width. A value of 1 means that no scaling is applied.
7600 *
7601 * @param scaleY The scaling factor.
7602 * @see #getPivotX()
7603 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007604 *
7605 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007606 */
7607 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007608 ensureTransformationInfo();
7609 final TransformationInfo info = mTransformationInfo;
7610 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007611 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007612 info.mScaleY = scaleY;
7613 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007614 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007615 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7616 mDisplayList.setScaleY(scaleY);
7617 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007618 }
7619 }
7620
7621 /**
7622 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7623 * and {@link #setScaleX(float) scaled}.
7624 *
7625 * @see #getRotation()
7626 * @see #getScaleX()
7627 * @see #getScaleY()
7628 * @see #getPivotY()
7629 * @return The x location of the pivot point.
7630 */
Chet Haasea5531132012-02-02 13:41:44 -08007631 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007632 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007633 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007634 }
7635
7636 /**
7637 * Sets the x location of the point around which the view is
7638 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007639 * By default, the pivot point is centered on the object.
7640 * Setting this property disables this behavior and causes the view to use only the
7641 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007642 *
7643 * @param pivotX The x location of the pivot point.
7644 * @see #getRotation()
7645 * @see #getScaleX()
7646 * @see #getScaleY()
7647 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007648 *
7649 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007650 */
7651 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007652 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007653 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007654 final TransformationInfo info = mTransformationInfo;
7655 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007656 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007657 info.mPivotX = pivotX;
7658 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007659 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007660 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7661 mDisplayList.setPivotX(pivotX);
7662 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007663 }
7664 }
7665
7666 /**
7667 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7668 * and {@link #setScaleY(float) scaled}.
7669 *
7670 * @see #getRotation()
7671 * @see #getScaleX()
7672 * @see #getScaleY()
7673 * @see #getPivotY()
7674 * @return The y location of the pivot point.
7675 */
Chet Haasea5531132012-02-02 13:41:44 -08007676 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007677 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007678 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007679 }
7680
7681 /**
7682 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007683 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7684 * Setting this property disables this behavior and causes the view to use only the
7685 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007686 *
7687 * @param pivotY The y location of the pivot point.
7688 * @see #getRotation()
7689 * @see #getScaleX()
7690 * @see #getScaleY()
7691 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007692 *
7693 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007694 */
7695 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007696 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007697 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007698 final TransformationInfo info = mTransformationInfo;
7699 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007700 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007701 info.mPivotY = pivotY;
7702 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007703 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007704 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7705 mDisplayList.setPivotY(pivotY);
7706 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007707 }
7708 }
7709
7710 /**
7711 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7712 * completely transparent and 1 means the view is completely opaque.
7713 *
Joe Onorato93162322010-09-16 15:42:01 -04007714 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007715 * @return The opacity of the view.
7716 */
Chet Haasea5531132012-02-02 13:41:44 -08007717 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007718 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007719 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007720 }
7721
7722 /**
Romain Guy171c5922011-01-06 10:04:23 -08007723 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7724 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007725 *
Romain Guy171c5922011-01-06 10:04:23 -08007726 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7727 * responsible for applying the opacity itself. Otherwise, calling this method is
7728 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007729 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007730 *
Chet Haasea5531132012-02-02 13:41:44 -08007731 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7732 * performance implications. It is generally best to use the alpha property sparingly and
7733 * transiently, as in the case of fading animations.</p>
7734 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007735 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007736 *
Joe Malin32736f02011-01-19 16:14:20 -08007737 * @see #setLayerType(int, android.graphics.Paint)
7738 *
Chet Haase73066682010-11-29 15:55:32 -08007739 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007740 */
7741 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007742 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007743 if (mTransformationInfo.mAlpha != alpha) {
7744 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007745 if (onSetAlpha((int) (alpha * 255))) {
7746 mPrivateFlags |= ALPHA_SET;
7747 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07007748 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007749 invalidate(true);
7750 } else {
7751 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07007752 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007753 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7754 mDisplayList.setAlpha(alpha);
7755 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007756 }
Chet Haaseed032702010-10-01 14:05:54 -07007757 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007758 }
7759
7760 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007761 * Faster version of setAlpha() which performs the same steps except there are
7762 * no calls to invalidate(). The caller of this function should perform proper invalidation
7763 * on the parent and this object. The return value indicates whether the subclass handles
7764 * alpha (the return value for onSetAlpha()).
7765 *
7766 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007767 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7768 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007769 */
7770 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007771 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007772 if (mTransformationInfo.mAlpha != alpha) {
7773 mTransformationInfo.mAlpha = alpha;
7774 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7775 if (subclassHandlesAlpha) {
7776 mPrivateFlags |= ALPHA_SET;
7777 return true;
7778 } else {
7779 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08007780 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7781 mDisplayList.setAlpha(alpha);
7782 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007783 }
Chet Haasea00f3862011-02-22 06:34:40 -08007784 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007785 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007786 }
7787
7788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 * Top position of this view relative to its parent.
7790 *
7791 * @return The top of this view, in pixels.
7792 */
7793 @ViewDebug.CapturedViewProperty
7794 public final int getTop() {
7795 return mTop;
7796 }
7797
7798 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007799 * Sets the top position of this view relative to its parent. This method is meant to be called
7800 * by the layout system and should not generally be called otherwise, because the property
7801 * may be changed at any time by the layout.
7802 *
7803 * @param top The top of this view, in pixels.
7804 */
7805 public final void setTop(int top) {
7806 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007807 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007808 final boolean matrixIsIdentity = mTransformationInfo == null
7809 || mTransformationInfo.mMatrixIsIdentity;
7810 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007811 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007812 int minTop;
7813 int yLoc;
7814 if (top < mTop) {
7815 minTop = top;
7816 yLoc = top - mTop;
7817 } else {
7818 minTop = mTop;
7819 yLoc = 0;
7820 }
Chet Haasee9140a72011-02-16 16:23:29 -08007821 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007822 }
7823 } else {
7824 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007825 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007826 }
7827
Chet Haaseed032702010-10-01 14:05:54 -07007828 int width = mRight - mLeft;
7829 int oldHeight = mBottom - mTop;
7830
Chet Haase21cd1382010-09-01 17:42:29 -07007831 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08007832 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7833 mDisplayList.setTop(mTop);
7834 }
Chet Haase21cd1382010-09-01 17:42:29 -07007835
Chet Haaseed032702010-10-01 14:05:54 -07007836 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7837
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007838 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007839 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7840 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007841 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007842 }
Chet Haase21cd1382010-09-01 17:42:29 -07007843 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007844 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007845 }
Chet Haase55dbb652010-12-21 20:15:08 -08007846 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007847 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007848 }
7849 }
7850
7851 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007852 * Bottom position of this view relative to its parent.
7853 *
7854 * @return The bottom of this view, in pixels.
7855 */
7856 @ViewDebug.CapturedViewProperty
7857 public final int getBottom() {
7858 return mBottom;
7859 }
7860
7861 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007862 * True if this view has changed since the last time being drawn.
7863 *
7864 * @return The dirty state of this view.
7865 */
7866 public boolean isDirty() {
7867 return (mPrivateFlags & DIRTY_MASK) != 0;
7868 }
7869
7870 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007871 * Sets the bottom position of this view relative to its parent. This method is meant to be
7872 * called by the layout system and should not generally be called otherwise, because the
7873 * property may be changed at any time by the layout.
7874 *
7875 * @param bottom The bottom of this view, in pixels.
7876 */
7877 public final void setBottom(int bottom) {
7878 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007879 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007880 final boolean matrixIsIdentity = mTransformationInfo == null
7881 || mTransformationInfo.mMatrixIsIdentity;
7882 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007883 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007884 int maxBottom;
7885 if (bottom < mBottom) {
7886 maxBottom = mBottom;
7887 } else {
7888 maxBottom = bottom;
7889 }
Chet Haasee9140a72011-02-16 16:23:29 -08007890 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007891 }
7892 } else {
7893 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007894 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007895 }
7896
Chet Haaseed032702010-10-01 14:05:54 -07007897 int width = mRight - mLeft;
7898 int oldHeight = mBottom - mTop;
7899
Chet Haase21cd1382010-09-01 17:42:29 -07007900 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08007901 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7902 mDisplayList.setBottom(mBottom);
7903 }
Chet Haase21cd1382010-09-01 17:42:29 -07007904
Chet Haaseed032702010-10-01 14:05:54 -07007905 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7906
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007907 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007908 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7909 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007910 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007911 }
Chet Haase21cd1382010-09-01 17:42:29 -07007912 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007913 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007914 }
Chet Haase55dbb652010-12-21 20:15:08 -08007915 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007916 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007917 }
7918 }
7919
7920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 * Left position of this view relative to its parent.
7922 *
7923 * @return The left edge of this view, in pixels.
7924 */
7925 @ViewDebug.CapturedViewProperty
7926 public final int getLeft() {
7927 return mLeft;
7928 }
7929
7930 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007931 * Sets the left position of this view relative to its parent. This method is meant to be called
7932 * by the layout system and should not generally be called otherwise, because the property
7933 * may be changed at any time by the layout.
7934 *
7935 * @param left The bottom of this view, in pixels.
7936 */
7937 public final void setLeft(int left) {
7938 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007939 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007940 final boolean matrixIsIdentity = mTransformationInfo == null
7941 || mTransformationInfo.mMatrixIsIdentity;
7942 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007943 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007944 int minLeft;
7945 int xLoc;
7946 if (left < mLeft) {
7947 minLeft = left;
7948 xLoc = left - mLeft;
7949 } else {
7950 minLeft = mLeft;
7951 xLoc = 0;
7952 }
Chet Haasee9140a72011-02-16 16:23:29 -08007953 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007954 }
7955 } else {
7956 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007957 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007958 }
7959
Chet Haaseed032702010-10-01 14:05:54 -07007960 int oldWidth = mRight - mLeft;
7961 int height = mBottom - mTop;
7962
Chet Haase21cd1382010-09-01 17:42:29 -07007963 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08007964 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7965 mDisplayList.setLeft(left);
7966 }
Chet Haase21cd1382010-09-01 17:42:29 -07007967
Chet Haaseed032702010-10-01 14:05:54 -07007968 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7969
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007970 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007971 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7972 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007973 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007974 }
Chet Haase21cd1382010-09-01 17:42:29 -07007975 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007976 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007977 }
Chet Haase55dbb652010-12-21 20:15:08 -08007978 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007979 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08007980 if (USE_DISPLAY_LIST_PROPERTIES) {
7981
7982 }
Chet Haase21cd1382010-09-01 17:42:29 -07007983 }
7984 }
7985
7986 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007987 * Right position of this view relative to its parent.
7988 *
7989 * @return The right edge of this view, in pixels.
7990 */
7991 @ViewDebug.CapturedViewProperty
7992 public final int getRight() {
7993 return mRight;
7994 }
7995
7996 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007997 * Sets the right position of this view relative to its parent. This method is meant to be called
7998 * by the layout system and should not generally be called otherwise, because the property
7999 * may be changed at any time by the layout.
8000 *
8001 * @param right The bottom of this view, in pixels.
8002 */
8003 public final void setRight(int right) {
8004 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008005 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008006 final boolean matrixIsIdentity = mTransformationInfo == null
8007 || mTransformationInfo.mMatrixIsIdentity;
8008 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008009 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008010 int maxRight;
8011 if (right < mRight) {
8012 maxRight = mRight;
8013 } else {
8014 maxRight = right;
8015 }
Chet Haasee9140a72011-02-16 16:23:29 -08008016 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008017 }
8018 } else {
8019 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008020 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008021 }
8022
Chet Haaseed032702010-10-01 14:05:54 -07008023 int oldWidth = mRight - mLeft;
8024 int height = mBottom - mTop;
8025
Chet Haase21cd1382010-09-01 17:42:29 -07008026 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008027 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8028 mDisplayList.setRight(mRight);
8029 }
Chet Haase21cd1382010-09-01 17:42:29 -07008030
Chet Haaseed032702010-10-01 14:05:54 -07008031 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8032
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008033 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008034 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8035 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008036 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008037 }
Chet Haase21cd1382010-09-01 17:42:29 -07008038 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008039 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008040 }
Chet Haase55dbb652010-12-21 20:15:08 -08008041 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008042 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008043 }
8044 }
8045
8046 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008047 * The visual x position of this view, in pixels. This is equivalent to the
8048 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008049 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008050 *
Chet Haasedf030d22010-07-30 17:22:38 -07008051 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008052 */
Chet Haasea5531132012-02-02 13:41:44 -08008053 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008054 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008055 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008056 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008057
Chet Haasedf030d22010-07-30 17:22:38 -07008058 /**
8059 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8060 * {@link #setTranslationX(float) translationX} property to be the difference between
8061 * the x value passed in and the current {@link #getLeft() left} property.
8062 *
8063 * @param x The visual x position of this view, in pixels.
8064 */
8065 public void setX(float x) {
8066 setTranslationX(x - mLeft);
8067 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008068
Chet Haasedf030d22010-07-30 17:22:38 -07008069 /**
8070 * The visual y position of this view, in pixels. This is equivalent to the
8071 * {@link #setTranslationY(float) translationY} property plus the current
8072 * {@link #getTop() top} property.
8073 *
8074 * @return The visual y position of this view, in pixels.
8075 */
Chet Haasea5531132012-02-02 13:41:44 -08008076 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008077 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008078 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008079 }
8080
8081 /**
8082 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8083 * {@link #setTranslationY(float) translationY} property to be the difference between
8084 * the y value passed in and the current {@link #getTop() top} property.
8085 *
8086 * @param y The visual y position of this view, in pixels.
8087 */
8088 public void setY(float y) {
8089 setTranslationY(y - mTop);
8090 }
8091
8092
8093 /**
8094 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8095 * This position is post-layout, in addition to wherever the object's
8096 * layout placed it.
8097 *
8098 * @return The horizontal position of this view relative to its left position, in pixels.
8099 */
Chet Haasea5531132012-02-02 13:41:44 -08008100 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008101 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008102 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008103 }
8104
8105 /**
8106 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8107 * This effectively positions the object post-layout, in addition to wherever the object's
8108 * layout placed it.
8109 *
8110 * @param translationX The horizontal position of this view relative to its left position,
8111 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008112 *
8113 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008114 */
8115 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008116 ensureTransformationInfo();
8117 final TransformationInfo info = mTransformationInfo;
8118 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008119 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008120 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008121 info.mTranslationX = translationX;
8122 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008123 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008124 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8125 mDisplayList.setTranslationX(translationX);
8126 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008127 }
8128 }
8129
8130 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008131 * The horizontal location of this view relative to its {@link #getTop() top} position.
8132 * This position is post-layout, in addition to wherever the object's
8133 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008134 *
Chet Haasedf030d22010-07-30 17:22:38 -07008135 * @return The vertical position of this view relative to its top position,
8136 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008137 */
Chet Haasea5531132012-02-02 13:41:44 -08008138 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008139 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008140 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008141 }
8142
8143 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008144 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8145 * This effectively positions the object post-layout, in addition to wherever the object's
8146 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008147 *
Chet Haasedf030d22010-07-30 17:22:38 -07008148 * @param translationY The vertical position of this view relative to its top position,
8149 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008150 *
8151 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008152 */
Chet Haasedf030d22010-07-30 17:22:38 -07008153 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008154 ensureTransformationInfo();
8155 final TransformationInfo info = mTransformationInfo;
8156 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008157 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008158 info.mTranslationY = translationY;
8159 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008160 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008161 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8162 mDisplayList.setTranslationY(translationY);
8163 }
Chet Haasedf030d22010-07-30 17:22:38 -07008164 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008165 }
8166
8167 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168 * Hit rectangle in parent's coordinates
8169 *
8170 * @param outRect The hit rectangle of the view.
8171 */
8172 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008173 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008174 final TransformationInfo info = mTransformationInfo;
8175 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008176 outRect.set(mLeft, mTop, mRight, mBottom);
8177 } else {
8178 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008179 tmpRect.set(-info.mPivotX, -info.mPivotY,
8180 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8181 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008182 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8183 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008184 }
8185 }
8186
8187 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008188 * Determines whether the given point, in local coordinates is inside the view.
8189 */
8190 /*package*/ final boolean pointInView(float localX, float localY) {
8191 return localX >= 0 && localX < (mRight - mLeft)
8192 && localY >= 0 && localY < (mBottom - mTop);
8193 }
8194
8195 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008196 * Utility method to determine whether the given point, in local coordinates,
8197 * is inside the view, where the area of the view is expanded by the slop factor.
8198 * This method is called while processing touch-move events to determine if the event
8199 * is still within the view.
8200 */
8201 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008202 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008203 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008204 }
8205
8206 /**
8207 * When a view has focus and the user navigates away from it, the next view is searched for
8208 * starting from the rectangle filled in by this method.
8209 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008210 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8211 * of the view. However, if your view maintains some idea of internal selection,
8212 * such as a cursor, or a selected row or column, you should override this method and
8213 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 *
8215 * @param r The rectangle to fill in, in this view's coordinates.
8216 */
8217 public void getFocusedRect(Rect r) {
8218 getDrawingRect(r);
8219 }
8220
8221 /**
8222 * If some part of this view is not clipped by any of its parents, then
8223 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008224 * coordinates (without taking possible View rotations into account), offset
8225 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8226 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008227 *
8228 * @param r If true is returned, r holds the global coordinates of the
8229 * visible portion of this view.
8230 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8231 * between this view and its root. globalOffet may be null.
8232 * @return true if r is non-empty (i.e. part of the view is visible at the
8233 * root level.
8234 */
8235 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8236 int width = mRight - mLeft;
8237 int height = mBottom - mTop;
8238 if (width > 0 && height > 0) {
8239 r.set(0, 0, width, height);
8240 if (globalOffset != null) {
8241 globalOffset.set(-mScrollX, -mScrollY);
8242 }
8243 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8244 }
8245 return false;
8246 }
8247
8248 public final boolean getGlobalVisibleRect(Rect r) {
8249 return getGlobalVisibleRect(r, null);
8250 }
8251
8252 public final boolean getLocalVisibleRect(Rect r) {
8253 Point offset = new Point();
8254 if (getGlobalVisibleRect(r, offset)) {
8255 r.offset(-offset.x, -offset.y); // make r local
8256 return true;
8257 }
8258 return false;
8259 }
8260
8261 /**
8262 * Offset this view's vertical location by the specified number of pixels.
8263 *
8264 * @param offset the number of pixels to offset the view by
8265 */
8266 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008267 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008268 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008269 final boolean matrixIsIdentity = mTransformationInfo == null
8270 || mTransformationInfo.mMatrixIsIdentity;
8271 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008272 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8273 invalidateViewProperty(false, false);
8274 } else {
8275 final ViewParent p = mParent;
8276 if (p != null && mAttachInfo != null) {
8277 final Rect r = mAttachInfo.mTmpInvalRect;
8278 int minTop;
8279 int maxBottom;
8280 int yLoc;
8281 if (offset < 0) {
8282 minTop = mTop + offset;
8283 maxBottom = mBottom;
8284 yLoc = offset;
8285 } else {
8286 minTop = mTop;
8287 maxBottom = mBottom + offset;
8288 yLoc = 0;
8289 }
8290 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8291 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008292 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008293 }
8294 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008295 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008296 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008297
Chet Haasec3aa3612010-06-17 08:50:37 -07008298 mTop += offset;
8299 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008300 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8301 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008302 invalidateViewProperty(false, false);
8303 } else {
8304 if (!matrixIsIdentity) {
8305 invalidateViewProperty(false, true);
8306 }
8307 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008308 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008310 }
8311
8312 /**
8313 * Offset this view's horizontal location by the specified amount of pixels.
8314 *
8315 * @param offset the numer of pixels to offset the view by
8316 */
8317 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008318 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008319 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008320 final boolean matrixIsIdentity = mTransformationInfo == null
8321 || mTransformationInfo.mMatrixIsIdentity;
8322 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008323 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8324 invalidateViewProperty(false, false);
8325 } else {
8326 final ViewParent p = mParent;
8327 if (p != null && mAttachInfo != null) {
8328 final Rect r = mAttachInfo.mTmpInvalRect;
8329 int minLeft;
8330 int maxRight;
8331 if (offset < 0) {
8332 minLeft = mLeft + offset;
8333 maxRight = mRight;
8334 } else {
8335 minLeft = mLeft;
8336 maxRight = mRight + offset;
8337 }
8338 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
8339 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008340 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008341 }
8342 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008343 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008344 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008345
Chet Haasec3aa3612010-06-17 08:50:37 -07008346 mLeft += offset;
8347 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008348 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8349 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008350 invalidateViewProperty(false, false);
8351 } else {
8352 if (!matrixIsIdentity) {
8353 invalidateViewProperty(false, true);
8354 }
8355 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008356 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 }
8359
8360 /**
8361 * Get the LayoutParams associated with this view. All views should have
8362 * layout parameters. These supply parameters to the <i>parent</i> of this
8363 * view specifying how it should be arranged. There are many subclasses of
8364 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8365 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008366 *
8367 * This method may return null if this View is not attached to a parent
8368 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8369 * was not invoked successfully. When a View is attached to a parent
8370 * ViewGroup, this method must not return null.
8371 *
8372 * @return The LayoutParams associated with this view, or null if no
8373 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008374 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008375 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 public ViewGroup.LayoutParams getLayoutParams() {
8377 return mLayoutParams;
8378 }
8379
8380 /**
8381 * Set the layout parameters associated with this view. These supply
8382 * parameters to the <i>parent</i> of this view specifying how it should be
8383 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8384 * correspond to the different subclasses of ViewGroup that are responsible
8385 * for arranging their children.
8386 *
Romain Guy01c174b2011-02-22 11:51:06 -08008387 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008388 */
8389 public void setLayoutParams(ViewGroup.LayoutParams params) {
8390 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008391 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 }
8393 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008394 if (mParent instanceof ViewGroup) {
8395 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008397 requestLayout();
8398 }
8399
8400 /**
8401 * Set the scrolled position of your view. This will cause a call to
8402 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8403 * invalidated.
8404 * @param x the x position to scroll to
8405 * @param y the y position to scroll to
8406 */
8407 public void scrollTo(int x, int y) {
8408 if (mScrollX != x || mScrollY != y) {
8409 int oldX = mScrollX;
8410 int oldY = mScrollY;
8411 mScrollX = x;
8412 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008413 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008414 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008415 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008416 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 }
8419 }
8420
8421 /**
8422 * Move the scrolled position of your view. This will cause a call to
8423 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8424 * invalidated.
8425 * @param x the amount of pixels to scroll by horizontally
8426 * @param y the amount of pixels to scroll by vertically
8427 */
8428 public void scrollBy(int x, int y) {
8429 scrollTo(mScrollX + x, mScrollY + y);
8430 }
8431
8432 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008433 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8434 * animation to fade the scrollbars out after a default delay. If a subclass
8435 * provides animated scrolling, the start delay should equal the duration
8436 * of the scrolling animation.</p>
8437 *
8438 * <p>The animation starts only if at least one of the scrollbars is
8439 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8440 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8441 * this method returns true, and false otherwise. If the animation is
8442 * started, this method calls {@link #invalidate()}; in that case the
8443 * caller should not call {@link #invalidate()}.</p>
8444 *
8445 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008446 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008447 *
8448 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8449 * and {@link #scrollTo(int, int)}.</p>
8450 *
8451 * @return true if the animation is played, false otherwise
8452 *
8453 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008454 * @see #scrollBy(int, int)
8455 * @see #scrollTo(int, int)
8456 * @see #isHorizontalScrollBarEnabled()
8457 * @see #isVerticalScrollBarEnabled()
8458 * @see #setHorizontalScrollBarEnabled(boolean)
8459 * @see #setVerticalScrollBarEnabled(boolean)
8460 */
8461 protected boolean awakenScrollBars() {
8462 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008463 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008464 }
8465
8466 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008467 * Trigger the scrollbars to draw.
8468 * This method differs from awakenScrollBars() only in its default duration.
8469 * initialAwakenScrollBars() will show the scroll bars for longer than
8470 * usual to give the user more of a chance to notice them.
8471 *
8472 * @return true if the animation is played, false otherwise.
8473 */
8474 private boolean initialAwakenScrollBars() {
8475 return mScrollCache != null &&
8476 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8477 }
8478
8479 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008480 * <p>
8481 * Trigger the scrollbars to draw. When invoked this method starts an
8482 * animation to fade the scrollbars out after a fixed delay. If a subclass
8483 * provides animated scrolling, the start delay should equal the duration of
8484 * the scrolling animation.
8485 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008486 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008487 * <p>
8488 * The animation starts only if at least one of the scrollbars is enabled,
8489 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8490 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8491 * this method returns true, and false otherwise. If the animation is
8492 * started, this method calls {@link #invalidate()}; in that case the caller
8493 * should not call {@link #invalidate()}.
8494 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008495 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008496 * <p>
8497 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008498 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008499 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008500 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008501 * @param startDelay the delay, in milliseconds, after which the animation
8502 * should start; when the delay is 0, the animation starts
8503 * immediately
8504 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008505 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008506 * @see #scrollBy(int, int)
8507 * @see #scrollTo(int, int)
8508 * @see #isHorizontalScrollBarEnabled()
8509 * @see #isVerticalScrollBarEnabled()
8510 * @see #setHorizontalScrollBarEnabled(boolean)
8511 * @see #setVerticalScrollBarEnabled(boolean)
8512 */
8513 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008514 return awakenScrollBars(startDelay, true);
8515 }
Joe Malin32736f02011-01-19 16:14:20 -08008516
Mike Cleron290947b2009-09-29 18:34:32 -07008517 /**
8518 * <p>
8519 * Trigger the scrollbars to draw. When invoked this method starts an
8520 * animation to fade the scrollbars out after a fixed delay. If a subclass
8521 * provides animated scrolling, the start delay should equal the duration of
8522 * the scrolling animation.
8523 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008524 *
Mike Cleron290947b2009-09-29 18:34:32 -07008525 * <p>
8526 * The animation starts only if at least one of the scrollbars is enabled,
8527 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8528 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8529 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008530 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008531 * is set to true; in that case the caller
8532 * should not call {@link #invalidate()}.
8533 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008534 *
Mike Cleron290947b2009-09-29 18:34:32 -07008535 * <p>
8536 * This method should be invoked everytime a subclass directly updates the
8537 * scroll parameters.
8538 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008539 *
Mike Cleron290947b2009-09-29 18:34:32 -07008540 * @param startDelay the delay, in milliseconds, after which the animation
8541 * should start; when the delay is 0, the animation starts
8542 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008543 *
Mike Cleron290947b2009-09-29 18:34:32 -07008544 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008545 *
Mike Cleron290947b2009-09-29 18:34:32 -07008546 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008547 *
Mike Cleron290947b2009-09-29 18:34:32 -07008548 * @see #scrollBy(int, int)
8549 * @see #scrollTo(int, int)
8550 * @see #isHorizontalScrollBarEnabled()
8551 * @see #isVerticalScrollBarEnabled()
8552 * @see #setHorizontalScrollBarEnabled(boolean)
8553 * @see #setVerticalScrollBarEnabled(boolean)
8554 */
8555 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008556 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008557
Mike Cleronf116bf82009-09-27 19:14:12 -07008558 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8559 return false;
8560 }
8561
8562 if (scrollCache.scrollBar == null) {
8563 scrollCache.scrollBar = new ScrollBarDrawable();
8564 }
8565
8566 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8567
Mike Cleron290947b2009-09-29 18:34:32 -07008568 if (invalidate) {
8569 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008570 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008571 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008572
8573 if (scrollCache.state == ScrollabilityCache.OFF) {
8574 // FIXME: this is copied from WindowManagerService.
8575 // We should get this value from the system when it
8576 // is possible to do so.
8577 final int KEY_REPEAT_FIRST_DELAY = 750;
8578 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8579 }
8580
8581 // Tell mScrollCache when we should start fading. This may
8582 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008583 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008584 scrollCache.fadeStartTime = fadeStartTime;
8585 scrollCache.state = ScrollabilityCache.ON;
8586
8587 // Schedule our fader to run, unscheduling any old ones first
8588 if (mAttachInfo != null) {
8589 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8590 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8591 }
8592
8593 return true;
8594 }
8595
8596 return false;
8597 }
8598
8599 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008600 * Do not invalidate views which are not visible and which are not running an animation. They
8601 * will not get drawn and they should not set dirty flags as if they will be drawn
8602 */
8603 private boolean skipInvalidate() {
8604 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8605 (!(mParent instanceof ViewGroup) ||
8606 !((ViewGroup) mParent).isViewTransitioning(this));
8607 }
8608 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008609 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008610 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8611 * in the future. This must be called from a UI thread. To call from a non-UI
8612 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008613 *
8614 * WARNING: This method is destructive to dirty.
8615 * @param dirty the rectangle representing the bounds of the dirty region
8616 */
8617 public void invalidate(Rect dirty) {
8618 if (ViewDebug.TRACE_HIERARCHY) {
8619 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8620 }
8621
Chet Haaseaceafe62011-08-26 15:44:33 -07008622 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008623 return;
8624 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008625 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008626 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8627 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008629 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008630 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008631 final ViewParent p = mParent;
8632 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008633 //noinspection PointlessBooleanExpression,ConstantConditions
8634 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8635 if (p != null && ai != null && ai.mHardwareAccelerated) {
8636 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008637 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008638 p.invalidateChild(this, null);
8639 return;
8640 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008642 if (p != null && ai != null) {
8643 final int scrollX = mScrollX;
8644 final int scrollY = mScrollY;
8645 final Rect r = ai.mTmpInvalRect;
8646 r.set(dirty.left - scrollX, dirty.top - scrollY,
8647 dirty.right - scrollX, dirty.bottom - scrollY);
8648 mParent.invalidateChild(this, r);
8649 }
8650 }
8651 }
8652
8653 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008654 * 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 -08008655 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008656 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8657 * will be called at some point in the future. This must be called from
8658 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 * @param l the left position of the dirty region
8660 * @param t the top position of the dirty region
8661 * @param r the right position of the dirty region
8662 * @param b the bottom position of the dirty region
8663 */
8664 public void invalidate(int l, int t, int r, int b) {
8665 if (ViewDebug.TRACE_HIERARCHY) {
8666 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8667 }
8668
Chet Haaseaceafe62011-08-26 15:44:33 -07008669 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008670 return;
8671 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008672 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008673 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8674 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008675 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008676 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008677 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 final ViewParent p = mParent;
8679 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008680 //noinspection PointlessBooleanExpression,ConstantConditions
8681 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8682 if (p != null && ai != null && ai.mHardwareAccelerated) {
8683 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008684 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008685 p.invalidateChild(this, null);
8686 return;
8687 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008689 if (p != null && ai != null && l < r && t < b) {
8690 final int scrollX = mScrollX;
8691 final int scrollY = mScrollY;
8692 final Rect tmpr = ai.mTmpInvalRect;
8693 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8694 p.invalidateChild(this, tmpr);
8695 }
8696 }
8697 }
8698
8699 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008700 * Invalidate the whole view. If the view is visible,
8701 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8702 * the future. This must be called from a UI thread. To call from a non-UI thread,
8703 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008704 */
8705 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008706 invalidate(true);
8707 }
Joe Malin32736f02011-01-19 16:14:20 -08008708
Chet Haaseed032702010-10-01 14:05:54 -07008709 /**
8710 * This is where the invalidate() work actually happens. A full invalidate()
8711 * causes the drawing cache to be invalidated, but this function can be called with
8712 * invalidateCache set to false to skip that invalidation step for cases that do not
8713 * need it (for example, a component that remains at the same dimensions with the same
8714 * content).
8715 *
8716 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8717 * well. This is usually true for a full invalidate, but may be set to false if the
8718 * View's contents or dimensions have not changed.
8719 */
Romain Guy849d0a32011-02-01 17:20:48 -08008720 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008721 if (ViewDebug.TRACE_HIERARCHY) {
8722 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8723 }
8724
Chet Haaseaceafe62011-08-26 15:44:33 -07008725 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008726 return;
8727 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008728 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008729 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008730 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8731 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008732 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008733 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008734 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008735 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008736 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008738 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008739 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008740 //noinspection PointlessBooleanExpression,ConstantConditions
8741 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8742 if (p != null && ai != null && ai.mHardwareAccelerated) {
8743 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008744 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008745 p.invalidateChild(this, null);
8746 return;
8747 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008748 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008750 if (p != null && ai != null) {
8751 final Rect r = ai.mTmpInvalRect;
8752 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8753 // Don't call invalidate -- we don't want to internally scroll
8754 // our own bounds
8755 p.invalidateChild(this, r);
8756 }
8757 }
8758 }
8759
8760 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07008761 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
8762 * set any flags or handle all of the cases handled by the default invalidation methods.
8763 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
8764 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
8765 * walk up the hierarchy, transforming the dirty rect as necessary.
8766 *
8767 * The method also handles normal invalidation logic if display list properties are not
8768 * being used in this view. The invalidateParent and forceRedraw flags are used by that
8769 * backup approach, to handle these cases used in the various property-setting methods.
8770 *
8771 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
8772 * are not being used in this view
8773 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
8774 * list properties are not being used in this view
8775 */
8776 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
8777 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
8778 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
8779 if (invalidateParent) {
8780 invalidateParentCaches();
8781 }
8782 if (forceRedraw) {
8783 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
8784 }
8785 invalidate(false);
8786 } else {
8787 final AttachInfo ai = mAttachInfo;
8788 final ViewParent p = mParent;
8789 if (p != null && ai != null) {
8790 final Rect r = ai.mTmpInvalRect;
8791 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8792 if (mParent instanceof ViewGroup) {
8793 ((ViewGroup) mParent).invalidateChildFast(this, r);
8794 } else {
8795 mParent.invalidateChild(this, r);
8796 }
8797 }
8798 }
8799 }
8800
8801 /**
8802 * Utility method to transform a given Rect by the current matrix of this view.
8803 */
8804 void transformRect(final Rect rect) {
8805 if (!getMatrix().isIdentity()) {
8806 RectF boundingRect = mAttachInfo.mTmpTransformRect;
8807 boundingRect.set(rect);
8808 getMatrix().mapRect(boundingRect);
8809 rect.set((int) (boundingRect.left - 0.5f),
8810 (int) (boundingRect.top - 0.5f),
8811 (int) (boundingRect.right + 0.5f),
8812 (int) (boundingRect.bottom + 0.5f));
8813 }
8814 }
8815
8816 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008817 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008818 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8819 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008820 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8821 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008822 *
8823 * @hide
8824 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008825 protected void invalidateParentCaches() {
8826 if (mParent instanceof View) {
8827 ((View) mParent).mPrivateFlags |= INVALIDATED;
8828 }
8829 }
Joe Malin32736f02011-01-19 16:14:20 -08008830
Romain Guy0fd89bf2011-01-26 15:41:30 -08008831 /**
8832 * Used to indicate that the parent of this view should be invalidated. This functionality
8833 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8834 * which is necessary when various parent-managed properties of the view change, such as
8835 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8836 * an invalidation event to the parent.
8837 *
8838 * @hide
8839 */
8840 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008841 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008842 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008843 }
8844 }
8845
8846 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008847 * Indicates whether this View is opaque. An opaque View guarantees that it will
8848 * draw all the pixels overlapping its bounds using a fully opaque color.
8849 *
8850 * Subclasses of View should override this method whenever possible to indicate
8851 * whether an instance is opaque. Opaque Views are treated in a special way by
8852 * the View hierarchy, possibly allowing it to perform optimizations during
8853 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008854 *
Romain Guy24443ea2009-05-11 11:56:30 -07008855 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008856 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008857 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008858 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008859 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008860 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8861 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008862 }
8863
Adam Powell20232d02010-12-08 21:08:53 -08008864 /**
8865 * @hide
8866 */
8867 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008868 // Opaque if:
8869 // - Has a background
8870 // - Background is opaque
8871 // - Doesn't have scrollbars or scrollbars are inside overlay
8872
8873 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8874 mPrivateFlags |= OPAQUE_BACKGROUND;
8875 } else {
8876 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8877 }
8878
8879 final int flags = mViewFlags;
8880 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8881 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8882 mPrivateFlags |= OPAQUE_SCROLLBARS;
8883 } else {
8884 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8885 }
8886 }
8887
8888 /**
8889 * @hide
8890 */
8891 protected boolean hasOpaqueScrollbars() {
8892 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008893 }
8894
8895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896 * @return A handler associated with the thread running the View. This
8897 * handler can be used to pump events in the UI events queue.
8898 */
8899 public Handler getHandler() {
8900 if (mAttachInfo != null) {
8901 return mAttachInfo.mHandler;
8902 }
8903 return null;
8904 }
8905
8906 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008907 * Gets the view root associated with the View.
8908 * @return The view root, or null if none.
8909 * @hide
8910 */
8911 public ViewRootImpl getViewRootImpl() {
8912 if (mAttachInfo != null) {
8913 return mAttachInfo.mViewRootImpl;
8914 }
8915 return null;
8916 }
8917
8918 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008919 * <p>Causes the Runnable to be added to the message queue.
8920 * The runnable will be run on the user interface thread.</p>
8921 *
8922 * <p>This method can be invoked from outside of the UI thread
8923 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008924 *
8925 * @param action The Runnable that will be executed.
8926 *
8927 * @return Returns true if the Runnable was successfully placed in to the
8928 * message queue. Returns false on failure, usually because the
8929 * looper processing the message queue is exiting.
8930 */
8931 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008932 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008933 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008934 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008935 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008936 // Assume that post will succeed later
8937 ViewRootImpl.getRunQueue().post(action);
8938 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008939 }
8940
8941 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008942 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008944 * The runnable will be run on the user interface thread.</p>
8945 *
8946 * <p>This method can be invoked from outside of the UI thread
8947 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 *
8949 * @param action The Runnable that will be executed.
8950 * @param delayMillis The delay (in milliseconds) until the Runnable
8951 * will be executed.
8952 *
8953 * @return true if the Runnable was successfully placed in to the
8954 * message queue. Returns false on failure, usually because the
8955 * looper processing the message queue is exiting. Note that a
8956 * result of true does not mean the Runnable will be processed --
8957 * if the looper is quit before the delivery time of the message
8958 * occurs then the message will be dropped.
8959 */
8960 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008961 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008962 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008963 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008964 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008965 // Assume that post will succeed later
8966 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8967 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008968 }
8969
8970 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08008971 * <p>Causes the Runnable to execute on the next animation time step.
8972 * The runnable will be run on the user interface thread.</p>
8973 *
8974 * <p>This method can be invoked from outside of the UI thread
8975 * only when this View is attached to a window.</p>
8976 *
8977 * @param action The Runnable that will be executed.
8978 *
8979 * @hide
8980 */
8981 public void postOnAnimation(Runnable action) {
8982 final AttachInfo attachInfo = mAttachInfo;
8983 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07008984 attachInfo.mViewRootImpl.mChoreographer.postCallback(
8985 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08008986 } else {
8987 // Assume that post will succeed later
8988 ViewRootImpl.getRunQueue().post(action);
8989 }
8990 }
8991
8992 /**
8993 * <p>Causes the Runnable to execute on the next animation time step,
8994 * after the specified amount of time elapses.
8995 * The runnable will be run on the user interface thread.</p>
8996 *
8997 * <p>This method can be invoked from outside of the UI thread
8998 * only when this View is attached to a window.</p>
8999 *
9000 * @param action The Runnable that will be executed.
9001 * @param delayMillis The delay (in milliseconds) until the Runnable
9002 * will be executed.
9003 *
9004 * @hide
9005 */
9006 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9007 final AttachInfo attachInfo = mAttachInfo;
9008 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009009 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9010 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009011 } else {
9012 // Assume that post will succeed later
9013 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9014 }
9015 }
9016
9017 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009018 * <p>Removes the specified Runnable from the message queue.</p>
9019 *
9020 * <p>This method can be invoked from outside of the UI thread
9021 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009022 *
9023 * @param action The Runnable to remove from the message handling queue
9024 *
9025 * @return true if this view could ask the Handler to remove the Runnable,
9026 * false otherwise. When the returned value is true, the Runnable
9027 * may or may not have been actually removed from the message queue
9028 * (for instance, if the Runnable was not in the queue already.)
9029 */
9030 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009031 if (action != null) {
9032 final AttachInfo attachInfo = mAttachInfo;
9033 if (attachInfo != null) {
9034 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009035 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9036 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009037 } else {
9038 // Assume that post will succeed later
9039 ViewRootImpl.getRunQueue().removeCallbacks(action);
9040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009042 return true;
9043 }
9044
9045 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009046 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9047 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009048 *
Romain Guye63a4f32011-08-11 11:33:31 -07009049 * <p>This method can be invoked from outside of the UI thread
9050 * only when this View is attached to a window.</p>
9051 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009052 * @see #invalidate()
9053 */
9054 public void postInvalidate() {
9055 postInvalidateDelayed(0);
9056 }
9057
9058 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009059 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9060 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
9061 *
9062 * <p>This method can be invoked from outside of the UI thread
9063 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 *
9065 * @param left The left coordinate of the rectangle to invalidate.
9066 * @param top The top coordinate of the rectangle to invalidate.
9067 * @param right The right coordinate of the rectangle to invalidate.
9068 * @param bottom The bottom coordinate of the rectangle to invalidate.
9069 *
9070 * @see #invalidate(int, int, int, int)
9071 * @see #invalidate(Rect)
9072 */
9073 public void postInvalidate(int left, int top, int right, int bottom) {
9074 postInvalidateDelayed(0, left, top, right, bottom);
9075 }
9076
9077 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009078 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9079 * loop. Waits for the specified amount of time.</p>
9080 *
9081 * <p>This method can be invoked from outside of the UI thread
9082 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009083 *
9084 * @param delayMilliseconds the duration in milliseconds to delay the
9085 * invalidation by
9086 */
9087 public void postInvalidateDelayed(long delayMilliseconds) {
9088 // We try only with the AttachInfo because there's no point in invalidating
9089 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009090 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009091 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009092 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009093 }
9094 }
9095
9096 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009097 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9098 * through the event loop. Waits for the specified amount of time.</p>
9099 *
9100 * <p>This method can be invoked from outside of the UI thread
9101 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 *
9103 * @param delayMilliseconds the duration in milliseconds to delay the
9104 * invalidation by
9105 * @param left The left coordinate of the rectangle to invalidate.
9106 * @param top The top coordinate of the rectangle to invalidate.
9107 * @param right The right coordinate of the rectangle to invalidate.
9108 * @param bottom The bottom coordinate of the rectangle to invalidate.
9109 */
9110 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9111 int right, int bottom) {
9112
9113 // We try only with the AttachInfo because there's no point in invalidating
9114 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009115 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009116 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009117 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9118 info.target = this;
9119 info.left = left;
9120 info.top = top;
9121 info.right = right;
9122 info.bottom = bottom;
9123
Jeff Browna175a5b2012-02-15 19:18:31 -08009124 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009125 }
9126 }
9127
9128 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08009129 * <p>Cause an invalidate to happen on the next animation time step, typically the
9130 * next display frame.</p>
9131 *
9132 * <p>This method can be invoked from outside of the UI thread
9133 * only when this View is attached to a window.</p>
9134 *
9135 * @hide
9136 */
9137 public void postInvalidateOnAnimation() {
9138 // We try only with the AttachInfo because there's no point in invalidating
9139 // if we are not attached to our window
9140 final AttachInfo attachInfo = mAttachInfo;
9141 if (attachInfo != null) {
9142 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
9143 }
9144 }
9145
9146 /**
9147 * <p>Cause an invalidate of the specified area to happen on the next animation
9148 * time step, typically the next display frame.</p>
9149 *
9150 * <p>This method can be invoked from outside of the UI thread
9151 * only when this View is attached to a window.</p>
9152 *
9153 * @param left The left coordinate of the rectangle to invalidate.
9154 * @param top The top coordinate of the rectangle to invalidate.
9155 * @param right The right coordinate of the rectangle to invalidate.
9156 * @param bottom The bottom coordinate of the rectangle to invalidate.
9157 *
9158 * @hide
9159 */
9160 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9161 // We try only with the AttachInfo because there's no point in invalidating
9162 // if we are not attached to our window
9163 final AttachInfo attachInfo = mAttachInfo;
9164 if (attachInfo != null) {
9165 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9166 info.target = this;
9167 info.left = left;
9168 info.top = top;
9169 info.right = right;
9170 info.bottom = bottom;
9171
9172 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9173 }
9174 }
9175
9176 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009177 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9178 * This event is sent at most once every
9179 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9180 */
9181 private void postSendViewScrolledAccessibilityEventCallback() {
9182 if (mSendViewScrolledAccessibilityEvent == null) {
9183 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9184 }
9185 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9186 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9187 postDelayed(mSendViewScrolledAccessibilityEvent,
9188 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9189 }
9190 }
9191
9192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009193 * Called by a parent to request that a child update its values for mScrollX
9194 * and mScrollY if necessary. This will typically be done if the child is
9195 * animating a scroll using a {@link android.widget.Scroller Scroller}
9196 * object.
9197 */
9198 public void computeScroll() {
9199 }
9200
9201 /**
9202 * <p>Indicate whether the horizontal edges are faded when the view is
9203 * scrolled horizontally.</p>
9204 *
9205 * @return true if the horizontal edges should are faded on scroll, false
9206 * otherwise
9207 *
9208 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009209 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 */
9211 public boolean isHorizontalFadingEdgeEnabled() {
9212 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9213 }
9214
9215 /**
9216 * <p>Define whether the horizontal edges should be faded when this view
9217 * is scrolled horizontally.</p>
9218 *
9219 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9220 * be faded when the view is scrolled
9221 * horizontally
9222 *
9223 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009224 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009225 */
9226 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9227 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9228 if (horizontalFadingEdgeEnabled) {
9229 initScrollCache();
9230 }
9231
9232 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9233 }
9234 }
9235
9236 /**
9237 * <p>Indicate whether the vertical edges are faded when the view is
9238 * scrolled horizontally.</p>
9239 *
9240 * @return true if the vertical edges should are faded on scroll, false
9241 * otherwise
9242 *
9243 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009244 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245 */
9246 public boolean isVerticalFadingEdgeEnabled() {
9247 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9248 }
9249
9250 /**
9251 * <p>Define whether the vertical edges should be faded when this view
9252 * is scrolled vertically.</p>
9253 *
9254 * @param verticalFadingEdgeEnabled true if the vertical edges should
9255 * be faded when the view is scrolled
9256 * vertically
9257 *
9258 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009259 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260 */
9261 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9262 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9263 if (verticalFadingEdgeEnabled) {
9264 initScrollCache();
9265 }
9266
9267 mViewFlags ^= FADING_EDGE_VERTICAL;
9268 }
9269 }
9270
9271 /**
9272 * Returns the strength, or intensity, of the top faded edge. The strength is
9273 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9274 * returns 0.0 or 1.0 but no value in between.
9275 *
9276 * Subclasses should override this method to provide a smoother fade transition
9277 * when scrolling occurs.
9278 *
9279 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9280 */
9281 protected float getTopFadingEdgeStrength() {
9282 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9283 }
9284
9285 /**
9286 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9287 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9288 * returns 0.0 or 1.0 but no value in between.
9289 *
9290 * Subclasses should override this method to provide a smoother fade transition
9291 * when scrolling occurs.
9292 *
9293 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9294 */
9295 protected float getBottomFadingEdgeStrength() {
9296 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9297 computeVerticalScrollRange() ? 1.0f : 0.0f;
9298 }
9299
9300 /**
9301 * Returns the strength, or intensity, of the left faded edge. The strength is
9302 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9303 * returns 0.0 or 1.0 but no value in between.
9304 *
9305 * Subclasses should override this method to provide a smoother fade transition
9306 * when scrolling occurs.
9307 *
9308 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9309 */
9310 protected float getLeftFadingEdgeStrength() {
9311 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9312 }
9313
9314 /**
9315 * Returns the strength, or intensity, of the right faded edge. The strength is
9316 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9317 * returns 0.0 or 1.0 but no value in between.
9318 *
9319 * Subclasses should override this method to provide a smoother fade transition
9320 * when scrolling occurs.
9321 *
9322 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9323 */
9324 protected float getRightFadingEdgeStrength() {
9325 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9326 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9327 }
9328
9329 /**
9330 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9331 * scrollbar is not drawn by default.</p>
9332 *
9333 * @return true if the horizontal scrollbar should be painted, false
9334 * otherwise
9335 *
9336 * @see #setHorizontalScrollBarEnabled(boolean)
9337 */
9338 public boolean isHorizontalScrollBarEnabled() {
9339 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9340 }
9341
9342 /**
9343 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9344 * scrollbar is not drawn by default.</p>
9345 *
9346 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9347 * be painted
9348 *
9349 * @see #isHorizontalScrollBarEnabled()
9350 */
9351 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9352 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9353 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009354 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009355 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009356 }
9357 }
9358
9359 /**
9360 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9361 * scrollbar is not drawn by default.</p>
9362 *
9363 * @return true if the vertical scrollbar should be painted, false
9364 * otherwise
9365 *
9366 * @see #setVerticalScrollBarEnabled(boolean)
9367 */
9368 public boolean isVerticalScrollBarEnabled() {
9369 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9370 }
9371
9372 /**
9373 * <p>Define whether the vertical scrollbar should be drawn or not. The
9374 * scrollbar is not drawn by default.</p>
9375 *
9376 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9377 * be painted
9378 *
9379 * @see #isVerticalScrollBarEnabled()
9380 */
9381 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9382 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9383 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009384 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009385 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009386 }
9387 }
9388
Adam Powell20232d02010-12-08 21:08:53 -08009389 /**
9390 * @hide
9391 */
9392 protected void recomputePadding() {
9393 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 }
Joe Malin32736f02011-01-19 16:14:20 -08009395
Mike Cleronfe81d382009-09-28 14:22:16 -07009396 /**
9397 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009398 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009399 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009400 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009401 */
9402 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9403 initScrollCache();
9404 final ScrollabilityCache scrollabilityCache = mScrollCache;
9405 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009406 if (fadeScrollbars) {
9407 scrollabilityCache.state = ScrollabilityCache.OFF;
9408 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009409 scrollabilityCache.state = ScrollabilityCache.ON;
9410 }
9411 }
Joe Malin32736f02011-01-19 16:14:20 -08009412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009413 /**
Joe Malin32736f02011-01-19 16:14:20 -08009414 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009415 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009416 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009417 * @return true if scrollbar fading is enabled
9418 */
9419 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009420 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009421 }
Joe Malin32736f02011-01-19 16:14:20 -08009422
Mike Cleron52f0a642009-09-28 18:21:37 -07009423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009424 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9425 * inset. When inset, they add to the padding of the view. And the scrollbars
9426 * can be drawn inside the padding area or on the edge of the view. For example,
9427 * if a view has a background drawable and you want to draw the scrollbars
9428 * inside the padding specified by the drawable, you can use
9429 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9430 * appear at the edge of the view, ignoring the padding, then you can use
9431 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9432 * @param style the style of the scrollbars. Should be one of
9433 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9434 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9435 * @see #SCROLLBARS_INSIDE_OVERLAY
9436 * @see #SCROLLBARS_INSIDE_INSET
9437 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9438 * @see #SCROLLBARS_OUTSIDE_INSET
9439 */
9440 public void setScrollBarStyle(int style) {
9441 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9442 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009443 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009444 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 }
9446 }
9447
9448 /**
9449 * <p>Returns the current scrollbar style.</p>
9450 * @return the current scrollbar style
9451 * @see #SCROLLBARS_INSIDE_OVERLAY
9452 * @see #SCROLLBARS_INSIDE_INSET
9453 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9454 * @see #SCROLLBARS_OUTSIDE_INSET
9455 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009456 @ViewDebug.ExportedProperty(mapping = {
9457 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9458 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9459 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9460 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9461 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009462 public int getScrollBarStyle() {
9463 return mViewFlags & SCROLLBARS_STYLE_MASK;
9464 }
9465
9466 /**
9467 * <p>Compute the horizontal range that the horizontal scrollbar
9468 * represents.</p>
9469 *
9470 * <p>The range is expressed in arbitrary units that must be the same as the
9471 * units used by {@link #computeHorizontalScrollExtent()} and
9472 * {@link #computeHorizontalScrollOffset()}.</p>
9473 *
9474 * <p>The default range is the drawing width of this view.</p>
9475 *
9476 * @return the total horizontal range represented by the horizontal
9477 * scrollbar
9478 *
9479 * @see #computeHorizontalScrollExtent()
9480 * @see #computeHorizontalScrollOffset()
9481 * @see android.widget.ScrollBarDrawable
9482 */
9483 protected int computeHorizontalScrollRange() {
9484 return getWidth();
9485 }
9486
9487 /**
9488 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9489 * within the horizontal range. This value is used to compute the position
9490 * of the thumb within the scrollbar's track.</p>
9491 *
9492 * <p>The range is expressed in arbitrary units that must be the same as the
9493 * units used by {@link #computeHorizontalScrollRange()} and
9494 * {@link #computeHorizontalScrollExtent()}.</p>
9495 *
9496 * <p>The default offset is the scroll offset of this view.</p>
9497 *
9498 * @return the horizontal offset of the scrollbar's thumb
9499 *
9500 * @see #computeHorizontalScrollRange()
9501 * @see #computeHorizontalScrollExtent()
9502 * @see android.widget.ScrollBarDrawable
9503 */
9504 protected int computeHorizontalScrollOffset() {
9505 return mScrollX;
9506 }
9507
9508 /**
9509 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9510 * within the horizontal range. This value is used to compute the length
9511 * of the thumb within the scrollbar's track.</p>
9512 *
9513 * <p>The range is expressed in arbitrary units that must be the same as the
9514 * units used by {@link #computeHorizontalScrollRange()} and
9515 * {@link #computeHorizontalScrollOffset()}.</p>
9516 *
9517 * <p>The default extent is the drawing width of this view.</p>
9518 *
9519 * @return the horizontal extent of the scrollbar's thumb
9520 *
9521 * @see #computeHorizontalScrollRange()
9522 * @see #computeHorizontalScrollOffset()
9523 * @see android.widget.ScrollBarDrawable
9524 */
9525 protected int computeHorizontalScrollExtent() {
9526 return getWidth();
9527 }
9528
9529 /**
9530 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9531 *
9532 * <p>The range is expressed in arbitrary units that must be the same as the
9533 * units used by {@link #computeVerticalScrollExtent()} and
9534 * {@link #computeVerticalScrollOffset()}.</p>
9535 *
9536 * @return the total vertical range represented by the vertical scrollbar
9537 *
9538 * <p>The default range is the drawing height of this view.</p>
9539 *
9540 * @see #computeVerticalScrollExtent()
9541 * @see #computeVerticalScrollOffset()
9542 * @see android.widget.ScrollBarDrawable
9543 */
9544 protected int computeVerticalScrollRange() {
9545 return getHeight();
9546 }
9547
9548 /**
9549 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9550 * within the horizontal range. This value is used to compute the position
9551 * of the thumb within the scrollbar's track.</p>
9552 *
9553 * <p>The range is expressed in arbitrary units that must be the same as the
9554 * units used by {@link #computeVerticalScrollRange()} and
9555 * {@link #computeVerticalScrollExtent()}.</p>
9556 *
9557 * <p>The default offset is the scroll offset of this view.</p>
9558 *
9559 * @return the vertical offset of the scrollbar's thumb
9560 *
9561 * @see #computeVerticalScrollRange()
9562 * @see #computeVerticalScrollExtent()
9563 * @see android.widget.ScrollBarDrawable
9564 */
9565 protected int computeVerticalScrollOffset() {
9566 return mScrollY;
9567 }
9568
9569 /**
9570 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9571 * within the vertical range. This value is used to compute the length
9572 * of the thumb within the scrollbar's track.</p>
9573 *
9574 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009575 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009576 * {@link #computeVerticalScrollOffset()}.</p>
9577 *
9578 * <p>The default extent is the drawing height of this view.</p>
9579 *
9580 * @return the vertical extent of the scrollbar's thumb
9581 *
9582 * @see #computeVerticalScrollRange()
9583 * @see #computeVerticalScrollOffset()
9584 * @see android.widget.ScrollBarDrawable
9585 */
9586 protected int computeVerticalScrollExtent() {
9587 return getHeight();
9588 }
9589
9590 /**
Adam Powell69159442011-06-13 17:53:06 -07009591 * Check if this view can be scrolled horizontally in a certain direction.
9592 *
9593 * @param direction Negative to check scrolling left, positive to check scrolling right.
9594 * @return true if this view can be scrolled in the specified direction, false otherwise.
9595 */
9596 public boolean canScrollHorizontally(int direction) {
9597 final int offset = computeHorizontalScrollOffset();
9598 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9599 if (range == 0) return false;
9600 if (direction < 0) {
9601 return offset > 0;
9602 } else {
9603 return offset < range - 1;
9604 }
9605 }
9606
9607 /**
9608 * Check if this view can be scrolled vertically in a certain direction.
9609 *
9610 * @param direction Negative to check scrolling up, positive to check scrolling down.
9611 * @return true if this view can be scrolled in the specified direction, false otherwise.
9612 */
9613 public boolean canScrollVertically(int direction) {
9614 final int offset = computeVerticalScrollOffset();
9615 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9616 if (range == 0) return false;
9617 if (direction < 0) {
9618 return offset > 0;
9619 } else {
9620 return offset < range - 1;
9621 }
9622 }
9623
9624 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009625 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9626 * scrollbars are painted only if they have been awakened first.</p>
9627 *
9628 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009629 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009630 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009631 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009632 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 // scrollbars are drawn only when the animation is running
9634 final ScrollabilityCache cache = mScrollCache;
9635 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009636
Mike Cleronf116bf82009-09-27 19:14:12 -07009637 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009638
Mike Cleronf116bf82009-09-27 19:14:12 -07009639 if (state == ScrollabilityCache.OFF) {
9640 return;
9641 }
Joe Malin32736f02011-01-19 16:14:20 -08009642
Mike Cleronf116bf82009-09-27 19:14:12 -07009643 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009644
Mike Cleronf116bf82009-09-27 19:14:12 -07009645 if (state == ScrollabilityCache.FADING) {
9646 // We're fading -- get our fade interpolation
9647 if (cache.interpolatorValues == null) {
9648 cache.interpolatorValues = new float[1];
9649 }
Joe Malin32736f02011-01-19 16:14:20 -08009650
Mike Cleronf116bf82009-09-27 19:14:12 -07009651 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009652
Mike Cleronf116bf82009-09-27 19:14:12 -07009653 // Stops the animation if we're done
9654 if (cache.scrollBarInterpolator.timeToValues(values) ==
9655 Interpolator.Result.FREEZE_END) {
9656 cache.state = ScrollabilityCache.OFF;
9657 } else {
9658 cache.scrollBar.setAlpha(Math.round(values[0]));
9659 }
Joe Malin32736f02011-01-19 16:14:20 -08009660
9661 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009662 // drawing. We only want this when we're fading so that
9663 // we prevent excessive redraws
9664 invalidate = true;
9665 } else {
9666 // We're just on -- but we may have been fading before so
9667 // reset alpha
9668 cache.scrollBar.setAlpha(255);
9669 }
9670
Joe Malin32736f02011-01-19 16:14:20 -08009671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009672 final int viewFlags = mViewFlags;
9673
9674 final boolean drawHorizontalScrollBar =
9675 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9676 final boolean drawVerticalScrollBar =
9677 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9678 && !isVerticalScrollBarHidden();
9679
9680 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9681 final int width = mRight - mLeft;
9682 final int height = mBottom - mTop;
9683
9684 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009685
Mike Reede8853fc2009-09-04 14:01:48 -04009686 final int scrollX = mScrollX;
9687 final int scrollY = mScrollY;
9688 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9689
Mike Cleronf116bf82009-09-27 19:14:12 -07009690 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009693 int size = scrollBar.getSize(false);
9694 if (size <= 0) {
9695 size = cache.scrollBarSize;
9696 }
9697
Mike Cleronf116bf82009-09-27 19:14:12 -07009698 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009699 computeHorizontalScrollOffset(),
9700 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009701 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009702 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009703 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009704 left = scrollX + (mPaddingLeft & inside);
9705 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9706 bottom = top + size;
9707 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9708 if (invalidate) {
9709 invalidate(left, top, right, bottom);
9710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711 }
9712
9713 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009714 int size = scrollBar.getSize(true);
9715 if (size <= 0) {
9716 size = cache.scrollBarSize;
9717 }
9718
Mike Reede8853fc2009-09-04 14:01:48 -04009719 scrollBar.setParameters(computeVerticalScrollRange(),
9720 computeVerticalScrollOffset(),
9721 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009722 switch (mVerticalScrollbarPosition) {
9723 default:
9724 case SCROLLBAR_POSITION_DEFAULT:
9725 case SCROLLBAR_POSITION_RIGHT:
9726 left = scrollX + width - size - (mUserPaddingRight & inside);
9727 break;
9728 case SCROLLBAR_POSITION_LEFT:
9729 left = scrollX + (mUserPaddingLeft & inside);
9730 break;
9731 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009732 top = scrollY + (mPaddingTop & inside);
9733 right = left + size;
9734 bottom = scrollY + height - (mUserPaddingBottom & inside);
9735 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9736 if (invalidate) {
9737 invalidate(left, top, right, bottom);
9738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 }
9740 }
9741 }
9742 }
Romain Guy8506ab42009-06-11 17:35:47 -07009743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009744 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009745 * 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 -08009746 * FastScroller is visible.
9747 * @return whether to temporarily hide the vertical scrollbar
9748 * @hide
9749 */
9750 protected boolean isVerticalScrollBarHidden() {
9751 return false;
9752 }
9753
9754 /**
9755 * <p>Draw the horizontal scrollbar if
9756 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9757 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009758 * @param canvas the canvas on which to draw the scrollbar
9759 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009760 *
9761 * @see #isHorizontalScrollBarEnabled()
9762 * @see #computeHorizontalScrollRange()
9763 * @see #computeHorizontalScrollExtent()
9764 * @see #computeHorizontalScrollOffset()
9765 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009766 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009767 */
Romain Guy8fb95422010-08-17 18:38:51 -07009768 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9769 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009770 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009771 scrollBar.draw(canvas);
9772 }
Mike Reede8853fc2009-09-04 14:01:48 -04009773
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009774 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9776 * returns true.</p>
9777 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778 * @param canvas the canvas on which to draw the scrollbar
9779 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 *
9781 * @see #isVerticalScrollBarEnabled()
9782 * @see #computeVerticalScrollRange()
9783 * @see #computeVerticalScrollExtent()
9784 * @see #computeVerticalScrollOffset()
9785 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009786 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009787 */
Romain Guy8fb95422010-08-17 18:38:51 -07009788 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9789 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009790 scrollBar.setBounds(l, t, r, b);
9791 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009792 }
9793
9794 /**
9795 * Implement this to do your drawing.
9796 *
9797 * @param canvas the canvas on which the background will be drawn
9798 */
9799 protected void onDraw(Canvas canvas) {
9800 }
9801
9802 /*
9803 * Caller is responsible for calling requestLayout if necessary.
9804 * (This allows addViewInLayout to not request a new layout.)
9805 */
9806 void assignParent(ViewParent parent) {
9807 if (mParent == null) {
9808 mParent = parent;
9809 } else if (parent == null) {
9810 mParent = null;
9811 } else {
9812 throw new RuntimeException("view " + this + " being added, but"
9813 + " it already has a parent");
9814 }
9815 }
9816
9817 /**
9818 * This is called when the view is attached to a window. At this point it
9819 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009820 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9821 * however it may be called any time before the first onDraw -- including
9822 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009823 *
9824 * @see #onDetachedFromWindow()
9825 */
9826 protected void onAttachedToWindow() {
9827 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9828 mParent.requestTransparentRegion(this);
9829 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009830 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9831 initialAwakenScrollBars();
9832 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9833 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009834 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009835 // Order is important here: LayoutDirection MUST be resolved before Padding
9836 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009837 resolveLayoutDirectionIfNeeded();
9838 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009839 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009840 if (isFocused()) {
9841 InputMethodManager imm = InputMethodManager.peekInstance();
9842 imm.focusIn(this);
9843 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009844 }
Cibu Johny86666632010-02-22 13:01:02 -08009845
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009846 /**
Romain Guybb9908b2012-03-08 11:14:07 -08009847 * @see #onScreenStateChanged(int)
9848 */
9849 void dispatchScreenStateChanged(int screenState) {
9850 onScreenStateChanged(screenState);
9851 }
9852
9853 /**
9854 * This method is called whenever the state of the screen this view is
9855 * attached to changes. A state change will usually occurs when the screen
9856 * turns on or off (whether it happens automatically or the user does it
9857 * manually.)
9858 *
9859 * @param screenState The new state of the screen. Can be either
9860 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
9861 */
9862 public void onScreenStateChanged(int screenState) {
9863 }
9864
9865 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009866 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9867 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009868 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009869 private void resolveLayoutDirectionIfNeeded() {
9870 // Do not resolve if it is not needed
9871 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9872
9873 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07009874 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009875
9876 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009877 switch (getLayoutDirection()) {
9878 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009879 // We cannot do the resolution if there is no parent
9880 if (mParent == null) return;
9881
Cibu Johny86666632010-02-22 13:01:02 -08009882 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009883 if (mParent instanceof ViewGroup) {
9884 ViewGroup viewGroup = ((ViewGroup) mParent);
9885
9886 // Check if the parent view group can resolve
9887 if (! viewGroup.canResolveLayoutDirection()) {
9888 return;
9889 }
9890 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9891 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9892 }
Cibu Johny86666632010-02-22 13:01:02 -08009893 }
9894 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009895 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009896 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009897 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009898 case LAYOUT_DIRECTION_LOCALE:
9899 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009900 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009901 }
9902 break;
9903 default:
9904 // Nothing to do, LTR by default
9905 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009906
9907 // Set to resolved
9908 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009909 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009910 // Resolve padding
9911 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009912 }
9913
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009914 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009915 * Called when layout direction has been resolved.
9916 *
9917 * The default implementation does nothing.
9918 */
9919 public void onResolvedLayoutDirectionChanged() {
9920 }
9921
9922 /**
9923 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009924 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009925 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009926 // If the user specified the absolute padding (either with android:padding or
9927 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9928 // use the default padding or the padding from the background drawable
9929 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009930 int resolvedLayoutDirection = getResolvedLayoutDirection();
9931 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009932 case LAYOUT_DIRECTION_RTL:
9933 // Start user padding override Right user padding. Otherwise, if Right user
9934 // padding is not defined, use the default Right padding. If Right user padding
9935 // is defined, just use it.
9936 if (mUserPaddingStart >= 0) {
9937 mUserPaddingRight = mUserPaddingStart;
9938 } else if (mUserPaddingRight < 0) {
9939 mUserPaddingRight = mPaddingRight;
9940 }
9941 if (mUserPaddingEnd >= 0) {
9942 mUserPaddingLeft = mUserPaddingEnd;
9943 } else if (mUserPaddingLeft < 0) {
9944 mUserPaddingLeft = mPaddingLeft;
9945 }
9946 break;
9947 case LAYOUT_DIRECTION_LTR:
9948 default:
9949 // Start user padding override Left user padding. Otherwise, if Left user
9950 // padding is not defined, use the default left padding. If Left user padding
9951 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009952 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009953 mUserPaddingLeft = mUserPaddingStart;
9954 } else if (mUserPaddingLeft < 0) {
9955 mUserPaddingLeft = mPaddingLeft;
9956 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009957 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009958 mUserPaddingRight = mUserPaddingEnd;
9959 } else if (mUserPaddingRight < 0) {
9960 mUserPaddingRight = mPaddingRight;
9961 }
9962 }
9963
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009964 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9965
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009966 if(isPaddingRelative()) {
9967 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
9968 } else {
9969 recomputePadding();
9970 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009971 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009972 }
9973
9974 /**
9975 * Resolve padding depending on the layout direction. Subclasses that care about
9976 * padding resolution should override this method. The default implementation does
9977 * nothing.
9978 *
9979 * @param layoutDirection the direction of the layout
9980 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -08009981 * @see {@link #LAYOUT_DIRECTION_LTR}
9982 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009983 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009984 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009985 }
9986
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009987 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009988 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009989 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009990 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009991 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009992 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009993 switch (getLayoutDirection()) {
9994 case LAYOUT_DIRECTION_INHERIT:
9995 return (mParent != null);
9996 default:
9997 return true;
9998 }
9999 }
10000
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010001 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010002 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10003 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010004 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010005 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010006 // Reset the current resolved bits
10007 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010008 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010009 // Reset also the text direction
10010 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010011 }
10012
10013 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010014 * Called during reset of resolved layout direction.
10015 *
10016 * Subclasses need to override this method to clear cached information that depends on the
10017 * resolved layout direction, or to inform child views that inherit their layout direction.
10018 *
10019 * The default implementation does nothing.
10020 */
10021 public void onResolvedLayoutDirectionReset() {
10022 }
10023
10024 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010025 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010026 *
10027 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010028 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010029 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010030 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080010031 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010032 }
10033
10034 /**
10035 * This is called when the view is detached from a window. At this point it
10036 * no longer has a surface for drawing.
10037 *
10038 * @see #onAttachedToWindow()
10039 */
10040 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080010041 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080010042
Romain Guya440b002010-02-24 15:57:54 -080010043 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050010044 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080010045 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070010046 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080010047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010049
Romain Guy6d7475d2011-07-27 16:28:21 -070010050 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010051
10052 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070010053 if (mDisplayList != null) {
10054 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
10055 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010056 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070010057 } else {
10058 if (mDisplayList != null) {
10059 // Should never happen
10060 mDisplayList.invalidate();
10061 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010062 }
10063
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080010064 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070010065
10066 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067 }
10068
10069 /**
10070 * @return The number of times this view has been attached to a window
10071 */
10072 protected int getWindowAttachCount() {
10073 return mWindowAttachCount;
10074 }
10075
10076 /**
10077 * Retrieve a unique token identifying the window this view is attached to.
10078 * @return Return the window's token for use in
10079 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
10080 */
10081 public IBinder getWindowToken() {
10082 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
10083 }
10084
10085 /**
10086 * Retrieve a unique token identifying the top-level "real" window of
10087 * the window that this view is attached to. That is, this is like
10088 * {@link #getWindowToken}, except if the window this view in is a panel
10089 * window (attached to another containing window), then the token of
10090 * the containing window is returned instead.
10091 *
10092 * @return Returns the associated window token, either
10093 * {@link #getWindowToken()} or the containing window's token.
10094 */
10095 public IBinder getApplicationWindowToken() {
10096 AttachInfo ai = mAttachInfo;
10097 if (ai != null) {
10098 IBinder appWindowToken = ai.mPanelParentWindowToken;
10099 if (appWindowToken == null) {
10100 appWindowToken = ai.mWindowToken;
10101 }
10102 return appWindowToken;
10103 }
10104 return null;
10105 }
10106
10107 /**
10108 * Retrieve private session object this view hierarchy is using to
10109 * communicate with the window manager.
10110 * @return the session object to communicate with the window manager
10111 */
10112 /*package*/ IWindowSession getWindowSession() {
10113 return mAttachInfo != null ? mAttachInfo.mSession : null;
10114 }
10115
10116 /**
10117 * @param info the {@link android.view.View.AttachInfo} to associated with
10118 * this view
10119 */
10120 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
10121 //System.out.println("Attached! " + this);
10122 mAttachInfo = info;
10123 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010124 // We will need to evaluate the drawable state at least once.
10125 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010126 if (mFloatingTreeObserver != null) {
10127 info.mTreeObserver.merge(mFloatingTreeObserver);
10128 mFloatingTreeObserver = null;
10129 }
10130 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
10131 mAttachInfo.mScrollContainers.add(this);
10132 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
10133 }
10134 performCollectViewAttributes(visibility);
10135 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080010136
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010137 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010138 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010139 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010140 if (listeners != null && listeners.size() > 0) {
10141 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10142 // perform the dispatching. The iterator is a safe guard against listeners that
10143 // could mutate the list by calling the various add/remove methods. This prevents
10144 // the array from being modified while we iterate it.
10145 for (OnAttachStateChangeListener listener : listeners) {
10146 listener.onViewAttachedToWindow(this);
10147 }
10148 }
10149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 int vis = info.mWindowVisibility;
10151 if (vis != GONE) {
10152 onWindowVisibilityChanged(vis);
10153 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010154 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
10155 // If nobody has evaluated the drawable state yet, then do it now.
10156 refreshDrawableState();
10157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010158 }
10159
10160 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 AttachInfo info = mAttachInfo;
10162 if (info != null) {
10163 int vis = info.mWindowVisibility;
10164 if (vis != GONE) {
10165 onWindowVisibilityChanged(GONE);
10166 }
10167 }
10168
10169 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010170
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010171 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010172 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010173 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010174 if (listeners != null && listeners.size() > 0) {
10175 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10176 // perform the dispatching. The iterator is a safe guard against listeners that
10177 // could mutate the list by calling the various add/remove methods. This prevents
10178 // the array from being modified while we iterate it.
10179 for (OnAttachStateChangeListener listener : listeners) {
10180 listener.onViewDetachedFromWindow(this);
10181 }
10182 }
10183
Romain Guy01d5edc2011-01-28 11:28:53 -080010184 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010185 mAttachInfo.mScrollContainers.remove(this);
10186 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10187 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010189 mAttachInfo = null;
10190 }
10191
10192 /**
10193 * Store this view hierarchy's frozen state into the given container.
10194 *
10195 * @param container The SparseArray in which to save the view's state.
10196 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010197 * @see #restoreHierarchyState(android.util.SparseArray)
10198 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10199 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010200 */
10201 public void saveHierarchyState(SparseArray<Parcelable> container) {
10202 dispatchSaveInstanceState(container);
10203 }
10204
10205 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010206 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10207 * this view and its children. May be overridden to modify how freezing happens to a
10208 * 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 -080010209 *
10210 * @param container The SparseArray in which to save the view's state.
10211 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010212 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10213 * @see #saveHierarchyState(android.util.SparseArray)
10214 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010215 */
10216 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10217 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10218 mPrivateFlags &= ~SAVE_STATE_CALLED;
10219 Parcelable state = onSaveInstanceState();
10220 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10221 throw new IllegalStateException(
10222 "Derived class did not call super.onSaveInstanceState()");
10223 }
10224 if (state != null) {
10225 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10226 // + ": " + state);
10227 container.put(mID, state);
10228 }
10229 }
10230 }
10231
10232 /**
10233 * Hook allowing a view to generate a representation of its internal state
10234 * that can later be used to create a new instance with that same state.
10235 * This state should only contain information that is not persistent or can
10236 * not be reconstructed later. For example, you will never store your
10237 * current position on screen because that will be computed again when a
10238 * new instance of the view is placed in its view hierarchy.
10239 * <p>
10240 * Some examples of things you may store here: the current cursor position
10241 * in a text view (but usually not the text itself since that is stored in a
10242 * content provider or other persistent storage), the currently selected
10243 * item in a list view.
10244 *
10245 * @return Returns a Parcelable object containing the view's current dynamic
10246 * state, or null if there is nothing interesting to save. The
10247 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010248 * @see #onRestoreInstanceState(android.os.Parcelable)
10249 * @see #saveHierarchyState(android.util.SparseArray)
10250 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010251 * @see #setSaveEnabled(boolean)
10252 */
10253 protected Parcelable onSaveInstanceState() {
10254 mPrivateFlags |= SAVE_STATE_CALLED;
10255 return BaseSavedState.EMPTY_STATE;
10256 }
10257
10258 /**
10259 * Restore this view hierarchy's frozen state from the given container.
10260 *
10261 * @param container The SparseArray which holds previously frozen states.
10262 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010263 * @see #saveHierarchyState(android.util.SparseArray)
10264 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10265 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010266 */
10267 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10268 dispatchRestoreInstanceState(container);
10269 }
10270
10271 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010272 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10273 * state for this view and its children. May be overridden to modify how restoring
10274 * happens to a view's children; for example, some views may want to not store state
10275 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 *
10277 * @param container The SparseArray which holds previously saved state.
10278 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010279 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10280 * @see #restoreHierarchyState(android.util.SparseArray)
10281 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010282 */
10283 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10284 if (mID != NO_ID) {
10285 Parcelable state = container.get(mID);
10286 if (state != null) {
10287 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10288 // + ": " + state);
10289 mPrivateFlags &= ~SAVE_STATE_CALLED;
10290 onRestoreInstanceState(state);
10291 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10292 throw new IllegalStateException(
10293 "Derived class did not call super.onRestoreInstanceState()");
10294 }
10295 }
10296 }
10297 }
10298
10299 /**
10300 * Hook allowing a view to re-apply a representation of its internal state that had previously
10301 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10302 * null state.
10303 *
10304 * @param state The frozen state that had previously been returned by
10305 * {@link #onSaveInstanceState}.
10306 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010307 * @see #onSaveInstanceState()
10308 * @see #restoreHierarchyState(android.util.SparseArray)
10309 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 */
10311 protected void onRestoreInstanceState(Parcelable state) {
10312 mPrivateFlags |= SAVE_STATE_CALLED;
10313 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010314 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10315 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010316 + "when two views of different type have the same id in the same hierarchy. "
10317 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010318 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010319 }
10320 }
10321
10322 /**
10323 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10324 *
10325 * @return the drawing start time in milliseconds
10326 */
10327 public long getDrawingTime() {
10328 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10329 }
10330
10331 /**
10332 * <p>Enables or disables the duplication of the parent's state into this view. When
10333 * duplication is enabled, this view gets its drawable state from its parent rather
10334 * than from its own internal properties.</p>
10335 *
10336 * <p>Note: in the current implementation, setting this property to true after the
10337 * view was added to a ViewGroup might have no effect at all. This property should
10338 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10339 *
10340 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10341 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010342 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010343 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10344 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 *
10346 * @param enabled True to enable duplication of the parent's drawable state, false
10347 * to disable it.
10348 *
10349 * @see #getDrawableState()
10350 * @see #isDuplicateParentStateEnabled()
10351 */
10352 public void setDuplicateParentStateEnabled(boolean enabled) {
10353 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10354 }
10355
10356 /**
10357 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10358 *
10359 * @return True if this view's drawable state is duplicated from the parent,
10360 * false otherwise
10361 *
10362 * @see #getDrawableState()
10363 * @see #setDuplicateParentStateEnabled(boolean)
10364 */
10365 public boolean isDuplicateParentStateEnabled() {
10366 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10367 }
10368
10369 /**
Romain Guy171c5922011-01-06 10:04:23 -080010370 * <p>Specifies the type of layer backing this view. The layer can be
10371 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10372 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010373 *
Romain Guy171c5922011-01-06 10:04:23 -080010374 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10375 * instance that controls how the layer is composed on screen. The following
10376 * properties of the paint are taken into account when composing the layer:</p>
10377 * <ul>
10378 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10379 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10380 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10381 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010382 *
Romain Guy171c5922011-01-06 10:04:23 -080010383 * <p>If this view has an alpha value set to < 1.0 by calling
10384 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10385 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10386 * equivalent to setting a hardware layer on this view and providing a paint with
10387 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010388 *
Romain Guy171c5922011-01-06 10:04:23 -080010389 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10390 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10391 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010392 *
Romain Guy171c5922011-01-06 10:04:23 -080010393 * @param layerType The ype of layer to use with this view, must be one of
10394 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10395 * {@link #LAYER_TYPE_HARDWARE}
10396 * @param paint The paint used to compose the layer. This argument is optional
10397 * and can be null. It is ignored when the layer type is
10398 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010399 *
10400 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010401 * @see #LAYER_TYPE_NONE
10402 * @see #LAYER_TYPE_SOFTWARE
10403 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010404 * @see #setAlpha(float)
10405 *
Romain Guy171c5922011-01-06 10:04:23 -080010406 * @attr ref android.R.styleable#View_layerType
10407 */
10408 public void setLayerType(int layerType, Paint paint) {
10409 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010410 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010411 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10412 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010413
Romain Guyd6cd5722011-01-17 14:42:41 -080010414 if (layerType == mLayerType) {
10415 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10416 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010417 invalidateParentCaches();
10418 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010419 }
10420 return;
10421 }
Romain Guy171c5922011-01-06 10:04:23 -080010422
10423 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010424 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010425 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010426 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010427 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010428 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010429 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010430 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010431 default:
10432 break;
Romain Guy171c5922011-01-06 10:04:23 -080010433 }
10434
10435 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010436 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10437 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10438 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010439
Romain Guy0fd89bf2011-01-26 15:41:30 -080010440 invalidateParentCaches();
10441 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010442 }
10443
10444 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010445 * Indicates whether this view has a static layer. A view with layer type
10446 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10447 * dynamic.
10448 */
10449 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010450 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010451 }
10452
10453 /**
Romain Guy171c5922011-01-06 10:04:23 -080010454 * Indicates what type of layer is currently associated with this view. By default
10455 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10456 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10457 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010458 *
Romain Guy171c5922011-01-06 10:04:23 -080010459 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10460 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010461 *
10462 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010463 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010464 * @see #LAYER_TYPE_NONE
10465 * @see #LAYER_TYPE_SOFTWARE
10466 * @see #LAYER_TYPE_HARDWARE
10467 */
10468 public int getLayerType() {
10469 return mLayerType;
10470 }
Joe Malin32736f02011-01-19 16:14:20 -080010471
Romain Guy6c319ca2011-01-11 14:29:25 -080010472 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010473 * Forces this view's layer to be created and this view to be rendered
10474 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10475 * invoking this method will have no effect.
10476 *
10477 * This method can for instance be used to render a view into its layer before
10478 * starting an animation. If this view is complex, rendering into the layer
10479 * before starting the animation will avoid skipping frames.
10480 *
10481 * @throws IllegalStateException If this view is not attached to a window
10482 *
10483 * @see #setLayerType(int, android.graphics.Paint)
10484 */
10485 public void buildLayer() {
10486 if (mLayerType == LAYER_TYPE_NONE) return;
10487
10488 if (mAttachInfo == null) {
10489 throw new IllegalStateException("This view must be attached to a window first");
10490 }
10491
10492 switch (mLayerType) {
10493 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010494 if (mAttachInfo.mHardwareRenderer != null &&
10495 mAttachInfo.mHardwareRenderer.isEnabled() &&
10496 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010497 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010498 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010499 break;
10500 case LAYER_TYPE_SOFTWARE:
10501 buildDrawingCache(true);
10502 break;
10503 }
10504 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010505
10506 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10507 void flushLayer() {
10508 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10509 mHardwareLayer.flush();
10510 }
10511 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010512
10513 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010514 * <p>Returns a hardware layer that can be used to draw this view again
10515 * without executing its draw method.</p>
10516 *
10517 * @return A HardwareLayer ready to render, or null if an error occurred.
10518 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010519 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010520 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10521 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010522 return null;
10523 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010524
10525 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010526
10527 final int width = mRight - mLeft;
10528 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010529
Romain Guy6c319ca2011-01-11 14:29:25 -080010530 if (width == 0 || height == 0) {
10531 return null;
10532 }
10533
10534 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10535 if (mHardwareLayer == null) {
10536 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10537 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070010538 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010539 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10540 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070010541 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010542 }
10543
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010544 // The layer is not valid if the underlying GPU resources cannot be allocated
10545 if (!mHardwareLayer.isValid()) {
10546 return null;
10547 }
10548
Chet Haasea1cff502012-02-21 13:43:44 -080010549 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080010550 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010551 }
10552
10553 return mHardwareLayer;
10554 }
Romain Guy171c5922011-01-06 10:04:23 -080010555
Romain Guy589b0bb2011-10-10 13:57:47 -070010556 /**
10557 * Destroys this View's hardware layer if possible.
10558 *
10559 * @return True if the layer was destroyed, false otherwise.
10560 *
10561 * @see #setLayerType(int, android.graphics.Paint)
10562 * @see #LAYER_TYPE_HARDWARE
10563 */
Romain Guy65b345f2011-07-27 18:51:50 -070010564 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010565 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010566 AttachInfo info = mAttachInfo;
10567 if (info != null && info.mHardwareRenderer != null &&
10568 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10569 mHardwareLayer.destroy();
10570 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010571
Romain Guy9c4b79a2011-11-10 19:23:58 -080010572 invalidate(true);
10573 invalidateParentCaches();
10574 }
Romain Guy65b345f2011-07-27 18:51:50 -070010575 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010576 }
Romain Guy65b345f2011-07-27 18:51:50 -070010577 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010578 }
10579
Romain Guy171c5922011-01-06 10:04:23 -080010580 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010581 * Destroys all hardware rendering resources. This method is invoked
10582 * when the system needs to reclaim resources. Upon execution of this
10583 * method, you should free any OpenGL resources created by the view.
10584 *
10585 * Note: you <strong>must</strong> call
10586 * <code>super.destroyHardwareResources()</code> when overriding
10587 * this method.
10588 *
10589 * @hide
10590 */
10591 protected void destroyHardwareResources() {
10592 destroyLayer();
10593 }
10594
10595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010596 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10597 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10598 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10599 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10600 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10601 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010602 *
Romain Guy171c5922011-01-06 10:04:23 -080010603 * <p>Enabling the drawing cache is similar to
10604 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010605 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10606 * drawing cache has no effect on rendering because the system uses a different mechanism
10607 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10608 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10609 * for information on how to enable software and hardware layers.</p>
10610 *
10611 * <p>This API can be used to manually generate
10612 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10613 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010614 *
10615 * @param enabled true to enable the drawing cache, false otherwise
10616 *
10617 * @see #isDrawingCacheEnabled()
10618 * @see #getDrawingCache()
10619 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010620 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010621 */
10622 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010623 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10625 }
10626
10627 /**
10628 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10629 *
10630 * @return true if the drawing cache is enabled
10631 *
10632 * @see #setDrawingCacheEnabled(boolean)
10633 * @see #getDrawingCache()
10634 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010635 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 public boolean isDrawingCacheEnabled() {
10637 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10638 }
10639
10640 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010641 * Debugging utility which recursively outputs the dirty state of a view and its
10642 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010643 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010644 * @hide
10645 */
Romain Guy676b1732011-02-14 14:45:33 -080010646 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010647 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10648 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10649 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10650 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10651 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10652 if (clear) {
10653 mPrivateFlags &= clearMask;
10654 }
10655 if (this instanceof ViewGroup) {
10656 ViewGroup parent = (ViewGroup) this;
10657 final int count = parent.getChildCount();
10658 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010659 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010660 child.outputDirtyFlags(indent + " ", clear, clearMask);
10661 }
10662 }
10663 }
10664
10665 /**
10666 * This method is used by ViewGroup to cause its children to restore or recreate their
10667 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10668 * to recreate its own display list, which would happen if it went through the normal
10669 * draw/dispatchDraw mechanisms.
10670 *
10671 * @hide
10672 */
10673 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010674
10675 /**
10676 * A view that is not attached or hardware accelerated cannot create a display list.
10677 * This method checks these conditions and returns the appropriate result.
10678 *
10679 * @return true if view has the ability to create a display list, false otherwise.
10680 *
10681 * @hide
10682 */
10683 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010684 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010685 }
Joe Malin32736f02011-01-19 16:14:20 -080010686
Chet Haasedaf98e92011-01-10 14:10:36 -080010687 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010688 * @return The HardwareRenderer associated with that view or null if hardware rendering
10689 * is not supported or this this has not been attached to a window.
10690 *
10691 * @hide
10692 */
10693 public HardwareRenderer getHardwareRenderer() {
10694 if (mAttachInfo != null) {
10695 return mAttachInfo.mHardwareRenderer;
10696 }
10697 return null;
10698 }
10699
10700 /**
Chet Haasea1cff502012-02-21 13:43:44 -080010701 * Returns a DisplayList. If the incoming displayList is null, one will be created.
10702 * Otherwise, the same display list will be returned (after having been rendered into
10703 * along the way, depending on the invalidation state of the view).
10704 *
10705 * @param displayList The previous version of this displayList, could be null.
10706 * @param isLayer Whether the requester of the display list is a layer. If so,
10707 * the view will avoid creating a layer inside the resulting display list.
10708 * @return A new or reused DisplayList object.
10709 */
10710 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
10711 if (!canHaveDisplayList()) {
10712 return null;
10713 }
10714
10715 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10716 displayList == null || !displayList.isValid() ||
10717 (!isLayer && mRecreateDisplayList))) {
10718 // Don't need to recreate the display list, just need to tell our
10719 // children to restore/recreate theirs
10720 if (displayList != null && displayList.isValid() &&
10721 !isLayer && !mRecreateDisplayList) {
10722 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10723 mPrivateFlags &= ~DIRTY_MASK;
10724 dispatchGetDisplayList();
10725
10726 return displayList;
10727 }
10728
10729 if (!isLayer) {
10730 // If we got here, we're recreating it. Mark it as such to ensure that
10731 // we copy in child display lists into ours in drawChild()
10732 mRecreateDisplayList = true;
10733 }
10734 if (displayList == null) {
10735 final String name = getClass().getSimpleName();
10736 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
10737 // If we're creating a new display list, make sure our parent gets invalidated
10738 // since they will need to recreate their display list to account for this
10739 // new child display list.
10740 invalidateParentCaches();
10741 }
10742
10743 boolean caching = false;
10744 final HardwareCanvas canvas = displayList.start();
10745 int restoreCount = 0;
10746 int width = mRight - mLeft;
10747 int height = mBottom - mTop;
10748
10749 try {
10750 canvas.setViewport(width, height);
10751 // The dirty rect should always be null for a display list
10752 canvas.onPreDraw(null);
10753 int layerType = (
10754 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
10755 getLayerType() : LAYER_TYPE_NONE;
10756 if (!isLayer && layerType == LAYER_TYPE_HARDWARE && USE_DISPLAY_LIST_PROPERTIES) {
10757 final HardwareLayer layer = getHardwareLayer();
10758 if (layer != null && layer.isValid()) {
10759 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
10760 } else {
10761 canvas.saveLayer(0, 0,
10762 mRight - mLeft, mBottom - mTop, mLayerPaint,
10763 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
10764 }
10765 caching = true;
10766 } else {
10767
10768 computeScroll();
10769
10770 if (!USE_DISPLAY_LIST_PROPERTIES) {
10771 restoreCount = canvas.save();
10772 }
10773 canvas.translate(-mScrollX, -mScrollY);
10774 if (!isLayer) {
10775 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10776 mPrivateFlags &= ~DIRTY_MASK;
10777 }
10778
10779 // Fast path for layouts with no backgrounds
10780 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10781 dispatchDraw(canvas);
10782 } else {
10783 draw(canvas);
10784 }
10785 }
10786 } finally {
10787 if (USE_DISPLAY_LIST_PROPERTIES) {
10788 canvas.restoreToCount(restoreCount);
10789 }
10790 canvas.onPostDraw();
10791
10792 displayList.end();
10793 if (USE_DISPLAY_LIST_PROPERTIES) {
10794 displayList.setCaching(caching);
10795 }
10796 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
10797 displayList.setLeftTopRightBottom(0, 0, width, height);
10798 } else {
10799 setDisplayListProperties(displayList);
10800 }
10801 }
10802 } else if (!isLayer) {
10803 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10804 mPrivateFlags &= ~DIRTY_MASK;
10805 }
10806
10807 return displayList;
10808 }
10809
10810 /**
10811 * Get the DisplayList for the HardwareLayer
10812 *
10813 * @param layer The HardwareLayer whose DisplayList we want
10814 * @return A DisplayList fopr the specified HardwareLayer
10815 */
10816 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
10817 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
10818 layer.setDisplayList(displayList);
10819 return displayList;
10820 }
10821
10822
10823 /**
Romain Guyb051e892010-09-28 19:09:36 -070010824 * <p>Returns a display list that can be used to draw this view again
10825 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010826 *
Romain Guyb051e892010-09-28 19:09:36 -070010827 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010828 *
10829 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010830 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010831 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080010832 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070010833 return mDisplayList;
10834 }
10835
10836 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010837 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010838 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010839 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010840 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010841 * @see #getDrawingCache(boolean)
10842 */
10843 public Bitmap getDrawingCache() {
10844 return getDrawingCache(false);
10845 }
10846
10847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010848 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10849 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10850 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10851 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10852 * request the drawing cache by calling this method and draw it on screen if the
10853 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010854 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010855 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10856 * this method will create a bitmap of the same size as this view. Because this bitmap
10857 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10858 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10859 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10860 * size than the view. This implies that your application must be able to handle this
10861 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010862 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010863 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10864 * the current density of the screen when the application is in compatibility
10865 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010866 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010867 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010868 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 * @see #setDrawingCacheEnabled(boolean)
10870 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010871 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010872 * @see #destroyDrawingCache()
10873 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010874 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010875 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10876 return null;
10877 }
10878 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010879 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010880 }
Romain Guy02890fd2010-08-06 17:58:44 -070010881 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010882 }
10883
10884 /**
10885 * <p>Frees the resources used by the drawing cache. If you call
10886 * {@link #buildDrawingCache()} manually without calling
10887 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10888 * should cleanup the cache with this method afterwards.</p>
10889 *
10890 * @see #setDrawingCacheEnabled(boolean)
10891 * @see #buildDrawingCache()
10892 * @see #getDrawingCache()
10893 */
10894 public void destroyDrawingCache() {
10895 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010896 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010897 mDrawingCache = null;
10898 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010899 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010900 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010901 mUnscaledDrawingCache = null;
10902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010903 }
10904
10905 /**
10906 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010907 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010908 * view will always be drawn on top of a solid color.
10909 *
10910 * @param color The background color to use for the drawing cache's bitmap
10911 *
10912 * @see #setDrawingCacheEnabled(boolean)
10913 * @see #buildDrawingCache()
10914 * @see #getDrawingCache()
10915 */
10916 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010917 if (color != mDrawingCacheBackgroundColor) {
10918 mDrawingCacheBackgroundColor = color;
10919 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010921 }
10922
10923 /**
10924 * @see #setDrawingCacheBackgroundColor(int)
10925 *
10926 * @return The background color to used for the drawing cache's bitmap
10927 */
10928 public int getDrawingCacheBackgroundColor() {
10929 return mDrawingCacheBackgroundColor;
10930 }
10931
10932 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010933 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010934 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010935 * @see #buildDrawingCache(boolean)
10936 */
10937 public void buildDrawingCache() {
10938 buildDrawingCache(false);
10939 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010940
Romain Guyfbd8f692009-06-26 14:51:58 -070010941 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010942 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10943 *
10944 * <p>If you call {@link #buildDrawingCache()} manually without calling
10945 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10946 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010947 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010948 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10949 * this method will create a bitmap of the same size as this view. Because this bitmap
10950 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10951 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10952 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10953 * size than the view. This implies that your application must be able to handle this
10954 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010955 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010956 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10957 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010958 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010959 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 *
10961 * @see #getDrawingCache()
10962 * @see #destroyDrawingCache()
10963 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010964 public void buildDrawingCache(boolean autoScale) {
10965 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010966 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010967 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968
10969 if (ViewDebug.TRACE_HIERARCHY) {
10970 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010972
Romain Guy8506ab42009-06-11 17:35:47 -070010973 int width = mRight - mLeft;
10974 int height = mBottom - mTop;
10975
10976 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010977 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010978
Romain Guye1123222009-06-29 14:24:56 -070010979 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010980 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10981 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010983
10984 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010985 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010986 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010987
10988 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010989 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010990 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010991 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10992 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010993 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010994 return;
10995 }
10996
10997 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010998 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010999
11000 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 Bitmap.Config quality;
11002 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011003 // Never pick ARGB_4444 because it looks awful
11004 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011005 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11006 case DRAWING_CACHE_QUALITY_AUTO:
11007 quality = Bitmap.Config.ARGB_8888;
11008 break;
11009 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011010 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011011 break;
11012 case DRAWING_CACHE_QUALITY_HIGH:
11013 quality = Bitmap.Config.ARGB_8888;
11014 break;
11015 default:
11016 quality = Bitmap.Config.ARGB_8888;
11017 break;
11018 }
11019 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070011020 // Optimization for translucent windows
11021 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080011022 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011023 }
11024
11025 // Try to cleanup memory
11026 if (bitmap != null) bitmap.recycle();
11027
11028 try {
11029 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070011030 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070011031 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070011032 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011033 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070011034 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011035 }
Adam Powell26153a32010-11-08 15:22:27 -080011036 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 } catch (OutOfMemoryError e) {
11038 // If there is not enough memory to create the bitmap cache, just
11039 // ignore the issue as bitmap caches are not required to draw the
11040 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070011041 if (autoScale) {
11042 mDrawingCache = null;
11043 } else {
11044 mUnscaledDrawingCache = null;
11045 }
Romain Guy0211a0a2011-02-14 16:34:59 -080011046 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011047 return;
11048 }
11049
11050 clear = drawingCacheBackgroundColor != 0;
11051 }
11052
11053 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011054 if (attachInfo != null) {
11055 canvas = attachInfo.mCanvas;
11056 if (canvas == null) {
11057 canvas = new Canvas();
11058 }
11059 canvas.setBitmap(bitmap);
11060 // Temporarily clobber the cached Canvas in case one of our children
11061 // is also using a drawing cache. Without this, the children would
11062 // steal the canvas by attaching their own bitmap to it and bad, bad
11063 // thing would happen (invisible views, corrupted drawings, etc.)
11064 attachInfo.mCanvas = null;
11065 } else {
11066 // This case should hopefully never or seldom happen
11067 canvas = new Canvas(bitmap);
11068 }
11069
11070 if (clear) {
11071 bitmap.eraseColor(drawingCacheBackgroundColor);
11072 }
11073
11074 computeScroll();
11075 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080011076
Romain Guye1123222009-06-29 14:24:56 -070011077 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011078 final float scale = attachInfo.mApplicationScale;
11079 canvas.scale(scale, scale);
11080 }
Joe Malin32736f02011-01-19 16:14:20 -080011081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011082 canvas.translate(-mScrollX, -mScrollY);
11083
Romain Guy5bcdff42009-05-14 21:27:18 -070011084 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080011085 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
11086 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070011087 mPrivateFlags |= DRAWING_CACHE_VALID;
11088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011089
11090 // Fast path for layouts with no backgrounds
11091 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11092 if (ViewDebug.TRACE_HIERARCHY) {
11093 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11094 }
Romain Guy5bcdff42009-05-14 21:27:18 -070011095 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011096 dispatchDraw(canvas);
11097 } else {
11098 draw(canvas);
11099 }
11100
11101 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011102 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011103
11104 if (attachInfo != null) {
11105 // Restore the cached Canvas for our siblings
11106 attachInfo.mCanvas = canvas;
11107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011108 }
11109 }
11110
11111 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011112 * Create a snapshot of the view into a bitmap. We should probably make
11113 * some form of this public, but should think about the API.
11114 */
Romain Guy223ff5c2010-03-02 17:07:47 -080011115 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011116 int width = mRight - mLeft;
11117 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011118
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011119 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070011120 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011121 width = (int) ((width * scale) + 0.5f);
11122 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080011123
Romain Guy8c11e312009-09-14 15:15:30 -070011124 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011125 if (bitmap == null) {
11126 throw new OutOfMemoryError();
11127 }
11128
Romain Guyc529d8d2011-09-06 15:01:39 -070011129 Resources resources = getResources();
11130 if (resources != null) {
11131 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
11132 }
Joe Malin32736f02011-01-19 16:14:20 -080011133
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011134 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011135 if (attachInfo != null) {
11136 canvas = attachInfo.mCanvas;
11137 if (canvas == null) {
11138 canvas = new Canvas();
11139 }
11140 canvas.setBitmap(bitmap);
11141 // Temporarily clobber the cached Canvas in case one of our children
11142 // is also using a drawing cache. Without this, the children would
11143 // steal the canvas by attaching their own bitmap to it and bad, bad
11144 // things would happen (invisible views, corrupted drawings, etc.)
11145 attachInfo.mCanvas = null;
11146 } else {
11147 // This case should hopefully never or seldom happen
11148 canvas = new Canvas(bitmap);
11149 }
11150
Romain Guy5bcdff42009-05-14 21:27:18 -070011151 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011152 bitmap.eraseColor(backgroundColor);
11153 }
11154
11155 computeScroll();
11156 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011157 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011158 canvas.translate(-mScrollX, -mScrollY);
11159
Romain Guy5bcdff42009-05-14 21:27:18 -070011160 // Temporarily remove the dirty mask
11161 int flags = mPrivateFlags;
11162 mPrivateFlags &= ~DIRTY_MASK;
11163
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011164 // Fast path for layouts with no backgrounds
11165 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11166 dispatchDraw(canvas);
11167 } else {
11168 draw(canvas);
11169 }
11170
Romain Guy5bcdff42009-05-14 21:27:18 -070011171 mPrivateFlags = flags;
11172
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011173 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011174 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011175
11176 if (attachInfo != null) {
11177 // Restore the cached Canvas for our siblings
11178 attachInfo.mCanvas = canvas;
11179 }
Romain Guy8506ab42009-06-11 17:35:47 -070011180
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011181 return bitmap;
11182 }
11183
11184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011185 * Indicates whether this View is currently in edit mode. A View is usually
11186 * in edit mode when displayed within a developer tool. For instance, if
11187 * this View is being drawn by a visual user interface builder, this method
11188 * should return true.
11189 *
11190 * Subclasses should check the return value of this method to provide
11191 * different behaviors if their normal behavior might interfere with the
11192 * host environment. For instance: the class spawns a thread in its
11193 * constructor, the drawing code relies on device-specific features, etc.
11194 *
11195 * This method is usually checked in the drawing code of custom widgets.
11196 *
11197 * @return True if this View is in edit mode, false otherwise.
11198 */
11199 public boolean isInEditMode() {
11200 return false;
11201 }
11202
11203 /**
11204 * If the View draws content inside its padding and enables fading edges,
11205 * it needs to support padding offsets. Padding offsets are added to the
11206 * fading edges to extend the length of the fade so that it covers pixels
11207 * drawn inside the padding.
11208 *
11209 * Subclasses of this class should override this method if they need
11210 * to draw content inside the padding.
11211 *
11212 * @return True if padding offset must be applied, false otherwise.
11213 *
11214 * @see #getLeftPaddingOffset()
11215 * @see #getRightPaddingOffset()
11216 * @see #getTopPaddingOffset()
11217 * @see #getBottomPaddingOffset()
11218 *
11219 * @since CURRENT
11220 */
11221 protected boolean isPaddingOffsetRequired() {
11222 return false;
11223 }
11224
11225 /**
11226 * Amount by which to extend the left fading region. Called only when
11227 * {@link #isPaddingOffsetRequired()} returns true.
11228 *
11229 * @return The left padding offset in pixels.
11230 *
11231 * @see #isPaddingOffsetRequired()
11232 *
11233 * @since CURRENT
11234 */
11235 protected int getLeftPaddingOffset() {
11236 return 0;
11237 }
11238
11239 /**
11240 * Amount by which to extend the right fading region. Called only when
11241 * {@link #isPaddingOffsetRequired()} returns true.
11242 *
11243 * @return The right padding offset in pixels.
11244 *
11245 * @see #isPaddingOffsetRequired()
11246 *
11247 * @since CURRENT
11248 */
11249 protected int getRightPaddingOffset() {
11250 return 0;
11251 }
11252
11253 /**
11254 * Amount by which to extend the top fading region. Called only when
11255 * {@link #isPaddingOffsetRequired()} returns true.
11256 *
11257 * @return The top padding offset in pixels.
11258 *
11259 * @see #isPaddingOffsetRequired()
11260 *
11261 * @since CURRENT
11262 */
11263 protected int getTopPaddingOffset() {
11264 return 0;
11265 }
11266
11267 /**
11268 * Amount by which to extend the bottom fading region. Called only when
11269 * {@link #isPaddingOffsetRequired()} returns true.
11270 *
11271 * @return The bottom padding offset in pixels.
11272 *
11273 * @see #isPaddingOffsetRequired()
11274 *
11275 * @since CURRENT
11276 */
11277 protected int getBottomPaddingOffset() {
11278 return 0;
11279 }
11280
11281 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011282 * @hide
11283 * @param offsetRequired
11284 */
11285 protected int getFadeTop(boolean offsetRequired) {
11286 int top = mPaddingTop;
11287 if (offsetRequired) top += getTopPaddingOffset();
11288 return top;
11289 }
11290
11291 /**
11292 * @hide
11293 * @param offsetRequired
11294 */
11295 protected int getFadeHeight(boolean offsetRequired) {
11296 int padding = mPaddingTop;
11297 if (offsetRequired) padding += getTopPaddingOffset();
11298 return mBottom - mTop - mPaddingBottom - padding;
11299 }
11300
11301 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011302 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011303 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011304 *
Romain Guy2bffd262010-09-12 17:40:02 -070011305 * <p>Even if this method returns true, it does not mean that every call
11306 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11307 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011308 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011309 * window is hardware accelerated,
11310 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11311 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011312 *
Romain Guy2bffd262010-09-12 17:40:02 -070011313 * @return True if the view is attached to a window and the window is
11314 * hardware accelerated; false in any other case.
11315 */
11316 public boolean isHardwareAccelerated() {
11317 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11318 }
Joe Malin32736f02011-01-19 16:14:20 -080011319
Romain Guy2bffd262010-09-12 17:40:02 -070011320 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011321 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11322 * case of an active Animation being run on the view.
11323 */
11324 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11325 Animation a, boolean scalingRequired) {
11326 Transformation invalidationTransform;
11327 final int flags = parent.mGroupFlags;
11328 final boolean initialized = a.isInitialized();
11329 if (!initialized) {
11330 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11331 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11332 onAnimationStart();
11333 }
11334
11335 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11336 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11337 if (parent.mInvalidationTransformation == null) {
11338 parent.mInvalidationTransformation = new Transformation();
11339 }
11340 invalidationTransform = parent.mInvalidationTransformation;
11341 a.getTransformation(drawingTime, invalidationTransform, 1f);
11342 } else {
11343 invalidationTransform = parent.mChildTransformation;
11344 }
11345 if (more) {
11346 if (!a.willChangeBounds()) {
11347 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11348 parent.FLAG_OPTIMIZE_INVALIDATE) {
11349 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11350 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11351 // The child need to draw an animation, potentially offscreen, so
11352 // make sure we do not cancel invalidate requests
11353 parent.mPrivateFlags |= DRAW_ANIMATION;
11354 parent.invalidate(mLeft, mTop, mRight, mBottom);
11355 }
11356 } else {
11357 if (parent.mInvalidateRegion == null) {
11358 parent.mInvalidateRegion = new RectF();
11359 }
11360 final RectF region = parent.mInvalidateRegion;
11361 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11362 invalidationTransform);
11363
11364 // The child need to draw an animation, potentially offscreen, so
11365 // make sure we do not cancel invalidate requests
11366 parent.mPrivateFlags |= DRAW_ANIMATION;
11367
11368 final int left = mLeft + (int) region.left;
11369 final int top = mTop + (int) region.top;
11370 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11371 top + (int) (region.height() + .5f));
11372 }
11373 }
11374 return more;
11375 }
11376
Chet Haasea1cff502012-02-21 13:43:44 -080011377 void setDisplayListProperties() {
11378 setDisplayListProperties(mDisplayList);
11379 }
11380
11381 /**
11382 * This method is called by getDisplayList() when a display list is created or re-rendered.
11383 * It sets or resets the current value of all properties on that display list (resetting is
11384 * necessary when a display list is being re-created, because we need to make sure that
11385 * previously-set transform values
11386 */
11387 void setDisplayListProperties(DisplayList displayList) {
11388 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
11389 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
11390 if (mParent instanceof ViewGroup) {
11391 displayList.setClipChildren(
11392 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
11393 }
11394 if (mAttachInfo != null && mAttachInfo.mScalingRequired &&
11395 mAttachInfo.mApplicationScale != 1.0f) {
11396 displayList.setApplicationScale(1f / mAttachInfo.mApplicationScale);
11397 }
11398 if (mTransformationInfo != null) {
11399 displayList.setTransformationInfo(mTransformationInfo.mAlpha,
11400 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
11401 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
11402 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
11403 mTransformationInfo.mScaleY);
11404 displayList.setCameraDistance(getCameraDistance());
11405 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
11406 displayList.setPivotX(getPivotX());
11407 displayList.setPivotY(getPivotY());
11408 }
11409 }
11410 }
11411 }
11412
Chet Haasebcca79a2012-02-14 08:45:14 -080011413 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011414 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11415 * This draw() method is an implementation detail and is not intended to be overridden or
11416 * to be called from anywhere else other than ViewGroup.drawChild().
11417 */
11418 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080011419 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
11420 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080011421 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011422 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011423 final int flags = parent.mGroupFlags;
11424
Chet Haasea1cff502012-02-21 13:43:44 -080011425 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080011426 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080011427 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011428 }
11429
11430 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011431 boolean concatMatrix = false;
11432
11433 boolean scalingRequired = false;
11434 boolean caching;
11435 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11436
11437 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080011438 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
11439 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011440 caching = true;
11441 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11442 } else {
11443 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11444 }
11445
Chet Haasebcca79a2012-02-14 08:45:14 -080011446 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011447 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011448 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011449 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011450 transformToApply = parent.mChildTransformation;
Chet Haasea1cff502012-02-21 13:43:44 -080011451 } else if ((flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011452 final boolean hasTransform =
11453 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011454 if (hasTransform) {
11455 final int transformType = parent.mChildTransformation.getTransformationType();
11456 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11457 parent.mChildTransformation : null;
11458 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11459 }
11460 }
11461
11462 concatMatrix |= !childHasIdentityMatrix;
11463
11464 // Sets the flag as early as possible to allow draw() implementations
11465 // to call invalidate() successfully when doing animations
11466 mPrivateFlags |= DRAWN;
11467
Chet Haasebcca79a2012-02-14 08:45:14 -080011468 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011469 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11470 return more;
11471 }
11472
11473 if (hardwareAccelerated) {
11474 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11475 // retain the flag's value temporarily in the mRecreateDisplayList flag
11476 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11477 mPrivateFlags &= ~INVALIDATED;
11478 }
11479
11480 computeScroll();
11481
11482 final int sx = mScrollX;
11483 final int sy = mScrollY;
11484
11485 DisplayList displayList = null;
11486 Bitmap cache = null;
11487 boolean hasDisplayList = false;
11488 if (caching) {
11489 if (!hardwareAccelerated) {
11490 if (layerType != LAYER_TYPE_NONE) {
11491 layerType = LAYER_TYPE_SOFTWARE;
11492 buildDrawingCache(true);
11493 }
11494 cache = getDrawingCache(true);
11495 } else {
11496 switch (layerType) {
11497 case LAYER_TYPE_SOFTWARE:
11498 buildDrawingCache(true);
11499 cache = getDrawingCache(true);
11500 break;
Chet Haasea1cff502012-02-21 13:43:44 -080011501 case LAYER_TYPE_HARDWARE:
11502 if (useDisplayListProperties) {
11503 hasDisplayList = canHaveDisplayList();
11504 }
11505 break;
Chet Haase64a48c12012-02-13 16:33:29 -080011506 case LAYER_TYPE_NONE:
11507 // Delay getting the display list until animation-driven alpha values are
11508 // set up and possibly passed on to the view
11509 hasDisplayList = canHaveDisplayList();
11510 break;
11511 }
11512 }
11513 }
Chet Haasea1cff502012-02-21 13:43:44 -080011514 useDisplayListProperties &= hasDisplayList;
Chet Haase64a48c12012-02-13 16:33:29 -080011515
11516 final boolean hasNoCache = cache == null || hasDisplayList;
11517 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11518 layerType != LAYER_TYPE_HARDWARE;
11519
Chet Haasea1cff502012-02-21 13:43:44 -080011520 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070011521 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080011522 restoreTo = canvas.save();
11523 }
Chet Haase64a48c12012-02-13 16:33:29 -080011524 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011525 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011526 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080011527 if (!useDisplayListProperties) {
11528 canvas.translate(mLeft, mTop);
11529 }
Chet Haase64a48c12012-02-13 16:33:29 -080011530 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080011531 if (useDisplayListProperties) {
11532 restoreTo = canvas.save();
11533 }
Chet Haase64a48c12012-02-13 16:33:29 -080011534 // mAttachInfo cannot be null, otherwise scalingRequired == false
11535 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11536 canvas.scale(scale, scale);
11537 }
11538 }
11539
Chet Haasea1cff502012-02-21 13:43:44 -080011540 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase64a48c12012-02-13 16:33:29 -080011541 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11542 if (transformToApply != null || !childHasIdentityMatrix) {
11543 int transX = 0;
11544 int transY = 0;
11545
11546 if (offsetForScroll) {
11547 transX = -sx;
11548 transY = -sy;
11549 }
11550
11551 if (transformToApply != null) {
11552 if (concatMatrix) {
11553 // Undo the scroll translation, apply the transformation matrix,
11554 // then redo the scroll translate to get the correct result.
Chet Haase89b7f2e2012-03-21 11:15:37 -070011555 canvas.translate(-transX, -transY);
11556 canvas.concat(transformToApply.getMatrix());
11557 canvas.translate(transX, transY);
Chet Haasea1cff502012-02-21 13:43:44 -080011558 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011559 }
11560
11561 float transformAlpha = transformToApply.getAlpha();
11562 if (transformAlpha < 1.0f) {
11563 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080011564 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011565 }
11566 }
11567
Chet Haasea1cff502012-02-21 13:43:44 -080011568 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080011569 canvas.translate(-transX, -transY);
11570 canvas.concat(getMatrix());
11571 canvas.translate(transX, transY);
11572 }
11573 }
11574
11575 if (alpha < 1.0f) {
Chet Haasea1cff502012-02-21 13:43:44 -080011576 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011577 if (hasNoCache) {
11578 final int multipliedAlpha = (int) (255 * alpha);
11579 if (!onSetAlpha(multipliedAlpha)) {
11580 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080011581 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080011582 layerType != LAYER_TYPE_NONE) {
11583 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11584 }
11585 if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070011586 final int scrollX = hasDisplayList ? 0 : sx;
11587 final int scrollY = hasDisplayList ? 0 : sy;
11588 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11589 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011590 }
11591 } else {
11592 // Alpha is handled by the child directly, clobber the layer's alpha
11593 mPrivateFlags |= ALPHA_SET;
11594 }
11595 }
11596 }
11597 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11598 onSetAlpha(255);
11599 mPrivateFlags &= ~ALPHA_SET;
11600 }
11601
Chet Haasea1cff502012-02-21 13:43:44 -080011602 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
11603 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080011604 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011605 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011606 } else {
11607 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011608 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011609 } else {
11610 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11611 }
11612 }
11613 }
11614
11615 if (hasDisplayList) {
11616 displayList = getDisplayList();
11617 if (!displayList.isValid()) {
11618 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11619 // to getDisplayList(), the display list will be marked invalid and we should not
11620 // try to use it again.
11621 displayList = null;
11622 hasDisplayList = false;
11623 }
11624 }
11625
11626 if (hasNoCache) {
11627 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080011628 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011629 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080011630 if (layer != null && layer.isValid()) {
11631 mLayerPaint.setAlpha((int) (alpha * 255));
11632 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11633 layerRendered = true;
11634 } else {
11635 final int scrollX = hasDisplayList ? 0 : sx;
11636 final int scrollY = hasDisplayList ? 0 : sy;
11637 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011638 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011639 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11640 }
11641 }
11642
11643 if (!layerRendered) {
11644 if (!hasDisplayList) {
11645 // Fast path for layouts with no backgrounds
11646 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11647 if (ViewDebug.TRACE_HIERARCHY) {
11648 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11649 }
11650 mPrivateFlags &= ~DIRTY_MASK;
11651 dispatchDraw(canvas);
11652 } else {
11653 draw(canvas);
11654 }
11655 } else {
11656 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011657 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011658 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011659 }
11660 }
11661 } else if (cache != null) {
11662 mPrivateFlags &= ~DIRTY_MASK;
11663 Paint cachePaint;
11664
11665 if (layerType == LAYER_TYPE_NONE) {
11666 cachePaint = parent.mCachePaint;
11667 if (cachePaint == null) {
11668 cachePaint = new Paint();
11669 cachePaint.setDither(false);
11670 parent.mCachePaint = cachePaint;
11671 }
11672 if (alpha < 1.0f) {
11673 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080011674 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
11675 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011676 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080011677 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080011678 }
11679 } else {
11680 cachePaint = mLayerPaint;
11681 cachePaint.setAlpha((int) (alpha * 255));
11682 }
11683 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11684 }
11685
Chet Haasea1cff502012-02-21 13:43:44 -080011686 if (restoreTo >= 0) {
11687 canvas.restoreToCount(restoreTo);
11688 }
Chet Haase64a48c12012-02-13 16:33:29 -080011689
11690 if (a != null && !more) {
11691 if (!hardwareAccelerated && !a.getFillAfter()) {
11692 onSetAlpha(255);
11693 }
11694 parent.finishAnimatingView(this, a);
11695 }
11696
11697 if (more && hardwareAccelerated) {
11698 // invalidation is the trigger to recreate display lists, so if we're using
11699 // display lists to render, force an invalidate to allow the animation to
11700 // continue drawing another frame
11701 parent.invalidate(true);
11702 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11703 // alpha animations should cause the child to recreate its display list
11704 invalidate(true);
11705 }
11706 }
11707
11708 mRecreateDisplayList = false;
11709
11710 return more;
11711 }
11712
11713 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011714 * Manually render this view (and all of its children) to the given Canvas.
11715 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011716 * called. When implementing a view, implement
11717 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11718 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011719 *
11720 * @param canvas The Canvas to which the View is rendered.
11721 */
11722 public void draw(Canvas canvas) {
11723 if (ViewDebug.TRACE_HIERARCHY) {
11724 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11725 }
11726
Romain Guy5bcdff42009-05-14 21:27:18 -070011727 final int privateFlags = mPrivateFlags;
11728 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11729 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11730 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011732 /*
11733 * Draw traversal performs several drawing steps which must be executed
11734 * in the appropriate order:
11735 *
11736 * 1. Draw the background
11737 * 2. If necessary, save the canvas' layers to prepare for fading
11738 * 3. Draw view's content
11739 * 4. Draw children
11740 * 5. If necessary, draw the fading edges and restore layers
11741 * 6. Draw decorations (scrollbars for instance)
11742 */
11743
11744 // Step 1, draw the background, if needed
11745 int saveCount;
11746
Romain Guy24443ea2009-05-11 11:56:30 -070011747 if (!dirtyOpaque) {
11748 final Drawable background = mBGDrawable;
11749 if (background != null) {
11750 final int scrollX = mScrollX;
11751 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011752
Romain Guy24443ea2009-05-11 11:56:30 -070011753 if (mBackgroundSizeChanged) {
11754 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11755 mBackgroundSizeChanged = false;
11756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757
Romain Guy24443ea2009-05-11 11:56:30 -070011758 if ((scrollX | scrollY) == 0) {
11759 background.draw(canvas);
11760 } else {
11761 canvas.translate(scrollX, scrollY);
11762 background.draw(canvas);
11763 canvas.translate(-scrollX, -scrollY);
11764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011765 }
11766 }
11767
11768 // skip step 2 & 5 if possible (common case)
11769 final int viewFlags = mViewFlags;
11770 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11771 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11772 if (!verticalEdges && !horizontalEdges) {
11773 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011774 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011775
11776 // Step 4, draw the children
11777 dispatchDraw(canvas);
11778
11779 // Step 6, draw decorations (scrollbars)
11780 onDrawScrollBars(canvas);
11781
11782 // we're done...
11783 return;
11784 }
11785
11786 /*
11787 * Here we do the full fledged routine...
11788 * (this is an uncommon case where speed matters less,
11789 * this is why we repeat some of the tests that have been
11790 * done above)
11791 */
11792
11793 boolean drawTop = false;
11794 boolean drawBottom = false;
11795 boolean drawLeft = false;
11796 boolean drawRight = false;
11797
11798 float topFadeStrength = 0.0f;
11799 float bottomFadeStrength = 0.0f;
11800 float leftFadeStrength = 0.0f;
11801 float rightFadeStrength = 0.0f;
11802
11803 // Step 2, save the canvas' layers
11804 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011805
11806 final boolean offsetRequired = isPaddingOffsetRequired();
11807 if (offsetRequired) {
11808 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011809 }
11810
11811 int left = mScrollX + paddingLeft;
11812 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011813 int top = mScrollY + getFadeTop(offsetRequired);
11814 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011815
11816 if (offsetRequired) {
11817 right += getRightPaddingOffset();
11818 bottom += getBottomPaddingOffset();
11819 }
11820
11821 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011822 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11823 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011824
11825 // clip the fade length if top and bottom fades overlap
11826 // overlapping fades produce odd-looking artifacts
11827 if (verticalEdges && (top + length > bottom - length)) {
11828 length = (bottom - top) / 2;
11829 }
11830
11831 // also clip horizontal fades if necessary
11832 if (horizontalEdges && (left + length > right - length)) {
11833 length = (right - left) / 2;
11834 }
11835
11836 if (verticalEdges) {
11837 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011838 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011839 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011840 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011841 }
11842
11843 if (horizontalEdges) {
11844 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011845 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011846 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011847 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011848 }
11849
11850 saveCount = canvas.getSaveCount();
11851
11852 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011853 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011854 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11855
11856 if (drawTop) {
11857 canvas.saveLayer(left, top, right, top + length, null, flags);
11858 }
11859
11860 if (drawBottom) {
11861 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11862 }
11863
11864 if (drawLeft) {
11865 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11866 }
11867
11868 if (drawRight) {
11869 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11870 }
11871 } else {
11872 scrollabilityCache.setFadeColor(solidColor);
11873 }
11874
11875 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011876 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011877
11878 // Step 4, draw the children
11879 dispatchDraw(canvas);
11880
11881 // Step 5, draw the fade effect and restore layers
11882 final Paint p = scrollabilityCache.paint;
11883 final Matrix matrix = scrollabilityCache.matrix;
11884 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011885
11886 if (drawTop) {
11887 matrix.setScale(1, fadeHeight * topFadeStrength);
11888 matrix.postTranslate(left, top);
11889 fade.setLocalMatrix(matrix);
11890 canvas.drawRect(left, top, right, top + length, p);
11891 }
11892
11893 if (drawBottom) {
11894 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11895 matrix.postRotate(180);
11896 matrix.postTranslate(left, bottom);
11897 fade.setLocalMatrix(matrix);
11898 canvas.drawRect(left, bottom - length, right, bottom, p);
11899 }
11900
11901 if (drawLeft) {
11902 matrix.setScale(1, fadeHeight * leftFadeStrength);
11903 matrix.postRotate(-90);
11904 matrix.postTranslate(left, top);
11905 fade.setLocalMatrix(matrix);
11906 canvas.drawRect(left, top, left + length, bottom, p);
11907 }
11908
11909 if (drawRight) {
11910 matrix.setScale(1, fadeHeight * rightFadeStrength);
11911 matrix.postRotate(90);
11912 matrix.postTranslate(right, top);
11913 fade.setLocalMatrix(matrix);
11914 canvas.drawRect(right - length, top, right, bottom, p);
11915 }
11916
11917 canvas.restoreToCount(saveCount);
11918
11919 // Step 6, draw decorations (scrollbars)
11920 onDrawScrollBars(canvas);
11921 }
11922
11923 /**
11924 * Override this if your view is known to always be drawn on top of a solid color background,
11925 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11926 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11927 * should be set to 0xFF.
11928 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011929 * @see #setVerticalFadingEdgeEnabled(boolean)
11930 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011931 *
11932 * @return The known solid color background for this view, or 0 if the color may vary
11933 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011934 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011935 public int getSolidColor() {
11936 return 0;
11937 }
11938
11939 /**
11940 * Build a human readable string representation of the specified view flags.
11941 *
11942 * @param flags the view flags to convert to a string
11943 * @return a String representing the supplied flags
11944 */
11945 private static String printFlags(int flags) {
11946 String output = "";
11947 int numFlags = 0;
11948 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11949 output += "TAKES_FOCUS";
11950 numFlags++;
11951 }
11952
11953 switch (flags & VISIBILITY_MASK) {
11954 case INVISIBLE:
11955 if (numFlags > 0) {
11956 output += " ";
11957 }
11958 output += "INVISIBLE";
11959 // USELESS HERE numFlags++;
11960 break;
11961 case GONE:
11962 if (numFlags > 0) {
11963 output += " ";
11964 }
11965 output += "GONE";
11966 // USELESS HERE numFlags++;
11967 break;
11968 default:
11969 break;
11970 }
11971 return output;
11972 }
11973
11974 /**
11975 * Build a human readable string representation of the specified private
11976 * view flags.
11977 *
11978 * @param privateFlags the private view flags to convert to a string
11979 * @return a String representing the supplied flags
11980 */
11981 private static String printPrivateFlags(int privateFlags) {
11982 String output = "";
11983 int numFlags = 0;
11984
11985 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11986 output += "WANTS_FOCUS";
11987 numFlags++;
11988 }
11989
11990 if ((privateFlags & FOCUSED) == FOCUSED) {
11991 if (numFlags > 0) {
11992 output += " ";
11993 }
11994 output += "FOCUSED";
11995 numFlags++;
11996 }
11997
11998 if ((privateFlags & SELECTED) == SELECTED) {
11999 if (numFlags > 0) {
12000 output += " ";
12001 }
12002 output += "SELECTED";
12003 numFlags++;
12004 }
12005
12006 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
12007 if (numFlags > 0) {
12008 output += " ";
12009 }
12010 output += "IS_ROOT_NAMESPACE";
12011 numFlags++;
12012 }
12013
12014 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
12015 if (numFlags > 0) {
12016 output += " ";
12017 }
12018 output += "HAS_BOUNDS";
12019 numFlags++;
12020 }
12021
12022 if ((privateFlags & DRAWN) == DRAWN) {
12023 if (numFlags > 0) {
12024 output += " ";
12025 }
12026 output += "DRAWN";
12027 // USELESS HERE numFlags++;
12028 }
12029 return output;
12030 }
12031
12032 /**
12033 * <p>Indicates whether or not this view's layout will be requested during
12034 * the next hierarchy layout pass.</p>
12035 *
12036 * @return true if the layout will be forced during next layout pass
12037 */
12038 public boolean isLayoutRequested() {
12039 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
12040 }
12041
12042 /**
12043 * Assign a size and position to a view and all of its
12044 * descendants
12045 *
12046 * <p>This is the second phase of the layout mechanism.
12047 * (The first is measuring). In this phase, each parent calls
12048 * layout on all of its children to position them.
12049 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080012050 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012051 *
Chet Haase9c087442011-01-12 16:20:16 -080012052 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012053 * Derived classes with children should override
12054 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080012055 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012056 *
12057 * @param l Left position, relative to parent
12058 * @param t Top position, relative to parent
12059 * @param r Right position, relative to parent
12060 * @param b Bottom position, relative to parent
12061 */
Romain Guy5429e1d2010-09-07 12:38:00 -070012062 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080012063 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070012064 int oldL = mLeft;
12065 int oldT = mTop;
12066 int oldB = mBottom;
12067 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012068 boolean changed = setFrame(l, t, r, b);
12069 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
12070 if (ViewDebug.TRACE_HIERARCHY) {
12071 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
12072 }
12073
12074 onLayout(changed, l, t, r, b);
12075 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070012076
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012077 ListenerInfo li = mListenerInfo;
12078 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070012079 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012080 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070012081 int numListeners = listenersCopy.size();
12082 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070012083 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070012084 }
12085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012086 }
12087 mPrivateFlags &= ~FORCE_LAYOUT;
12088 }
12089
12090 /**
12091 * Called from layout when this view should
12092 * assign a size and position to each of its children.
12093 *
12094 * Derived classes with children should override
12095 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070012096 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012097 * @param changed This is a new size or position for this view
12098 * @param left Left position, relative to parent
12099 * @param top Top position, relative to parent
12100 * @param right Right position, relative to parent
12101 * @param bottom Bottom position, relative to parent
12102 */
12103 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
12104 }
12105
12106 /**
12107 * Assign a size and position to this view.
12108 *
12109 * This is called from layout.
12110 *
12111 * @param left Left position, relative to parent
12112 * @param top Top position, relative to parent
12113 * @param right Right position, relative to parent
12114 * @param bottom Bottom position, relative to parent
12115 * @return true if the new size and position are different than the
12116 * previous ones
12117 * {@hide}
12118 */
12119 protected boolean setFrame(int left, int top, int right, int bottom) {
12120 boolean changed = false;
12121
12122 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070012123 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012124 + right + "," + bottom + ")");
12125 }
12126
12127 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
12128 changed = true;
12129
12130 // Remember our drawn bit
12131 int drawn = mPrivateFlags & DRAWN;
12132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012133 int oldWidth = mRight - mLeft;
12134 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070012135 int newWidth = right - left;
12136 int newHeight = bottom - top;
12137 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
12138
12139 // Invalidate our old position
12140 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012141
12142 mLeft = left;
12143 mTop = top;
12144 mRight = right;
12145 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080012146 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
12147 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
12148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012149
12150 mPrivateFlags |= HAS_BOUNDS;
12151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012152
Chet Haase75755e22011-07-18 17:48:25 -070012153 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012154 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
12155 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012156 if (mTransformationInfo != null) {
12157 mTransformationInfo.mMatrixDirty = true;
12158 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012160 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
12161 }
12162
12163 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
12164 // If we are visible, force the DRAWN bit to on so that
12165 // this invalidate will go through (at least to our parent).
12166 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012167 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012168 // the DRAWN bit.
12169 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070012170 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080012171 // parent display list may need to be recreated based on a change in the bounds
12172 // of any child
12173 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012174 }
12175
12176 // Reset drawn bit to original value (invalidate turns it off)
12177 mPrivateFlags |= drawn;
12178
12179 mBackgroundSizeChanged = true;
12180 }
12181 return changed;
12182 }
12183
12184 /**
12185 * Finalize inflating a view from XML. This is called as the last phase
12186 * of inflation, after all child views have been added.
12187 *
12188 * <p>Even if the subclass overrides onFinishInflate, they should always be
12189 * sure to call the super method, so that we get called.
12190 */
12191 protected void onFinishInflate() {
12192 }
12193
12194 /**
12195 * Returns the resources associated with this view.
12196 *
12197 * @return Resources object.
12198 */
12199 public Resources getResources() {
12200 return mResources;
12201 }
12202
12203 /**
12204 * Invalidates the specified Drawable.
12205 *
12206 * @param drawable the drawable to invalidate
12207 */
12208 public void invalidateDrawable(Drawable drawable) {
12209 if (verifyDrawable(drawable)) {
12210 final Rect dirty = drawable.getBounds();
12211 final int scrollX = mScrollX;
12212 final int scrollY = mScrollY;
12213
12214 invalidate(dirty.left + scrollX, dirty.top + scrollY,
12215 dirty.right + scrollX, dirty.bottom + scrollY);
12216 }
12217 }
12218
12219 /**
12220 * Schedules an action on a drawable to occur at a specified time.
12221 *
12222 * @param who the recipient of the action
12223 * @param what the action to run on the drawable
12224 * @param when the time at which the action must occur. Uses the
12225 * {@link SystemClock#uptimeMillis} timebase.
12226 */
12227 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080012228 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012229 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080012230 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012231 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
12232 Choreographer.CALLBACK_ANIMATION, what, who,
12233 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080012234 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012235 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080012236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012237 }
12238 }
12239
12240 /**
12241 * Cancels a scheduled action on a drawable.
12242 *
12243 * @param who the recipient of the action
12244 * @param what the action to cancel
12245 */
12246 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080012247 if (verifyDrawable(who) && what != null) {
12248 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012249 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12250 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080012251 } else {
12252 ViewRootImpl.getRunQueue().removeCallbacks(what);
12253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012254 }
12255 }
12256
12257 /**
12258 * Unschedule any events associated with the given Drawable. This can be
12259 * used when selecting a new Drawable into a view, so that the previous
12260 * one is completely unscheduled.
12261 *
12262 * @param who The Drawable to unschedule.
12263 *
12264 * @see #drawableStateChanged
12265 */
12266 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080012267 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012268 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12269 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012270 }
12271 }
12272
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012273 /**
12274 * Return the layout direction of a given Drawable.
12275 *
12276 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012277 */
12278 public int getResolvedLayoutDirection(Drawable who) {
12279 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070012280 }
12281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012282 /**
12283 * If your view subclass is displaying its own Drawable objects, it should
12284 * override this function and return true for any Drawable it is
12285 * displaying. This allows animations for those drawables to be
12286 * scheduled.
12287 *
12288 * <p>Be sure to call through to the super class when overriding this
12289 * function.
12290 *
12291 * @param who The Drawable to verify. Return true if it is one you are
12292 * displaying, else return the result of calling through to the
12293 * super class.
12294 *
12295 * @return boolean If true than the Drawable is being displayed in the
12296 * view; else false and it is not allowed to animate.
12297 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012298 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12299 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012300 */
12301 protected boolean verifyDrawable(Drawable who) {
12302 return who == mBGDrawable;
12303 }
12304
12305 /**
12306 * This function is called whenever the state of the view changes in such
12307 * a way that it impacts the state of drawables being shown.
12308 *
12309 * <p>Be sure to call through to the superclass when overriding this
12310 * function.
12311 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012312 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012313 */
12314 protected void drawableStateChanged() {
12315 Drawable d = mBGDrawable;
12316 if (d != null && d.isStateful()) {
12317 d.setState(getDrawableState());
12318 }
12319 }
12320
12321 /**
12322 * Call this to force a view to update its drawable state. This will cause
12323 * drawableStateChanged to be called on this view. Views that are interested
12324 * in the new state should call getDrawableState.
12325 *
12326 * @see #drawableStateChanged
12327 * @see #getDrawableState
12328 */
12329 public void refreshDrawableState() {
12330 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12331 drawableStateChanged();
12332
12333 ViewParent parent = mParent;
12334 if (parent != null) {
12335 parent.childDrawableStateChanged(this);
12336 }
12337 }
12338
12339 /**
12340 * Return an array of resource IDs of the drawable states representing the
12341 * current state of the view.
12342 *
12343 * @return The current drawable state
12344 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012345 * @see Drawable#setState(int[])
12346 * @see #drawableStateChanged()
12347 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012348 */
12349 public final int[] getDrawableState() {
12350 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12351 return mDrawableState;
12352 } else {
12353 mDrawableState = onCreateDrawableState(0);
12354 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12355 return mDrawableState;
12356 }
12357 }
12358
12359 /**
12360 * Generate the new {@link android.graphics.drawable.Drawable} state for
12361 * this view. This is called by the view
12362 * system when the cached Drawable state is determined to be invalid. To
12363 * retrieve the current state, you should use {@link #getDrawableState}.
12364 *
12365 * @param extraSpace if non-zero, this is the number of extra entries you
12366 * would like in the returned array in which you can place your own
12367 * states.
12368 *
12369 * @return Returns an array holding the current {@link Drawable} state of
12370 * the view.
12371 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012372 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012373 */
12374 protected int[] onCreateDrawableState(int extraSpace) {
12375 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12376 mParent instanceof View) {
12377 return ((View) mParent).onCreateDrawableState(extraSpace);
12378 }
12379
12380 int[] drawableState;
12381
12382 int privateFlags = mPrivateFlags;
12383
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012384 int viewStateIndex = 0;
12385 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12386 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12387 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012388 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012389 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12390 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012391 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12392 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012393 // This is set if HW acceleration is requested, even if the current
12394 // process doesn't allow it. This is just to allow app preview
12395 // windows to better match their app.
12396 viewStateIndex |= VIEW_STATE_ACCELERATED;
12397 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012398 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012399
Christopher Tate3d4bf172011-03-28 16:16:46 -070012400 final int privateFlags2 = mPrivateFlags2;
12401 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12402 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012404 drawableState = VIEW_STATE_SETS[viewStateIndex];
12405
12406 //noinspection ConstantIfStatement
12407 if (false) {
12408 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12409 Log.i("View", toString()
12410 + " pressed=" + ((privateFlags & PRESSED) != 0)
12411 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12412 + " fo=" + hasFocus()
12413 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012414 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012415 + ": " + Arrays.toString(drawableState));
12416 }
12417
12418 if (extraSpace == 0) {
12419 return drawableState;
12420 }
12421
12422 final int[] fullState;
12423 if (drawableState != null) {
12424 fullState = new int[drawableState.length + extraSpace];
12425 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12426 } else {
12427 fullState = new int[extraSpace];
12428 }
12429
12430 return fullState;
12431 }
12432
12433 /**
12434 * Merge your own state values in <var>additionalState</var> into the base
12435 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012436 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012437 *
12438 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012439 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012440 * own additional state values.
12441 *
12442 * @param additionalState The additional state values you would like
12443 * added to <var>baseState</var>; this array is not modified.
12444 *
12445 * @return As a convenience, the <var>baseState</var> array you originally
12446 * passed into the function is returned.
12447 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012448 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012449 */
12450 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12451 final int N = baseState.length;
12452 int i = N - 1;
12453 while (i >= 0 && baseState[i] == 0) {
12454 i--;
12455 }
12456 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12457 return baseState;
12458 }
12459
12460 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012461 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12462 * on all Drawable objects associated with this view.
12463 */
12464 public void jumpDrawablesToCurrentState() {
12465 if (mBGDrawable != null) {
12466 mBGDrawable.jumpToCurrentState();
12467 }
12468 }
12469
12470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012471 * Sets the background color for this view.
12472 * @param color the color of the background
12473 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012474 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012475 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012476 if (mBGDrawable instanceof ColorDrawable) {
12477 ((ColorDrawable) mBGDrawable).setColor(color);
12478 } else {
12479 setBackgroundDrawable(new ColorDrawable(color));
12480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012481 }
12482
12483 /**
12484 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012485 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012486 * @param resid The identifier of the resource.
12487 * @attr ref android.R.styleable#View_background
12488 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012489 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012490 public void setBackgroundResource(int resid) {
12491 if (resid != 0 && resid == mBackgroundResource) {
12492 return;
12493 }
12494
12495 Drawable d= null;
12496 if (resid != 0) {
12497 d = mResources.getDrawable(resid);
12498 }
12499 setBackgroundDrawable(d);
12500
12501 mBackgroundResource = resid;
12502 }
12503
12504 /**
12505 * Set the background to a given Drawable, or remove the background. If the
12506 * background has padding, this View's padding is set to the background's
12507 * padding. However, when a background is removed, this View's padding isn't
12508 * touched. If setting the padding is desired, please use
12509 * {@link #setPadding(int, int, int, int)}.
12510 *
12511 * @param d The Drawable to use as the background, or null to remove the
12512 * background
12513 */
12514 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012515 if (d == mBGDrawable) {
12516 return;
12517 }
12518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012519 boolean requestLayout = false;
12520
12521 mBackgroundResource = 0;
12522
12523 /*
12524 * Regardless of whether we're setting a new background or not, we want
12525 * to clear the previous drawable.
12526 */
12527 if (mBGDrawable != null) {
12528 mBGDrawable.setCallback(null);
12529 unscheduleDrawable(mBGDrawable);
12530 }
12531
12532 if (d != null) {
12533 Rect padding = sThreadLocal.get();
12534 if (padding == null) {
12535 padding = new Rect();
12536 sThreadLocal.set(padding);
12537 }
12538 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012539 switch (d.getResolvedLayoutDirectionSelf()) {
12540 case LAYOUT_DIRECTION_RTL:
12541 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12542 break;
12543 case LAYOUT_DIRECTION_LTR:
12544 default:
12545 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012547 }
12548
12549 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12550 // if it has a different minimum size, we should layout again
12551 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12552 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12553 requestLayout = true;
12554 }
12555
12556 d.setCallback(this);
12557 if (d.isStateful()) {
12558 d.setState(getDrawableState());
12559 }
12560 d.setVisible(getVisibility() == VISIBLE, false);
12561 mBGDrawable = d;
12562
12563 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12564 mPrivateFlags &= ~SKIP_DRAW;
12565 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12566 requestLayout = true;
12567 }
12568 } else {
12569 /* Remove the background */
12570 mBGDrawable = null;
12571
12572 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12573 /*
12574 * This view ONLY drew the background before and we're removing
12575 * the background, so now it won't draw anything
12576 * (hence we SKIP_DRAW)
12577 */
12578 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12579 mPrivateFlags |= SKIP_DRAW;
12580 }
12581
12582 /*
12583 * When the background is set, we try to apply its padding to this
12584 * View. When the background is removed, we don't touch this View's
12585 * padding. This is noted in the Javadocs. Hence, we don't need to
12586 * requestLayout(), the invalidate() below is sufficient.
12587 */
12588
12589 // The old background's minimum size could have affected this
12590 // View's layout, so let's requestLayout
12591 requestLayout = true;
12592 }
12593
Romain Guy8f1344f52009-05-15 16:03:59 -070012594 computeOpaqueFlags();
12595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012596 if (requestLayout) {
12597 requestLayout();
12598 }
12599
12600 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012601 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012602 }
12603
12604 /**
12605 * Gets the background drawable
12606 * @return The drawable used as the background for this view, if any.
12607 */
12608 public Drawable getBackground() {
12609 return mBGDrawable;
12610 }
12611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012612 /**
12613 * Sets the padding. The view may add on the space required to display
12614 * the scrollbars, depending on the style and visibility of the scrollbars.
12615 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12616 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12617 * from the values set in this call.
12618 *
12619 * @attr ref android.R.styleable#View_padding
12620 * @attr ref android.R.styleable#View_paddingBottom
12621 * @attr ref android.R.styleable#View_paddingLeft
12622 * @attr ref android.R.styleable#View_paddingRight
12623 * @attr ref android.R.styleable#View_paddingTop
12624 * @param left the left padding in pixels
12625 * @param top the top padding in pixels
12626 * @param right the right padding in pixels
12627 * @param bottom the bottom padding in pixels
12628 */
12629 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012630 mUserPaddingStart = -1;
12631 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012632 mUserPaddingRelative = false;
12633
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012634 internalSetPadding(left, top, right, bottom);
12635 }
12636
12637 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080012638 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012639 mUserPaddingRight = right;
12640 mUserPaddingBottom = bottom;
12641
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012642 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012643 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070012644
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012645 // Common case is there are no scroll bars.
12646 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012647 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012648 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012649 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012650 switch (mVerticalScrollbarPosition) {
12651 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012652 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12653 left += offset;
12654 } else {
12655 right += offset;
12656 }
12657 break;
Adam Powell20232d02010-12-08 21:08:53 -080012658 case SCROLLBAR_POSITION_RIGHT:
12659 right += offset;
12660 break;
12661 case SCROLLBAR_POSITION_LEFT:
12662 left += offset;
12663 break;
12664 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012665 }
Adam Powell20232d02010-12-08 21:08:53 -080012666 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012667 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12668 ? 0 : getHorizontalScrollbarHeight();
12669 }
12670 }
Romain Guy8506ab42009-06-11 17:35:47 -070012671
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012672 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012673 changed = true;
12674 mPaddingLeft = left;
12675 }
12676 if (mPaddingTop != top) {
12677 changed = true;
12678 mPaddingTop = top;
12679 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012680 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012681 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012682 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012683 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012684 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012685 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012686 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012687 }
12688
12689 if (changed) {
12690 requestLayout();
12691 }
12692 }
12693
12694 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012695 * Sets the relative padding. The view may add on the space required to display
12696 * the scrollbars, depending on the style and visibility of the scrollbars.
12697 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12698 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12699 * from the values set in this call.
12700 *
12701 * @attr ref android.R.styleable#View_padding
12702 * @attr ref android.R.styleable#View_paddingBottom
12703 * @attr ref android.R.styleable#View_paddingStart
12704 * @attr ref android.R.styleable#View_paddingEnd
12705 * @attr ref android.R.styleable#View_paddingTop
12706 * @param start the start padding in pixels
12707 * @param top the top padding in pixels
12708 * @param end the end padding in pixels
12709 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012710 */
12711 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012712 mUserPaddingStart = start;
12713 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012714 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012715
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012716 switch(getResolvedLayoutDirection()) {
12717 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012718 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012719 break;
12720 case LAYOUT_DIRECTION_LTR:
12721 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012722 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012723 }
12724 }
12725
12726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012727 * Returns the top padding of this view.
12728 *
12729 * @return the top padding in pixels
12730 */
12731 public int getPaddingTop() {
12732 return mPaddingTop;
12733 }
12734
12735 /**
12736 * Returns the bottom padding of this view. If there are inset and enabled
12737 * scrollbars, this value may include the space required to display the
12738 * scrollbars as well.
12739 *
12740 * @return the bottom padding in pixels
12741 */
12742 public int getPaddingBottom() {
12743 return mPaddingBottom;
12744 }
12745
12746 /**
12747 * Returns the left padding of this view. If there are inset and enabled
12748 * scrollbars, this value may include the space required to display the
12749 * scrollbars as well.
12750 *
12751 * @return the left padding in pixels
12752 */
12753 public int getPaddingLeft() {
12754 return mPaddingLeft;
12755 }
12756
12757 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070012758 * Returns the start padding of this view depending on its resolved layout direction.
12759 * If there are inset and enabled scrollbars, this value may include the space
12760 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012761 *
12762 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012763 */
12764 public int getPaddingStart() {
12765 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12766 mPaddingRight : mPaddingLeft;
12767 }
12768
12769 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012770 * Returns the right padding of this view. If there are inset and enabled
12771 * scrollbars, this value may include the space required to display the
12772 * scrollbars as well.
12773 *
12774 * @return the right padding in pixels
12775 */
12776 public int getPaddingRight() {
12777 return mPaddingRight;
12778 }
12779
12780 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070012781 * Returns the end padding of this view depending on its resolved layout direction.
12782 * If there are inset and enabled scrollbars, this value may include the space
12783 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012784 *
12785 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012786 */
12787 public int getPaddingEnd() {
12788 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12789 mPaddingLeft : mPaddingRight;
12790 }
12791
12792 /**
12793 * Return if the padding as been set thru relative values
12794 * {@link #setPaddingRelative(int, int, int, int)} or thru
12795 * @attr ref android.R.styleable#View_paddingStart or
12796 * @attr ref android.R.styleable#View_paddingEnd
12797 *
12798 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012799 */
12800 public boolean isPaddingRelative() {
12801 return mUserPaddingRelative;
12802 }
12803
12804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012805 * Changes the selection state of this view. A view can be selected or not.
12806 * Note that selection is not the same as focus. Views are typically
12807 * selected in the context of an AdapterView like ListView or GridView;
12808 * the selected view is the view that is highlighted.
12809 *
12810 * @param selected true if the view must be selected, false otherwise
12811 */
12812 public void setSelected(boolean selected) {
12813 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12814 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012815 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012816 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012817 refreshDrawableState();
12818 dispatchSetSelected(selected);
12819 }
12820 }
12821
12822 /**
12823 * Dispatch setSelected to all of this View's children.
12824 *
12825 * @see #setSelected(boolean)
12826 *
12827 * @param selected The new selected state
12828 */
12829 protected void dispatchSetSelected(boolean selected) {
12830 }
12831
12832 /**
12833 * Indicates the selection state of this view.
12834 *
12835 * @return true if the view is selected, false otherwise
12836 */
12837 @ViewDebug.ExportedProperty
12838 public boolean isSelected() {
12839 return (mPrivateFlags & SELECTED) != 0;
12840 }
12841
12842 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012843 * Changes the activated state of this view. A view can be activated or not.
12844 * Note that activation is not the same as selection. Selection is
12845 * a transient property, representing the view (hierarchy) the user is
12846 * currently interacting with. Activation is a longer-term state that the
12847 * user can move views in and out of. For example, in a list view with
12848 * single or multiple selection enabled, the views in the current selection
12849 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12850 * here.) The activated state is propagated down to children of the view it
12851 * is set on.
12852 *
12853 * @param activated true if the view must be activated, false otherwise
12854 */
12855 public void setActivated(boolean activated) {
12856 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12857 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012858 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012859 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012860 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012861 }
12862 }
12863
12864 /**
12865 * Dispatch setActivated to all of this View's children.
12866 *
12867 * @see #setActivated(boolean)
12868 *
12869 * @param activated The new activated state
12870 */
12871 protected void dispatchSetActivated(boolean activated) {
12872 }
12873
12874 /**
12875 * Indicates the activation state of this view.
12876 *
12877 * @return true if the view is activated, false otherwise
12878 */
12879 @ViewDebug.ExportedProperty
12880 public boolean isActivated() {
12881 return (mPrivateFlags & ACTIVATED) != 0;
12882 }
12883
12884 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012885 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12886 * observer can be used to get notifications when global events, like
12887 * layout, happen.
12888 *
12889 * The returned ViewTreeObserver observer is not guaranteed to remain
12890 * valid for the lifetime of this View. If the caller of this method keeps
12891 * a long-lived reference to ViewTreeObserver, it should always check for
12892 * the return value of {@link ViewTreeObserver#isAlive()}.
12893 *
12894 * @return The ViewTreeObserver for this view's hierarchy.
12895 */
12896 public ViewTreeObserver getViewTreeObserver() {
12897 if (mAttachInfo != null) {
12898 return mAttachInfo.mTreeObserver;
12899 }
12900 if (mFloatingTreeObserver == null) {
12901 mFloatingTreeObserver = new ViewTreeObserver();
12902 }
12903 return mFloatingTreeObserver;
12904 }
12905
12906 /**
12907 * <p>Finds the topmost view in the current view hierarchy.</p>
12908 *
12909 * @return the topmost view containing this view
12910 */
12911 public View getRootView() {
12912 if (mAttachInfo != null) {
12913 final View v = mAttachInfo.mRootView;
12914 if (v != null) {
12915 return v;
12916 }
12917 }
Romain Guy8506ab42009-06-11 17:35:47 -070012918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012919 View parent = this;
12920
12921 while (parent.mParent != null && parent.mParent instanceof View) {
12922 parent = (View) parent.mParent;
12923 }
12924
12925 return parent;
12926 }
12927
12928 /**
12929 * <p>Computes the coordinates of this view on the screen. The argument
12930 * must be an array of two integers. After the method returns, the array
12931 * contains the x and y location in that order.</p>
12932 *
12933 * @param location an array of two integers in which to hold the coordinates
12934 */
12935 public void getLocationOnScreen(int[] location) {
12936 getLocationInWindow(location);
12937
12938 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012939 if (info != null) {
12940 location[0] += info.mWindowLeft;
12941 location[1] += info.mWindowTop;
12942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012943 }
12944
12945 /**
12946 * <p>Computes the coordinates of this view in its window. The argument
12947 * must be an array of two integers. After the method returns, the array
12948 * contains the x and y location in that order.</p>
12949 *
12950 * @param location an array of two integers in which to hold the coordinates
12951 */
12952 public void getLocationInWindow(int[] location) {
12953 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012954 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012955 }
12956
Gilles Debunne6583ce52011-12-06 18:09:02 -080012957 if (mAttachInfo == null) {
12958 // When the view is not attached to a window, this method does not make sense
12959 location[0] = location[1] = 0;
12960 return;
12961 }
12962
Gilles Debunnecea45132011-11-24 02:19:27 +010012963 float[] position = mAttachInfo.mTmpTransformLocation;
12964 position[0] = position[1] = 0.0f;
12965
12966 if (!hasIdentityMatrix()) {
12967 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012969
Gilles Debunnecea45132011-11-24 02:19:27 +010012970 position[0] += mLeft;
12971 position[1] += mTop;
12972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012973 ViewParent viewParent = mParent;
12974 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012975 final View view = (View) viewParent;
12976
12977 position[0] -= view.mScrollX;
12978 position[1] -= view.mScrollY;
12979
12980 if (!view.hasIdentityMatrix()) {
12981 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012982 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012983
12984 position[0] += view.mLeft;
12985 position[1] += view.mTop;
12986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012987 viewParent = view.mParent;
12988 }
Romain Guy8506ab42009-06-11 17:35:47 -070012989
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012990 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012991 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012992 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12993 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012994 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012995
12996 location[0] = (int) (position[0] + 0.5f);
12997 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012998 }
12999
13000 /**
13001 * {@hide}
13002 * @param id the id of the view to be found
13003 * @return the view of the specified id, null if cannot be found
13004 */
13005 protected View findViewTraversal(int id) {
13006 if (id == mID) {
13007 return this;
13008 }
13009 return null;
13010 }
13011
13012 /**
13013 * {@hide}
13014 * @param tag the tag of the view to be found
13015 * @return the view of specified tag, null if cannot be found
13016 */
13017 protected View findViewWithTagTraversal(Object tag) {
13018 if (tag != null && tag.equals(mTag)) {
13019 return this;
13020 }
13021 return null;
13022 }
13023
13024 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013025 * {@hide}
13026 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070013027 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080013028 * @return The first view that matches the predicate or null.
13029 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070013030 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080013031 if (predicate.apply(this)) {
13032 return this;
13033 }
13034 return null;
13035 }
13036
13037 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013038 * Look for a child view with the given id. If this view has the given
13039 * id, return this view.
13040 *
13041 * @param id The id to search for.
13042 * @return The view that has the given id in the hierarchy or null
13043 */
13044 public final View findViewById(int id) {
13045 if (id < 0) {
13046 return null;
13047 }
13048 return findViewTraversal(id);
13049 }
13050
13051 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070013052 * Finds a view by its unuque and stable accessibility id.
13053 *
13054 * @param accessibilityId The searched accessibility id.
13055 * @return The found view.
13056 */
13057 final View findViewByAccessibilityId(int accessibilityId) {
13058 if (accessibilityId < 0) {
13059 return null;
13060 }
13061 return findViewByAccessibilityIdTraversal(accessibilityId);
13062 }
13063
13064 /**
13065 * Performs the traversal to find a view by its unuque and stable accessibility id.
13066 *
13067 * <strong>Note:</strong>This method does not stop at the root namespace
13068 * boundary since the user can touch the screen at an arbitrary location
13069 * potentially crossing the root namespace bounday which will send an
13070 * accessibility event to accessibility services and they should be able
13071 * to obtain the event source. Also accessibility ids are guaranteed to be
13072 * unique in the window.
13073 *
13074 * @param accessibilityId The accessibility id.
13075 * @return The found view.
13076 */
13077 View findViewByAccessibilityIdTraversal(int accessibilityId) {
13078 if (getAccessibilityViewId() == accessibilityId) {
13079 return this;
13080 }
13081 return null;
13082 }
13083
13084 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013085 * Look for a child view with the given tag. If this view has the given
13086 * tag, return this view.
13087 *
13088 * @param tag The tag to search for, using "tag.equals(getTag())".
13089 * @return The View that has the given tag in the hierarchy or null
13090 */
13091 public final View findViewWithTag(Object tag) {
13092 if (tag == null) {
13093 return null;
13094 }
13095 return findViewWithTagTraversal(tag);
13096 }
13097
13098 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013099 * {@hide}
13100 * Look for a child view that matches the specified predicate.
13101 * If this view matches the predicate, return this view.
13102 *
13103 * @param predicate The predicate to evaluate.
13104 * @return The first view that matches the predicate or null.
13105 */
13106 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070013107 return findViewByPredicateTraversal(predicate, null);
13108 }
13109
13110 /**
13111 * {@hide}
13112 * Look for a child view that matches the specified predicate,
13113 * starting with the specified view and its descendents and then
13114 * recusively searching the ancestors and siblings of that view
13115 * until this view is reached.
13116 *
13117 * This method is useful in cases where the predicate does not match
13118 * a single unique view (perhaps multiple views use the same id)
13119 * and we are trying to find the view that is "closest" in scope to the
13120 * starting view.
13121 *
13122 * @param start The view to start from.
13123 * @param predicate The predicate to evaluate.
13124 * @return The first view that matches the predicate or null.
13125 */
13126 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
13127 View childToSkip = null;
13128 for (;;) {
13129 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
13130 if (view != null || start == this) {
13131 return view;
13132 }
13133
13134 ViewParent parent = start.getParent();
13135 if (parent == null || !(parent instanceof View)) {
13136 return null;
13137 }
13138
13139 childToSkip = start;
13140 start = (View) parent;
13141 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080013142 }
13143
13144 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013145 * Sets the identifier for this view. The identifier does not have to be
13146 * unique in this view's hierarchy. The identifier should be a positive
13147 * number.
13148 *
13149 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070013150 * @see #getId()
13151 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013152 *
13153 * @param id a number used to identify the view
13154 *
13155 * @attr ref android.R.styleable#View_id
13156 */
13157 public void setId(int id) {
13158 mID = id;
13159 }
13160
13161 /**
13162 * {@hide}
13163 *
13164 * @param isRoot true if the view belongs to the root namespace, false
13165 * otherwise
13166 */
13167 public void setIsRootNamespace(boolean isRoot) {
13168 if (isRoot) {
13169 mPrivateFlags |= IS_ROOT_NAMESPACE;
13170 } else {
13171 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
13172 }
13173 }
13174
13175 /**
13176 * {@hide}
13177 *
13178 * @return true if the view belongs to the root namespace, false otherwise
13179 */
13180 public boolean isRootNamespace() {
13181 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
13182 }
13183
13184 /**
13185 * Returns this view's identifier.
13186 *
13187 * @return a positive integer used to identify the view or {@link #NO_ID}
13188 * if the view has no ID
13189 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013190 * @see #setId(int)
13191 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013192 * @attr ref android.R.styleable#View_id
13193 */
13194 @ViewDebug.CapturedViewProperty
13195 public int getId() {
13196 return mID;
13197 }
13198
13199 /**
13200 * Returns this view's tag.
13201 *
13202 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070013203 *
13204 * @see #setTag(Object)
13205 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013206 */
13207 @ViewDebug.ExportedProperty
13208 public Object getTag() {
13209 return mTag;
13210 }
13211
13212 /**
13213 * Sets the tag associated with this view. A tag can be used to mark
13214 * a view in its hierarchy and does not have to be unique within the
13215 * hierarchy. Tags can also be used to store data within a view without
13216 * resorting to another data structure.
13217 *
13218 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070013219 *
13220 * @see #getTag()
13221 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013222 */
13223 public void setTag(final Object tag) {
13224 mTag = tag;
13225 }
13226
13227 /**
Romain Guyd90a3312009-05-06 14:54:28 -070013228 * Returns the tag associated with this view and the specified key.
13229 *
13230 * @param key The key identifying the tag
13231 *
13232 * @return the Object stored in this view as a tag
13233 *
13234 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070013235 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070013236 */
13237 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013238 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070013239 return null;
13240 }
13241
13242 /**
13243 * Sets a tag associated with this view and a key. A tag can be used
13244 * to mark a view in its hierarchy and does not have to be unique within
13245 * the hierarchy. Tags can also be used to store data within a view
13246 * without resorting to another data structure.
13247 *
13248 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070013249 * application to ensure it is unique (see the <a
13250 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
13251 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070013252 * the Android framework or not associated with any package will cause
13253 * an {@link IllegalArgumentException} to be thrown.
13254 *
13255 * @param key The key identifying the tag
13256 * @param tag An Object to tag the view with
13257 *
13258 * @throws IllegalArgumentException If they specified key is not valid
13259 *
13260 * @see #setTag(Object)
13261 * @see #getTag(int)
13262 */
13263 public void setTag(int key, final Object tag) {
13264 // If the package id is 0x00 or 0x01, it's either an undefined package
13265 // or a framework id
13266 if ((key >>> 24) < 2) {
13267 throw new IllegalArgumentException("The key must be an application-specific "
13268 + "resource id.");
13269 }
13270
Adam Powell2b2f6d62011-09-23 11:15:39 -070013271 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013272 }
13273
13274 /**
13275 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
13276 * framework id.
13277 *
13278 * @hide
13279 */
13280 public void setTagInternal(int key, Object tag) {
13281 if ((key >>> 24) != 0x1) {
13282 throw new IllegalArgumentException("The key must be a framework-specific "
13283 + "resource id.");
13284 }
13285
Adam Powell2b2f6d62011-09-23 11:15:39 -070013286 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013287 }
13288
Adam Powell2b2f6d62011-09-23 11:15:39 -070013289 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013290 if (mKeyedTags == null) {
13291 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013292 }
13293
Adam Powell7db82ac2011-09-22 19:44:04 -070013294 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013295 }
13296
13297 /**
Romain Guy13922e02009-05-12 17:56:14 -070013298 * @param consistency The type of consistency. See ViewDebug for more information.
13299 *
13300 * @hide
13301 */
13302 protected boolean dispatchConsistencyCheck(int consistency) {
13303 return onConsistencyCheck(consistency);
13304 }
13305
13306 /**
13307 * Method that subclasses should implement to check their consistency. The type of
13308 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013309 *
Romain Guy13922e02009-05-12 17:56:14 -070013310 * @param consistency The type of consistency. See ViewDebug for more information.
13311 *
13312 * @throws IllegalStateException if the view is in an inconsistent state.
13313 *
13314 * @hide
13315 */
13316 protected boolean onConsistencyCheck(int consistency) {
13317 boolean result = true;
13318
13319 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13320 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13321
13322 if (checkLayout) {
13323 if (getParent() == null) {
13324 result = false;
13325 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13326 "View " + this + " does not have a parent.");
13327 }
13328
13329 if (mAttachInfo == null) {
13330 result = false;
13331 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13332 "View " + this + " is not attached to a window.");
13333 }
13334 }
13335
13336 if (checkDrawing) {
13337 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13338 // from their draw() method
13339
13340 if ((mPrivateFlags & DRAWN) != DRAWN &&
13341 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13342 result = false;
13343 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13344 "View " + this + " was invalidated but its drawing cache is valid.");
13345 }
13346 }
13347
13348 return result;
13349 }
13350
13351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013352 * Prints information about this view in the log output, with the tag
13353 * {@link #VIEW_LOG_TAG}.
13354 *
13355 * @hide
13356 */
13357 public void debug() {
13358 debug(0);
13359 }
13360
13361 /**
13362 * Prints information about this view in the log output, with the tag
13363 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13364 * indentation defined by the <code>depth</code>.
13365 *
13366 * @param depth the indentation level
13367 *
13368 * @hide
13369 */
13370 protected void debug(int depth) {
13371 String output = debugIndent(depth - 1);
13372
13373 output += "+ " + this;
13374 int id = getId();
13375 if (id != -1) {
13376 output += " (id=" + id + ")";
13377 }
13378 Object tag = getTag();
13379 if (tag != null) {
13380 output += " (tag=" + tag + ")";
13381 }
13382 Log.d(VIEW_LOG_TAG, output);
13383
13384 if ((mPrivateFlags & FOCUSED) != 0) {
13385 output = debugIndent(depth) + " FOCUSED";
13386 Log.d(VIEW_LOG_TAG, output);
13387 }
13388
13389 output = debugIndent(depth);
13390 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13391 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13392 + "} ";
13393 Log.d(VIEW_LOG_TAG, output);
13394
13395 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13396 || mPaddingBottom != 0) {
13397 output = debugIndent(depth);
13398 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13399 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13400 Log.d(VIEW_LOG_TAG, output);
13401 }
13402
13403 output = debugIndent(depth);
13404 output += "mMeasureWidth=" + mMeasuredWidth +
13405 " mMeasureHeight=" + mMeasuredHeight;
13406 Log.d(VIEW_LOG_TAG, output);
13407
13408 output = debugIndent(depth);
13409 if (mLayoutParams == null) {
13410 output += "BAD! no layout params";
13411 } else {
13412 output = mLayoutParams.debug(output);
13413 }
13414 Log.d(VIEW_LOG_TAG, output);
13415
13416 output = debugIndent(depth);
13417 output += "flags={";
13418 output += View.printFlags(mViewFlags);
13419 output += "}";
13420 Log.d(VIEW_LOG_TAG, output);
13421
13422 output = debugIndent(depth);
13423 output += "privateFlags={";
13424 output += View.printPrivateFlags(mPrivateFlags);
13425 output += "}";
13426 Log.d(VIEW_LOG_TAG, output);
13427 }
13428
13429 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013430 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013431 *
13432 * @param depth the indentation level
13433 * @return a String containing (depth * 2 + 3) * 2 white spaces
13434 *
13435 * @hide
13436 */
13437 protected static String debugIndent(int depth) {
13438 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13439 for (int i = 0; i < (depth * 2) + 3; i++) {
13440 spaces.append(' ').append(' ');
13441 }
13442 return spaces.toString();
13443 }
13444
13445 /**
13446 * <p>Return the offset of the widget's text baseline from the widget's top
13447 * boundary. If this widget does not support baseline alignment, this
13448 * method returns -1. </p>
13449 *
13450 * @return the offset of the baseline within the widget's bounds or -1
13451 * if baseline alignment is not supported
13452 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013453 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013454 public int getBaseline() {
13455 return -1;
13456 }
13457
13458 /**
13459 * Call this when something has changed which has invalidated the
13460 * layout of this view. This will schedule a layout pass of the view
13461 * tree.
13462 */
13463 public void requestLayout() {
13464 if (ViewDebug.TRACE_HIERARCHY) {
13465 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13466 }
13467
13468 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013469 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013470
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013471 if (mParent != null) {
13472 if (mLayoutParams != null) {
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080013473 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013474 }
13475 if (!mParent.isLayoutRequested()) {
13476 mParent.requestLayout();
13477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013478 }
13479 }
13480
13481 /**
13482 * Forces this view to be laid out during the next layout pass.
13483 * This method does not call requestLayout() or forceLayout()
13484 * on the parent.
13485 */
13486 public void forceLayout() {
13487 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013488 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013489 }
13490
13491 /**
13492 * <p>
13493 * This is called to find out how big a view should be. The parent
13494 * supplies constraint information in the width and height parameters.
13495 * </p>
13496 *
13497 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013498 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013499 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013500 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013501 * </p>
13502 *
13503 *
13504 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13505 * parent
13506 * @param heightMeasureSpec Vertical space requirements as imposed by the
13507 * parent
13508 *
13509 * @see #onMeasure(int, int)
13510 */
13511 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13512 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13513 widthMeasureSpec != mOldWidthMeasureSpec ||
13514 heightMeasureSpec != mOldHeightMeasureSpec) {
13515
13516 // first clears the measured dimension flag
13517 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13518
13519 if (ViewDebug.TRACE_HIERARCHY) {
13520 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13521 }
13522
13523 // measure ourselves, this should set the measured dimension flag back
13524 onMeasure(widthMeasureSpec, heightMeasureSpec);
13525
13526 // flag not set, setMeasuredDimension() was not invoked, we raise
13527 // an exception to warn the developer
13528 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13529 throw new IllegalStateException("onMeasure() did not set the"
13530 + " measured dimension by calling"
13531 + " setMeasuredDimension()");
13532 }
13533
13534 mPrivateFlags |= LAYOUT_REQUIRED;
13535 }
13536
13537 mOldWidthMeasureSpec = widthMeasureSpec;
13538 mOldHeightMeasureSpec = heightMeasureSpec;
13539 }
13540
13541 /**
13542 * <p>
13543 * Measure the view and its content to determine the measured width and the
13544 * measured height. This method is invoked by {@link #measure(int, int)} and
13545 * should be overriden by subclasses to provide accurate and efficient
13546 * measurement of their contents.
13547 * </p>
13548 *
13549 * <p>
13550 * <strong>CONTRACT:</strong> When overriding this method, you
13551 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13552 * measured width and height of this view. Failure to do so will trigger an
13553 * <code>IllegalStateException</code>, thrown by
13554 * {@link #measure(int, int)}. Calling the superclass'
13555 * {@link #onMeasure(int, int)} is a valid use.
13556 * </p>
13557 *
13558 * <p>
13559 * The base class implementation of measure defaults to the background size,
13560 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13561 * override {@link #onMeasure(int, int)} to provide better measurements of
13562 * their content.
13563 * </p>
13564 *
13565 * <p>
13566 * If this method is overridden, it is the subclass's responsibility to make
13567 * sure the measured height and width are at least the view's minimum height
13568 * and width ({@link #getSuggestedMinimumHeight()} and
13569 * {@link #getSuggestedMinimumWidth()}).
13570 * </p>
13571 *
13572 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13573 * The requirements are encoded with
13574 * {@link android.view.View.MeasureSpec}.
13575 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13576 * The requirements are encoded with
13577 * {@link android.view.View.MeasureSpec}.
13578 *
13579 * @see #getMeasuredWidth()
13580 * @see #getMeasuredHeight()
13581 * @see #setMeasuredDimension(int, int)
13582 * @see #getSuggestedMinimumHeight()
13583 * @see #getSuggestedMinimumWidth()
13584 * @see android.view.View.MeasureSpec#getMode(int)
13585 * @see android.view.View.MeasureSpec#getSize(int)
13586 */
13587 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13588 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13589 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13590 }
13591
13592 /**
13593 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13594 * measured width and measured height. Failing to do so will trigger an
13595 * exception at measurement time.</p>
13596 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013597 * @param measuredWidth The measured width of this view. May be a complex
13598 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13599 * {@link #MEASURED_STATE_TOO_SMALL}.
13600 * @param measuredHeight The measured height of this view. May be a complex
13601 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13602 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603 */
13604 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13605 mMeasuredWidth = measuredWidth;
13606 mMeasuredHeight = measuredHeight;
13607
13608 mPrivateFlags |= MEASURED_DIMENSION_SET;
13609 }
13610
13611 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013612 * Merge two states as returned by {@link #getMeasuredState()}.
13613 * @param curState The current state as returned from a view or the result
13614 * of combining multiple views.
13615 * @param newState The new view state to combine.
13616 * @return Returns a new integer reflecting the combination of the two
13617 * states.
13618 */
13619 public static int combineMeasuredStates(int curState, int newState) {
13620 return curState | newState;
13621 }
13622
13623 /**
13624 * Version of {@link #resolveSizeAndState(int, int, int)}
13625 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13626 */
13627 public static int resolveSize(int size, int measureSpec) {
13628 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13629 }
13630
13631 /**
13632 * Utility to reconcile a desired size and state, with constraints imposed
13633 * by a MeasureSpec. Will take the desired size, unless a different size
13634 * is imposed by the constraints. The returned value is a compound integer,
13635 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13636 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13637 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013638 *
13639 * @param size How big the view wants to be
13640 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013641 * @return Size information bit mask as defined by
13642 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013643 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013644 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013645 int result = size;
13646 int specMode = MeasureSpec.getMode(measureSpec);
13647 int specSize = MeasureSpec.getSize(measureSpec);
13648 switch (specMode) {
13649 case MeasureSpec.UNSPECIFIED:
13650 result = size;
13651 break;
13652 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013653 if (specSize < size) {
13654 result = specSize | MEASURED_STATE_TOO_SMALL;
13655 } else {
13656 result = size;
13657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013658 break;
13659 case MeasureSpec.EXACTLY:
13660 result = specSize;
13661 break;
13662 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013663 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013664 }
13665
13666 /**
13667 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013668 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013669 * by the MeasureSpec.
13670 *
13671 * @param size Default size for this view
13672 * @param measureSpec Constraints imposed by the parent
13673 * @return The size this view should be.
13674 */
13675 public static int getDefaultSize(int size, int measureSpec) {
13676 int result = size;
13677 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013678 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013679
13680 switch (specMode) {
13681 case MeasureSpec.UNSPECIFIED:
13682 result = size;
13683 break;
13684 case MeasureSpec.AT_MOST:
13685 case MeasureSpec.EXACTLY:
13686 result = specSize;
13687 break;
13688 }
13689 return result;
13690 }
13691
13692 /**
13693 * Returns the suggested minimum height that the view should use. This
13694 * returns the maximum of the view's minimum height
13695 * and the background's minimum height
13696 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13697 * <p>
13698 * When being used in {@link #onMeasure(int, int)}, the caller should still
13699 * ensure the returned height is within the requirements of the parent.
13700 *
13701 * @return The suggested minimum height of the view.
13702 */
13703 protected int getSuggestedMinimumHeight() {
13704 int suggestedMinHeight = mMinHeight;
13705
13706 if (mBGDrawable != null) {
13707 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13708 if (suggestedMinHeight < bgMinHeight) {
13709 suggestedMinHeight = bgMinHeight;
13710 }
13711 }
13712
13713 return suggestedMinHeight;
13714 }
13715
13716 /**
13717 * Returns the suggested minimum width that the view should use. This
13718 * returns the maximum of the view's minimum width)
13719 * and the background's minimum width
13720 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13721 * <p>
13722 * When being used in {@link #onMeasure(int, int)}, the caller should still
13723 * ensure the returned width is within the requirements of the parent.
13724 *
13725 * @return The suggested minimum width of the view.
13726 */
13727 protected int getSuggestedMinimumWidth() {
13728 int suggestedMinWidth = mMinWidth;
13729
13730 if (mBGDrawable != null) {
13731 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13732 if (suggestedMinWidth < bgMinWidth) {
13733 suggestedMinWidth = bgMinWidth;
13734 }
13735 }
13736
13737 return suggestedMinWidth;
13738 }
13739
13740 /**
13741 * Sets the minimum height of the view. It is not guaranteed the view will
13742 * be able to achieve this minimum height (for example, if its parent layout
13743 * constrains it with less available height).
13744 *
13745 * @param minHeight The minimum height the view will try to be.
13746 */
13747 public void setMinimumHeight(int minHeight) {
13748 mMinHeight = minHeight;
13749 }
13750
13751 /**
13752 * Sets the minimum width of the view. It is not guaranteed the view will
13753 * be able to achieve this minimum width (for example, if its parent layout
13754 * constrains it with less available width).
13755 *
13756 * @param minWidth The minimum width the view will try to be.
13757 */
13758 public void setMinimumWidth(int minWidth) {
13759 mMinWidth = minWidth;
13760 }
13761
13762 /**
13763 * Get the animation currently associated with this view.
13764 *
13765 * @return The animation that is currently playing or
13766 * scheduled to play for this view.
13767 */
13768 public Animation getAnimation() {
13769 return mCurrentAnimation;
13770 }
13771
13772 /**
13773 * Start the specified animation now.
13774 *
13775 * @param animation the animation to start now
13776 */
13777 public void startAnimation(Animation animation) {
13778 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13779 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013780 invalidateParentCaches();
13781 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013782 }
13783
13784 /**
13785 * Cancels any animations for this view.
13786 */
13787 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013788 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013789 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013791 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013792 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013793 }
13794
13795 /**
13796 * Sets the next animation to play for this view.
13797 * If you want the animation to play immediately, use
13798 * startAnimation. This method provides allows fine-grained
13799 * control over the start time and invalidation, but you
13800 * must make sure that 1) the animation has a start time set, and
13801 * 2) the view will be invalidated when the animation is supposed to
13802 * start.
13803 *
13804 * @param animation The next animation, or null.
13805 */
13806 public void setAnimation(Animation animation) {
13807 mCurrentAnimation = animation;
13808 if (animation != null) {
13809 animation.reset();
13810 }
13811 }
13812
13813 /**
13814 * Invoked by a parent ViewGroup to notify the start of the animation
13815 * currently associated with this view. If you override this method,
13816 * always call super.onAnimationStart();
13817 *
13818 * @see #setAnimation(android.view.animation.Animation)
13819 * @see #getAnimation()
13820 */
13821 protected void onAnimationStart() {
13822 mPrivateFlags |= ANIMATION_STARTED;
13823 }
13824
13825 /**
13826 * Invoked by a parent ViewGroup to notify the end of the animation
13827 * currently associated with this view. If you override this method,
13828 * always call super.onAnimationEnd();
13829 *
13830 * @see #setAnimation(android.view.animation.Animation)
13831 * @see #getAnimation()
13832 */
13833 protected void onAnimationEnd() {
13834 mPrivateFlags &= ~ANIMATION_STARTED;
13835 }
13836
13837 /**
13838 * Invoked if there is a Transform that involves alpha. Subclass that can
13839 * draw themselves with the specified alpha should return true, and then
13840 * respect that alpha when their onDraw() is called. If this returns false
13841 * then the view may be redirected to draw into an offscreen buffer to
13842 * fulfill the request, which will look fine, but may be slower than if the
13843 * subclass handles it internally. The default implementation returns false.
13844 *
13845 * @param alpha The alpha (0..255) to apply to the view's drawing
13846 * @return true if the view can draw with the specified alpha.
13847 */
13848 protected boolean onSetAlpha(int alpha) {
13849 return false;
13850 }
13851
13852 /**
13853 * This is used by the RootView to perform an optimization when
13854 * the view hierarchy contains one or several SurfaceView.
13855 * SurfaceView is always considered transparent, but its children are not,
13856 * therefore all View objects remove themselves from the global transparent
13857 * region (passed as a parameter to this function).
13858 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013859 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013860 *
13861 * @return Returns true if the effective visibility of the view at this
13862 * point is opaque, regardless of the transparent region; returns false
13863 * if it is possible for underlying windows to be seen behind the view.
13864 *
13865 * {@hide}
13866 */
13867 public boolean gatherTransparentRegion(Region region) {
13868 final AttachInfo attachInfo = mAttachInfo;
13869 if (region != null && attachInfo != null) {
13870 final int pflags = mPrivateFlags;
13871 if ((pflags & SKIP_DRAW) == 0) {
13872 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13873 // remove it from the transparent region.
13874 final int[] location = attachInfo.mTransparentLocation;
13875 getLocationInWindow(location);
13876 region.op(location[0], location[1], location[0] + mRight - mLeft,
13877 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13878 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13879 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13880 // exists, so we remove the background drawable's non-transparent
13881 // parts from this transparent region.
13882 applyDrawableToTransparentRegion(mBGDrawable, region);
13883 }
13884 }
13885 return true;
13886 }
13887
13888 /**
13889 * Play a sound effect for this view.
13890 *
13891 * <p>The framework will play sound effects for some built in actions, such as
13892 * clicking, but you may wish to play these effects in your widget,
13893 * for instance, for internal navigation.
13894 *
13895 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13896 * {@link #isSoundEffectsEnabled()} is true.
13897 *
13898 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13899 */
13900 public void playSoundEffect(int soundConstant) {
13901 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13902 return;
13903 }
13904 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13905 }
13906
13907 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013908 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013909 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013910 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013911 *
13912 * <p>The framework will provide haptic feedback for some built in actions,
13913 * such as long presses, but you may wish to provide feedback for your
13914 * own widget.
13915 *
13916 * <p>The feedback will only be performed if
13917 * {@link #isHapticFeedbackEnabled()} is true.
13918 *
13919 * @param feedbackConstant One of the constants defined in
13920 * {@link HapticFeedbackConstants}
13921 */
13922 public boolean performHapticFeedback(int feedbackConstant) {
13923 return performHapticFeedback(feedbackConstant, 0);
13924 }
13925
13926 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013927 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013928 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013929 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013930 *
13931 * @param feedbackConstant One of the constants defined in
13932 * {@link HapticFeedbackConstants}
13933 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13934 */
13935 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13936 if (mAttachInfo == null) {
13937 return false;
13938 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013939 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013940 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013941 && !isHapticFeedbackEnabled()) {
13942 return false;
13943 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013944 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13945 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013946 }
13947
13948 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013949 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013950 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13951 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013952 */
13953 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013954 if (visibility != mSystemUiVisibility) {
13955 mSystemUiVisibility = visibility;
13956 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13957 mParent.recomputeViewAttributes(this);
13958 }
Joe Onorato664644d2011-01-23 17:53:23 -080013959 }
13960 }
13961
13962 /**
13963 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013964 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13965 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013966 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013967 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013968 return mSystemUiVisibility;
13969 }
13970
Scott Mainec6331b2011-05-24 16:55:56 -070013971 /**
13972 * Set a listener to receive callbacks when the visibility of the system bar changes.
13973 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13974 */
Joe Onorato664644d2011-01-23 17:53:23 -080013975 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013976 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013977 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13978 mParent.recomputeViewAttributes(this);
13979 }
13980 }
13981
13982 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013983 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13984 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013985 */
13986 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013987 ListenerInfo li = mListenerInfo;
13988 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13989 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013990 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013991 }
13992 }
13993
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013994 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13995 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13996 if (val != mSystemUiVisibility) {
13997 setSystemUiVisibility(val);
13998 }
13999 }
14000
Joe Onorato664644d2011-01-23 17:53:23 -080014001 /**
Joe Malin32736f02011-01-19 16:14:20 -080014002 * Creates an image that the system displays during the drag and drop
14003 * operation. This is called a &quot;drag shadow&quot;. The default implementation
14004 * for a DragShadowBuilder based on a View returns an image that has exactly the same
14005 * appearance as the given View. The default also positions the center of the drag shadow
14006 * directly under the touch point. If no View is provided (the constructor with no parameters
14007 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
14008 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
14009 * default is an invisible drag shadow.
14010 * <p>
14011 * You are not required to use the View you provide to the constructor as the basis of the
14012 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
14013 * anything you want as the drag shadow.
14014 * </p>
14015 * <p>
14016 * You pass a DragShadowBuilder object to the system when you start the drag. The system
14017 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
14018 * size and position of the drag shadow. It uses this data to construct a
14019 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
14020 * so that your application can draw the shadow image in the Canvas.
14021 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070014022 *
14023 * <div class="special reference">
14024 * <h3>Developer Guides</h3>
14025 * <p>For a guide to implementing drag and drop features, read the
14026 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14027 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070014028 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014029 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070014030 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070014031
14032 /**
Joe Malin32736f02011-01-19 16:14:20 -080014033 * Constructs a shadow image builder based on a View. By default, the resulting drag
14034 * shadow will have the same appearance and dimensions as the View, with the touch point
14035 * over the center of the View.
14036 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070014037 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014038 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070014039 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070014040 }
14041
Christopher Tate17ed60c2011-01-18 12:50:26 -080014042 /**
14043 * Construct a shadow builder object with no associated View. This
14044 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
14045 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
14046 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080014047 * reference to any View object. If they are not overridden, then the result is an
14048 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080014049 */
14050 public DragShadowBuilder() {
14051 mView = new WeakReference<View>(null);
14052 }
14053
14054 /**
14055 * Returns the View object that had been passed to the
14056 * {@link #View.DragShadowBuilder(View)}
14057 * constructor. If that View parameter was {@code null} or if the
14058 * {@link #View.DragShadowBuilder()}
14059 * constructor was used to instantiate the builder object, this method will return
14060 * null.
14061 *
14062 * @return The View object associate with this builder object.
14063 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070014064 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070014065 final public View getView() {
14066 return mView.get();
14067 }
14068
Christopher Tate2c095f32010-10-04 14:13:40 -070014069 /**
Joe Malin32736f02011-01-19 16:14:20 -080014070 * Provides the metrics for the shadow image. These include the dimensions of
14071 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070014072 * be centered under the touch location while dragging.
14073 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014074 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080014075 * same as the dimensions of the View itself and centers the shadow under
14076 * the touch point.
14077 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070014078 *
Joe Malin32736f02011-01-19 16:14:20 -080014079 * @param shadowSize A {@link android.graphics.Point} containing the width and height
14080 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
14081 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
14082 * image.
14083 *
14084 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
14085 * shadow image that should be underneath the touch point during the drag and drop
14086 * operation. Your application must set {@link android.graphics.Point#x} to the
14087 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070014088 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014089 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070014090 final View view = mView.get();
14091 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014092 shadowSize.set(view.getWidth(), view.getHeight());
14093 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070014094 } else {
14095 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
14096 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014097 }
14098
14099 /**
Joe Malin32736f02011-01-19 16:14:20 -080014100 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
14101 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014102 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070014103 *
Joe Malin32736f02011-01-19 16:14:20 -080014104 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070014105 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014106 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070014107 final View view = mView.get();
14108 if (view != null) {
14109 view.draw(canvas);
14110 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014111 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070014112 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014113 }
14114 }
14115
14116 /**
Joe Malin32736f02011-01-19 16:14:20 -080014117 * Starts a drag and drop operation. When your application calls this method, it passes a
14118 * {@link android.view.View.DragShadowBuilder} object to the system. The
14119 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
14120 * to get metrics for the drag shadow, and then calls the object's
14121 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
14122 * <p>
14123 * Once the system has the drag shadow, it begins the drag and drop operation by sending
14124 * drag events to all the View objects in your application that are currently visible. It does
14125 * this either by calling the View object's drag listener (an implementation of
14126 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
14127 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
14128 * Both are passed a {@link android.view.DragEvent} object that has a
14129 * {@link android.view.DragEvent#getAction()} value of
14130 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
14131 * </p>
14132 * <p>
14133 * Your application can invoke startDrag() on any attached View object. The View object does not
14134 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
14135 * be related to the View the user selected for dragging.
14136 * </p>
14137 * @param data A {@link android.content.ClipData} object pointing to the data to be
14138 * transferred by the drag and drop operation.
14139 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
14140 * drag shadow.
14141 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
14142 * drop operation. This Object is put into every DragEvent object sent by the system during the
14143 * current drag.
14144 * <p>
14145 * myLocalState is a lightweight mechanism for the sending information from the dragged View
14146 * to the target Views. For example, it can contain flags that differentiate between a
14147 * a copy operation and a move operation.
14148 * </p>
14149 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
14150 * so the parameter should be set to 0.
14151 * @return {@code true} if the method completes successfully, or
14152 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
14153 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070014154 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014155 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014156 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070014157 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014158 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070014159 }
14160 boolean okay = false;
14161
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014162 Point shadowSize = new Point();
14163 Point shadowTouchPoint = new Point();
14164 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070014165
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014166 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
14167 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
14168 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070014169 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014170
Chris Tatea32dcf72010-10-14 12:13:50 -070014171 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014172 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
14173 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070014174 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014175 Surface surface = new Surface();
14176 try {
14177 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014178 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070014179 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070014180 + " surface=" + surface);
14181 if (token != null) {
14182 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070014183 try {
Chris Tate6b391282010-10-14 15:48:59 -070014184 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014185 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070014186 } finally {
14187 surface.unlockCanvasAndPost(canvas);
14188 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014189
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014190 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080014191
14192 // Cache the local state object for delivery with DragEvents
14193 root.setLocalDragState(myLocalState);
14194
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014195 // repurpose 'shadowSize' for the last touch point
14196 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070014197
Christopher Tatea53146c2010-09-07 11:57:52 -070014198 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014199 shadowSize.x, shadowSize.y,
14200 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070014201 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070014202
14203 // Off and running! Release our local surface instance; the drag
14204 // shadow surface is now managed by the system process.
14205 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070014206 }
14207 } catch (Exception e) {
14208 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
14209 surface.destroy();
14210 }
14211
14212 return okay;
14213 }
14214
Christopher Tatea53146c2010-09-07 11:57:52 -070014215 /**
Joe Malin32736f02011-01-19 16:14:20 -080014216 * Handles drag events sent by the system following a call to
14217 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
14218 *<p>
14219 * When the system calls this method, it passes a
14220 * {@link android.view.DragEvent} object. A call to
14221 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
14222 * in DragEvent. The method uses these to determine what is happening in the drag and drop
14223 * operation.
14224 * @param event The {@link android.view.DragEvent} sent by the system.
14225 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
14226 * in DragEvent, indicating the type of drag event represented by this object.
14227 * @return {@code true} if the method was successful, otherwise {@code false}.
14228 * <p>
14229 * The method should return {@code true} in response to an action type of
14230 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
14231 * operation.
14232 * </p>
14233 * <p>
14234 * The method should also return {@code true} in response to an action type of
14235 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
14236 * {@code false} if it didn't.
14237 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014238 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070014239 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070014240 return false;
14241 }
14242
14243 /**
Joe Malin32736f02011-01-19 16:14:20 -080014244 * Detects if this View is enabled and has a drag event listener.
14245 * If both are true, then it calls the drag event listener with the
14246 * {@link android.view.DragEvent} it received. If the drag event listener returns
14247 * {@code true}, then dispatchDragEvent() returns {@code true}.
14248 * <p>
14249 * For all other cases, the method calls the
14250 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
14251 * method and returns its result.
14252 * </p>
14253 * <p>
14254 * This ensures that a drag event is always consumed, even if the View does not have a drag
14255 * event listener. However, if the View has a listener and the listener returns true, then
14256 * onDragEvent() is not called.
14257 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014258 */
14259 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080014260 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014261 ListenerInfo li = mListenerInfo;
14262 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
14263 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070014264 return true;
14265 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014266 return onDragEvent(event);
14267 }
14268
Christopher Tate3d4bf172011-03-28 16:16:46 -070014269 boolean canAcceptDrag() {
14270 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
14271 }
14272
Christopher Tatea53146c2010-09-07 11:57:52 -070014273 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070014274 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
14275 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070014276 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070014277 */
14278 public void onCloseSystemDialogs(String reason) {
14279 }
Joe Malin32736f02011-01-19 16:14:20 -080014280
Dianne Hackbornffa42482009-09-23 22:20:11 -070014281 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014282 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014283 * update a Region being computed for
14284 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014285 * that any non-transparent parts of the Drawable are removed from the
14286 * given transparent region.
14287 *
14288 * @param dr The Drawable whose transparency is to be applied to the region.
14289 * @param region A Region holding the current transparency information,
14290 * where any parts of the region that are set are considered to be
14291 * transparent. On return, this region will be modified to have the
14292 * transparency information reduced by the corresponding parts of the
14293 * Drawable that are not transparent.
14294 * {@hide}
14295 */
14296 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14297 if (DBG) {
14298 Log.i("View", "Getting transparent region for: " + this);
14299 }
14300 final Region r = dr.getTransparentRegion();
14301 final Rect db = dr.getBounds();
14302 final AttachInfo attachInfo = mAttachInfo;
14303 if (r != null && attachInfo != null) {
14304 final int w = getRight()-getLeft();
14305 final int h = getBottom()-getTop();
14306 if (db.left > 0) {
14307 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14308 r.op(0, 0, db.left, h, Region.Op.UNION);
14309 }
14310 if (db.right < w) {
14311 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14312 r.op(db.right, 0, w, h, Region.Op.UNION);
14313 }
14314 if (db.top > 0) {
14315 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14316 r.op(0, 0, w, db.top, Region.Op.UNION);
14317 }
14318 if (db.bottom < h) {
14319 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14320 r.op(0, db.bottom, w, h, Region.Op.UNION);
14321 }
14322 final int[] location = attachInfo.mTransparentLocation;
14323 getLocationInWindow(location);
14324 r.translate(location[0], location[1]);
14325 region.op(r, Region.Op.INTERSECT);
14326 } else {
14327 region.op(db, Region.Op.DIFFERENCE);
14328 }
14329 }
14330
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014331 private void checkForLongClick(int delayOffset) {
14332 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14333 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014334
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014335 if (mPendingCheckForLongPress == null) {
14336 mPendingCheckForLongPress = new CheckForLongPress();
14337 }
14338 mPendingCheckForLongPress.rememberWindowAttachCount();
14339 postDelayed(mPendingCheckForLongPress,
14340 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014342 }
14343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014344 /**
14345 * Inflate a view from an XML resource. This convenience method wraps the {@link
14346 * LayoutInflater} class, which provides a full range of options for view inflation.
14347 *
14348 * @param context The Context object for your activity or application.
14349 * @param resource The resource ID to inflate
14350 * @param root A view group that will be the parent. Used to properly inflate the
14351 * layout_* parameters.
14352 * @see LayoutInflater
14353 */
14354 public static View inflate(Context context, int resource, ViewGroup root) {
14355 LayoutInflater factory = LayoutInflater.from(context);
14356 return factory.inflate(resource, root);
14357 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014359 /**
Adam Powell637d3372010-08-25 14:37:03 -070014360 * Scroll the view with standard behavior for scrolling beyond the normal
14361 * content boundaries. Views that call this method should override
14362 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14363 * results of an over-scroll operation.
14364 *
14365 * Views can use this method to handle any touch or fling-based scrolling.
14366 *
14367 * @param deltaX Change in X in pixels
14368 * @param deltaY Change in Y in pixels
14369 * @param scrollX Current X scroll value in pixels before applying deltaX
14370 * @param scrollY Current Y scroll value in pixels before applying deltaY
14371 * @param scrollRangeX Maximum content scroll range along the X axis
14372 * @param scrollRangeY Maximum content scroll range along the Y axis
14373 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14374 * along the X axis.
14375 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14376 * along the Y axis.
14377 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14378 * @return true if scrolling was clamped to an over-scroll boundary along either
14379 * axis, false otherwise.
14380 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014381 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014382 protected boolean overScrollBy(int deltaX, int deltaY,
14383 int scrollX, int scrollY,
14384 int scrollRangeX, int scrollRangeY,
14385 int maxOverScrollX, int maxOverScrollY,
14386 boolean isTouchEvent) {
14387 final int overScrollMode = mOverScrollMode;
14388 final boolean canScrollHorizontal =
14389 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14390 final boolean canScrollVertical =
14391 computeVerticalScrollRange() > computeVerticalScrollExtent();
14392 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14393 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14394 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14395 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14396
14397 int newScrollX = scrollX + deltaX;
14398 if (!overScrollHorizontal) {
14399 maxOverScrollX = 0;
14400 }
14401
14402 int newScrollY = scrollY + deltaY;
14403 if (!overScrollVertical) {
14404 maxOverScrollY = 0;
14405 }
14406
14407 // Clamp values if at the limits and record
14408 final int left = -maxOverScrollX;
14409 final int right = maxOverScrollX + scrollRangeX;
14410 final int top = -maxOverScrollY;
14411 final int bottom = maxOverScrollY + scrollRangeY;
14412
14413 boolean clampedX = false;
14414 if (newScrollX > right) {
14415 newScrollX = right;
14416 clampedX = true;
14417 } else if (newScrollX < left) {
14418 newScrollX = left;
14419 clampedX = true;
14420 }
14421
14422 boolean clampedY = false;
14423 if (newScrollY > bottom) {
14424 newScrollY = bottom;
14425 clampedY = true;
14426 } else if (newScrollY < top) {
14427 newScrollY = top;
14428 clampedY = true;
14429 }
14430
14431 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14432
14433 return clampedX || clampedY;
14434 }
14435
14436 /**
14437 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14438 * respond to the results of an over-scroll operation.
14439 *
14440 * @param scrollX New X scroll value in pixels
14441 * @param scrollY New Y scroll value in pixels
14442 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14443 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14444 */
14445 protected void onOverScrolled(int scrollX, int scrollY,
14446 boolean clampedX, boolean clampedY) {
14447 // Intentionally empty.
14448 }
14449
14450 /**
14451 * Returns the over-scroll mode for this view. The result will be
14452 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14453 * (allow over-scrolling only if the view content is larger than the container),
14454 * or {@link #OVER_SCROLL_NEVER}.
14455 *
14456 * @return This view's over-scroll mode.
14457 */
14458 public int getOverScrollMode() {
14459 return mOverScrollMode;
14460 }
14461
14462 /**
14463 * Set the over-scroll mode for this view. Valid over-scroll modes are
14464 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14465 * (allow over-scrolling only if the view content is larger than the container),
14466 * or {@link #OVER_SCROLL_NEVER}.
14467 *
14468 * Setting the over-scroll mode of a view will have an effect only if the
14469 * view is capable of scrolling.
14470 *
14471 * @param overScrollMode The new over-scroll mode for this view.
14472 */
14473 public void setOverScrollMode(int overScrollMode) {
14474 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14475 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14476 overScrollMode != OVER_SCROLL_NEVER) {
14477 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14478 }
14479 mOverScrollMode = overScrollMode;
14480 }
14481
14482 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014483 * Gets a scale factor that determines the distance the view should scroll
14484 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14485 * @return The vertical scroll scale factor.
14486 * @hide
14487 */
14488 protected float getVerticalScrollFactor() {
14489 if (mVerticalScrollFactor == 0) {
14490 TypedValue outValue = new TypedValue();
14491 if (!mContext.getTheme().resolveAttribute(
14492 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14493 throw new IllegalStateException(
14494 "Expected theme to define listPreferredItemHeight.");
14495 }
14496 mVerticalScrollFactor = outValue.getDimension(
14497 mContext.getResources().getDisplayMetrics());
14498 }
14499 return mVerticalScrollFactor;
14500 }
14501
14502 /**
14503 * Gets a scale factor that determines the distance the view should scroll
14504 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14505 * @return The horizontal scroll scale factor.
14506 * @hide
14507 */
14508 protected float getHorizontalScrollFactor() {
14509 // TODO: Should use something else.
14510 return getVerticalScrollFactor();
14511 }
14512
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014513 /**
14514 * Return the value specifying the text direction or policy that was set with
14515 * {@link #setTextDirection(int)}.
14516 *
14517 * @return the defined text direction. It can be one of:
14518 *
14519 * {@link #TEXT_DIRECTION_INHERIT},
14520 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14521 * {@link #TEXT_DIRECTION_ANY_RTL},
14522 * {@link #TEXT_DIRECTION_LTR},
14523 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014524 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014525 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014526 @ViewDebug.ExportedProperty(category = "text", mapping = {
14527 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
14528 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
14529 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
14530 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
14531 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
14532 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
14533 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014534 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014535 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014536 }
14537
14538 /**
14539 * Set the text direction.
14540 *
14541 * @param textDirection the direction to set. Should be one of:
14542 *
14543 * {@link #TEXT_DIRECTION_INHERIT},
14544 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14545 * {@link #TEXT_DIRECTION_ANY_RTL},
14546 * {@link #TEXT_DIRECTION_LTR},
14547 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014548 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014549 */
14550 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014551 if (getTextDirection() != textDirection) {
14552 // Reset the current text direction
14553 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
14554 // Set the new text direction
14555 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
14556 // Reset the current resolved text direction
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014557 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014558 // Ask for a layout pass
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014559 requestLayout();
14560 }
14561 }
14562
14563 /**
14564 * Return the resolved text direction.
14565 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014566 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
14567 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
14568 * up the parent chain of the view. if there is no parent, then it will return the default
14569 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
14570 *
14571 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014572 *
Doug Feltcb3791202011-07-07 11:57:48 -070014573 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14574 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014575 * {@link #TEXT_DIRECTION_LTR},
14576 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014577 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014578 */
14579 public int getResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014580 // The text direction is not inherited so return it back
14581 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014582 resolveTextDirection();
14583 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014584 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014585 }
14586
14587 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014588 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14589 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014590 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014591 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014592 // Reset any previous text direction resolution
14593 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
14594
14595 // Set resolved text direction flag depending on text direction flag
14596 final int textDirection = getTextDirection();
14597 switch(textDirection) {
14598 case TEXT_DIRECTION_INHERIT:
14599 if (canResolveTextDirection()) {
14600 ViewGroup viewGroup = ((ViewGroup) mParent);
14601
14602 // Set current resolved direction to the same value as the parent's one
14603 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
14604 switch (parentResolvedDirection) {
14605 case TEXT_DIRECTION_FIRST_STRONG:
14606 case TEXT_DIRECTION_ANY_RTL:
14607 case TEXT_DIRECTION_LTR:
14608 case TEXT_DIRECTION_RTL:
14609 case TEXT_DIRECTION_LOCALE:
14610 mPrivateFlags2 |=
14611 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
14612 break;
14613 default:
14614 // Default resolved direction is "first strong" heuristic
14615 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
14616 }
14617 } else {
14618 // We cannot do the resolution if there is no parent, so use the default one
14619 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
14620 }
14621 break;
14622 case TEXT_DIRECTION_FIRST_STRONG:
14623 case TEXT_DIRECTION_ANY_RTL:
14624 case TEXT_DIRECTION_LTR:
14625 case TEXT_DIRECTION_RTL:
14626 case TEXT_DIRECTION_LOCALE:
14627 // Resolved direction is the same as text direction
14628 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
14629 break;
14630 default:
14631 // Default resolved direction is "first strong" heuristic
14632 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014633 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014634
14635 // Set to resolved
14636 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014637 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014638 }
14639
14640 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014641 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014642 * resolution should override this method.
14643 *
14644 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014645 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014646 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014647 }
14648
14649 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014650 * Check if text direction resolution can be done.
14651 *
14652 * @return true if text direction resolution can be done otherwise return false.
14653 */
14654 public boolean canResolveTextDirection() {
14655 switch (getTextDirection()) {
14656 case TEXT_DIRECTION_INHERIT:
14657 return (mParent != null) && (mParent instanceof ViewGroup);
14658 default:
14659 return true;
14660 }
14661 }
14662
14663 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014664 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014665 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014666 * reset is done.
14667 */
14668 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014669 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014670 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014671 }
14672
14673 /**
14674 * Called when text direction is reset. Subclasses that care about text direction reset should
14675 * override this method and do a reset of the text direction of their children. The default
14676 * implementation does nothing.
14677 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014678 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014679 }
14680
Chet Haaseb39f0512011-05-24 14:36:40 -070014681 //
14682 // Properties
14683 //
14684 /**
14685 * A Property wrapper around the <code>alpha</code> functionality handled by the
14686 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14687 */
Chet Haased47f1532011-12-16 11:18:52 -080014688 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014689 @Override
14690 public void setValue(View object, float value) {
14691 object.setAlpha(value);
14692 }
14693
14694 @Override
14695 public Float get(View object) {
14696 return object.getAlpha();
14697 }
14698 };
14699
14700 /**
14701 * A Property wrapper around the <code>translationX</code> functionality handled by the
14702 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14703 */
Chet Haased47f1532011-12-16 11:18:52 -080014704 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014705 @Override
14706 public void setValue(View object, float value) {
14707 object.setTranslationX(value);
14708 }
14709
14710 @Override
14711 public Float get(View object) {
14712 return object.getTranslationX();
14713 }
14714 };
14715
14716 /**
14717 * A Property wrapper around the <code>translationY</code> functionality handled by the
14718 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14719 */
Chet Haased47f1532011-12-16 11:18:52 -080014720 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014721 @Override
14722 public void setValue(View object, float value) {
14723 object.setTranslationY(value);
14724 }
14725
14726 @Override
14727 public Float get(View object) {
14728 return object.getTranslationY();
14729 }
14730 };
14731
14732 /**
14733 * A Property wrapper around the <code>x</code> functionality handled by the
14734 * {@link View#setX(float)} and {@link View#getX()} methods.
14735 */
Chet Haased47f1532011-12-16 11:18:52 -080014736 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014737 @Override
14738 public void setValue(View object, float value) {
14739 object.setX(value);
14740 }
14741
14742 @Override
14743 public Float get(View object) {
14744 return object.getX();
14745 }
14746 };
14747
14748 /**
14749 * A Property wrapper around the <code>y</code> functionality handled by the
14750 * {@link View#setY(float)} and {@link View#getY()} methods.
14751 */
Chet Haased47f1532011-12-16 11:18:52 -080014752 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014753 @Override
14754 public void setValue(View object, float value) {
14755 object.setY(value);
14756 }
14757
14758 @Override
14759 public Float get(View object) {
14760 return object.getY();
14761 }
14762 };
14763
14764 /**
14765 * A Property wrapper around the <code>rotation</code> functionality handled by the
14766 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14767 */
Chet Haased47f1532011-12-16 11:18:52 -080014768 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014769 @Override
14770 public void setValue(View object, float value) {
14771 object.setRotation(value);
14772 }
14773
14774 @Override
14775 public Float get(View object) {
14776 return object.getRotation();
14777 }
14778 };
14779
14780 /**
14781 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14782 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14783 */
Chet Haased47f1532011-12-16 11:18:52 -080014784 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014785 @Override
14786 public void setValue(View object, float value) {
14787 object.setRotationX(value);
14788 }
14789
14790 @Override
14791 public Float get(View object) {
14792 return object.getRotationX();
14793 }
14794 };
14795
14796 /**
14797 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14798 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14799 */
Chet Haased47f1532011-12-16 11:18:52 -080014800 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014801 @Override
14802 public void setValue(View object, float value) {
14803 object.setRotationY(value);
14804 }
14805
14806 @Override
14807 public Float get(View object) {
14808 return object.getRotationY();
14809 }
14810 };
14811
14812 /**
14813 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14814 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14815 */
Chet Haased47f1532011-12-16 11:18:52 -080014816 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014817 @Override
14818 public void setValue(View object, float value) {
14819 object.setScaleX(value);
14820 }
14821
14822 @Override
14823 public Float get(View object) {
14824 return object.getScaleX();
14825 }
14826 };
14827
14828 /**
14829 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14830 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14831 */
Chet Haased47f1532011-12-16 11:18:52 -080014832 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014833 @Override
14834 public void setValue(View object, float value) {
14835 object.setScaleY(value);
14836 }
14837
14838 @Override
14839 public Float get(View object) {
14840 return object.getScaleY();
14841 }
14842 };
14843
Jeff Brown33bbfd22011-02-24 20:55:35 -080014844 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014845 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14846 * Each MeasureSpec represents a requirement for either the width or the height.
14847 * A MeasureSpec is comprised of a size and a mode. There are three possible
14848 * modes:
14849 * <dl>
14850 * <dt>UNSPECIFIED</dt>
14851 * <dd>
14852 * The parent has not imposed any constraint on the child. It can be whatever size
14853 * it wants.
14854 * </dd>
14855 *
14856 * <dt>EXACTLY</dt>
14857 * <dd>
14858 * The parent has determined an exact size for the child. The child is going to be
14859 * given those bounds regardless of how big it wants to be.
14860 * </dd>
14861 *
14862 * <dt>AT_MOST</dt>
14863 * <dd>
14864 * The child can be as large as it wants up to the specified size.
14865 * </dd>
14866 * </dl>
14867 *
14868 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14869 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14870 */
14871 public static class MeasureSpec {
14872 private static final int MODE_SHIFT = 30;
14873 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14874
14875 /**
14876 * Measure specification mode: The parent has not imposed any constraint
14877 * on the child. It can be whatever size it wants.
14878 */
14879 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14880
14881 /**
14882 * Measure specification mode: The parent has determined an exact size
14883 * for the child. The child is going to be given those bounds regardless
14884 * of how big it wants to be.
14885 */
14886 public static final int EXACTLY = 1 << MODE_SHIFT;
14887
14888 /**
14889 * Measure specification mode: The child can be as large as it wants up
14890 * to the specified size.
14891 */
14892 public static final int AT_MOST = 2 << MODE_SHIFT;
14893
14894 /**
14895 * Creates a measure specification based on the supplied size and mode.
14896 *
14897 * The mode must always be one of the following:
14898 * <ul>
14899 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14900 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14901 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14902 * </ul>
14903 *
14904 * @param size the size of the measure specification
14905 * @param mode the mode of the measure specification
14906 * @return the measure specification based on size and mode
14907 */
14908 public static int makeMeasureSpec(int size, int mode) {
14909 return size + mode;
14910 }
14911
14912 /**
14913 * Extracts the mode from the supplied measure specification.
14914 *
14915 * @param measureSpec the measure specification to extract the mode from
14916 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14917 * {@link android.view.View.MeasureSpec#AT_MOST} or
14918 * {@link android.view.View.MeasureSpec#EXACTLY}
14919 */
14920 public static int getMode(int measureSpec) {
14921 return (measureSpec & MODE_MASK);
14922 }
14923
14924 /**
14925 * Extracts the size from the supplied measure specification.
14926 *
14927 * @param measureSpec the measure specification to extract the size from
14928 * @return the size in pixels defined in the supplied measure specification
14929 */
14930 public static int getSize(int measureSpec) {
14931 return (measureSpec & ~MODE_MASK);
14932 }
14933
14934 /**
14935 * Returns a String representation of the specified measure
14936 * specification.
14937 *
14938 * @param measureSpec the measure specification to convert to a String
14939 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14940 */
14941 public static String toString(int measureSpec) {
14942 int mode = getMode(measureSpec);
14943 int size = getSize(measureSpec);
14944
14945 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14946
14947 if (mode == UNSPECIFIED)
14948 sb.append("UNSPECIFIED ");
14949 else if (mode == EXACTLY)
14950 sb.append("EXACTLY ");
14951 else if (mode == AT_MOST)
14952 sb.append("AT_MOST ");
14953 else
14954 sb.append(mode).append(" ");
14955
14956 sb.append(size);
14957 return sb.toString();
14958 }
14959 }
14960
14961 class CheckForLongPress implements Runnable {
14962
14963 private int mOriginalWindowAttachCount;
14964
14965 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014966 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014967 && mOriginalWindowAttachCount == mWindowAttachCount) {
14968 if (performLongClick()) {
14969 mHasPerformedLongPress = true;
14970 }
14971 }
14972 }
14973
14974 public void rememberWindowAttachCount() {
14975 mOriginalWindowAttachCount = mWindowAttachCount;
14976 }
14977 }
Joe Malin32736f02011-01-19 16:14:20 -080014978
Adam Powelle14579b2009-12-16 18:39:52 -080014979 private final class CheckForTap implements Runnable {
14980 public void run() {
14981 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014982 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014983 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014984 }
14985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014986
Adam Powella35d7682010-03-12 14:48:13 -080014987 private final class PerformClick implements Runnable {
14988 public void run() {
14989 performClick();
14990 }
14991 }
14992
Dianne Hackborn63042d62011-01-26 18:56:29 -080014993 /** @hide */
14994 public void hackTurnOffWindowResizeAnim(boolean off) {
14995 mAttachInfo.mTurnOffWindowResizeAnim = off;
14996 }
Joe Malin32736f02011-01-19 16:14:20 -080014997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014998 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014999 * This method returns a ViewPropertyAnimator object, which can be used to animate
15000 * specific properties on this View.
15001 *
15002 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
15003 */
15004 public ViewPropertyAnimator animate() {
15005 if (mAnimator == null) {
15006 mAnimator = new ViewPropertyAnimator(this);
15007 }
15008 return mAnimator;
15009 }
15010
15011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015012 * Interface definition for a callback to be invoked when a key event is
15013 * dispatched to this view. The callback will be invoked before the key
15014 * event is given to the view.
15015 */
15016 public interface OnKeyListener {
15017 /**
15018 * Called when a key is dispatched to a view. This allows listeners to
15019 * get a chance to respond before the target view.
15020 *
15021 * @param v The view the key has been dispatched to.
15022 * @param keyCode The code for the physical key that was pressed
15023 * @param event The KeyEvent object containing full information about
15024 * the event.
15025 * @return True if the listener has consumed the event, false otherwise.
15026 */
15027 boolean onKey(View v, int keyCode, KeyEvent event);
15028 }
15029
15030 /**
15031 * Interface definition for a callback to be invoked when a touch event is
15032 * dispatched to this view. The callback will be invoked before the touch
15033 * event is given to the view.
15034 */
15035 public interface OnTouchListener {
15036 /**
15037 * Called when a touch event is dispatched to a view. This allows listeners to
15038 * get a chance to respond before the target view.
15039 *
15040 * @param v The view the touch event has been dispatched to.
15041 * @param event The MotionEvent object containing full information about
15042 * the event.
15043 * @return True if the listener has consumed the event, false otherwise.
15044 */
15045 boolean onTouch(View v, MotionEvent event);
15046 }
15047
15048 /**
Jeff Brown10b62902011-06-20 16:40:37 -070015049 * Interface definition for a callback to be invoked when a hover event is
15050 * dispatched to this view. The callback will be invoked before the hover
15051 * event is given to the view.
15052 */
15053 public interface OnHoverListener {
15054 /**
15055 * Called when a hover event is dispatched to a view. This allows listeners to
15056 * get a chance to respond before the target view.
15057 *
15058 * @param v The view the hover event has been dispatched to.
15059 * @param event The MotionEvent object containing full information about
15060 * the event.
15061 * @return True if the listener has consumed the event, false otherwise.
15062 */
15063 boolean onHover(View v, MotionEvent event);
15064 }
15065
15066 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015067 * Interface definition for a callback to be invoked when a generic motion event is
15068 * dispatched to this view. The callback will be invoked before the generic motion
15069 * event is given to the view.
15070 */
15071 public interface OnGenericMotionListener {
15072 /**
15073 * Called when a generic motion event is dispatched to a view. This allows listeners to
15074 * get a chance to respond before the target view.
15075 *
15076 * @param v The view the generic motion event has been dispatched to.
15077 * @param event The MotionEvent object containing full information about
15078 * the event.
15079 * @return True if the listener has consumed the event, false otherwise.
15080 */
15081 boolean onGenericMotion(View v, MotionEvent event);
15082 }
15083
15084 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015085 * Interface definition for a callback to be invoked when a view has been clicked and held.
15086 */
15087 public interface OnLongClickListener {
15088 /**
15089 * Called when a view has been clicked and held.
15090 *
15091 * @param v The view that was clicked and held.
15092 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080015093 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015094 */
15095 boolean onLongClick(View v);
15096 }
15097
15098 /**
Chris Tate32affef2010-10-18 15:29:21 -070015099 * Interface definition for a callback to be invoked when a drag is being dispatched
15100 * to this view. The callback will be invoked before the hosting view's own
15101 * onDrag(event) method. If the listener wants to fall back to the hosting view's
15102 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070015103 *
15104 * <div class="special reference">
15105 * <h3>Developer Guides</h3>
15106 * <p>For a guide to implementing drag and drop features, read the
15107 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15108 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070015109 */
15110 public interface OnDragListener {
15111 /**
15112 * Called when a drag event is dispatched to a view. This allows listeners
15113 * to get a chance to override base View behavior.
15114 *
Joe Malin32736f02011-01-19 16:14:20 -080015115 * @param v The View that received the drag event.
15116 * @param event The {@link android.view.DragEvent} object for the drag event.
15117 * @return {@code true} if the drag event was handled successfully, or {@code false}
15118 * if the drag event was not handled. Note that {@code false} will trigger the View
15119 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070015120 */
15121 boolean onDrag(View v, DragEvent event);
15122 }
15123
15124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015125 * Interface definition for a callback to be invoked when the focus state of
15126 * a view changed.
15127 */
15128 public interface OnFocusChangeListener {
15129 /**
15130 * Called when the focus state of a view has changed.
15131 *
15132 * @param v The view whose state has changed.
15133 * @param hasFocus The new focus state of v.
15134 */
15135 void onFocusChange(View v, boolean hasFocus);
15136 }
15137
15138 /**
15139 * Interface definition for a callback to be invoked when a view is clicked.
15140 */
15141 public interface OnClickListener {
15142 /**
15143 * Called when a view has been clicked.
15144 *
15145 * @param v The view that was clicked.
15146 */
15147 void onClick(View v);
15148 }
15149
15150 /**
15151 * Interface definition for a callback to be invoked when the context menu
15152 * for this view is being built.
15153 */
15154 public interface OnCreateContextMenuListener {
15155 /**
15156 * Called when the context menu for this view is being built. It is not
15157 * safe to hold onto the menu after this method returns.
15158 *
15159 * @param menu The context menu that is being built
15160 * @param v The view for which the context menu is being built
15161 * @param menuInfo Extra information about the item for which the
15162 * context menu should be shown. This information will vary
15163 * depending on the class of v.
15164 */
15165 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
15166 }
15167
Joe Onorato664644d2011-01-23 17:53:23 -080015168 /**
15169 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015170 * visibility. This reports <strong>global</strong> changes to the system UI
15171 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080015172 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070015173 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080015174 */
15175 public interface OnSystemUiVisibilityChangeListener {
15176 /**
15177 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070015178 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080015179 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040015180 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015181 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
15182 * <strong>global</strong> state of the UI visibility flags, not what your
15183 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080015184 */
15185 public void onSystemUiVisibilityChange(int visibility);
15186 }
15187
Adam Powell4afd62b2011-02-18 15:02:18 -080015188 /**
15189 * Interface definition for a callback to be invoked when this view is attached
15190 * or detached from its window.
15191 */
15192 public interface OnAttachStateChangeListener {
15193 /**
15194 * Called when the view is attached to a window.
15195 * @param v The view that was attached
15196 */
15197 public void onViewAttachedToWindow(View v);
15198 /**
15199 * Called when the view is detached from a window.
15200 * @param v The view that was detached
15201 */
15202 public void onViewDetachedFromWindow(View v);
15203 }
15204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015205 private final class UnsetPressedState implements Runnable {
15206 public void run() {
15207 setPressed(false);
15208 }
15209 }
15210
15211 /**
15212 * Base class for derived classes that want to save and restore their own
15213 * state in {@link android.view.View#onSaveInstanceState()}.
15214 */
15215 public static class BaseSavedState extends AbsSavedState {
15216 /**
15217 * Constructor used when reading from a parcel. Reads the state of the superclass.
15218 *
15219 * @param source
15220 */
15221 public BaseSavedState(Parcel source) {
15222 super(source);
15223 }
15224
15225 /**
15226 * Constructor called by derived classes when creating their SavedState objects
15227 *
15228 * @param superState The state of the superclass of this view
15229 */
15230 public BaseSavedState(Parcelable superState) {
15231 super(superState);
15232 }
15233
15234 public static final Parcelable.Creator<BaseSavedState> CREATOR =
15235 new Parcelable.Creator<BaseSavedState>() {
15236 public BaseSavedState createFromParcel(Parcel in) {
15237 return new BaseSavedState(in);
15238 }
15239
15240 public BaseSavedState[] newArray(int size) {
15241 return new BaseSavedState[size];
15242 }
15243 };
15244 }
15245
15246 /**
15247 * A set of information given to a view when it is attached to its parent
15248 * window.
15249 */
15250 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015251 interface Callbacks {
15252 void playSoundEffect(int effectId);
15253 boolean performHapticFeedback(int effectId, boolean always);
15254 }
15255
15256 /**
15257 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
15258 * to a Handler. This class contains the target (View) to invalidate and
15259 * the coordinates of the dirty rectangle.
15260 *
15261 * For performance purposes, this class also implements a pool of up to
15262 * POOL_LIMIT objects that get reused. This reduces memory allocations
15263 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015264 */
Romain Guyd928d682009-03-31 17:52:16 -070015265 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015266 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070015267 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
15268 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070015269 public InvalidateInfo newInstance() {
15270 return new InvalidateInfo();
15271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015272
Romain Guyd928d682009-03-31 17:52:16 -070015273 public void onAcquired(InvalidateInfo element) {
15274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015275
Romain Guyd928d682009-03-31 17:52:16 -070015276 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070015277 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070015278 }
15279 }, POOL_LIMIT)
15280 );
15281
15282 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015283 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015284
15285 View target;
15286
15287 int left;
15288 int top;
15289 int right;
15290 int bottom;
15291
Romain Guyd928d682009-03-31 17:52:16 -070015292 public void setNextPoolable(InvalidateInfo element) {
15293 mNext = element;
15294 }
15295
15296 public InvalidateInfo getNextPoolable() {
15297 return mNext;
15298 }
15299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015300 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070015301 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015302 }
15303
15304 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070015305 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015306 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015307
15308 public boolean isPooled() {
15309 return mIsPooled;
15310 }
15311
15312 public void setPooled(boolean isPooled) {
15313 mIsPooled = isPooled;
15314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015315 }
15316
15317 final IWindowSession mSession;
15318
15319 final IWindow mWindow;
15320
15321 final IBinder mWindowToken;
15322
15323 final Callbacks mRootCallbacks;
15324
Romain Guy59a12ca2011-06-09 17:48:21 -070015325 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080015326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015327 /**
15328 * The top view of the hierarchy.
15329 */
15330 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070015331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015332 IBinder mPanelParentWindowToken;
15333 Surface mSurface;
15334
Romain Guyb051e892010-09-28 19:09:36 -070015335 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080015336 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070015337 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080015338
Romain Guy7e4e5612012-03-05 14:37:29 -080015339 boolean mScreenOn;
15340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015341 /**
Romain Guy8506ab42009-06-11 17:35:47 -070015342 * Scale factor used by the compatibility mode
15343 */
15344 float mApplicationScale;
15345
15346 /**
15347 * Indicates whether the application is in compatibility mode
15348 */
15349 boolean mScalingRequired;
15350
15351 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015352 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080015353 */
15354 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080015355
Dianne Hackborn63042d62011-01-26 18:56:29 -080015356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015357 * Left position of this view's window
15358 */
15359 int mWindowLeft;
15360
15361 /**
15362 * Top position of this view's window
15363 */
15364 int mWindowTop;
15365
15366 /**
Adam Powell26153a32010-11-08 15:22:27 -080015367 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070015368 */
Adam Powell26153a32010-11-08 15:22:27 -080015369 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070015370
15371 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015372 * For windows that are full-screen but using insets to layout inside
15373 * of the screen decorations, these are the current insets for the
15374 * content of the window.
15375 */
15376 final Rect mContentInsets = new Rect();
15377
15378 /**
15379 * For windows that are full-screen but using insets to layout inside
15380 * of the screen decorations, these are the current insets for the
15381 * actual visible parts of the window.
15382 */
15383 final Rect mVisibleInsets = new Rect();
15384
15385 /**
15386 * The internal insets given by this window. This value is
15387 * supplied by the client (through
15388 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
15389 * be given to the window manager when changed to be used in laying
15390 * out windows behind it.
15391 */
15392 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
15393 = new ViewTreeObserver.InternalInsetsInfo();
15394
15395 /**
15396 * All views in the window's hierarchy that serve as scroll containers,
15397 * used to determine if the window can be resized or must be panned
15398 * to adjust for a soft input area.
15399 */
15400 final ArrayList<View> mScrollContainers = new ArrayList<View>();
15401
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070015402 final KeyEvent.DispatcherState mKeyDispatchState
15403 = new KeyEvent.DispatcherState();
15404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015405 /**
15406 * Indicates whether the view's window currently has the focus.
15407 */
15408 boolean mHasWindowFocus;
15409
15410 /**
15411 * The current visibility of the window.
15412 */
15413 int mWindowVisibility;
15414
15415 /**
15416 * Indicates the time at which drawing started to occur.
15417 */
15418 long mDrawingTime;
15419
15420 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070015421 * Indicates whether or not ignoring the DIRTY_MASK flags.
15422 */
15423 boolean mIgnoreDirtyState;
15424
15425 /**
Romain Guy02ccac62011-06-24 13:20:23 -070015426 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
15427 * to avoid clearing that flag prematurely.
15428 */
15429 boolean mSetIgnoreDirtyState = false;
15430
15431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015432 * Indicates whether the view's window is currently in touch mode.
15433 */
15434 boolean mInTouchMode;
15435
15436 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015437 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015438 * the next time it performs a traversal
15439 */
15440 boolean mRecomputeGlobalAttributes;
15441
15442 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015443 * Always report new attributes at next traversal.
15444 */
15445 boolean mForceReportNewAttributes;
15446
15447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015448 * Set during a traveral if any views want to keep the screen on.
15449 */
15450 boolean mKeepScreenOn;
15451
15452 /**
Joe Onorato664644d2011-01-23 17:53:23 -080015453 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
15454 */
15455 int mSystemUiVisibility;
15456
15457 /**
15458 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
15459 * attached.
15460 */
15461 boolean mHasSystemUiListeners;
15462
15463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015464 * Set if the visibility of any views has changed.
15465 */
15466 boolean mViewVisibilityChanged;
15467
15468 /**
15469 * Set to true if a view has been scrolled.
15470 */
15471 boolean mViewScrollChanged;
15472
15473 /**
15474 * Global to the view hierarchy used as a temporary for dealing with
15475 * x/y points in the transparent region computations.
15476 */
15477 final int[] mTransparentLocation = new int[2];
15478
15479 /**
15480 * Global to the view hierarchy used as a temporary for dealing with
15481 * x/y points in the ViewGroup.invalidateChild implementation.
15482 */
15483 final int[] mInvalidateChildLocation = new int[2];
15484
Chet Haasec3aa3612010-06-17 08:50:37 -070015485
15486 /**
15487 * Global to the view hierarchy used as a temporary for dealing with
15488 * x/y location when view is transformed.
15489 */
15490 final float[] mTmpTransformLocation = new float[2];
15491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015492 /**
15493 * The view tree observer used to dispatch global events like
15494 * layout, pre-draw, touch mode change, etc.
15495 */
15496 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15497
15498 /**
15499 * A Canvas used by the view hierarchy to perform bitmap caching.
15500 */
15501 Canvas mCanvas;
15502
15503 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015504 * The view root impl.
15505 */
15506 final ViewRootImpl mViewRootImpl;
15507
15508 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015509 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015510 * handler can be used to pump events in the UI events queue.
15511 */
15512 final Handler mHandler;
15513
15514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015515 * Temporary for use in computing invalidate rectangles while
15516 * calling up the hierarchy.
15517 */
15518 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015519
15520 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015521 * Temporary for use in computing hit areas with transformed views
15522 */
15523 final RectF mTmpTransformRect = new RectF();
15524
15525 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015526 * Temporary list for use in collecting focusable descendents of a view.
15527 */
15528 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015530 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015531 * The id of the window for accessibility purposes.
15532 */
15533 int mAccessibilityWindowId = View.NO_ID;
15534
15535 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015536 * Creates a new set of attachment information with the specified
15537 * events handler and thread.
15538 *
15539 * @param handler the events handler the view must use
15540 */
15541 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015542 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015543 mSession = session;
15544 mWindow = window;
15545 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015546 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015547 mHandler = handler;
15548 mRootCallbacks = effectPlayer;
15549 }
15550 }
15551
15552 /**
15553 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15554 * is supported. This avoids keeping too many unused fields in most
15555 * instances of View.</p>
15556 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015557 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015558
Mike Cleronf116bf82009-09-27 19:14:12 -070015559 /**
15560 * Scrollbars are not visible
15561 */
15562 public static final int OFF = 0;
15563
15564 /**
15565 * Scrollbars are visible
15566 */
15567 public static final int ON = 1;
15568
15569 /**
15570 * Scrollbars are fading away
15571 */
15572 public static final int FADING = 2;
15573
15574 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015576 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015577 public int scrollBarDefaultDelayBeforeFade;
15578 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015579
15580 public int scrollBarSize;
15581 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015582 public float[] interpolatorValues;
15583 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015584
15585 public final Paint paint;
15586 public final Matrix matrix;
15587 public Shader shader;
15588
Mike Cleronf116bf82009-09-27 19:14:12 -070015589 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15590
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015591 private static final float[] OPAQUE = { 255 };
15592 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015593
Mike Cleronf116bf82009-09-27 19:14:12 -070015594 /**
15595 * When fading should start. This time moves into the future every time
15596 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15597 */
15598 public long fadeStartTime;
15599
15600
15601 /**
15602 * The current state of the scrollbars: ON, OFF, or FADING
15603 */
15604 public int state = OFF;
15605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015606 private int mLastColor;
15607
Mike Cleronf116bf82009-09-27 19:14:12 -070015608 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015609 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15610 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015611 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15612 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015613
15614 paint = new Paint();
15615 matrix = new Matrix();
15616 // use use a height of 1, and then wack the matrix each time we
15617 // actually use it.
15618 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015620 paint.setShader(shader);
15621 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015622 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015623 }
Romain Guy8506ab42009-06-11 17:35:47 -070015624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015625 public void setFadeColor(int color) {
15626 if (color != 0 && color != mLastColor) {
15627 mLastColor = color;
15628 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015629
Romain Guye55e1a72009-08-27 10:42:26 -070015630 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15631 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015633 paint.setShader(shader);
15634 // Restore the default transfer mode (src_over)
15635 paint.setXfermode(null);
15636 }
15637 }
Joe Malin32736f02011-01-19 16:14:20 -080015638
Mike Cleronf116bf82009-09-27 19:14:12 -070015639 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015640 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015641 if (now >= fadeStartTime) {
15642
15643 // the animation fades the scrollbars out by changing
15644 // the opacity (alpha) from fully opaque to fully
15645 // transparent
15646 int nextFrame = (int) now;
15647 int framesCount = 0;
15648
15649 Interpolator interpolator = scrollBarInterpolator;
15650
15651 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015652 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015653
15654 // End transparent
15655 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015656 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015657
15658 state = FADING;
15659
15660 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015661 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015662 }
15663 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015664 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015665
Svetoslav Ganova0156172011-06-26 17:55:44 -070015666 /**
15667 * Resuable callback for sending
15668 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15669 */
15670 private class SendViewScrolledAccessibilityEvent implements Runnable {
15671 public volatile boolean mIsPending;
15672
15673 public void run() {
15674 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15675 mIsPending = false;
15676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015677 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015678
15679 /**
15680 * <p>
15681 * This class represents a delegate that can be registered in a {@link View}
15682 * to enhance accessibility support via composition rather via inheritance.
15683 * It is specifically targeted to widget developers that extend basic View
15684 * classes i.e. classes in package android.view, that would like their
15685 * applications to be backwards compatible.
15686 * </p>
15687 * <p>
15688 * A scenario in which a developer would like to use an accessibility delegate
15689 * is overriding a method introduced in a later API version then the minimal API
15690 * version supported by the application. For example, the method
15691 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15692 * in API version 4 when the accessibility APIs were first introduced. If a
15693 * developer would like his application to run on API version 4 devices (assuming
15694 * all other APIs used by the application are version 4 or lower) and take advantage
15695 * of this method, instead of overriding the method which would break the application's
15696 * backwards compatibility, he can override the corresponding method in this
15697 * delegate and register the delegate in the target View if the API version of
15698 * the system is high enough i.e. the API version is same or higher to the API
15699 * version that introduced
15700 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15701 * </p>
15702 * <p>
15703 * Here is an example implementation:
15704 * </p>
15705 * <code><pre><p>
15706 * if (Build.VERSION.SDK_INT >= 14) {
15707 * // If the API version is equal of higher than the version in
15708 * // which onInitializeAccessibilityNodeInfo was introduced we
15709 * // register a delegate with a customized implementation.
15710 * View view = findViewById(R.id.view_id);
15711 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15712 * public void onInitializeAccessibilityNodeInfo(View host,
15713 * AccessibilityNodeInfo info) {
15714 * // Let the default implementation populate the info.
15715 * super.onInitializeAccessibilityNodeInfo(host, info);
15716 * // Set some other information.
15717 * info.setEnabled(host.isEnabled());
15718 * }
15719 * });
15720 * }
15721 * </code></pre></p>
15722 * <p>
15723 * This delegate contains methods that correspond to the accessibility methods
15724 * in View. If a delegate has been specified the implementation in View hands
15725 * off handling to the corresponding method in this delegate. The default
15726 * implementation the delegate methods behaves exactly as the corresponding
15727 * method in View for the case of no accessibility delegate been set. Hence,
15728 * to customize the behavior of a View method, clients can override only the
15729 * corresponding delegate method without altering the behavior of the rest
15730 * accessibility related methods of the host view.
15731 * </p>
15732 */
15733 public static class AccessibilityDelegate {
15734
15735 /**
15736 * Sends an accessibility event of the given type. If accessibility is not
15737 * enabled this method has no effect.
15738 * <p>
15739 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15740 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15741 * been set.
15742 * </p>
15743 *
15744 * @param host The View hosting the delegate.
15745 * @param eventType The type of the event to send.
15746 *
15747 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15748 */
15749 public void sendAccessibilityEvent(View host, int eventType) {
15750 host.sendAccessibilityEventInternal(eventType);
15751 }
15752
15753 /**
15754 * Sends an accessibility event. This method behaves exactly as
15755 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15756 * empty {@link AccessibilityEvent} and does not perform a check whether
15757 * accessibility is enabled.
15758 * <p>
15759 * The default implementation behaves as
15760 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15761 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15762 * the case of no accessibility delegate been set.
15763 * </p>
15764 *
15765 * @param host The View hosting the delegate.
15766 * @param event The event to send.
15767 *
15768 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15769 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15770 */
15771 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15772 host.sendAccessibilityEventUncheckedInternal(event);
15773 }
15774
15775 /**
15776 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15777 * to its children for adding their text content to the event.
15778 * <p>
15779 * The default implementation behaves as
15780 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15781 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15782 * the case of no accessibility delegate been set.
15783 * </p>
15784 *
15785 * @param host The View hosting the delegate.
15786 * @param event The event.
15787 * @return True if the event population was completed.
15788 *
15789 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15790 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15791 */
15792 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15793 return host.dispatchPopulateAccessibilityEventInternal(event);
15794 }
15795
15796 /**
15797 * Gives a chance to the host View to populate the accessibility event with its
15798 * text content.
15799 * <p>
15800 * The default implementation behaves as
15801 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15802 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15803 * the case of no accessibility delegate been set.
15804 * </p>
15805 *
15806 * @param host The View hosting the delegate.
15807 * @param event The accessibility event which to populate.
15808 *
15809 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15810 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15811 */
15812 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15813 host.onPopulateAccessibilityEventInternal(event);
15814 }
15815
15816 /**
15817 * Initializes an {@link AccessibilityEvent} with information about the
15818 * the host View which is the event source.
15819 * <p>
15820 * The default implementation behaves as
15821 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15822 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15823 * the case of no accessibility delegate been set.
15824 * </p>
15825 *
15826 * @param host The View hosting the delegate.
15827 * @param event The event to initialize.
15828 *
15829 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15830 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15831 */
15832 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15833 host.onInitializeAccessibilityEventInternal(event);
15834 }
15835
15836 /**
15837 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15838 * <p>
15839 * The default implementation behaves as
15840 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15841 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15842 * the case of no accessibility delegate been set.
15843 * </p>
15844 *
15845 * @param host The View hosting the delegate.
15846 * @param info The instance to initialize.
15847 *
15848 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15849 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15850 */
15851 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15852 host.onInitializeAccessibilityNodeInfoInternal(info);
15853 }
15854
15855 /**
15856 * Called when a child of the host View has requested sending an
15857 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15858 * to augment the event.
15859 * <p>
15860 * The default implementation behaves as
15861 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15862 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15863 * the case of no accessibility delegate been set.
15864 * </p>
15865 *
15866 * @param host The View hosting the delegate.
15867 * @param child The child which requests sending the event.
15868 * @param event The event to be sent.
15869 * @return True if the event should be sent
15870 *
15871 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15872 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15873 */
15874 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15875 AccessibilityEvent event) {
15876 return host.onRequestSendAccessibilityEventInternal(child, event);
15877 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015878
15879 /**
15880 * Gets the provider for managing a virtual view hierarchy rooted at this View
15881 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15882 * that explore the window content.
15883 * <p>
15884 * The default implementation behaves as
15885 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15886 * the case of no accessibility delegate been set.
15887 * </p>
15888 *
15889 * @return The provider.
15890 *
15891 * @see AccessibilityNodeProvider
15892 */
15893 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15894 return null;
15895 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015897}