blob: 5d7c8cd15669c5cd68d30c3d99d8b8825eb5e55d [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.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070074import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070075
Doug Feltcb3791202011-07-07 11:57:48 -070076import com.android.internal.R;
77import com.android.internal.util.Predicate;
78import com.android.internal.view.menu.MenuBuilder;
79
Christopher Tatea0374192010-10-05 13:06:41 -070080import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070081import java.lang.reflect.InvocationTargetException;
82import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.ArrayList;
84import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070085import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080086import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/**
89 * <p>
90 * This class represents the basic building block for user interface components. A View
91 * occupies a rectangular area on the screen and is responsible for drawing and
92 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070093 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
95 * are invisible containers that hold other Views (or other ViewGroups) and define
96 * their layout properties.
97 * </p>
98 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070099 * <div class="special reference">
100 * <h3>Developer Guides</h3>
101 * <p>For information about using this class to develop your application's user interface,
102 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <a name="Using"></a>
106 * <h3>Using Views</h3>
107 * <p>
108 * All of the views in a window are arranged in a single tree. You can add views
109 * either from code or by specifying a tree of views in one or more XML layout
110 * files. There are many specialized subclasses of views that act as controls or
111 * are capable of displaying text, images, or other content.
112 * </p>
113 * <p>
114 * Once you have created a tree of views, there are typically a few types of
115 * common operations you may wish to perform:
116 * <ul>
117 * <li><strong>Set properties:</strong> for example setting the text of a
118 * {@link android.widget.TextView}. The available properties and the methods
119 * that set them will vary among the different subclasses of views. Note that
120 * properties that are known at build time can be set in the XML layout
121 * files.</li>
122 * <li><strong>Set focus:</strong> The framework will handled moving focus in
123 * response to user input. To force focus to a specific view, call
124 * {@link #requestFocus}.</li>
125 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
126 * that will be notified when something interesting happens to the view. For
127 * example, all views will let you set a listener to be notified when the view
128 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700129 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700130 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * </ul>
135 * </p>
136 * <p><em>
137 * Note: The Android framework is responsible for measuring, laying out and
138 * drawing views. You should not call methods that perform these actions on
139 * views yourself unless you are actually implementing a
140 * {@link android.view.ViewGroup}.
141 * </em></p>
142 *
143 * <a name="Lifecycle"></a>
144 * <h3>Implementing a Custom View</h3>
145 *
146 * <p>
147 * To implement a custom view, you will usually begin by providing overrides for
148 * some of the standard methods that the framework calls on all views. You do
149 * not need to override all of these methods. In fact, you can start by just
150 * overriding {@link #onDraw(android.graphics.Canvas)}.
151 * <table border="2" width="85%" align="center" cellpadding="5">
152 * <thead>
153 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
154 * </thead>
155 *
156 * <tbody>
157 * <tr>
158 * <td rowspan="2">Creation</td>
159 * <td>Constructors</td>
160 * <td>There is a form of the constructor that are called when the view
161 * is created from code and a form that is called when the view is
162 * inflated from a layout file. The second form should parse and apply
163 * any attributes defined in the layout file.
164 * </td>
165 * </tr>
166 * <tr>
167 * <td><code>{@link #onFinishInflate()}</code></td>
168 * <td>Called after a view and all of its children has been inflated
169 * from XML.</td>
170 * </tr>
171 *
172 * <tr>
173 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700174 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * <td>Called to determine the size requirements for this view and all
176 * of its children.
177 * </td>
178 * </tr>
179 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called when this view should assign a size and position to all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when the size of this view has changed.
188 * </td>
189 * </tr>
190 *
191 * <tr>
192 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when the view should render its content.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when a new key event occurs.
202 * </td>
203 * </tr>
204 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700205 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * <td>Called when a key up event occurs.
207 * </td>
208 * </tr>
209 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700210 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * <td>Called when a trackball motion event occurs.
212 * </td>
213 * </tr>
214 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700215 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * <td>Called when a touch screen motion event occurs.
217 * </td>
218 * </tr>
219 *
220 * <tr>
221 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when the view gains or loses focus.
224 * </td>
225 * </tr>
226 *
227 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the window containing the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="3">Attaching</td>
235 * <td><code>{@link #onAttachedToWindow()}</code></td>
236 * <td>Called when the view is attached to a window.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td><code>{@link #onDetachedFromWindow}</code></td>
242 * <td>Called when the view is detached from its window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700247 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * <td>Called when the visibility of the window containing the view
249 * has changed.
250 * </td>
251 * </tr>
252 * </tbody>
253 *
254 * </table>
255 * </p>
256 *
257 * <a name="IDs"></a>
258 * <h3>IDs</h3>
259 * Views may have an integer id associated with them. These ids are typically
260 * assigned in the layout XML files, and are used to find specific views within
261 * the view tree. A common pattern is to:
262 * <ul>
263 * <li>Define a Button in the layout file and assign it a unique ID.
264 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700265 * &lt;Button
266 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * android:layout_width="wrap_content"
268 * android:layout_height="wrap_content"
269 * android:text="@string/my_button_text"/&gt;
270 * </pre></li>
271 * <li>From the onCreate method of an Activity, find the Button
272 * <pre class="prettyprint">
273 * Button myButton = (Button) findViewById(R.id.my_button);
274 * </pre></li>
275 * </ul>
276 * <p>
277 * View IDs need not be unique throughout the tree, but it is good practice to
278 * ensure that they are at least unique within the part of the tree you are
279 * searching.
280 * </p>
281 *
282 * <a name="Position"></a>
283 * <h3>Position</h3>
284 * <p>
285 * The geometry of a view is that of a rectangle. A view has a location,
286 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
287 * two dimensions, expressed as a width and a height. The unit for location
288 * and dimensions is the pixel.
289 * </p>
290 *
291 * <p>
292 * It is possible to retrieve the location of a view by invoking the methods
293 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
294 * coordinate of the rectangle representing the view. The latter returns the
295 * top, or Y, coordinate of the rectangle representing the view. These methods
296 * both return the location of the view relative to its parent. For instance,
297 * when getLeft() returns 20, that means the view is located 20 pixels to the
298 * right of the left edge of its direct parent.
299 * </p>
300 *
301 * <p>
302 * In addition, several convenience methods are offered to avoid unnecessary
303 * computations, namely {@link #getRight()} and {@link #getBottom()}.
304 * These methods return the coordinates of the right and bottom edges of the
305 * rectangle representing the view. For instance, calling {@link #getRight()}
306 * is similar to the following computation: <code>getLeft() + getWidth()</code>
307 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
308 * </p>
309 *
310 * <a name="SizePaddingMargins"></a>
311 * <h3>Size, padding and margins</h3>
312 * <p>
313 * The size of a view is expressed with a width and a height. A view actually
314 * possess two pairs of width and height values.
315 * </p>
316 *
317 * <p>
318 * The first pair is known as <em>measured width</em> and
319 * <em>measured height</em>. These dimensions define how big a view wants to be
320 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
321 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
322 * and {@link #getMeasuredHeight()}.
323 * </p>
324 *
325 * <p>
326 * The second pair is simply known as <em>width</em> and <em>height</em>, or
327 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
328 * dimensions define the actual size of the view on screen, at drawing time and
329 * after layout. These values may, but do not have to, be different from the
330 * measured width and height. The width and height can be obtained by calling
331 * {@link #getWidth()} and {@link #getHeight()}.
332 * </p>
333 *
334 * <p>
335 * To measure its dimensions, a view takes into account its padding. The padding
336 * is expressed in pixels for the left, top, right and bottom parts of the view.
337 * Padding can be used to offset the content of the view by a specific amount of
338 * pixels. For instance, a left padding of 2 will push the view's content by
339 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700340 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
341 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
342 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
343 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 * </p>
345 *
346 * <p>
347 * Even though a view can define a padding, it does not provide any support for
348 * margins. However, view groups provide such a support. Refer to
349 * {@link android.view.ViewGroup} and
350 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
351 * </p>
352 *
353 * <a name="Layout"></a>
354 * <h3>Layout</h3>
355 * <p>
356 * Layout is a two pass process: a measure pass and a layout pass. The measuring
357 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
358 * of the view tree. Each view pushes dimension specifications down the tree
359 * during the recursion. At the end of the measure pass, every view has stored
360 * its measurements. The second pass happens in
361 * {@link #layout(int,int,int,int)} and is also top-down. During
362 * this pass each parent is responsible for positioning all of its children
363 * using the sizes computed in the measure pass.
364 * </p>
365 *
366 * <p>
367 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
368 * {@link #getMeasuredHeight()} values must be set, along with those for all of
369 * that view's descendants. A view's measured width and measured height values
370 * must respect the constraints imposed by the view's parents. This guarantees
371 * that at the end of the measure pass, all parents accept all of their
372 * children's measurements. A parent view may call measure() more than once on
373 * its children. For example, the parent may measure each child once with
374 * unspecified dimensions to find out how big they want to be, then call
375 * measure() on them again with actual numbers if the sum of all the children's
376 * unconstrained sizes is too big or too small.
377 * </p>
378 *
379 * <p>
380 * The measure pass uses two classes to communicate dimensions. The
381 * {@link MeasureSpec} class is used by views to tell their parents how they
382 * want to be measured and positioned. The base LayoutParams class just
383 * describes how big the view wants to be for both width and height. For each
384 * dimension, it can specify one of:
385 * <ul>
386 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800387 * <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 -0800388 * (minus padding)
389 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
390 * enclose its content (plus padding).
391 * </ul>
392 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
393 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
394 * an X and Y value.
395 * </p>
396 *
397 * <p>
398 * MeasureSpecs are used to push requirements down the tree from parent to
399 * child. A MeasureSpec can be in one of three modes:
400 * <ul>
401 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
402 * of a child view. For example, a LinearLayout may call measure() on its child
403 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
404 * tall the child view wants to be given a width of 240 pixels.
405 * <li>EXACTLY: This is used by the parent to impose an exact size on the
406 * child. The child must use this size, and guarantee that all of its
407 * descendants will fit within this size.
408 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
409 * child. The child must gurantee that it and all of its descendants will fit
410 * within this size.
411 * </ul>
412 * </p>
413 *
414 * <p>
415 * To intiate a layout, call {@link #requestLayout}. This method is typically
416 * called by a view on itself when it believes that is can no longer fit within
417 * its current bounds.
418 * </p>
419 *
420 * <a name="Drawing"></a>
421 * <h3>Drawing</h3>
422 * <p>
423 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700424 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * this means that parents will draw before (i.e., behind) their children, with
426 * siblings drawn in the order they appear in the tree.
427 * If you set a background drawable for a View, then the View will draw it for you
428 * before calling back to its <code>onDraw()</code> method.
429 * </p>
430 *
431 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700432 * 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 -0800433 * </p>
434 *
435 * <p>
436 * To force a view to draw, call {@link #invalidate()}.
437 * </p>
438 *
439 * <a name="EventHandlingThreading"></a>
440 * <h3>Event Handling and Threading</h3>
441 * <p>
442 * The basic cycle of a view is as follows:
443 * <ol>
444 * <li>An event comes in and is dispatched to the appropriate view. The view
445 * handles the event and notifies any listeners.</li>
446 * <li>If in the course of processing the event, the view's bounds may need
447 * to be changed, the view will call {@link #requestLayout()}.</li>
448 * <li>Similarly, if in the course of processing the event the view's appearance
449 * may need to be changed, the view will call {@link #invalidate()}.</li>
450 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
451 * the framework will take care of measuring, laying out, and drawing the tree
452 * as appropriate.</li>
453 * </ol>
454 * </p>
455 *
456 * <p><em>Note: The entire view tree is single threaded. You must always be on
457 * the UI thread when calling any method on any view.</em>
458 * If you are doing work on other threads and want to update the state of a view
459 * from that thread, you should use a {@link Handler}.
460 * </p>
461 *
462 * <a name="FocusHandling"></a>
463 * <h3>Focus Handling</h3>
464 * <p>
465 * The framework will handle routine focus movement in response to user input.
466 * This includes changing the focus as views are removed or hidden, or as new
467 * views become available. Views indicate their willingness to take focus
468 * through the {@link #isFocusable} method. To change whether a view can take
469 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
470 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
471 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
472 * </p>
473 * <p>
474 * Focus movement is based on an algorithm which finds the nearest neighbor in a
475 * given direction. In rare cases, the default algorithm may not match the
476 * intended behavior of the developer. In these situations, you can provide
477 * explicit overrides by using these XML attributes in the layout file:
478 * <pre>
479 * nextFocusDown
480 * nextFocusLeft
481 * nextFocusRight
482 * nextFocusUp
483 * </pre>
484 * </p>
485 *
486 *
487 * <p>
488 * To get a particular view to take focus, call {@link #requestFocus()}.
489 * </p>
490 *
491 * <a name="TouchMode"></a>
492 * <h3>Touch Mode</h3>
493 * <p>
494 * When a user is navigating a user interface via directional keys such as a D-pad, it is
495 * necessary to give focus to actionable items such as buttons so the user can see
496 * what will take input. If the device has touch capabilities, however, and the user
497 * begins interacting with the interface by touching it, it is no longer necessary to
498 * always highlight, or give focus to, a particular view. This motivates a mode
499 * for interaction named 'touch mode'.
500 * </p>
501 * <p>
502 * For a touch capable device, once the user touches the screen, the device
503 * will enter touch mode. From this point onward, only views for which
504 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
505 * Other views that are touchable, like buttons, will not take focus when touched; they will
506 * only fire the on click listeners.
507 * </p>
508 * <p>
509 * Any time a user hits a directional key, such as a D-pad direction, the view device will
510 * exit touch mode, and find a view to take focus, so that the user may resume interacting
511 * with the user interface without touching the screen again.
512 * </p>
513 * <p>
514 * The touch mode state is maintained across {@link android.app.Activity}s. Call
515 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
516 * </p>
517 *
518 * <a name="Scrolling"></a>
519 * <h3>Scrolling</h3>
520 * <p>
521 * The framework provides basic support for views that wish to internally
522 * scroll their content. This includes keeping track of the X and Y scroll
523 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800524 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700525 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * </p>
527 *
528 * <a name="Tags"></a>
529 * <h3>Tags</h3>
530 * <p>
531 * Unlike IDs, tags are not used to identify views. Tags are essentially an
532 * extra piece of information that can be associated with a view. They are most
533 * often used as a convenience to store data related to views in the views
534 * themselves rather than by putting them in a separate structure.
535 * </p>
536 *
537 * <a name="Animation"></a>
538 * <h3>Animation</h3>
539 * <p>
540 * You can attach an {@link Animation} object to a view using
541 * {@link #setAnimation(Animation)} or
542 * {@link #startAnimation(Animation)}. The animation can alter the scale,
543 * rotation, translation and alpha of a view over time. If the animation is
544 * attached to a view that has children, the animation will affect the entire
545 * subtree rooted by that node. When an animation is started, the framework will
546 * take care of redrawing the appropriate views until the animation completes.
547 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800548 * <p>
549 * Starting with Android 3.0, the preferred way of animating views is to use the
550 * {@link android.animation} package APIs.
551 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 *
Jeff Brown85a31762010-09-01 17:01:00 -0700553 * <a name="Security"></a>
554 * <h3>Security</h3>
555 * <p>
556 * Sometimes it is essential that an application be able to verify that an action
557 * is being performed with the full knowledge and consent of the user, such as
558 * granting a permission request, making a purchase or clicking on an advertisement.
559 * Unfortunately, a malicious application could try to spoof the user into
560 * performing these actions, unaware, by concealing the intended purpose of the view.
561 * As a remedy, the framework offers a touch filtering mechanism that can be used to
562 * improve the security of views that provide access to sensitive functionality.
563 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700564 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800565 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700566 * will discard touches that are received whenever the view's window is obscured by
567 * another visible window. As a result, the view will not receive touches whenever a
568 * toast, dialog or other window appears above the view's window.
569 * </p><p>
570 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700571 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
572 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700573 * </p>
574 *
Romain Guy171c5922011-01-06 10:04:23 -0800575 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700576 * @attr ref android.R.styleable#View_background
577 * @attr ref android.R.styleable#View_clickable
578 * @attr ref android.R.styleable#View_contentDescription
579 * @attr ref android.R.styleable#View_drawingCacheQuality
580 * @attr ref android.R.styleable#View_duplicateParentState
581 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700582 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700583 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800606 * @attr ref android.R.styleable#View_paddingStart
607 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800609 * @attr ref android.R.styleable#View_rotation
610 * @attr ref android.R.styleable#View_rotationX
611 * @attr ref android.R.styleable#View_rotationY
612 * @attr ref android.R.styleable#View_scaleX
613 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * @attr ref android.R.styleable#View_scrollX
615 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700616 * @attr ref android.R.styleable#View_scrollbarSize
617 * @attr ref android.R.styleable#View_scrollbarStyle
618 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700619 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
620 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
622 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarThumbVertical
624 * @attr ref android.R.styleable#View_scrollbarTrackVertical
625 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
626 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700627 * @attr ref android.R.styleable#View_soundEffectsEnabled
628 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700629 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800630 * @attr ref android.R.styleable#View_transformPivotX
631 * @attr ref android.R.styleable#View_transformPivotY
632 * @attr ref android.R.styleable#View_translationX
633 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700634 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 *
636 * @see android.view.ViewGroup
637 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700638public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
639 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 private static final boolean DBG = false;
641
642 /**
643 * The logging tag used by this class with android.util.Log.
644 */
645 protected static final String VIEW_LOG_TAG = "View";
646
647 /**
648 * Used to mark a View that has no ID.
649 */
650 public static final int NO_ID = -1;
651
652 /**
653 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
654 * calling setFlags.
655 */
656 private static final int NOT_FOCUSABLE = 0x00000000;
657
658 /**
659 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
660 * setFlags.
661 */
662 private static final int FOCUSABLE = 0x00000001;
663
664 /**
665 * Mask for use with setFlags indicating bits used for focus.
666 */
667 private static final int FOCUSABLE_MASK = 0x00000001;
668
669 /**
670 * This view will adjust its padding to fit sytem windows (e.g. status bar)
671 */
672 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
673
674 /**
Scott Main812634c22011-07-27 13:22:35 -0700675 * This view is visible.
676 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
677 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public static final int VISIBLE = 0x00000000;
680
681 /**
682 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700683 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
684 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 */
686 public static final int INVISIBLE = 0x00000004;
687
688 /**
689 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700690 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
691 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 */
693 public static final int GONE = 0x00000008;
694
695 /**
696 * Mask for use with setFlags indicating bits used for visibility.
697 * {@hide}
698 */
699 static final int VISIBILITY_MASK = 0x0000000C;
700
701 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
702
703 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700704 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 * Use with ENABLED_MASK when calling setFlags.
706 * {@hide}
707 */
708 static final int ENABLED = 0x00000000;
709
710 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700711 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 * Use with ENABLED_MASK when calling setFlags.
713 * {@hide}
714 */
715 static final int DISABLED = 0x00000020;
716
717 /**
718 * Mask for use with setFlags indicating bits used for indicating whether
719 * this view is enabled
720 * {@hide}
721 */
722 static final int ENABLED_MASK = 0x00000020;
723
724 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700725 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
726 * called and further optimizations will be performed. It is okay to have
727 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 * {@hide}
729 */
730 static final int WILL_NOT_DRAW = 0x00000080;
731
732 /**
733 * Mask for use with setFlags indicating bits used for indicating whether
734 * this view is will draw
735 * {@hide}
736 */
737 static final int DRAW_MASK = 0x00000080;
738
739 /**
740 * <p>This view doesn't show scrollbars.</p>
741 * {@hide}
742 */
743 static final int SCROLLBARS_NONE = 0x00000000;
744
745 /**
746 * <p>This view shows horizontal scrollbars.</p>
747 * {@hide}
748 */
749 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
750
751 /**
752 * <p>This view shows vertical scrollbars.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_VERTICAL = 0x00000200;
756
757 /**
758 * <p>Mask for use with setFlags indicating bits used for indicating which
759 * scrollbars are enabled.</p>
760 * {@hide}
761 */
762 static final int SCROLLBARS_MASK = 0x00000300;
763
Jeff Brown85a31762010-09-01 17:01:00 -0700764 /**
765 * Indicates that the view should filter touches when its window is obscured.
766 * Refer to the class comments for more information about this security feature.
767 * {@hide}
768 */
769 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
770
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700771 /**
772 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
773 * that they are optional and should be skipped if the window has
774 * requested system UI flags that ignore those insets for layout.
775 */
776 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777
778 /**
779 * <p>This view doesn't show fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_NONE = 0x00000000;
783
784 /**
785 * <p>This view shows horizontal fading edges.</p>
786 * {@hide}
787 */
788 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
789
790 /**
791 * <p>This view shows vertical fading edges.</p>
792 * {@hide}
793 */
794 static final int FADING_EDGE_VERTICAL = 0x00002000;
795
796 /**
797 * <p>Mask for use with setFlags indicating bits used for indicating which
798 * fading edges are enabled.</p>
799 * {@hide}
800 */
801 static final int FADING_EDGE_MASK = 0x00003000;
802
803 /**
804 * <p>Indicates this view can be clicked. When clickable, a View reacts
805 * to clicks by notifying the OnClickListener.<p>
806 * {@hide}
807 */
808 static final int CLICKABLE = 0x00004000;
809
810 /**
811 * <p>Indicates this view is caching its drawing into a bitmap.</p>
812 * {@hide}
813 */
814 static final int DRAWING_CACHE_ENABLED = 0x00008000;
815
816 /**
817 * <p>Indicates that no icicle should be saved for this view.<p>
818 * {@hide}
819 */
820 static final int SAVE_DISABLED = 0x000010000;
821
822 /**
823 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
824 * property.</p>
825 * {@hide}
826 */
827 static final int SAVE_DISABLED_MASK = 0x000010000;
828
829 /**
830 * <p>Indicates that no drawing cache should ever be created for this view.<p>
831 * {@hide}
832 */
833 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
834
835 /**
836 * <p>Indicates this view can take / keep focus when int touch mode.</p>
837 * {@hide}
838 */
839 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
840
841 /**
842 * <p>Enables low quality mode for the drawing cache.</p>
843 */
844 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
845
846 /**
847 * <p>Enables high quality mode for the drawing cache.</p>
848 */
849 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
850
851 /**
852 * <p>Enables automatic quality mode for the drawing cache.</p>
853 */
854 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
855
856 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
857 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
858 };
859
860 /**
861 * <p>Mask for use with setFlags indicating bits used for the cache
862 * quality property.</p>
863 * {@hide}
864 */
865 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
866
867 /**
868 * <p>
869 * Indicates this view can be long clicked. When long clickable, a View
870 * reacts to long clicks by notifying the OnLongClickListener or showing a
871 * context menu.
872 * </p>
873 * {@hide}
874 */
875 static final int LONG_CLICKABLE = 0x00200000;
876
877 /**
878 * <p>Indicates that this view gets its drawable states from its direct parent
879 * and ignores its original internal states.</p>
880 *
881 * @hide
882 */
883 static final int DUPLICATE_PARENT_STATE = 0x00400000;
884
885 /**
886 * The scrollbar style to display the scrollbars inside the content area,
887 * without increasing the padding. The scrollbars will be overlaid with
888 * translucency on the view's content.
889 */
890 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
891
892 /**
893 * The scrollbar style to display the scrollbars inside the padded area,
894 * increasing the padding of the view. The scrollbars will not overlap the
895 * content area of the view.
896 */
897 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
898
899 /**
900 * The scrollbar style to display the scrollbars at the edge of the view,
901 * without increasing the padding. The scrollbars will be overlaid with
902 * translucency.
903 */
904 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
905
906 /**
907 * The scrollbar style to display the scrollbars at the edge of the view,
908 * increasing the padding of the view. The scrollbars will only overlap the
909 * background, if any.
910 */
911 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
912
913 /**
914 * Mask to check if the scrollbar style is overlay or inset.
915 * {@hide}
916 */
917 static final int SCROLLBARS_INSET_MASK = 0x01000000;
918
919 /**
920 * Mask to check if the scrollbar style is inside or outside.
921 * {@hide}
922 */
923 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
924
925 /**
926 * Mask for scrollbar style.
927 * {@hide}
928 */
929 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
930
931 /**
932 * View flag indicating that the screen should remain on while the
933 * window containing this view is visible to the user. This effectively
934 * takes care of automatically setting the WindowManager's
935 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
936 */
937 public static final int KEEP_SCREEN_ON = 0x04000000;
938
939 /**
940 * View flag indicating whether this view should have sound effects enabled
941 * for events such as clicking and touching.
942 */
943 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
944
945 /**
946 * View flag indicating whether this view should have haptic feedback
947 * enabled for events such as long presses.
948 */
949 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
950
951 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700952 * <p>Indicates that the view hierarchy should stop saving state when
953 * it reaches this view. If state saving is initiated immediately at
954 * the view, it will be allowed.
955 * {@hide}
956 */
957 static final int PARENT_SAVE_DISABLED = 0x20000000;
958
959 /**
960 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
961 * {@hide}
962 */
963 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
964
965 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700966 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
967 * should add all focusable Views regardless if they are focusable in touch mode.
968 */
969 public static final int FOCUSABLES_ALL = 0x00000000;
970
971 /**
972 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
973 * should add only Views focusable in touch mode.
974 */
975 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
976
977 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700978 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 * item.
980 */
981 public static final int FOCUS_BACKWARD = 0x00000001;
982
983 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700984 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 * item.
986 */
987 public static final int FOCUS_FORWARD = 0x00000002;
988
989 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700990 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 */
992 public static final int FOCUS_LEFT = 0x00000011;
993
994 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700995 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 */
997 public static final int FOCUS_UP = 0x00000021;
998
999 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001000 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 */
1002 public static final int FOCUS_RIGHT = 0x00000042;
1003
1004 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001005 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 */
1007 public static final int FOCUS_DOWN = 0x00000082;
1008
1009 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001010 * Bits of {@link #getMeasuredWidthAndState()} and
1011 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1012 */
1013 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1014
1015 /**
1016 * Bits of {@link #getMeasuredWidthAndState()} and
1017 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1018 */
1019 public static final int MEASURED_STATE_MASK = 0xff000000;
1020
1021 /**
1022 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1023 * for functions that combine both width and height into a single int,
1024 * such as {@link #getMeasuredState()} and the childState argument of
1025 * {@link #resolveSizeAndState(int, int, int)}.
1026 */
1027 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1028
1029 /**
1030 * Bit of {@link #getMeasuredWidthAndState()} and
1031 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1032 * is smaller that the space the view would like to have.
1033 */
1034 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1035
1036 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 * Base View state sets
1038 */
1039 // Singles
1040 /**
1041 * Indicates the view has no states set. States are used with
1042 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1043 * view depending on its state.
1044 *
1045 * @see android.graphics.drawable.Drawable
1046 * @see #getDrawableState()
1047 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001048 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 /**
1050 * Indicates the view is enabled. States are used with
1051 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1052 * view depending on its state.
1053 *
1054 * @see android.graphics.drawable.Drawable
1055 * @see #getDrawableState()
1056 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001057 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 /**
1059 * Indicates the view is focused. States are used with
1060 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1061 * view depending on its state.
1062 *
1063 * @see android.graphics.drawable.Drawable
1064 * @see #getDrawableState()
1065 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001066 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 /**
1068 * Indicates the view is selected. States are used with
1069 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1070 * view depending on its state.
1071 *
1072 * @see android.graphics.drawable.Drawable
1073 * @see #getDrawableState()
1074 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001075 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 /**
1077 * Indicates the view is pressed. States are used with
1078 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1079 * view depending on its state.
1080 *
1081 * @see android.graphics.drawable.Drawable
1082 * @see #getDrawableState()
1083 * @hide
1084 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001085 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 /**
1087 * Indicates the view's window has focus. States are used with
1088 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1089 * view depending on its state.
1090 *
1091 * @see android.graphics.drawable.Drawable
1092 * @see #getDrawableState()
1093 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001094 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 // Doubles
1096 /**
1097 * Indicates the view is enabled and has the focus.
1098 *
1099 * @see #ENABLED_STATE_SET
1100 * @see #FOCUSED_STATE_SET
1101 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001102 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 /**
1104 * Indicates the view is enabled and selected.
1105 *
1106 * @see #ENABLED_STATE_SET
1107 * @see #SELECTED_STATE_SET
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view is enabled and that its window has focus.
1112 *
1113 * @see #ENABLED_STATE_SET
1114 * @see #WINDOW_FOCUSED_STATE_SET
1115 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001116 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 /**
1118 * Indicates the view is focused and selected.
1119 *
1120 * @see #FOCUSED_STATE_SET
1121 * @see #SELECTED_STATE_SET
1122 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001123 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 /**
1125 * Indicates the view has the focus and that its window has the focus.
1126 *
1127 * @see #FOCUSED_STATE_SET
1128 * @see #WINDOW_FOCUSED_STATE_SET
1129 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001130 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 /**
1132 * Indicates the view is selected and that its window has the focus.
1133 *
1134 * @see #SELECTED_STATE_SET
1135 * @see #WINDOW_FOCUSED_STATE_SET
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 // Triples
1139 /**
1140 * Indicates the view is enabled, focused and selected.
1141 *
1142 * @see #ENABLED_STATE_SET
1143 * @see #FOCUSED_STATE_SET
1144 * @see #SELECTED_STATE_SET
1145 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001146 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 /**
1148 * Indicates the view is enabled, focused and its window has the focus.
1149 *
1150 * @see #ENABLED_STATE_SET
1151 * @see #FOCUSED_STATE_SET
1152 * @see #WINDOW_FOCUSED_STATE_SET
1153 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001154 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 /**
1156 * Indicates the view is enabled, selected and its window has the focus.
1157 *
1158 * @see #ENABLED_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[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view is focused, selected and its window has the focus.
1165 *
1166 * @see #FOCUSED_STATE_SET
1167 * @see #SELECTED_STATE_SET
1168 * @see #WINDOW_FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 /**
1172 * Indicates the view is enabled, focused, selected and its window
1173 * has the focus.
1174 *
1175 * @see #ENABLED_STATE_SET
1176 * @see #FOCUSED_STATE_SET
1177 * @see #SELECTED_STATE_SET
1178 * @see #WINDOW_FOCUSED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 /**
1182 * Indicates the view is pressed and its window has the focus.
1183 *
1184 * @see #PRESSED_STATE_SET
1185 * @see #WINDOW_FOCUSED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is pressed and selected.
1190 *
1191 * @see #PRESSED_STATE_SET
1192 * @see #SELECTED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is pressed, selected and its window has the focus.
1197 *
1198 * @see #PRESSED_STATE_SET
1199 * @see #SELECTED_STATE_SET
1200 * @see #WINDOW_FOCUSED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is pressed and focused.
1205 *
1206 * @see #PRESSED_STATE_SET
1207 * @see #FOCUSED_STATE_SET
1208 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001209 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 /**
1211 * Indicates the view is pressed, focused and its window has the focus.
1212 *
1213 * @see #PRESSED_STATE_SET
1214 * @see #FOCUSED_STATE_SET
1215 * @see #WINDOW_FOCUSED_STATE_SET
1216 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001217 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 /**
1219 * Indicates the view is pressed, focused and selected.
1220 *
1221 * @see #PRESSED_STATE_SET
1222 * @see #SELECTED_STATE_SET
1223 * @see #FOCUSED_STATE_SET
1224 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001225 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 /**
1227 * Indicates the view is pressed, focused, selected and its window has the focus.
1228 *
1229 * @see #PRESSED_STATE_SET
1230 * @see #FOCUSED_STATE_SET
1231 * @see #SELECTED_STATE_SET
1232 * @see #WINDOW_FOCUSED_STATE_SET
1233 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001234 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 /**
1236 * Indicates the view is pressed and enabled.
1237 *
1238 * @see #PRESSED_STATE_SET
1239 * @see #ENABLED_STATE_SET
1240 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001241 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 /**
1243 * Indicates the view is pressed, enabled and its window has the focus.
1244 *
1245 * @see #PRESSED_STATE_SET
1246 * @see #ENABLED_STATE_SET
1247 * @see #WINDOW_FOCUSED_STATE_SET
1248 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001249 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 /**
1251 * Indicates the view is pressed, enabled and selected.
1252 *
1253 * @see #PRESSED_STATE_SET
1254 * @see #ENABLED_STATE_SET
1255 * @see #SELECTED_STATE_SET
1256 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001257 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 /**
1259 * Indicates the view is pressed, enabled, selected and its window has the
1260 * focus.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #ENABLED_STATE_SET
1264 * @see #SELECTED_STATE_SET
1265 * @see #WINDOW_FOCUSED_STATE_SET
1266 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001267 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * Indicates the view is pressed, enabled and focused.
1270 *
1271 * @see #PRESSED_STATE_SET
1272 * @see #ENABLED_STATE_SET
1273 * @see #FOCUSED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed, enabled, focused and its window has the
1278 * focus.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #ENABLED_STATE_SET
1282 * @see #FOCUSED_STATE_SET
1283 * @see #WINDOW_FOCUSED_STATE_SET
1284 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001285 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 /**
1287 * Indicates the view is pressed, enabled, focused and selected.
1288 *
1289 * @see #PRESSED_STATE_SET
1290 * @see #ENABLED_STATE_SET
1291 * @see #SELECTED_STATE_SET
1292 * @see #FOCUSED_STATE_SET
1293 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001294 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 /**
1296 * Indicates the view is pressed, enabled, focused, selected and its window
1297 * has the focus.
1298 *
1299 * @see #PRESSED_STATE_SET
1300 * @see #ENABLED_STATE_SET
1301 * @see #SELECTED_STATE_SET
1302 * @see #FOCUSED_STATE_SET
1303 * @see #WINDOW_FOCUSED_STATE_SET
1304 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001305 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306
1307 /**
1308 * The order here is very important to {@link #getDrawableState()}
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 private static final int[][] VIEW_STATE_SETS;
1311
Romain Guyb051e892010-09-28 19:09:36 -07001312 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1313 static final int VIEW_STATE_SELECTED = 1 << 1;
1314 static final int VIEW_STATE_FOCUSED = 1 << 2;
1315 static final int VIEW_STATE_ENABLED = 1 << 3;
1316 static final int VIEW_STATE_PRESSED = 1 << 4;
1317 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001318 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001319 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001320 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1321 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001322
1323 static final int[] VIEW_STATE_IDS = new int[] {
1324 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1325 R.attr.state_selected, VIEW_STATE_SELECTED,
1326 R.attr.state_focused, VIEW_STATE_FOCUSED,
1327 R.attr.state_enabled, VIEW_STATE_ENABLED,
1328 R.attr.state_pressed, VIEW_STATE_PRESSED,
1329 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001330 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001331 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001332 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1333 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 };
1335
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001337 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1338 throw new IllegalStateException(
1339 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1340 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001342 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001344 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001346 orderedIds[i * 2] = viewState;
1347 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001348 }
1349 }
1350 }
Romain Guyb051e892010-09-28 19:09:36 -07001351 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1352 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1353 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001354 int numBits = Integer.bitCount(i);
1355 int[] set = new int[numBits];
1356 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001357 for (int j = 0; j < orderedIds.length; j += 2) {
1358 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 set[pos++] = orderedIds[j];
1360 }
1361 }
1362 VIEW_STATE_SETS[i] = set;
1363 }
1364
1365 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1366 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1367 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1368 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1370 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1371 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1372 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1373 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1374 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1375 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1376 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1377 | VIEW_STATE_FOCUSED];
1378 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1379 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1380 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1381 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1382 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1383 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1385 | VIEW_STATE_ENABLED];
1386 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1387 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1388 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1390 | VIEW_STATE_ENABLED];
1391 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1393 | VIEW_STATE_ENABLED];
1394 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1395 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1396 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1397
1398 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1399 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1401 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1403 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1405 | VIEW_STATE_PRESSED];
1406 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1408 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1410 | VIEW_STATE_PRESSED];
1411 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1413 | VIEW_STATE_PRESSED];
1414 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1416 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1417 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1421 | VIEW_STATE_PRESSED];
1422 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1424 | VIEW_STATE_PRESSED];
1425 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1427 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1428 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1430 | VIEW_STATE_PRESSED];
1431 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1434 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1436 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1437 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1439 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1440 | VIEW_STATE_PRESSED];
1441 }
1442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001444 * Accessibility event types that are dispatched for text population.
1445 */
1446 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1447 AccessibilityEvent.TYPE_VIEW_CLICKED
1448 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1449 | AccessibilityEvent.TYPE_VIEW_SELECTED
1450 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1451 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1452 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001453 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001454 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1455 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001456
1457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 * Temporary Rect currently for use in setBackground(). This will probably
1459 * be extended in the future to hold our own class with more than just
1460 * a Rect. :)
1461 */
1462 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001463
1464 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001465 * Temporary flag, used to enable processing of View properties in the native DisplayList
1466 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1467 * apps.
1468 * @hide
1469 */
Chet Haase8d56b0e2012-04-02 16:34:48 -07001470 public static final boolean USE_DISPLAY_LIST_PROPERTIES = true;
Chet Haasea1cff502012-02-21 13:43:44 -08001471
1472 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001473 * Map used to store views' tags.
1474 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001475 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001478 * The next available accessiiblity id.
1479 */
1480 private static int sNextAccessibilityViewId;
1481
1482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 * The animation currently associated with this view.
1484 * @hide
1485 */
1486 protected Animation mCurrentAnimation = null;
1487
1488 /**
1489 * Width as measured during measure pass.
1490 * {@hide}
1491 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001492 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001493 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494
1495 /**
1496 * Height as measured during measure pass.
1497 * {@hide}
1498 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001499 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001500 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501
1502 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001503 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1504 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1505 * its display list. This flag, used only when hw accelerated, allows us to clear the
1506 * flag while retaining this information until it's needed (at getDisplayList() time and
1507 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1508 *
1509 * {@hide}
1510 */
1511 boolean mRecreateDisplayList = false;
1512
1513 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 * The view's identifier.
1515 * {@hide}
1516 *
1517 * @see #setId(int)
1518 * @see #getId()
1519 */
1520 @ViewDebug.ExportedProperty(resolveId = true)
1521 int mID = NO_ID;
1522
1523 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001524 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001525 */
1526 int mAccessibilityViewId = NO_ID;
1527
1528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 * The view's tag.
1530 * {@hide}
1531 *
1532 * @see #setTag(Object)
1533 * @see #getTag()
1534 */
1535 protected Object mTag;
1536
1537 // for mPrivateFlags:
1538 /** {@hide} */
1539 static final int WANTS_FOCUS = 0x00000001;
1540 /** {@hide} */
1541 static final int FOCUSED = 0x00000002;
1542 /** {@hide} */
1543 static final int SELECTED = 0x00000004;
1544 /** {@hide} */
1545 static final int IS_ROOT_NAMESPACE = 0x00000008;
1546 /** {@hide} */
1547 static final int HAS_BOUNDS = 0x00000010;
1548 /** {@hide} */
1549 static final int DRAWN = 0x00000020;
1550 /**
1551 * When this flag is set, this view is running an animation on behalf of its
1552 * children and should therefore not cancel invalidate requests, even if they
1553 * lie outside of this view's bounds.
1554 *
1555 * {@hide}
1556 */
1557 static final int DRAW_ANIMATION = 0x00000040;
1558 /** {@hide} */
1559 static final int SKIP_DRAW = 0x00000080;
1560 /** {@hide} */
1561 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1562 /** {@hide} */
1563 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1564 /** {@hide} */
1565 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1566 /** {@hide} */
1567 static final int MEASURED_DIMENSION_SET = 0x00000800;
1568 /** {@hide} */
1569 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001570 /** {@hide} */
1571 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572
1573 private static final int PRESSED = 0x00004000;
1574
1575 /** {@hide} */
1576 static final int DRAWING_CACHE_VALID = 0x00008000;
1577 /**
1578 * Flag used to indicate that this view should be drawn once more (and only once
1579 * more) after its animation has completed.
1580 * {@hide}
1581 */
1582 static final int ANIMATION_STARTED = 0x00010000;
1583
1584 private static final int SAVE_STATE_CALLED = 0x00020000;
1585
1586 /**
1587 * Indicates that the View returned true when onSetAlpha() was called and that
1588 * the alpha must be restored.
1589 * {@hide}
1590 */
1591 static final int ALPHA_SET = 0x00040000;
1592
1593 /**
1594 * Set by {@link #setScrollContainer(boolean)}.
1595 */
1596 static final int SCROLL_CONTAINER = 0x00080000;
1597
1598 /**
1599 * Set by {@link #setScrollContainer(boolean)}.
1600 */
1601 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1602
1603 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001604 * View flag indicating whether this view was invalidated (fully or partially.)
1605 *
1606 * @hide
1607 */
1608 static final int DIRTY = 0x00200000;
1609
1610 /**
1611 * View flag indicating whether this view was invalidated by an opaque
1612 * invalidate request.
1613 *
1614 * @hide
1615 */
1616 static final int DIRTY_OPAQUE = 0x00400000;
1617
1618 /**
1619 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1620 *
1621 * @hide
1622 */
1623 static final int DIRTY_MASK = 0x00600000;
1624
1625 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001626 * Indicates whether the background is opaque.
1627 *
1628 * @hide
1629 */
1630 static final int OPAQUE_BACKGROUND = 0x00800000;
1631
1632 /**
1633 * Indicates whether the scrollbars are opaque.
1634 *
1635 * @hide
1636 */
1637 static final int OPAQUE_SCROLLBARS = 0x01000000;
1638
1639 /**
1640 * Indicates whether the view is opaque.
1641 *
1642 * @hide
1643 */
1644 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001645
Adam Powelle14579b2009-12-16 18:39:52 -08001646 /**
1647 * Indicates a prepressed state;
1648 * the short time between ACTION_DOWN and recognizing
1649 * a 'real' press. Prepressed is used to recognize quick taps
1650 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001651 *
Adam Powelle14579b2009-12-16 18:39:52 -08001652 * @hide
1653 */
1654 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001655
Adam Powellc9fbaab2010-02-16 17:16:19 -08001656 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001657 * Indicates whether the view is temporarily detached.
1658 *
1659 * @hide
1660 */
1661 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001662
Adam Powell8568c3a2010-04-19 14:26:11 -07001663 /**
1664 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001665 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001666 * @hide
1667 */
1668 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001669
1670 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001671 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1672 * @hide
1673 */
1674 private static final int HOVERED = 0x10000000;
1675
1676 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001677 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1678 * for transform operations
1679 *
1680 * @hide
1681 */
Adam Powellf37df072010-09-17 16:22:49 -07001682 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001683
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001684 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001685 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001686
Chet Haasefd2b0022010-08-06 13:08:56 -07001687 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001688 * Indicates that this view was specifically invalidated, not just dirtied because some
1689 * child view was invalidated. The flag is used to determine when we need to recreate
1690 * a view's display list (as opposed to just returning a reference to its existing
1691 * display list).
1692 *
1693 * @hide
1694 */
1695 static final int INVALIDATED = 0x80000000;
1696
Christopher Tate3d4bf172011-03-28 16:16:46 -07001697 /* Masks for mPrivateFlags2 */
1698
1699 /**
1700 * Indicates that this view has reported that it can accept the current drag's content.
1701 * Cleared when the drag operation concludes.
1702 * @hide
1703 */
1704 static final int DRAG_CAN_ACCEPT = 0x00000001;
1705
1706 /**
1707 * Indicates that this view is currently directly under the drag location in a
1708 * drag-and-drop operation involving content that it can accept. Cleared when
1709 * the drag exits the view, or when the drag operation concludes.
1710 * @hide
1711 */
1712 static final int DRAG_HOVERED = 0x00000002;
1713
Cibu Johny86666632010-02-22 13:01:02 -08001714 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001715 * Horizontal layout direction of this view is from Left to Right.
1716 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001717 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001718 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001719
1720 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001721 * Horizontal layout direction of this view is from Right to Left.
1722 * Use with {@link #setLayoutDirection}.
1723 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001724 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001725
1726 /**
1727 * Horizontal layout direction of this view is inherited from its parent.
1728 * Use with {@link #setLayoutDirection}.
1729 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001730 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001731
1732 /**
1733 * Horizontal layout direction of this view is from deduced from the default language
1734 * script for the locale. Use with {@link #setLayoutDirection}.
1735 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001736 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001737
1738 /**
1739 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001740 * @hide
1741 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001742 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1743
1744 /**
1745 * Mask for use with private flags indicating bits used for horizontal layout direction.
1746 * @hide
1747 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001748 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001749
1750 /**
1751 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1752 * right-to-left direction.
1753 * @hide
1754 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001755 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001756
1757 /**
1758 * Indicates whether the view horizontal layout direction has been resolved.
1759 * @hide
1760 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001761 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001762
1763 /**
1764 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1765 * @hide
1766 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001767 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001768
1769 /*
1770 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1771 * flag value.
1772 * @hide
1773 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001774 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1775 LAYOUT_DIRECTION_LTR,
1776 LAYOUT_DIRECTION_RTL,
1777 LAYOUT_DIRECTION_INHERIT,
1778 LAYOUT_DIRECTION_LOCALE
1779 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001780
1781 /**
1782 * Default horizontal layout direction.
1783 * @hide
1784 */
1785 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001786
Cibu Johny86666632010-02-22 13:01:02 -08001787
Adam Powell539ee872012-02-03 19:00:49 -08001788 /**
1789 * Indicates that the view is tracking some sort of transient state
1790 * that the app should not need to be aware of, but that the framework
1791 * should take special care to preserve.
1792 *
1793 * @hide
1794 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001796
1797
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001798 /**
1799 * Text direction is inherited thru {@link ViewGroup}
1800 */
1801 public static final int TEXT_DIRECTION_INHERIT = 0;
1802
1803 /**
1804 * Text direction is using "first strong algorithm". The first strong directional character
1805 * determines the paragraph direction. If there is no strong directional character, the
1806 * paragraph direction is the view's resolved layout direction.
1807 */
1808 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1809
1810 /**
1811 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1812 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1813 * If there are neither, the paragraph direction is the view's resolved layout direction.
1814 */
1815 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1816
1817 /**
1818 * Text direction is forced to LTR.
1819 */
1820 public static final int TEXT_DIRECTION_LTR = 3;
1821
1822 /**
1823 * Text direction is forced to RTL.
1824 */
1825 public static final int TEXT_DIRECTION_RTL = 4;
1826
1827 /**
1828 * Text direction is coming from the system Locale.
1829 */
1830 public static final int TEXT_DIRECTION_LOCALE = 5;
1831
1832 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001833 * Default text direction is inherited
1834 */
1835 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1836
1837 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1839 * @hide
1840 */
1841 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1842
1843 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 * Mask for use with private flags indicating bits used for text direction.
1845 * @hide
1846 */
1847 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1848
1849 /**
1850 * Array of text direction flags for mapping attribute "textDirection" to correct
1851 * flag value.
1852 * @hide
1853 */
1854 private static final int[] TEXT_DIRECTION_FLAGS = {
1855 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1856 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1857 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1858 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1859 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1860 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1861 };
1862
1863 /**
1864 * Indicates whether the view text direction has been resolved.
1865 * @hide
1866 */
1867 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1868
1869 /**
1870 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1871 * @hide
1872 */
1873 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1874
1875 /**
1876 * Mask for use with private flags indicating bits used for resolved text direction.
1877 * @hide
1878 */
1879 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1880
1881 /**
1882 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1883 * @hide
1884 */
1885 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1886 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1887
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001888 /*
1889 * Default text alignment. The text alignment of this View is inherited from its parent.
1890 * Use with {@link #setTextAlignment(int)}
1891 */
1892 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1893
1894 /**
1895 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1896 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1897 *
1898 * Use with {@link #setTextAlignment(int)}
1899 */
1900 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1901
1902 /**
1903 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1904 *
1905 * Use with {@link #setTextAlignment(int)}
1906 */
1907 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1908
1909 /**
1910 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1911 *
1912 * Use with {@link #setTextAlignment(int)}
1913 */
1914 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1915
1916 /**
1917 * Center the paragraph, e.g. ALIGN_CENTER.
1918 *
1919 * Use with {@link #setTextAlignment(int)}
1920 */
1921 public static final int TEXT_ALIGNMENT_CENTER = 4;
1922
1923 /**
1924 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1925 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1926 *
1927 * Use with {@link #setTextAlignment(int)}
1928 */
1929 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1930
1931 /**
1932 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1933 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1934 *
1935 * Use with {@link #setTextAlignment(int)}
1936 */
1937 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1938
1939 /**
1940 * Default text alignment is inherited
1941 */
1942 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1943
1944 /**
1945 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1946 * @hide
1947 */
1948 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
1949
1950 /**
1951 * Mask for use with private flags indicating bits used for text alignment.
1952 * @hide
1953 */
1954 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
1955
1956 /**
1957 * Array of text direction flags for mapping attribute "textAlignment" to correct
1958 * flag value.
1959 * @hide
1960 */
1961 private static final int[] TEXT_ALIGNMENT_FLAGS = {
1962 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
1963 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
1964 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
1965 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
1966 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
1967 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
1968 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
1969 };
1970
1971 /**
1972 * Indicates whether the view text alignment has been resolved.
1973 * @hide
1974 */
1975 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
1976
1977 /**
1978 * Bit shift to get the resolved text alignment.
1979 * @hide
1980 */
1981 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
1982
1983 /**
1984 * Mask for use with private flags indicating bits used for text alignment.
1985 * @hide
1986 */
1987 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
1988
1989 /**
1990 * Indicates whether if the view text alignment has been resolved to gravity
1991 */
1992 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
1993 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
1994
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001995
Christopher Tate3d4bf172011-03-28 16:16:46 -07001996 /* End of masks for mPrivateFlags2 */
1997
1998 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1999
Chet Haasedaf98e92011-01-10 14:10:36 -08002000 /**
Adam Powell637d3372010-08-25 14:37:03 -07002001 * Always allow a user to over-scroll this view, provided it is a
2002 * view that can scroll.
2003 *
2004 * @see #getOverScrollMode()
2005 * @see #setOverScrollMode(int)
2006 */
2007 public static final int OVER_SCROLL_ALWAYS = 0;
2008
2009 /**
2010 * Allow a user to over-scroll this view only if the content is large
2011 * enough to meaningfully scroll, provided it is a view that can scroll.
2012 *
2013 * @see #getOverScrollMode()
2014 * @see #setOverScrollMode(int)
2015 */
2016 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2017
2018 /**
2019 * Never allow a user to over-scroll this view.
2020 *
2021 * @see #getOverScrollMode()
2022 * @see #setOverScrollMode(int)
2023 */
2024 public static final int OVER_SCROLL_NEVER = 2;
2025
2026 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002027 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2028 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002029 *
Joe Malin32736f02011-01-19 16:14:20 -08002030 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002031 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002032 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002033
2034 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002035 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2036 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002037 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002038 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002039 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002040 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002041 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002042 *
Joe Malin32736f02011-01-19 16:14:20 -08002043 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002044 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002045 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2046
2047 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002048 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2049 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002050 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002051 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002052 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2053 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2054 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002055 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002056 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2057 * window flags) for displaying content using every last pixel on the display.
2058 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002059 * <p>There is a limitation: because navigation controls are so important, the least user
2060 * interaction will cause them to reappear immediately. When this happens, both
2061 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2062 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002063 *
2064 * @see #setSystemUiVisibility(int)
2065 */
2066 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2067
2068 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002069 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2070 * into the normal fullscreen mode so that its content can take over the screen
2071 * while still allowing the user to interact with the application.
2072 *
2073 * <p>This has the same visual effect as
2074 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2075 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2076 * meaning that non-critical screen decorations (such as the status bar) will be
2077 * hidden while the user is in the View's window, focusing the experience on
2078 * that content. Unlike the window flag, if you are using ActionBar in
2079 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2080 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2081 * hide the action bar.
2082 *
2083 * <p>This approach to going fullscreen is best used over the window flag when
2084 * it is a transient state -- that is, the application does this at certain
2085 * points in its user interaction where it wants to allow the user to focus
2086 * on content, but not as a continuous state. For situations where the application
2087 * would like to simply stay full screen the entire time (such as a game that
2088 * wants to take over the screen), the
2089 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2090 * is usually a better approach. The state set here will be removed by the system
2091 * in various situations (such as the user moving to another application) like
2092 * the other system UI states.
2093 *
2094 * <p>When using this flag, the application should provide some easy facility
2095 * for the user to go out of it. A common example would be in an e-book
2096 * reader, where tapping on the screen brings back whatever screen and UI
2097 * decorations that had been hidden while the user was immersed in reading
2098 * the book.
2099 *
2100 * @see #setSystemUiVisibility(int)
2101 */
2102 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2103
2104 /**
2105 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2106 * flags, we would like a stable view of the content insets given to
2107 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2108 * will always represent the worst case that the application can expect
2109 * as a continue state. In practice this means with any of system bar,
2110 * nav bar, and status bar shown, but not the space that would be needed
2111 * for an input method.
2112 *
2113 * <p>If you are using ActionBar in
2114 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2115 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2116 * insets it adds to those given to the application.
2117 */
2118 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2119
2120 /**
2121 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2122 * to be layed out as if it has requested
2123 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2124 * allows it to avoid artifacts when switching in and out of that mode, at
2125 * the expense that some of its user interface may be covered by screen
2126 * decorations when they are shown. You can perform layout of your inner
2127 * UI elements to account for the navagation system UI through the
2128 * {@link #fitSystemWindows(Rect)} method.
2129 */
2130 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2131
2132 /**
2133 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2134 * to be layed out as if it has requested
2135 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2136 * allows it to avoid artifacts when switching in and out of that mode, at
2137 * the expense that some of its user interface may be covered by screen
2138 * decorations when they are shown. You can perform layout of your inner
2139 * UI elements to account for non-fullscreen system UI through the
2140 * {@link #fitSystemWindows(Rect)} method.
2141 */
2142 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2143
2144 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002145 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2146 */
2147 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2148
2149 /**
2150 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2151 */
2152 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002153
2154 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002155 * @hide
2156 *
2157 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2158 * out of the public fields to keep the undefined bits out of the developer's way.
2159 *
2160 * Flag to make the status bar not expandable. Unless you also
2161 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2162 */
2163 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2164
2165 /**
2166 * @hide
2167 *
2168 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2169 * out of the public fields to keep the undefined bits out of the developer's way.
2170 *
2171 * Flag to hide notification icons and scrolling ticker text.
2172 */
2173 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2174
2175 /**
2176 * @hide
2177 *
2178 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2179 * out of the public fields to keep the undefined bits out of the developer's way.
2180 *
2181 * Flag to disable incoming notification alerts. This will not block
2182 * icons, but it will block sound, vibrating and other visual or aural notifications.
2183 */
2184 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2185
2186 /**
2187 * @hide
2188 *
2189 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2190 * out of the public fields to keep the undefined bits out of the developer's way.
2191 *
2192 * Flag to hide only the scrolling ticker. Note that
2193 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2194 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2195 */
2196 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2197
2198 /**
2199 * @hide
2200 *
2201 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2202 * out of the public fields to keep the undefined bits out of the developer's way.
2203 *
2204 * Flag to hide the center system info area.
2205 */
2206 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2207
2208 /**
2209 * @hide
2210 *
2211 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2212 * out of the public fields to keep the undefined bits out of the developer's way.
2213 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002214 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002215 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2216 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002217 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002218
2219 /**
2220 * @hide
2221 *
2222 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2223 * out of the public fields to keep the undefined bits out of the developer's way.
2224 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002225 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002226 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2227 */
2228 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2229
2230 /**
2231 * @hide
2232 *
2233 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2234 * out of the public fields to keep the undefined bits out of the developer's way.
2235 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002236 * Flag to hide only the clock. You might use this if your activity has
2237 * its own clock making the status bar's clock redundant.
2238 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002239 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2240
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002241 /**
2242 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002243 *
2244 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2245 * out of the public fields to keep the undefined bits out of the developer's way.
2246 *
2247 * Flag to hide only the recent apps button. Don't use this
2248 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2249 */
2250 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2251
2252 /**
2253 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002254 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002255 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002256
2257 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002258 * These are the system UI flags that can be cleared by events outside
2259 * of an application. Currently this is just the ability to tap on the
2260 * screen while hiding the navigation bar to have it return.
2261 * @hide
2262 */
2263 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002264 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2265 | SYSTEM_UI_FLAG_FULLSCREEN;
2266
2267 /**
2268 * Flags that can impact the layout in relation to system UI.
2269 */
2270 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2271 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2272 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002273
2274 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002275 * Find views that render the specified text.
2276 *
2277 * @see #findViewsWithText(ArrayList, CharSequence, int)
2278 */
2279 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2280
2281 /**
2282 * Find find views that contain the specified content description.
2283 *
2284 * @see #findViewsWithText(ArrayList, CharSequence, int)
2285 */
2286 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2287
2288 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002289 * Find views that contain {@link AccessibilityNodeProvider}. Such
2290 * a View is a root of virtual view hierarchy and may contain the searched
2291 * text. If this flag is set Views with providers are automatically
2292 * added and it is a responsibility of the client to call the APIs of
2293 * the provider to determine whether the virtual tree rooted at this View
2294 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2295 * represeting the virtual views with this text.
2296 *
2297 * @see #findViewsWithText(ArrayList, CharSequence, int)
2298 *
2299 * @hide
2300 */
2301 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2302
2303 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002304 * Indicates that the screen has changed state and is now off.
2305 *
2306 * @see #onScreenStateChanged(int)
2307 */
2308 public static final int SCREEN_STATE_OFF = 0x0;
2309
2310 /**
2311 * Indicates that the screen has changed state and is now on.
2312 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002313 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002314 */
2315 public static final int SCREEN_STATE_ON = 0x1;
2316
2317 /**
Adam Powell637d3372010-08-25 14:37:03 -07002318 * Controls the over-scroll mode for this view.
2319 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2320 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2321 * and {@link #OVER_SCROLL_NEVER}.
2322 */
2323 private int mOverScrollMode;
2324
2325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 * The parent this view is attached to.
2327 * {@hide}
2328 *
2329 * @see #getParent()
2330 */
2331 protected ViewParent mParent;
2332
2333 /**
2334 * {@hide}
2335 */
2336 AttachInfo mAttachInfo;
2337
2338 /**
2339 * {@hide}
2340 */
Romain Guy809a7f62009-05-14 15:44:42 -07002341 @ViewDebug.ExportedProperty(flagMapping = {
2342 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2343 name = "FORCE_LAYOUT"),
2344 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2345 name = "LAYOUT_REQUIRED"),
2346 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002347 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002348 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2349 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2350 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2351 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2352 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002354 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355
2356 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002357 * This view's request for the visibility of the status bar.
2358 * @hide
2359 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002360 @ViewDebug.ExportedProperty(flagMapping = {
2361 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2362 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2363 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2364 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2365 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2366 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2367 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2368 equals = SYSTEM_UI_FLAG_VISIBLE,
2369 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2370 })
Joe Onorato664644d2011-01-23 17:53:23 -08002371 int mSystemUiVisibility;
2372
2373 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 * Count of how many windows this view has been attached to.
2375 */
2376 int mWindowAttachCount;
2377
2378 /**
2379 * The layout parameters associated with this view and used by the parent
2380 * {@link android.view.ViewGroup} to determine how this view should be
2381 * laid out.
2382 * {@hide}
2383 */
2384 protected ViewGroup.LayoutParams mLayoutParams;
2385
2386 /**
2387 * The view flags hold various views states.
2388 * {@hide}
2389 */
2390 @ViewDebug.ExportedProperty
2391 int mViewFlags;
2392
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002393 static class TransformationInfo {
2394 /**
2395 * The transform matrix for the View. This transform is calculated internally
2396 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2397 * is used by default. Do *not* use this variable directly; instead call
2398 * getMatrix(), which will automatically recalculate the matrix if necessary
2399 * to get the correct matrix based on the latest rotation and scale properties.
2400 */
2401 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002402
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002403 /**
2404 * The transform matrix for the View. This transform is calculated internally
2405 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2406 * is used by default. Do *not* use this variable directly; instead call
2407 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2408 * to get the correct matrix based on the latest rotation and scale properties.
2409 */
2410 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002411
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002412 /**
2413 * An internal variable that tracks whether we need to recalculate the
2414 * transform matrix, based on whether the rotation or scaleX/Y properties
2415 * have changed since the matrix was last calculated.
2416 */
2417 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002418
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002419 /**
2420 * An internal variable that tracks whether we need to recalculate the
2421 * transform matrix, based on whether the rotation or scaleX/Y properties
2422 * have changed since the matrix was last calculated.
2423 */
2424 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002425
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002426 /**
2427 * A variable that tracks whether we need to recalculate the
2428 * transform matrix, based on whether the rotation or scaleX/Y properties
2429 * have changed since the matrix was last calculated. This variable
2430 * is only valid after a call to updateMatrix() or to a function that
2431 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2432 */
2433 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002434
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002435 /**
2436 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2437 */
2438 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002439
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002440 /**
2441 * This matrix is used when computing the matrix for 3D rotations.
2442 */
2443 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002444
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002445 /**
2446 * These prev values are used to recalculate a centered pivot point when necessary. The
2447 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2448 * set), so thes values are only used then as well.
2449 */
2450 private int mPrevWidth = -1;
2451 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002452
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002453 /**
2454 * The degrees rotation around the vertical axis through the pivot point.
2455 */
2456 @ViewDebug.ExportedProperty
2457 float mRotationY = 0f;
2458
2459 /**
2460 * The degrees rotation around the horizontal axis through the pivot point.
2461 */
2462 @ViewDebug.ExportedProperty
2463 float mRotationX = 0f;
2464
2465 /**
2466 * The degrees rotation around the pivot point.
2467 */
2468 @ViewDebug.ExportedProperty
2469 float mRotation = 0f;
2470
2471 /**
2472 * The amount of translation of the object away from its left property (post-layout).
2473 */
2474 @ViewDebug.ExportedProperty
2475 float mTranslationX = 0f;
2476
2477 /**
2478 * The amount of translation of the object away from its top property (post-layout).
2479 */
2480 @ViewDebug.ExportedProperty
2481 float mTranslationY = 0f;
2482
2483 /**
2484 * The amount of scale in the x direction around the pivot point. A
2485 * value of 1 means no scaling is applied.
2486 */
2487 @ViewDebug.ExportedProperty
2488 float mScaleX = 1f;
2489
2490 /**
2491 * The amount of scale in the y direction around the pivot point. A
2492 * value of 1 means no scaling is applied.
2493 */
2494 @ViewDebug.ExportedProperty
2495 float mScaleY = 1f;
2496
2497 /**
Chet Haasea33de552012-02-03 16:28:24 -08002498 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002499 */
2500 @ViewDebug.ExportedProperty
2501 float mPivotX = 0f;
2502
2503 /**
Chet Haasea33de552012-02-03 16:28:24 -08002504 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002505 */
2506 @ViewDebug.ExportedProperty
2507 float mPivotY = 0f;
2508
2509 /**
2510 * The opacity of the View. This is a value from 0 to 1, where 0 means
2511 * completely transparent and 1 means completely opaque.
2512 */
2513 @ViewDebug.ExportedProperty
2514 float mAlpha = 1f;
2515 }
2516
2517 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002518
Joe Malin32736f02011-01-19 16:14:20 -08002519 private boolean mLastIsOpaque;
2520
Chet Haasefd2b0022010-08-06 13:08:56 -07002521 /**
2522 * Convenience value to check for float values that are close enough to zero to be considered
2523 * zero.
2524 */
Romain Guy2542d192010-08-18 11:47:12 -07002525 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002526
2527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 * The distance in pixels from the left edge of this view's parent
2529 * to the left edge of this view.
2530 * {@hide}
2531 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002532 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 protected int mLeft;
2534 /**
2535 * The distance in pixels from the left edge of this view's parent
2536 * to the right edge of this view.
2537 * {@hide}
2538 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002539 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 protected int mRight;
2541 /**
2542 * The distance in pixels from the top edge of this view's parent
2543 * to the top edge of this view.
2544 * {@hide}
2545 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002546 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 protected int mTop;
2548 /**
2549 * The distance in pixels from the top edge of this view's parent
2550 * to the bottom edge of this view.
2551 * {@hide}
2552 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002553 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 protected int mBottom;
2555
2556 /**
2557 * The offset, in pixels, by which the content of this view is scrolled
2558 * horizontally.
2559 * {@hide}
2560 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002561 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 protected int mScrollX;
2563 /**
2564 * The offset, in pixels, by which the content of this view is scrolled
2565 * vertically.
2566 * {@hide}
2567 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002568 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 protected int mScrollY;
2570
2571 /**
2572 * The left padding in pixels, that is the distance in pixels between the
2573 * left edge of this view and the left edge of its content.
2574 * {@hide}
2575 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002576 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 protected int mPaddingLeft;
2578 /**
2579 * The right padding in pixels, that is the distance in pixels between the
2580 * right edge of this view and the right edge of its content.
2581 * {@hide}
2582 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002583 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 protected int mPaddingRight;
2585 /**
2586 * The top padding in pixels, that is the distance in pixels between the
2587 * top edge of this view and the top edge of its content.
2588 * {@hide}
2589 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002590 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 protected int mPaddingTop;
2592 /**
2593 * The bottom padding in pixels, that is the distance in pixels between the
2594 * bottom edge of this view and the bottom edge of its content.
2595 * {@hide}
2596 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002597 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 protected int mPaddingBottom;
2599
2600 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002601 * Briefly describes the view and is primarily used for accessibility support.
2602 */
2603 private CharSequence mContentDescription;
2604
2605 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002607 *
2608 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002610 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002611 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612
2613 /**
2614 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002615 *
2616 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002618 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002619 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002621 /**
Adam Powell20232d02010-12-08 21:08:53 -08002622 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002623 *
2624 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002625 */
2626 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002627 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002628
2629 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002630 * Cache if the user padding is relative.
2631 *
2632 */
2633 @ViewDebug.ExportedProperty(category = "padding")
2634 boolean mUserPaddingRelative;
2635
2636 /**
2637 * Cache the paddingStart set by the user to append to the scrollbar's size.
2638 *
2639 */
2640 @ViewDebug.ExportedProperty(category = "padding")
2641 int mUserPaddingStart;
2642
2643 /**
2644 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2645 *
2646 */
2647 @ViewDebug.ExportedProperty(category = "padding")
2648 int mUserPaddingEnd;
2649
2650 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002651 * @hide
2652 */
2653 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2654 /**
2655 * @hide
2656 */
2657 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658
Philip Milne6c8ea062012-04-03 17:38:43 -07002659 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660
2661 private int mBackgroundResource;
2662 private boolean mBackgroundSizeChanged;
2663
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002664 static class ListenerInfo {
2665 /**
2666 * Listener used to dispatch focus change events.
2667 * This field should be made private, so it is hidden from the SDK.
2668 * {@hide}
2669 */
2670 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002672 /**
2673 * Listeners for layout change events.
2674 */
2675 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002676
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002677 /**
2678 * Listeners for attach events.
2679 */
2680 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002681
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002682 /**
2683 * Listener used to dispatch click events.
2684 * This field should be made private, so it is hidden from the SDK.
2685 * {@hide}
2686 */
2687 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002689 /**
2690 * Listener used to dispatch long click events.
2691 * This field should be made private, so it is hidden from the SDK.
2692 * {@hide}
2693 */
2694 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002696 /**
2697 * Listener used to build the context menu.
2698 * This field should be made private, so it is hidden from the SDK.
2699 * {@hide}
2700 */
2701 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002703 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002705 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002707 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002708
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002709 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002710
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002711 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002712
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002713 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2714 }
2715
2716 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 /**
2719 * The application environment this view lives in.
2720 * This field should be made private, so it is hidden from the SDK.
2721 * {@hide}
2722 */
2723 protected Context mContext;
2724
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002725 private final Resources mResources;
2726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 private ScrollabilityCache mScrollCache;
2728
2729 private int[] mDrawableState = null;
2730
Romain Guy0211a0a2011-02-14 16:34:59 -08002731 /**
2732 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002733 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002734 * @hide
2735 */
2736 public boolean mCachingFailed;
2737
Romain Guy02890fd2010-08-06 17:58:44 -07002738 private Bitmap mDrawingCache;
2739 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002740 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002741 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742
2743 /**
2744 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2745 * the user may specify which view to go to next.
2746 */
2747 private int mNextFocusLeftId = View.NO_ID;
2748
2749 /**
2750 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2751 * the user may specify which view to go to next.
2752 */
2753 private int mNextFocusRightId = View.NO_ID;
2754
2755 /**
2756 * When this view has focus and the next focus is {@link #FOCUS_UP},
2757 * the user may specify which view to go to next.
2758 */
2759 private int mNextFocusUpId = View.NO_ID;
2760
2761 /**
2762 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2763 * the user may specify which view to go to next.
2764 */
2765 private int mNextFocusDownId = View.NO_ID;
2766
Jeff Brown4e6319b2010-12-13 10:36:51 -08002767 /**
2768 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2769 * the user may specify which view to go to next.
2770 */
2771 int mNextFocusForwardId = View.NO_ID;
2772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002774 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002775 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002776 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 private UnsetPressedState mUnsetPressedState;
2779
2780 /**
2781 * Whether the long press's action has been invoked. The tap's action is invoked on the
2782 * up event while a long press is invoked as soon as the long press duration is reached, so
2783 * a long press could be performed before the tap is checked, in which case the tap's action
2784 * should not be invoked.
2785 */
2786 private boolean mHasPerformedLongPress;
2787
2788 /**
2789 * The minimum height of the view. We'll try our best to have the height
2790 * of this view to at least this amount.
2791 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002792 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 private int mMinHeight;
2794
2795 /**
2796 * The minimum width of the view. We'll try our best to have the width
2797 * of this view to at least this amount.
2798 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002799 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 private int mMinWidth;
2801
2802 /**
2803 * The delegate to handle touch events that are physically in this view
2804 * but should be handled by another view.
2805 */
2806 private TouchDelegate mTouchDelegate = null;
2807
2808 /**
2809 * Solid color to use as a background when creating the drawing cache. Enables
2810 * the cache to use 16 bit bitmaps instead of 32 bit.
2811 */
2812 private int mDrawingCacheBackgroundColor = 0;
2813
2814 /**
2815 * Special tree observer used when mAttachInfo is null.
2816 */
2817 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002818
Adam Powelle14579b2009-12-16 18:39:52 -08002819 /**
2820 * Cache the touch slop from the context that created the view.
2821 */
2822 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002825 * Object that handles automatic animation of view properties.
2826 */
2827 private ViewPropertyAnimator mAnimator = null;
2828
2829 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002830 * Flag indicating that a drag can cross window boundaries. When
2831 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2832 * with this flag set, all visible applications will be able to participate
2833 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002834 *
2835 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002836 */
2837 public static final int DRAG_FLAG_GLOBAL = 1;
2838
2839 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002840 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2841 */
2842 private float mVerticalScrollFactor;
2843
2844 /**
Adam Powell20232d02010-12-08 21:08:53 -08002845 * Position of the vertical scroll bar.
2846 */
2847 private int mVerticalScrollbarPosition;
2848
2849 /**
2850 * Position the scroll bar at the default position as determined by the system.
2851 */
2852 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2853
2854 /**
2855 * Position the scroll bar along the left edge.
2856 */
2857 public static final int SCROLLBAR_POSITION_LEFT = 1;
2858
2859 /**
2860 * Position the scroll bar along the right edge.
2861 */
2862 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2863
2864 /**
Romain Guy171c5922011-01-06 10:04:23 -08002865 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002866 *
2867 * @see #getLayerType()
2868 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002869 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002870 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002871 */
2872 public static final int LAYER_TYPE_NONE = 0;
2873
2874 /**
2875 * <p>Indicates that the view has a software layer. A software layer is backed
2876 * by a bitmap and causes the view to be rendered using Android's software
2877 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002878 *
Romain Guy171c5922011-01-06 10:04:23 -08002879 * <p>Software layers have various usages:</p>
2880 * <p>When the application is not using hardware acceleration, a software layer
2881 * is useful to apply a specific color filter and/or blending mode and/or
2882 * translucency to a view and all its children.</p>
2883 * <p>When the application is using hardware acceleration, a software layer
2884 * is useful to render drawing primitives not supported by the hardware
2885 * accelerated pipeline. It can also be used to cache a complex view tree
2886 * into a texture and reduce the complexity of drawing operations. For instance,
2887 * when animating a complex view tree with a translation, a software layer can
2888 * be used to render the view tree only once.</p>
2889 * <p>Software layers should be avoided when the affected view tree updates
2890 * often. Every update will require to re-render the software layer, which can
2891 * potentially be slow (particularly when hardware acceleration is turned on
2892 * since the layer will have to be uploaded into a hardware texture after every
2893 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002894 *
2895 * @see #getLayerType()
2896 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002897 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002898 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002899 */
2900 public static final int LAYER_TYPE_SOFTWARE = 1;
2901
2902 /**
2903 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2904 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2905 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2906 * rendering pipeline, but only if hardware acceleration is turned on for the
2907 * view hierarchy. When hardware acceleration is turned off, hardware layers
2908 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002909 *
Romain Guy171c5922011-01-06 10:04:23 -08002910 * <p>A hardware layer is useful to apply a specific color filter and/or
2911 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002912 * <p>A hardware layer can be used to cache a complex view tree into a
2913 * texture and reduce the complexity of drawing operations. For instance,
2914 * when animating a complex view tree with a translation, a hardware layer can
2915 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002916 * <p>A hardware layer can also be used to increase the rendering quality when
2917 * rotation transformations are applied on a view. It can also be used to
2918 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002919 *
2920 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002921 * @see #setLayerType(int, android.graphics.Paint)
2922 * @see #LAYER_TYPE_NONE
2923 * @see #LAYER_TYPE_SOFTWARE
2924 */
2925 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002926
Romain Guy3aaff3a2011-01-12 14:18:47 -08002927 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2928 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2929 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2930 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2931 })
Romain Guy171c5922011-01-06 10:04:23 -08002932 int mLayerType = LAYER_TYPE_NONE;
2933 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002934 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002935
2936 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002937 * Set to true when the view is sending hover accessibility events because it
2938 * is the innermost hovered view.
2939 */
2940 private boolean mSendingHoverAccessibilityEvents;
2941
2942 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 * Simple constructor to use when creating a view from code.
2944 *
2945 * @param context The Context the view is running in, through which it can
2946 * access the current theme, resources, etc.
2947 */
2948 public View(Context context) {
2949 mContext = context;
2950 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07002951 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002952 // Set layout and text direction defaults
2953 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002954 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
2955 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08002956 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002957 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002958 mUserPaddingStart = -1;
2959 mUserPaddingEnd = -1;
2960 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 }
2962
2963 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07002964 * Delegate for injecting accessiblity functionality.
2965 */
2966 AccessibilityDelegate mAccessibilityDelegate;
2967
2968 /**
2969 * Consistency verifier for debugging purposes.
2970 * @hide
2971 */
2972 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2973 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2974 new InputEventConsistencyVerifier(this, 0) : null;
2975
2976 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 * Constructor that is called when inflating a view from XML. This is called
2978 * when a view is being constructed from an XML file, supplying attributes
2979 * that were specified in the XML file. This version uses a default style of
2980 * 0, so the only attribute values applied are those in the Context's Theme
2981 * and the given AttributeSet.
2982 *
2983 * <p>
2984 * The method onFinishInflate() will be called after all children have been
2985 * added.
2986 *
2987 * @param context The Context the view is running in, through which it can
2988 * access the current theme, resources, etc.
2989 * @param attrs The attributes of the XML tag that is inflating the view.
2990 * @see #View(Context, AttributeSet, int)
2991 */
2992 public View(Context context, AttributeSet attrs) {
2993 this(context, attrs, 0);
2994 }
2995
2996 /**
2997 * Perform inflation from XML and apply a class-specific base style. This
2998 * constructor of View allows subclasses to use their own base style when
2999 * they are inflating. For example, a Button class's constructor would call
3000 * this version of the super class constructor and supply
3001 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3002 * the theme's button style to modify all of the base view attributes (in
3003 * particular its background) as well as the Button class's attributes.
3004 *
3005 * @param context The Context the view is running in, through which it can
3006 * access the current theme, resources, etc.
3007 * @param attrs The attributes of the XML tag that is inflating the view.
3008 * @param defStyle The default style to apply to this view. If 0, no style
3009 * will be applied (beyond what is included in the theme). This may
3010 * either be an attribute resource, whose value will be retrieved
3011 * from the current theme, or an explicit style resource.
3012 * @see #View(Context, AttributeSet)
3013 */
3014 public View(Context context, AttributeSet attrs, int defStyle) {
3015 this(context);
3016
3017 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3018 defStyle, 0);
3019
3020 Drawable background = null;
3021
3022 int leftPadding = -1;
3023 int topPadding = -1;
3024 int rightPadding = -1;
3025 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003026 int startPadding = -1;
3027 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028
3029 int padding = -1;
3030
3031 int viewFlagValues = 0;
3032 int viewFlagMasks = 0;
3033
3034 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 int x = 0;
3037 int y = 0;
3038
Chet Haase73066682010-11-29 15:55:32 -08003039 float tx = 0;
3040 float ty = 0;
3041 float rotation = 0;
3042 float rotationX = 0;
3043 float rotationY = 0;
3044 float sx = 1f;
3045 float sy = 1f;
3046 boolean transformSet = false;
3047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3049
Adam Powell637d3372010-08-25 14:37:03 -07003050 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 final int N = a.getIndexCount();
3052 for (int i = 0; i < N; i++) {
3053 int attr = a.getIndex(i);
3054 switch (attr) {
3055 case com.android.internal.R.styleable.View_background:
3056 background = a.getDrawable(attr);
3057 break;
3058 case com.android.internal.R.styleable.View_padding:
3059 padding = a.getDimensionPixelSize(attr, -1);
3060 break;
3061 case com.android.internal.R.styleable.View_paddingLeft:
3062 leftPadding = a.getDimensionPixelSize(attr, -1);
3063 break;
3064 case com.android.internal.R.styleable.View_paddingTop:
3065 topPadding = a.getDimensionPixelSize(attr, -1);
3066 break;
3067 case com.android.internal.R.styleable.View_paddingRight:
3068 rightPadding = a.getDimensionPixelSize(attr, -1);
3069 break;
3070 case com.android.internal.R.styleable.View_paddingBottom:
3071 bottomPadding = a.getDimensionPixelSize(attr, -1);
3072 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003073 case com.android.internal.R.styleable.View_paddingStart:
3074 startPadding = a.getDimensionPixelSize(attr, -1);
3075 break;
3076 case com.android.internal.R.styleable.View_paddingEnd:
3077 endPadding = a.getDimensionPixelSize(attr, -1);
3078 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 case com.android.internal.R.styleable.View_scrollX:
3080 x = a.getDimensionPixelOffset(attr, 0);
3081 break;
3082 case com.android.internal.R.styleable.View_scrollY:
3083 y = a.getDimensionPixelOffset(attr, 0);
3084 break;
Chet Haase73066682010-11-29 15:55:32 -08003085 case com.android.internal.R.styleable.View_alpha:
3086 setAlpha(a.getFloat(attr, 1f));
3087 break;
3088 case com.android.internal.R.styleable.View_transformPivotX:
3089 setPivotX(a.getDimensionPixelOffset(attr, 0));
3090 break;
3091 case com.android.internal.R.styleable.View_transformPivotY:
3092 setPivotY(a.getDimensionPixelOffset(attr, 0));
3093 break;
3094 case com.android.internal.R.styleable.View_translationX:
3095 tx = a.getDimensionPixelOffset(attr, 0);
3096 transformSet = true;
3097 break;
3098 case com.android.internal.R.styleable.View_translationY:
3099 ty = a.getDimensionPixelOffset(attr, 0);
3100 transformSet = true;
3101 break;
3102 case com.android.internal.R.styleable.View_rotation:
3103 rotation = a.getFloat(attr, 0);
3104 transformSet = true;
3105 break;
3106 case com.android.internal.R.styleable.View_rotationX:
3107 rotationX = a.getFloat(attr, 0);
3108 transformSet = true;
3109 break;
3110 case com.android.internal.R.styleable.View_rotationY:
3111 rotationY = a.getFloat(attr, 0);
3112 transformSet = true;
3113 break;
3114 case com.android.internal.R.styleable.View_scaleX:
3115 sx = a.getFloat(attr, 1f);
3116 transformSet = true;
3117 break;
3118 case com.android.internal.R.styleable.View_scaleY:
3119 sy = a.getFloat(attr, 1f);
3120 transformSet = true;
3121 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 case com.android.internal.R.styleable.View_id:
3123 mID = a.getResourceId(attr, NO_ID);
3124 break;
3125 case com.android.internal.R.styleable.View_tag:
3126 mTag = a.getText(attr);
3127 break;
3128 case com.android.internal.R.styleable.View_fitsSystemWindows:
3129 if (a.getBoolean(attr, false)) {
3130 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3131 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3132 }
3133 break;
3134 case com.android.internal.R.styleable.View_focusable:
3135 if (a.getBoolean(attr, false)) {
3136 viewFlagValues |= FOCUSABLE;
3137 viewFlagMasks |= FOCUSABLE_MASK;
3138 }
3139 break;
3140 case com.android.internal.R.styleable.View_focusableInTouchMode:
3141 if (a.getBoolean(attr, false)) {
3142 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3143 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3144 }
3145 break;
3146 case com.android.internal.R.styleable.View_clickable:
3147 if (a.getBoolean(attr, false)) {
3148 viewFlagValues |= CLICKABLE;
3149 viewFlagMasks |= CLICKABLE;
3150 }
3151 break;
3152 case com.android.internal.R.styleable.View_longClickable:
3153 if (a.getBoolean(attr, false)) {
3154 viewFlagValues |= LONG_CLICKABLE;
3155 viewFlagMasks |= LONG_CLICKABLE;
3156 }
3157 break;
3158 case com.android.internal.R.styleable.View_saveEnabled:
3159 if (!a.getBoolean(attr, true)) {
3160 viewFlagValues |= SAVE_DISABLED;
3161 viewFlagMasks |= SAVE_DISABLED_MASK;
3162 }
3163 break;
3164 case com.android.internal.R.styleable.View_duplicateParentState:
3165 if (a.getBoolean(attr, false)) {
3166 viewFlagValues |= DUPLICATE_PARENT_STATE;
3167 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3168 }
3169 break;
3170 case com.android.internal.R.styleable.View_visibility:
3171 final int visibility = a.getInt(attr, 0);
3172 if (visibility != 0) {
3173 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3174 viewFlagMasks |= VISIBILITY_MASK;
3175 }
3176 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003177 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003178 // Clear any layout direction flags (included resolved bits) already set
3179 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3180 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003181 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003182 final int value = (layoutDirection != -1) ?
3183 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3184 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003185 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 case com.android.internal.R.styleable.View_drawingCacheQuality:
3187 final int cacheQuality = a.getInt(attr, 0);
3188 if (cacheQuality != 0) {
3189 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3190 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3191 }
3192 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003193 case com.android.internal.R.styleable.View_contentDescription:
3194 mContentDescription = a.getString(attr);
3195 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3197 if (!a.getBoolean(attr, true)) {
3198 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3199 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3200 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003201 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3203 if (!a.getBoolean(attr, true)) {
3204 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3205 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3206 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003207 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 case R.styleable.View_scrollbars:
3209 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3210 if (scrollbars != SCROLLBARS_NONE) {
3211 viewFlagValues |= scrollbars;
3212 viewFlagMasks |= SCROLLBARS_MASK;
3213 initializeScrollbars(a);
3214 }
3215 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003216 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003218 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3219 // Ignore the attribute starting with ICS
3220 break;
3221 }
3222 // With builds < ICS, fall through and apply fading edges
3223 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3225 if (fadingEdge != FADING_EDGE_NONE) {
3226 viewFlagValues |= fadingEdge;
3227 viewFlagMasks |= FADING_EDGE_MASK;
3228 initializeFadingEdge(a);
3229 }
3230 break;
3231 case R.styleable.View_scrollbarStyle:
3232 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3233 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3234 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3235 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3236 }
3237 break;
3238 case R.styleable.View_isScrollContainer:
3239 setScrollContainer = true;
3240 if (a.getBoolean(attr, false)) {
3241 setScrollContainer(true);
3242 }
3243 break;
3244 case com.android.internal.R.styleable.View_keepScreenOn:
3245 if (a.getBoolean(attr, false)) {
3246 viewFlagValues |= KEEP_SCREEN_ON;
3247 viewFlagMasks |= KEEP_SCREEN_ON;
3248 }
3249 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003250 case R.styleable.View_filterTouchesWhenObscured:
3251 if (a.getBoolean(attr, false)) {
3252 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3253 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3254 }
3255 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 case R.styleable.View_nextFocusLeft:
3257 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3258 break;
3259 case R.styleable.View_nextFocusRight:
3260 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3261 break;
3262 case R.styleable.View_nextFocusUp:
3263 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3264 break;
3265 case R.styleable.View_nextFocusDown:
3266 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3267 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003268 case R.styleable.View_nextFocusForward:
3269 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3270 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 case R.styleable.View_minWidth:
3272 mMinWidth = a.getDimensionPixelSize(attr, 0);
3273 break;
3274 case R.styleable.View_minHeight:
3275 mMinHeight = a.getDimensionPixelSize(attr, 0);
3276 break;
Romain Guy9a817362009-05-01 10:57:14 -07003277 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003278 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003279 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003280 + "be used within a restricted context");
3281 }
3282
Romain Guy9a817362009-05-01 10:57:14 -07003283 final String handlerName = a.getString(attr);
3284 if (handlerName != null) {
3285 setOnClickListener(new OnClickListener() {
3286 private Method mHandler;
3287
3288 public void onClick(View v) {
3289 if (mHandler == null) {
3290 try {
3291 mHandler = getContext().getClass().getMethod(handlerName,
3292 View.class);
3293 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003294 int id = getId();
3295 String idText = id == NO_ID ? "" : " with id '"
3296 + getContext().getResources().getResourceEntryName(
3297 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003298 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003299 handlerName + "(View) in the activity "
3300 + getContext().getClass() + " for onClick handler"
3301 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003302 }
3303 }
3304
3305 try {
3306 mHandler.invoke(getContext(), View.this);
3307 } catch (IllegalAccessException e) {
3308 throw new IllegalStateException("Could not execute non "
3309 + "public method of the activity", e);
3310 } catch (InvocationTargetException e) {
3311 throw new IllegalStateException("Could not execute "
3312 + "method of the activity", e);
3313 }
3314 }
3315 });
3316 }
3317 break;
Adam Powell637d3372010-08-25 14:37:03 -07003318 case R.styleable.View_overScrollMode:
3319 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3320 break;
Adam Powell20232d02010-12-08 21:08:53 -08003321 case R.styleable.View_verticalScrollbarPosition:
3322 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3323 break;
Romain Guy171c5922011-01-06 10:04:23 -08003324 case R.styleable.View_layerType:
3325 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3326 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003327 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003328 // Clear any text direction flag already set
3329 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3330 // Set the text direction flags depending on the value of the attribute
3331 final int textDirection = a.getInt(attr, -1);
3332 if (textDirection != -1) {
3333 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3334 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003335 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003336 case R.styleable.View_textAlignment:
3337 // Clear any text alignment flag already set
3338 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3339 // Set the text alignment flag depending on the value of the attribute
3340 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3341 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3342 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 }
3344 }
3345
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003346 a.recycle();
3347
Adam Powell637d3372010-08-25 14:37:03 -07003348 setOverScrollMode(overScrollMode);
3349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003351 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 }
3353
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003354 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3355 // layout direction). Those cached values will be used later during padding resolution.
3356 mUserPaddingStart = startPadding;
3357 mUserPaddingEnd = endPadding;
3358
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003359 updateUserPaddingRelative();
3360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 if (padding >= 0) {
3362 leftPadding = padding;
3363 topPadding = padding;
3364 rightPadding = padding;
3365 bottomPadding = padding;
3366 }
3367
3368 // If the user specified the padding (either with android:padding or
3369 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3370 // use the default padding or the padding from the background drawable
3371 // (stored at this point in mPadding*)
3372 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3373 topPadding >= 0 ? topPadding : mPaddingTop,
3374 rightPadding >= 0 ? rightPadding : mPaddingRight,
3375 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3376
3377 if (viewFlagMasks != 0) {
3378 setFlags(viewFlagValues, viewFlagMasks);
3379 }
3380
3381 // Needs to be called after mViewFlags is set
3382 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3383 recomputePadding();
3384 }
3385
3386 if (x != 0 || y != 0) {
3387 scrollTo(x, y);
3388 }
3389
Chet Haase73066682010-11-29 15:55:32 -08003390 if (transformSet) {
3391 setTranslationX(tx);
3392 setTranslationY(ty);
3393 setRotation(rotation);
3394 setRotationX(rotationX);
3395 setRotationY(rotationY);
3396 setScaleX(sx);
3397 setScaleY(sy);
3398 }
3399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3401 setScrollContainer(true);
3402 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003403
3404 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 }
3406
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003407 private void updateUserPaddingRelative() {
3408 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3409 }
3410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 /**
3412 * Non-public constructor for use in testing
3413 */
3414 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003415 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 }
3417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 /**
3419 * <p>
3420 * Initializes the fading edges from a given set of styled attributes. This
3421 * method should be called by subclasses that need fading edges and when an
3422 * instance of these subclasses is created programmatically rather than
3423 * being inflated from XML. This method is automatically called when the XML
3424 * is inflated.
3425 * </p>
3426 *
3427 * @param a the styled attributes set to initialize the fading edges from
3428 */
3429 protected void initializeFadingEdge(TypedArray a) {
3430 initScrollCache();
3431
3432 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3433 R.styleable.View_fadingEdgeLength,
3434 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3435 }
3436
3437 /**
3438 * Returns the size of the vertical faded edges used to indicate that more
3439 * content in this view is visible.
3440 *
3441 * @return The size in pixels of the vertical faded edge or 0 if vertical
3442 * faded edges are not enabled for this view.
3443 * @attr ref android.R.styleable#View_fadingEdgeLength
3444 */
3445 public int getVerticalFadingEdgeLength() {
3446 if (isVerticalFadingEdgeEnabled()) {
3447 ScrollabilityCache cache = mScrollCache;
3448 if (cache != null) {
3449 return cache.fadingEdgeLength;
3450 }
3451 }
3452 return 0;
3453 }
3454
3455 /**
3456 * Set the size of the faded edge used to indicate that more content in this
3457 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003458 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3459 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3460 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 *
3462 * @param length The size in pixels of the faded edge used to indicate that more
3463 * content in this view is visible.
3464 */
3465 public void setFadingEdgeLength(int length) {
3466 initScrollCache();
3467 mScrollCache.fadingEdgeLength = length;
3468 }
3469
3470 /**
3471 * Returns the size of the horizontal faded edges used to indicate that more
3472 * content in this view is visible.
3473 *
3474 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3475 * faded edges are not enabled for this view.
3476 * @attr ref android.R.styleable#View_fadingEdgeLength
3477 */
3478 public int getHorizontalFadingEdgeLength() {
3479 if (isHorizontalFadingEdgeEnabled()) {
3480 ScrollabilityCache cache = mScrollCache;
3481 if (cache != null) {
3482 return cache.fadingEdgeLength;
3483 }
3484 }
3485 return 0;
3486 }
3487
3488 /**
3489 * Returns the width of the vertical scrollbar.
3490 *
3491 * @return The width in pixels of the vertical scrollbar or 0 if there
3492 * is no vertical scrollbar.
3493 */
3494 public int getVerticalScrollbarWidth() {
3495 ScrollabilityCache cache = mScrollCache;
3496 if (cache != null) {
3497 ScrollBarDrawable scrollBar = cache.scrollBar;
3498 if (scrollBar != null) {
3499 int size = scrollBar.getSize(true);
3500 if (size <= 0) {
3501 size = cache.scrollBarSize;
3502 }
3503 return size;
3504 }
3505 return 0;
3506 }
3507 return 0;
3508 }
3509
3510 /**
3511 * Returns the height of the horizontal scrollbar.
3512 *
3513 * @return The height in pixels of the horizontal scrollbar or 0 if
3514 * there is no horizontal scrollbar.
3515 */
3516 protected int getHorizontalScrollbarHeight() {
3517 ScrollabilityCache cache = mScrollCache;
3518 if (cache != null) {
3519 ScrollBarDrawable scrollBar = cache.scrollBar;
3520 if (scrollBar != null) {
3521 int size = scrollBar.getSize(false);
3522 if (size <= 0) {
3523 size = cache.scrollBarSize;
3524 }
3525 return size;
3526 }
3527 return 0;
3528 }
3529 return 0;
3530 }
3531
3532 /**
3533 * <p>
3534 * Initializes the scrollbars from a given set of styled attributes. This
3535 * method should be called by subclasses that need scrollbars and when an
3536 * instance of these subclasses is created programmatically rather than
3537 * being inflated from XML. This method is automatically called when the XML
3538 * is inflated.
3539 * </p>
3540 *
3541 * @param a the styled attributes set to initialize the scrollbars from
3542 */
3543 protected void initializeScrollbars(TypedArray a) {
3544 initScrollCache();
3545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003547
Mike Cleronf116bf82009-09-27 19:14:12 -07003548 if (scrollabilityCache.scrollBar == null) {
3549 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3550 }
Joe Malin32736f02011-01-19 16:14:20 -08003551
Romain Guy8bda2482010-03-02 11:42:11 -08003552 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553
Mike Cleronf116bf82009-09-27 19:14:12 -07003554 if (!fadeScrollbars) {
3555 scrollabilityCache.state = ScrollabilityCache.ON;
3556 }
3557 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003558
3559
Mike Cleronf116bf82009-09-27 19:14:12 -07003560 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3561 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3562 .getScrollBarFadeDuration());
3563 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3564 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3565 ViewConfiguration.getScrollDefaultDelay());
3566
Joe Malin32736f02011-01-19 16:14:20 -08003567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3569 com.android.internal.R.styleable.View_scrollbarSize,
3570 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3571
3572 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3573 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3574
3575 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3576 if (thumb != null) {
3577 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3578 }
3579
3580 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3581 false);
3582 if (alwaysDraw) {
3583 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3584 }
3585
3586 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3587 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3588
3589 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3590 if (thumb != null) {
3591 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3592 }
3593
3594 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3595 false);
3596 if (alwaysDraw) {
3597 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3598 }
3599
3600 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003601 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 }
3603
3604 /**
3605 * <p>
3606 * Initalizes the scrollability cache if necessary.
3607 * </p>
3608 */
3609 private void initScrollCache() {
3610 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003611 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 }
3613 }
3614
Philip Milne6c8ea062012-04-03 17:38:43 -07003615 private ScrollabilityCache getScrollCache() {
3616 initScrollCache();
3617 return mScrollCache;
3618 }
3619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 /**
Adam Powell20232d02010-12-08 21:08:53 -08003621 * Set the position of the vertical scroll bar. Should be one of
3622 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3623 * {@link #SCROLLBAR_POSITION_RIGHT}.
3624 *
3625 * @param position Where the vertical scroll bar should be positioned.
3626 */
3627 public void setVerticalScrollbarPosition(int position) {
3628 if (mVerticalScrollbarPosition != position) {
3629 mVerticalScrollbarPosition = position;
3630 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003631 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003632 }
3633 }
3634
3635 /**
3636 * @return The position where the vertical scroll bar will show, if applicable.
3637 * @see #setVerticalScrollbarPosition(int)
3638 */
3639 public int getVerticalScrollbarPosition() {
3640 return mVerticalScrollbarPosition;
3641 }
3642
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003643 ListenerInfo getListenerInfo() {
3644 if (mListenerInfo != null) {
3645 return mListenerInfo;
3646 }
3647 mListenerInfo = new ListenerInfo();
3648 return mListenerInfo;
3649 }
3650
Adam Powell20232d02010-12-08 21:08:53 -08003651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 * Register a callback to be invoked when focus of this view changed.
3653 *
3654 * @param l The callback that will run.
3655 */
3656 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003657 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 }
3659
3660 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003661 * Add a listener that will be called when the bounds of the view change due to
3662 * layout processing.
3663 *
3664 * @param listener The listener that will be called when layout bounds change.
3665 */
3666 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003667 ListenerInfo li = getListenerInfo();
3668 if (li.mOnLayoutChangeListeners == null) {
3669 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003670 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003671 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3672 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003673 }
Chet Haase21cd1382010-09-01 17:42:29 -07003674 }
3675
3676 /**
3677 * Remove a listener for layout changes.
3678 *
3679 * @param listener The listener for layout bounds change.
3680 */
3681 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003682 ListenerInfo li = mListenerInfo;
3683 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003684 return;
3685 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003686 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003687 }
3688
3689 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003690 * Add a listener for attach state changes.
3691 *
3692 * This listener will be called whenever this view is attached or detached
3693 * from a window. Remove the listener using
3694 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3695 *
3696 * @param listener Listener to attach
3697 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3698 */
3699 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003700 ListenerInfo li = getListenerInfo();
3701 if (li.mOnAttachStateChangeListeners == null) {
3702 li.mOnAttachStateChangeListeners
3703 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003704 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003705 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003706 }
3707
3708 /**
3709 * Remove a listener for attach state changes. The listener will receive no further
3710 * notification of window attach/detach events.
3711 *
3712 * @param listener Listener to remove
3713 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3714 */
3715 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003716 ListenerInfo li = mListenerInfo;
3717 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003718 return;
3719 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003720 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003721 }
3722
3723 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 * Returns the focus-change callback registered for this view.
3725 *
3726 * @return The callback, or null if one is not registered.
3727 */
3728 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003729 ListenerInfo li = mListenerInfo;
3730 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 }
3732
3733 /**
3734 * Register a callback to be invoked when this view is clicked. If this view is not
3735 * clickable, it becomes clickable.
3736 *
3737 * @param l The callback that will run
3738 *
3739 * @see #setClickable(boolean)
3740 */
3741 public void setOnClickListener(OnClickListener l) {
3742 if (!isClickable()) {
3743 setClickable(true);
3744 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003745 getListenerInfo().mOnClickListener = l;
3746 }
3747
3748 /**
3749 * Return whether this view has an attached OnClickListener. Returns
3750 * true if there is a listener, false if there is none.
3751 */
3752 public boolean hasOnClickListeners() {
3753 ListenerInfo li = mListenerInfo;
3754 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 }
3756
3757 /**
3758 * Register a callback to be invoked when this view is clicked and held. If this view is not
3759 * long clickable, it becomes long clickable.
3760 *
3761 * @param l The callback that will run
3762 *
3763 * @see #setLongClickable(boolean)
3764 */
3765 public void setOnLongClickListener(OnLongClickListener l) {
3766 if (!isLongClickable()) {
3767 setLongClickable(true);
3768 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003769 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 }
3771
3772 /**
3773 * Register a callback to be invoked when the context menu for this view is
3774 * being built. If this view is not long clickable, it becomes long clickable.
3775 *
3776 * @param l The callback that will run
3777 *
3778 */
3779 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3780 if (!isLongClickable()) {
3781 setLongClickable(true);
3782 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003783 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784 }
3785
3786 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003787 * Call this view's OnClickListener, if it is defined. Performs all normal
3788 * actions associated with clicking: reporting accessibility event, playing
3789 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 *
3791 * @return True there was an assigned OnClickListener that was called, false
3792 * otherwise is returned.
3793 */
3794 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003795 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3796
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003797 ListenerInfo li = mListenerInfo;
3798 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003800 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 return true;
3802 }
3803
3804 return false;
3805 }
3806
3807 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003808 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3809 * this only calls the listener, and does not do any associated clicking
3810 * actions like reporting an accessibility event.
3811 *
3812 * @return True there was an assigned OnClickListener that was called, false
3813 * otherwise is returned.
3814 */
3815 public boolean callOnClick() {
3816 ListenerInfo li = mListenerInfo;
3817 if (li != null && li.mOnClickListener != null) {
3818 li.mOnClickListener.onClick(this);
3819 return true;
3820 }
3821 return false;
3822 }
3823
3824 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003825 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3826 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003828 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 */
3830 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003831 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003834 ListenerInfo li = mListenerInfo;
3835 if (li != null && li.mOnLongClickListener != null) {
3836 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 }
3838 if (!handled) {
3839 handled = showContextMenu();
3840 }
3841 if (handled) {
3842 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3843 }
3844 return handled;
3845 }
3846
3847 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003848 * Performs button-related actions during a touch down event.
3849 *
3850 * @param event The event.
3851 * @return True if the down was consumed.
3852 *
3853 * @hide
3854 */
3855 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3856 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3857 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3858 return true;
3859 }
3860 }
3861 return false;
3862 }
3863
3864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 * Bring up the context menu for this view.
3866 *
3867 * @return Whether a context menu was displayed.
3868 */
3869 public boolean showContextMenu() {
3870 return getParent().showContextMenuForChild(this);
3871 }
3872
3873 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003874 * Bring up the context menu for this view, referring to the item under the specified point.
3875 *
3876 * @param x The referenced x coordinate.
3877 * @param y The referenced y coordinate.
3878 * @param metaState The keyboard modifiers that were pressed.
3879 * @return Whether a context menu was displayed.
3880 *
3881 * @hide
3882 */
3883 public boolean showContextMenu(float x, float y, int metaState) {
3884 return showContextMenu();
3885 }
3886
3887 /**
Adam Powell6e346362010-07-23 10:18:23 -07003888 * Start an action mode.
3889 *
3890 * @param callback Callback that will control the lifecycle of the action mode
3891 * @return The new action mode if it is started, null otherwise
3892 *
3893 * @see ActionMode
3894 */
3895 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003896 ViewParent parent = getParent();
3897 if (parent == null) return null;
3898 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003899 }
3900
3901 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 * Register a callback to be invoked when a key is pressed in this view.
3903 * @param l the key listener to attach to this view
3904 */
3905 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003906 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 }
3908
3909 /**
3910 * Register a callback to be invoked when a touch event is sent to this view.
3911 * @param l the touch listener to attach to this view
3912 */
3913 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003914 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 }
3916
3917 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003918 * Register a callback to be invoked when a generic motion event is sent to this view.
3919 * @param l the generic motion listener to attach to this view
3920 */
3921 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003922 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003923 }
3924
3925 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003926 * Register a callback to be invoked when a hover event is sent to this view.
3927 * @param l the hover listener to attach to this view
3928 */
3929 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003930 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003931 }
3932
3933 /**
Joe Malin32736f02011-01-19 16:14:20 -08003934 * Register a drag event listener callback object for this View. The parameter is
3935 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3936 * View, the system calls the
3937 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3938 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003939 */
3940 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003941 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003942 }
3943
3944 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003945 * Give this view focus. This will cause
3946 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 *
3948 * Note: this does not check whether this {@link View} should get focus, it just
3949 * gives it focus no matter what. It should only be called internally by framework
3950 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3951 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003952 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3953 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 * focus moved when requestFocus() is called. It may not always
3955 * apply, in which case use the default View.FOCUS_DOWN.
3956 * @param previouslyFocusedRect The rectangle of the view that had focus
3957 * prior in this View's coordinate system.
3958 */
3959 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3960 if (DBG) {
3961 System.out.println(this + " requestFocus()");
3962 }
3963
3964 if ((mPrivateFlags & FOCUSED) == 0) {
3965 mPrivateFlags |= FOCUSED;
3966
3967 if (mParent != null) {
3968 mParent.requestChildFocus(this, this);
3969 }
3970
3971 onFocusChanged(true, direction, previouslyFocusedRect);
3972 refreshDrawableState();
3973 }
3974 }
3975
3976 /**
3977 * Request that a rectangle of this view be visible on the screen,
3978 * scrolling if necessary just enough.
3979 *
3980 * <p>A View should call this if it maintains some notion of which part
3981 * of its content is interesting. For example, a text editing view
3982 * should call this when its cursor moves.
3983 *
3984 * @param rectangle The rectangle.
3985 * @return Whether any parent scrolled.
3986 */
3987 public boolean requestRectangleOnScreen(Rect rectangle) {
3988 return requestRectangleOnScreen(rectangle, false);
3989 }
3990
3991 /**
3992 * Request that a rectangle of this view be visible on the screen,
3993 * scrolling if necessary just enough.
3994 *
3995 * <p>A View should call this if it maintains some notion of which part
3996 * of its content is interesting. For example, a text editing view
3997 * should call this when its cursor moves.
3998 *
3999 * <p>When <code>immediate</code> is set to true, scrolling will not be
4000 * animated.
4001 *
4002 * @param rectangle The rectangle.
4003 * @param immediate True to forbid animated scrolling, false otherwise
4004 * @return Whether any parent scrolled.
4005 */
4006 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4007 View child = this;
4008 ViewParent parent = mParent;
4009 boolean scrolled = false;
4010 while (parent != null) {
4011 scrolled |= parent.requestChildRectangleOnScreen(child,
4012 rectangle, immediate);
4013
4014 // offset rect so next call has the rectangle in the
4015 // coordinate system of its direct child.
4016 rectangle.offset(child.getLeft(), child.getTop());
4017 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4018
4019 if (!(parent instanceof View)) {
4020 break;
4021 }
Romain Guy8506ab42009-06-11 17:35:47 -07004022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 child = (View) parent;
4024 parent = child.getParent();
4025 }
4026 return scrolled;
4027 }
4028
4029 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004030 * Called when this view wants to give up focus. If focus is cleared
4031 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4032 * <p>
4033 * <strong>Note:</strong> When a View clears focus the framework is trying
4034 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004035 * View is the first from the top that can take focus, then all callbacks
4036 * related to clearing focus will be invoked after wich the framework will
4037 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004038 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 */
4040 public void clearFocus() {
4041 if (DBG) {
4042 System.out.println(this + " clearFocus()");
4043 }
4044
4045 if ((mPrivateFlags & FOCUSED) != 0) {
4046 mPrivateFlags &= ~FOCUSED;
4047
4048 if (mParent != null) {
4049 mParent.clearChildFocus(this);
4050 }
4051
4052 onFocusChanged(false, 0, null);
4053 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004054
4055 ensureInputFocusOnFirstFocusable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 }
4057 }
4058
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004059 void ensureInputFocusOnFirstFocusable() {
4060 View root = getRootView();
4061 if (root != null) {
4062 // Find the first focusble from the top.
4063 View next = root.focusSearch(FOCUS_FORWARD);
4064 if (next != null) {
4065 // Giving focus to the found focusable will not
4066 // perform a search since we found a view that is
4067 // guaranteed to be able to take focus.
4068 next.requestFocus(FOCUS_FORWARD);
4069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 }
4071 }
4072
4073 /**
4074 * Called internally by the view system when a new view is getting focus.
4075 * This is what clears the old focus.
4076 */
4077 void unFocus() {
4078 if (DBG) {
4079 System.out.println(this + " unFocus()");
4080 }
4081
4082 if ((mPrivateFlags & FOCUSED) != 0) {
4083 mPrivateFlags &= ~FOCUSED;
4084
4085 onFocusChanged(false, 0, null);
4086 refreshDrawableState();
4087 }
4088 }
4089
4090 /**
4091 * Returns true if this view has focus iteself, or is the ancestor of the
4092 * view that has focus.
4093 *
4094 * @return True if this view has or contains focus, false otherwise.
4095 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004096 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 public boolean hasFocus() {
4098 return (mPrivateFlags & FOCUSED) != 0;
4099 }
4100
4101 /**
4102 * Returns true if this view is focusable or if it contains a reachable View
4103 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4104 * is a View whose parents do not block descendants focus.
4105 *
4106 * Only {@link #VISIBLE} views are considered focusable.
4107 *
4108 * @return True if the view is focusable or if the view contains a focusable
4109 * View, false otherwise.
4110 *
4111 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4112 */
4113 public boolean hasFocusable() {
4114 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4115 }
4116
4117 /**
4118 * Called by the view system when the focus state of this view changes.
4119 * When the focus change event is caused by directional navigation, direction
4120 * and previouslyFocusedRect provide insight into where the focus is coming from.
4121 * When overriding, be sure to call up through to the super class so that
4122 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004123 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 * @param gainFocus True if the View has focus; false otherwise.
4125 * @param direction The direction focus has moved when requestFocus()
4126 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004127 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4128 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4129 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4131 * system, of the previously focused view. If applicable, this will be
4132 * passed in as finer grained information about where the focus is coming
4133 * from (in addition to direction). Will be <code>null</code> otherwise.
4134 */
4135 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004136 if (gainFocus) {
4137 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4138 }
4139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 InputMethodManager imm = InputMethodManager.peekInstance();
4141 if (!gainFocus) {
4142 if (isPressed()) {
4143 setPressed(false);
4144 }
4145 if (imm != null && mAttachInfo != null
4146 && mAttachInfo.mHasWindowFocus) {
4147 imm.focusOut(this);
4148 }
Romain Guya2431d02009-04-30 16:30:00 -07004149 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 } else if (imm != null && mAttachInfo != null
4151 && mAttachInfo.mHasWindowFocus) {
4152 imm.focusIn(this);
4153 }
Romain Guy8506ab42009-06-11 17:35:47 -07004154
Romain Guy0fd89bf2011-01-26 15:41:30 -08004155 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004156 ListenerInfo li = mListenerInfo;
4157 if (li != null && li.mOnFocusChangeListener != null) {
4158 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 }
Joe Malin32736f02011-01-19 16:14:20 -08004160
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004161 if (mAttachInfo != null) {
4162 mAttachInfo.mKeyDispatchState.reset(this);
4163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 }
4165
4166 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004167 * Sends an accessibility event of the given type. If accessiiblity is
4168 * not enabled this method has no effect. The default implementation calls
4169 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4170 * to populate information about the event source (this View), then calls
4171 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4172 * populate the text content of the event source including its descendants,
4173 * and last calls
4174 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4175 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004176 * <p>
4177 * If an {@link AccessibilityDelegate} has been specified via calling
4178 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4179 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4180 * responsible for handling this call.
4181 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004182 *
Scott Mainb303d832011-10-12 16:45:18 -07004183 * @param eventType The type of the event to send, as defined by several types from
4184 * {@link android.view.accessibility.AccessibilityEvent}, such as
4185 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4186 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004187 *
4188 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4189 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4190 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004191 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004192 */
4193 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004194 if (mAccessibilityDelegate != null) {
4195 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4196 } else {
4197 sendAccessibilityEventInternal(eventType);
4198 }
4199 }
4200
4201 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004202 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4203 * {@link AccessibilityEvent} to make an announcement which is related to some
4204 * sort of a context change for which none of the events representing UI transitions
4205 * is a good fit. For example, announcing a new page in a book. If accessibility
4206 * is not enabled this method does nothing.
4207 *
4208 * @param text The announcement text.
4209 */
4210 public void announceForAccessibility(CharSequence text) {
4211 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4212 AccessibilityEvent event = AccessibilityEvent.obtain(
4213 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4214 event.getText().add(text);
4215 sendAccessibilityEventUnchecked(event);
4216 }
4217 }
4218
4219 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004220 * @see #sendAccessibilityEvent(int)
4221 *
4222 * Note: Called from the default {@link AccessibilityDelegate}.
4223 */
4224 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004225 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4226 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4227 }
4228 }
4229
4230 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004231 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4232 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004233 * perform a check whether accessibility is enabled.
4234 * <p>
4235 * If an {@link AccessibilityDelegate} has been specified via calling
4236 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4237 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4238 * is responsible for handling this call.
4239 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004240 *
4241 * @param event The event to send.
4242 *
4243 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004244 */
4245 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004246 if (mAccessibilityDelegate != null) {
4247 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4248 } else {
4249 sendAccessibilityEventUncheckedInternal(event);
4250 }
4251 }
4252
4253 /**
4254 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4255 *
4256 * Note: Called from the default {@link AccessibilityDelegate}.
4257 */
4258 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004259 if (!isShown()) {
4260 return;
4261 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004262 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004263 // Only a subset of accessibility events populates text content.
4264 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4265 dispatchPopulateAccessibilityEvent(event);
4266 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004267 // In the beginning we called #isShown(), so we know that getParent() is not null.
4268 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004269 }
4270
4271 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004272 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4273 * to its children for adding their text content to the event. Note that the
4274 * event text is populated in a separate dispatch path since we add to the
4275 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004276 * A typical implementation will call
4277 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4278 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4279 * on each child. Override this method if custom population of the event text
4280 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004281 * <p>
4282 * If an {@link AccessibilityDelegate} has been specified via calling
4283 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4284 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4285 * is responsible for handling this call.
4286 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004287 * <p>
4288 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4289 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4290 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004291 *
4292 * @param event The event.
4293 *
4294 * @return True if the event population was completed.
4295 */
4296 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004297 if (mAccessibilityDelegate != null) {
4298 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4299 } else {
4300 return dispatchPopulateAccessibilityEventInternal(event);
4301 }
4302 }
4303
4304 /**
4305 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4306 *
4307 * Note: Called from the default {@link AccessibilityDelegate}.
4308 */
4309 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004310 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004311 return false;
4312 }
4313
4314 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004315 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004316 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004317 * text content. While this method is free to modify event
4318 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004319 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4320 * <p>
4321 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004322 * to the text added by the super implementation:
4323 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004324 * super.onPopulateAccessibilityEvent(event);
4325 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4326 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4327 * mCurrentDate.getTimeInMillis(), flags);
4328 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004329 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004330 * <p>
4331 * If an {@link AccessibilityDelegate} has been specified via calling
4332 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4333 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4334 * is responsible for handling this call.
4335 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004336 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4337 * information to the event, in case the default implementation has basic information to add.
4338 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004339 *
4340 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004341 *
4342 * @see #sendAccessibilityEvent(int)
4343 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004344 */
4345 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004346 if (mAccessibilityDelegate != null) {
4347 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4348 } else {
4349 onPopulateAccessibilityEventInternal(event);
4350 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004351 }
4352
4353 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004354 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4355 *
4356 * Note: Called from the default {@link AccessibilityDelegate}.
4357 */
4358 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4359
4360 }
4361
4362 /**
4363 * Initializes an {@link AccessibilityEvent} with information about
4364 * this View which is the event source. In other words, the source of
4365 * an accessibility event is the view whose state change triggered firing
4366 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004367 * <p>
4368 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004369 * to properties set by the super implementation:
4370 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4371 * super.onInitializeAccessibilityEvent(event);
4372 * event.setPassword(true);
4373 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004374 * <p>
4375 * If an {@link AccessibilityDelegate} has been specified via calling
4376 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4377 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4378 * is responsible for handling this call.
4379 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004380 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4381 * information to the event, in case the default implementation has basic information to add.
4382 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004383 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004384 *
4385 * @see #sendAccessibilityEvent(int)
4386 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4387 */
4388 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004389 if (mAccessibilityDelegate != null) {
4390 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4391 } else {
4392 onInitializeAccessibilityEventInternal(event);
4393 }
4394 }
4395
4396 /**
4397 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4398 *
4399 * Note: Called from the default {@link AccessibilityDelegate}.
4400 */
4401 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004402 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004403 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004404 event.setPackageName(getContext().getPackageName());
4405 event.setEnabled(isEnabled());
4406 event.setContentDescription(mContentDescription);
4407
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004408 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4409 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4410 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4411 FOCUSABLES_ALL);
4412 event.setItemCount(focusablesTempList.size());
4413 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4414 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004415 }
4416 }
4417
4418 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004419 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4420 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4421 * This method is responsible for obtaining an accessibility node info from a
4422 * pool of reusable instances and calling
4423 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4424 * initialize the former.
4425 * <p>
4426 * Note: The client is responsible for recycling the obtained instance by calling
4427 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4428 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004429 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004430 * @return A populated {@link AccessibilityNodeInfo}.
4431 *
4432 * @see AccessibilityNodeInfo
4433 */
4434 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004435 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4436 if (provider != null) {
4437 return provider.createAccessibilityNodeInfo(View.NO_ID);
4438 } else {
4439 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4440 onInitializeAccessibilityNodeInfo(info);
4441 return info;
4442 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004443 }
4444
4445 /**
4446 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4447 * The base implementation sets:
4448 * <ul>
4449 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004450 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4451 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004452 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4453 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4454 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4455 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4456 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4457 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4458 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4459 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4460 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4461 * </ul>
4462 * <p>
4463 * Subclasses should override this method, call the super implementation,
4464 * and set additional attributes.
4465 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004466 * <p>
4467 * If an {@link AccessibilityDelegate} has been specified via calling
4468 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4469 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4470 * is responsible for handling this call.
4471 * </p>
4472 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004473 * @param info The instance to initialize.
4474 */
4475 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004476 if (mAccessibilityDelegate != null) {
4477 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4478 } else {
4479 onInitializeAccessibilityNodeInfoInternal(info);
4480 }
4481 }
4482
4483 /**
4484 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4485 *
4486 * Note: Called from the default {@link AccessibilityDelegate}.
4487 */
4488 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004489 Rect bounds = mAttachInfo.mTmpInvalRect;
4490 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004491 info.setBoundsInParent(bounds);
4492
4493 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4494 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004495 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004496 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4497 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004498
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004499 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4500 ViewParent parent = getParent();
4501 if (parent instanceof View) {
4502 View parentView = (View) parent;
4503 info.setParent(parentView);
4504 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004505 }
4506
4507 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004508 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004509 info.setContentDescription(getContentDescription());
4510
4511 info.setEnabled(isEnabled());
4512 info.setClickable(isClickable());
4513 info.setFocusable(isFocusable());
4514 info.setFocused(isFocused());
4515 info.setSelected(isSelected());
4516 info.setLongClickable(isLongClickable());
4517
4518 // TODO: These make sense only if we are in an AdapterView but all
4519 // views can be selected. Maybe from accessiiblity perspective
4520 // we should report as selectable view in an AdapterView.
4521 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4522 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4523
4524 if (isFocusable()) {
4525 if (isFocused()) {
4526 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4527 } else {
4528 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4529 }
4530 }
4531 }
4532
4533 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004534 * Sets a delegate for implementing accessibility support via compositon as
4535 * opposed to inheritance. The delegate's primary use is for implementing
4536 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4537 *
4538 * @param delegate The delegate instance.
4539 *
4540 * @see AccessibilityDelegate
4541 */
4542 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4543 mAccessibilityDelegate = delegate;
4544 }
4545
4546 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004547 * Gets the provider for managing a virtual view hierarchy rooted at this View
4548 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4549 * that explore the window content.
4550 * <p>
4551 * If this method returns an instance, this instance is responsible for managing
4552 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4553 * View including the one representing the View itself. Similarly the returned
4554 * instance is responsible for performing accessibility actions on any virtual
4555 * view or the root view itself.
4556 * </p>
4557 * <p>
4558 * If an {@link AccessibilityDelegate} has been specified via calling
4559 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4560 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4561 * is responsible for handling this call.
4562 * </p>
4563 *
4564 * @return The provider.
4565 *
4566 * @see AccessibilityNodeProvider
4567 */
4568 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4569 if (mAccessibilityDelegate != null) {
4570 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4571 } else {
4572 return null;
4573 }
4574 }
4575
4576 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004577 * Gets the unique identifier of this view on the screen for accessibility purposes.
4578 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4579 *
4580 * @return The view accessibility id.
4581 *
4582 * @hide
4583 */
4584 public int getAccessibilityViewId() {
4585 if (mAccessibilityViewId == NO_ID) {
4586 mAccessibilityViewId = sNextAccessibilityViewId++;
4587 }
4588 return mAccessibilityViewId;
4589 }
4590
4591 /**
4592 * Gets the unique identifier of the window in which this View reseides.
4593 *
4594 * @return The window accessibility id.
4595 *
4596 * @hide
4597 */
4598 public int getAccessibilityWindowId() {
4599 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4600 }
4601
4602 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004603 * Gets the {@link View} description. It briefly describes the view and is
4604 * primarily used for accessibility support. Set this property to enable
4605 * better accessibility support for your application. This is especially
4606 * true for views that do not have textual representation (For example,
4607 * ImageButton).
4608 *
4609 * @return The content descriptiopn.
4610 *
4611 * @attr ref android.R.styleable#View_contentDescription
4612 */
4613 public CharSequence getContentDescription() {
4614 return mContentDescription;
4615 }
4616
4617 /**
4618 * Sets the {@link View} description. It briefly describes the view and is
4619 * primarily used for accessibility support. Set this property to enable
4620 * better accessibility support for your application. This is especially
4621 * true for views that do not have textual representation (For example,
4622 * ImageButton).
4623 *
4624 * @param contentDescription The content description.
4625 *
4626 * @attr ref android.R.styleable#View_contentDescription
4627 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004628 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004629 public void setContentDescription(CharSequence contentDescription) {
4630 mContentDescription = contentDescription;
4631 }
4632
4633 /**
Romain Guya2431d02009-04-30 16:30:00 -07004634 * Invoked whenever this view loses focus, either by losing window focus or by losing
4635 * focus within its window. This method can be used to clear any state tied to the
4636 * focus. For instance, if a button is held pressed with the trackball and the window
4637 * loses focus, this method can be used to cancel the press.
4638 *
4639 * Subclasses of View overriding this method should always call super.onFocusLost().
4640 *
4641 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004642 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004643 *
4644 * @hide pending API council approval
4645 */
4646 protected void onFocusLost() {
4647 resetPressedState();
4648 }
4649
4650 private void resetPressedState() {
4651 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4652 return;
4653 }
4654
4655 if (isPressed()) {
4656 setPressed(false);
4657
4658 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004659 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004660 }
4661 }
4662 }
4663
4664 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 * Returns true if this view has focus
4666 *
4667 * @return True if this view has focus, false otherwise.
4668 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004669 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 public boolean isFocused() {
4671 return (mPrivateFlags & FOCUSED) != 0;
4672 }
4673
4674 /**
4675 * Find the view in the hierarchy rooted at this view that currently has
4676 * focus.
4677 *
4678 * @return The view that currently has focus, or null if no focused view can
4679 * be found.
4680 */
4681 public View findFocus() {
4682 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4683 }
4684
4685 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004686 * Indicates whether this view is one of the set of scrollable containers in
4687 * its window.
4688 *
4689 * @return whether this view is one of the set of scrollable containers in
4690 * its window
4691 *
4692 * @attr ref android.R.styleable#View_isScrollContainer
4693 */
4694 public boolean isScrollContainer() {
4695 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4696 }
4697
4698 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 * Change whether this view is one of the set of scrollable containers in
4700 * its window. This will be used to determine whether the window can
4701 * resize or must pan when a soft input area is open -- scrollable
4702 * containers allow the window to use resize mode since the container
4703 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004704 *
4705 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004706 */
4707 public void setScrollContainer(boolean isScrollContainer) {
4708 if (isScrollContainer) {
4709 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4710 mAttachInfo.mScrollContainers.add(this);
4711 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4712 }
4713 mPrivateFlags |= SCROLL_CONTAINER;
4714 } else {
4715 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4716 mAttachInfo.mScrollContainers.remove(this);
4717 }
4718 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4719 }
4720 }
4721
4722 /**
4723 * Returns the quality of the drawing cache.
4724 *
4725 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4726 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4727 *
4728 * @see #setDrawingCacheQuality(int)
4729 * @see #setDrawingCacheEnabled(boolean)
4730 * @see #isDrawingCacheEnabled()
4731 *
4732 * @attr ref android.R.styleable#View_drawingCacheQuality
4733 */
4734 public int getDrawingCacheQuality() {
4735 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4736 }
4737
4738 /**
4739 * Set the drawing cache quality of this view. This value is used only when the
4740 * drawing cache is enabled
4741 *
4742 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4743 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4744 *
4745 * @see #getDrawingCacheQuality()
4746 * @see #setDrawingCacheEnabled(boolean)
4747 * @see #isDrawingCacheEnabled()
4748 *
4749 * @attr ref android.R.styleable#View_drawingCacheQuality
4750 */
4751 public void setDrawingCacheQuality(int quality) {
4752 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4753 }
4754
4755 /**
4756 * Returns whether the screen should remain on, corresponding to the current
4757 * value of {@link #KEEP_SCREEN_ON}.
4758 *
4759 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4760 *
4761 * @see #setKeepScreenOn(boolean)
4762 *
4763 * @attr ref android.R.styleable#View_keepScreenOn
4764 */
4765 public boolean getKeepScreenOn() {
4766 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4767 }
4768
4769 /**
4770 * Controls whether the screen should remain on, modifying the
4771 * value of {@link #KEEP_SCREEN_ON}.
4772 *
4773 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4774 *
4775 * @see #getKeepScreenOn()
4776 *
4777 * @attr ref android.R.styleable#View_keepScreenOn
4778 */
4779 public void setKeepScreenOn(boolean keepScreenOn) {
4780 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4781 }
4782
4783 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004784 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4785 * @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 -08004786 *
4787 * @attr ref android.R.styleable#View_nextFocusLeft
4788 */
4789 public int getNextFocusLeftId() {
4790 return mNextFocusLeftId;
4791 }
4792
4793 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004794 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4795 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4796 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 *
4798 * @attr ref android.R.styleable#View_nextFocusLeft
4799 */
4800 public void setNextFocusLeftId(int nextFocusLeftId) {
4801 mNextFocusLeftId = nextFocusLeftId;
4802 }
4803
4804 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004805 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4806 * @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 -08004807 *
4808 * @attr ref android.R.styleable#View_nextFocusRight
4809 */
4810 public int getNextFocusRightId() {
4811 return mNextFocusRightId;
4812 }
4813
4814 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004815 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4816 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4817 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 *
4819 * @attr ref android.R.styleable#View_nextFocusRight
4820 */
4821 public void setNextFocusRightId(int nextFocusRightId) {
4822 mNextFocusRightId = nextFocusRightId;
4823 }
4824
4825 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004826 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4827 * @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 -08004828 *
4829 * @attr ref android.R.styleable#View_nextFocusUp
4830 */
4831 public int getNextFocusUpId() {
4832 return mNextFocusUpId;
4833 }
4834
4835 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004836 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4837 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4838 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839 *
4840 * @attr ref android.R.styleable#View_nextFocusUp
4841 */
4842 public void setNextFocusUpId(int nextFocusUpId) {
4843 mNextFocusUpId = nextFocusUpId;
4844 }
4845
4846 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004847 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4848 * @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 -08004849 *
4850 * @attr ref android.R.styleable#View_nextFocusDown
4851 */
4852 public int getNextFocusDownId() {
4853 return mNextFocusDownId;
4854 }
4855
4856 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004857 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4858 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4859 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 *
4861 * @attr ref android.R.styleable#View_nextFocusDown
4862 */
4863 public void setNextFocusDownId(int nextFocusDownId) {
4864 mNextFocusDownId = nextFocusDownId;
4865 }
4866
4867 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004868 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4869 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4870 *
4871 * @attr ref android.R.styleable#View_nextFocusForward
4872 */
4873 public int getNextFocusForwardId() {
4874 return mNextFocusForwardId;
4875 }
4876
4877 /**
4878 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4879 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4880 * decide automatically.
4881 *
4882 * @attr ref android.R.styleable#View_nextFocusForward
4883 */
4884 public void setNextFocusForwardId(int nextFocusForwardId) {
4885 mNextFocusForwardId = nextFocusForwardId;
4886 }
4887
4888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 * Returns the visibility of this view and all of its ancestors
4890 *
4891 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4892 */
4893 public boolean isShown() {
4894 View current = this;
4895 //noinspection ConstantConditions
4896 do {
4897 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4898 return false;
4899 }
4900 ViewParent parent = current.mParent;
4901 if (parent == null) {
4902 return false; // We are not attached to the view root
4903 }
4904 if (!(parent instanceof View)) {
4905 return true;
4906 }
4907 current = (View) parent;
4908 } while (current != null);
4909
4910 return false;
4911 }
4912
4913 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004914 * Called by the view hierarchy when the content insets for a window have
4915 * changed, to allow it to adjust its content to fit within those windows.
4916 * The content insets tell you the space that the status bar, input method,
4917 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004918 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004919 * <p>You do not normally need to deal with this function, since the default
4920 * window decoration given to applications takes care of applying it to the
4921 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
4922 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
4923 * and your content can be placed under those system elements. You can then
4924 * use this method within your view hierarchy if you have parts of your UI
4925 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004927 * <p>The default implementation of this method simply applies the content
4928 * inset's to the view's padding. This can be enabled through
4929 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
4930 * the method and handle the insets however you would like. Note that the
4931 * insets provided by the framework are always relative to the far edges
4932 * of the window, not accounting for the location of the called view within
4933 * that window. (In fact when this method is called you do not yet know
4934 * where the layout will place the view, as it is done before layout happens.)
4935 *
4936 * <p>Note: unlike many View methods, there is no dispatch phase to this
4937 * call. If you are overriding it in a ViewGroup and want to allow the
4938 * call to continue to your children, you must be sure to call the super
4939 * implementation.
4940 *
4941 * @param insets Current content insets of the window. Prior to
4942 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
4943 * the insets or else you and Android will be unhappy.
4944 *
4945 * @return Return true if this view applied the insets and it should not
4946 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 */
4948 protected boolean fitSystemWindows(Rect insets) {
4949 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004950 mUserPaddingStart = -1;
4951 mUserPaddingEnd = -1;
4952 mUserPaddingRelative = false;
4953 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
4954 || mAttachInfo == null
4955 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
4956 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
4957 return true;
4958 } else {
4959 internalSetPadding(0, 0, 0, 0);
4960 return false;
4961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 }
4963 return false;
4964 }
4965
4966 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004967 * Set whether or not this view should account for system screen decorations
4968 * such as the status bar and inset its content. This allows this view to be
4969 * positioned in absolute screen coordinates and remain visible to the user.
4970 *
4971 * <p>This should only be used by top-level window decor views.
4972 *
4973 * @param fitSystemWindows true to inset content for system screen decorations, false for
4974 * default behavior.
4975 *
4976 * @attr ref android.R.styleable#View_fitsSystemWindows
4977 */
4978 public void setFitsSystemWindows(boolean fitSystemWindows) {
4979 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4980 }
4981
4982 /**
4983 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4984 * will account for system screen decorations such as the status bar and inset its
4985 * content. This allows the view to be positioned in absolute screen coordinates
4986 * and remain visible to the user.
4987 *
4988 * @return true if this view will adjust its content bounds for system screen decorations.
4989 *
4990 * @attr ref android.R.styleable#View_fitsSystemWindows
4991 */
4992 public boolean fitsSystemWindows() {
4993 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4994 }
4995
4996 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004997 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
4998 */
4999 public void requestFitSystemWindows() {
5000 if (mParent != null) {
5001 mParent.requestFitSystemWindows();
5002 }
5003 }
5004
5005 /**
5006 * For use by PhoneWindow to make its own system window fitting optional.
5007 * @hide
5008 */
5009 public void makeOptionalFitsSystemWindows() {
5010 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5011 }
5012
5013 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 * Returns the visibility status for this view.
5015 *
5016 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5017 * @attr ref android.R.styleable#View_visibility
5018 */
5019 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005020 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5021 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5022 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005023 })
5024 public int getVisibility() {
5025 return mViewFlags & VISIBILITY_MASK;
5026 }
5027
5028 /**
5029 * Set the enabled state of this view.
5030 *
5031 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5032 * @attr ref android.R.styleable#View_visibility
5033 */
5034 @RemotableViewMethod
5035 public void setVisibility(int visibility) {
5036 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005037 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 }
5039
5040 /**
5041 * Returns the enabled status for this view. The interpretation of the
5042 * enabled state varies by subclass.
5043 *
5044 * @return True if this view is enabled, false otherwise.
5045 */
5046 @ViewDebug.ExportedProperty
5047 public boolean isEnabled() {
5048 return (mViewFlags & ENABLED_MASK) == ENABLED;
5049 }
5050
5051 /**
5052 * Set the enabled state of this view. The interpretation of the enabled
5053 * state varies by subclass.
5054 *
5055 * @param enabled True if this view is enabled, false otherwise.
5056 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005057 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005059 if (enabled == isEnabled()) return;
5060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005061 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5062
5063 /*
5064 * The View most likely has to change its appearance, so refresh
5065 * the drawable state.
5066 */
5067 refreshDrawableState();
5068
5069 // Invalidate too, since the default behavior for views is to be
5070 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005071 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 }
5073
5074 /**
5075 * Set whether this view can receive the focus.
5076 *
5077 * Setting this to false will also ensure that this view is not focusable
5078 * in touch mode.
5079 *
5080 * @param focusable If true, this view can receive the focus.
5081 *
5082 * @see #setFocusableInTouchMode(boolean)
5083 * @attr ref android.R.styleable#View_focusable
5084 */
5085 public void setFocusable(boolean focusable) {
5086 if (!focusable) {
5087 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5088 }
5089 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5090 }
5091
5092 /**
5093 * Set whether this view can receive focus while in touch mode.
5094 *
5095 * Setting this to true will also ensure that this view is focusable.
5096 *
5097 * @param focusableInTouchMode If true, this view can receive the focus while
5098 * in touch mode.
5099 *
5100 * @see #setFocusable(boolean)
5101 * @attr ref android.R.styleable#View_focusableInTouchMode
5102 */
5103 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5104 // Focusable in touch mode should always be set before the focusable flag
5105 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5106 // which, in touch mode, will not successfully request focus on this view
5107 // because the focusable in touch mode flag is not set
5108 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5109 if (focusableInTouchMode) {
5110 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5111 }
5112 }
5113
5114 /**
5115 * Set whether this view should have sound effects enabled for events such as
5116 * clicking and touching.
5117 *
5118 * <p>You may wish to disable sound effects for a view if you already play sounds,
5119 * for instance, a dial key that plays dtmf tones.
5120 *
5121 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5122 * @see #isSoundEffectsEnabled()
5123 * @see #playSoundEffect(int)
5124 * @attr ref android.R.styleable#View_soundEffectsEnabled
5125 */
5126 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5127 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5128 }
5129
5130 /**
5131 * @return whether this view should have sound effects enabled for events such as
5132 * clicking and touching.
5133 *
5134 * @see #setSoundEffectsEnabled(boolean)
5135 * @see #playSoundEffect(int)
5136 * @attr ref android.R.styleable#View_soundEffectsEnabled
5137 */
5138 @ViewDebug.ExportedProperty
5139 public boolean isSoundEffectsEnabled() {
5140 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5141 }
5142
5143 /**
5144 * Set whether this view should have haptic feedback for events such as
5145 * long presses.
5146 *
5147 * <p>You may wish to disable haptic feedback if your view already controls
5148 * its own haptic feedback.
5149 *
5150 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5151 * @see #isHapticFeedbackEnabled()
5152 * @see #performHapticFeedback(int)
5153 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5154 */
5155 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5156 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5157 }
5158
5159 /**
5160 * @return whether this view should have haptic feedback enabled for events
5161 * long presses.
5162 *
5163 * @see #setHapticFeedbackEnabled(boolean)
5164 * @see #performHapticFeedback(int)
5165 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5166 */
5167 @ViewDebug.ExportedProperty
5168 public boolean isHapticFeedbackEnabled() {
5169 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5170 }
5171
5172 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005173 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005174 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005175 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5176 * {@link #LAYOUT_DIRECTION_RTL},
5177 * {@link #LAYOUT_DIRECTION_INHERIT} or
5178 * {@link #LAYOUT_DIRECTION_LOCALE}.
5179 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005180 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005181 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005182 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5183 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5184 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5185 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005186 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005187 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005188 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005189 }
5190
5191 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005192 * Set the layout direction for this view. This will propagate a reset of layout direction
5193 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005194 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005195 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5196 * {@link #LAYOUT_DIRECTION_RTL},
5197 * {@link #LAYOUT_DIRECTION_INHERIT} or
5198 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005199 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005200 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005201 */
5202 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005203 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005204 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005205 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005206 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005207 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005208 // Set the new layout direction (filtered) and ask for a layout pass
5209 mPrivateFlags2 |=
5210 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5211 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005212 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005213 }
5214
5215 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005216 * Returns the resolved layout direction for this view.
5217 *
5218 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005219 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005220 */
5221 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005222 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5223 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005224 })
5225 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005226 // The layout diretion will be resolved only if needed
5227 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5228 resolveLayoutDirection();
5229 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005230 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005231 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5232 }
5233
5234 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005235 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5236 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005237 *
5238 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005239 */
5240 @ViewDebug.ExportedProperty(category = "layout")
5241 public boolean isLayoutRtl() {
5242 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5243 }
5244
5245 /**
Adam Powell539ee872012-02-03 19:00:49 -08005246 * Indicates whether the view is currently tracking transient state that the
5247 * app should not need to concern itself with saving and restoring, but that
5248 * the framework should take special note to preserve when possible.
5249 *
5250 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005251 */
5252 @ViewDebug.ExportedProperty(category = "layout")
5253 public boolean hasTransientState() {
5254 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5255 }
5256
5257 /**
5258 * Set whether this view is currently tracking transient state that the
5259 * framework should attempt to preserve when possible.
5260 *
5261 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005262 */
5263 public void setHasTransientState(boolean hasTransientState) {
5264 if (hasTransientState() == hasTransientState) return;
5265
5266 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5267 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5268 if (mParent != null) {
5269 try {
5270 mParent.childHasTransientStateChanged(this, hasTransientState);
5271 } catch (AbstractMethodError e) {
5272 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5273 " does not fully implement ViewParent", e);
5274 }
5275 }
5276 }
5277
5278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 * If this view doesn't do any drawing on its own, set this flag to
5280 * allow further optimizations. By default, this flag is not set on
5281 * View, but could be set on some View subclasses such as ViewGroup.
5282 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005283 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5284 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 *
5286 * @param willNotDraw whether or not this View draw on its own
5287 */
5288 public void setWillNotDraw(boolean willNotDraw) {
5289 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5290 }
5291
5292 /**
5293 * Returns whether or not this View draws on its own.
5294 *
5295 * @return true if this view has nothing to draw, false otherwise
5296 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005297 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005298 public boolean willNotDraw() {
5299 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5300 }
5301
5302 /**
5303 * When a View's drawing cache is enabled, drawing is redirected to an
5304 * offscreen bitmap. Some views, like an ImageView, must be able to
5305 * bypass this mechanism if they already draw a single bitmap, to avoid
5306 * unnecessary usage of the memory.
5307 *
5308 * @param willNotCacheDrawing true if this view does not cache its
5309 * drawing, false otherwise
5310 */
5311 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5312 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5313 }
5314
5315 /**
5316 * Returns whether or not this View can cache its drawing or not.
5317 *
5318 * @return true if this view does not cache its drawing, false otherwise
5319 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005320 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 public boolean willNotCacheDrawing() {
5322 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5323 }
5324
5325 /**
5326 * Indicates whether this view reacts to click events or not.
5327 *
5328 * @return true if the view is clickable, false otherwise
5329 *
5330 * @see #setClickable(boolean)
5331 * @attr ref android.R.styleable#View_clickable
5332 */
5333 @ViewDebug.ExportedProperty
5334 public boolean isClickable() {
5335 return (mViewFlags & CLICKABLE) == CLICKABLE;
5336 }
5337
5338 /**
5339 * Enables or disables click events for this view. When a view
5340 * is clickable it will change its state to "pressed" on every click.
5341 * Subclasses should set the view clickable to visually react to
5342 * user's clicks.
5343 *
5344 * @param clickable true to make the view clickable, false otherwise
5345 *
5346 * @see #isClickable()
5347 * @attr ref android.R.styleable#View_clickable
5348 */
5349 public void setClickable(boolean clickable) {
5350 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5351 }
5352
5353 /**
5354 * Indicates whether this view reacts to long click events or not.
5355 *
5356 * @return true if the view is long clickable, false otherwise
5357 *
5358 * @see #setLongClickable(boolean)
5359 * @attr ref android.R.styleable#View_longClickable
5360 */
5361 public boolean isLongClickable() {
5362 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5363 }
5364
5365 /**
5366 * Enables or disables long click events for this view. When a view is long
5367 * clickable it reacts to the user holding down the button for a longer
5368 * duration than a tap. This event can either launch the listener or a
5369 * context menu.
5370 *
5371 * @param longClickable true to make the view long clickable, false otherwise
5372 * @see #isLongClickable()
5373 * @attr ref android.R.styleable#View_longClickable
5374 */
5375 public void setLongClickable(boolean longClickable) {
5376 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5377 }
5378
5379 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005380 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 *
5382 * @see #isClickable()
5383 * @see #setClickable(boolean)
5384 *
5385 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5386 * the View's internal state from a previously set "pressed" state.
5387 */
5388 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005389 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 if (pressed) {
5392 mPrivateFlags |= PRESSED;
5393 } else {
5394 mPrivateFlags &= ~PRESSED;
5395 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005396
5397 if (needsRefresh) {
5398 refreshDrawableState();
5399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 dispatchSetPressed(pressed);
5401 }
5402
5403 /**
5404 * Dispatch setPressed to all of this View's children.
5405 *
5406 * @see #setPressed(boolean)
5407 *
5408 * @param pressed The new pressed state
5409 */
5410 protected void dispatchSetPressed(boolean pressed) {
5411 }
5412
5413 /**
5414 * Indicates whether the view is currently in pressed state. Unless
5415 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5416 * the pressed state.
5417 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005418 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 * @see #isClickable()
5420 * @see #setClickable(boolean)
5421 *
5422 * @return true if the view is currently pressed, false otherwise
5423 */
5424 public boolean isPressed() {
5425 return (mPrivateFlags & PRESSED) == PRESSED;
5426 }
5427
5428 /**
5429 * Indicates whether this view will save its state (that is,
5430 * whether its {@link #onSaveInstanceState} method will be called).
5431 *
5432 * @return Returns true if the view state saving is enabled, else false.
5433 *
5434 * @see #setSaveEnabled(boolean)
5435 * @attr ref android.R.styleable#View_saveEnabled
5436 */
5437 public boolean isSaveEnabled() {
5438 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5439 }
5440
5441 /**
5442 * Controls whether the saving of this view's state is
5443 * enabled (that is, whether its {@link #onSaveInstanceState} method
5444 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005445 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 * for its state to be saved. This flag can only disable the
5447 * saving of this view; any child views may still have their state saved.
5448 *
5449 * @param enabled Set to false to <em>disable</em> state saving, or true
5450 * (the default) to allow it.
5451 *
5452 * @see #isSaveEnabled()
5453 * @see #setId(int)
5454 * @see #onSaveInstanceState()
5455 * @attr ref android.R.styleable#View_saveEnabled
5456 */
5457 public void setSaveEnabled(boolean enabled) {
5458 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5459 }
5460
Jeff Brown85a31762010-09-01 17:01:00 -07005461 /**
5462 * Gets whether the framework should discard touches when the view's
5463 * window is obscured by another visible window.
5464 * Refer to the {@link View} security documentation for more details.
5465 *
5466 * @return True if touch filtering is enabled.
5467 *
5468 * @see #setFilterTouchesWhenObscured(boolean)
5469 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5470 */
5471 @ViewDebug.ExportedProperty
5472 public boolean getFilterTouchesWhenObscured() {
5473 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5474 }
5475
5476 /**
5477 * Sets whether the framework should discard touches when the view's
5478 * window is obscured by another visible window.
5479 * Refer to the {@link View} security documentation for more details.
5480 *
5481 * @param enabled True if touch filtering should be enabled.
5482 *
5483 * @see #getFilterTouchesWhenObscured
5484 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5485 */
5486 public void setFilterTouchesWhenObscured(boolean enabled) {
5487 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5488 FILTER_TOUCHES_WHEN_OBSCURED);
5489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490
5491 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005492 * Indicates whether the entire hierarchy under this view will save its
5493 * state when a state saving traversal occurs from its parent. The default
5494 * is true; if false, these views will not be saved unless
5495 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5496 *
5497 * @return Returns true if the view state saving from parent is enabled, else false.
5498 *
5499 * @see #setSaveFromParentEnabled(boolean)
5500 */
5501 public boolean isSaveFromParentEnabled() {
5502 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5503 }
5504
5505 /**
5506 * Controls whether the entire hierarchy under this view will save its
5507 * state when a state saving traversal occurs from its parent. The default
5508 * is true; if false, these views will not be saved unless
5509 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5510 *
5511 * @param enabled Set to false to <em>disable</em> state saving, or true
5512 * (the default) to allow it.
5513 *
5514 * @see #isSaveFromParentEnabled()
5515 * @see #setId(int)
5516 * @see #onSaveInstanceState()
5517 */
5518 public void setSaveFromParentEnabled(boolean enabled) {
5519 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5520 }
5521
5522
5523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 * Returns whether this View is able to take focus.
5525 *
5526 * @return True if this view can take focus, or false otherwise.
5527 * @attr ref android.R.styleable#View_focusable
5528 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005529 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005530 public final boolean isFocusable() {
5531 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5532 }
5533
5534 /**
5535 * When a view is focusable, it may not want to take focus when in touch mode.
5536 * For example, a button would like focus when the user is navigating via a D-pad
5537 * so that the user can click on it, but once the user starts touching the screen,
5538 * the button shouldn't take focus
5539 * @return Whether the view is focusable in touch mode.
5540 * @attr ref android.R.styleable#View_focusableInTouchMode
5541 */
5542 @ViewDebug.ExportedProperty
5543 public final boolean isFocusableInTouchMode() {
5544 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5545 }
5546
5547 /**
5548 * Find the nearest view in the specified direction that can take focus.
5549 * This does not actually give focus to that view.
5550 *
5551 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5552 *
5553 * @return The nearest focusable in the specified direction, or null if none
5554 * can be found.
5555 */
5556 public View focusSearch(int direction) {
5557 if (mParent != null) {
5558 return mParent.focusSearch(this, direction);
5559 } else {
5560 return null;
5561 }
5562 }
5563
5564 /**
5565 * This method is the last chance for the focused view and its ancestors to
5566 * respond to an arrow key. This is called when the focused view did not
5567 * consume the key internally, nor could the view system find a new view in
5568 * the requested direction to give focus to.
5569 *
5570 * @param focused The currently focused view.
5571 * @param direction The direction focus wants to move. One of FOCUS_UP,
5572 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5573 * @return True if the this view consumed this unhandled move.
5574 */
5575 public boolean dispatchUnhandledMove(View focused, int direction) {
5576 return false;
5577 }
5578
5579 /**
5580 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005581 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005583 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5584 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 * @return The user specified next view, or null if there is none.
5586 */
5587 View findUserSetNextFocus(View root, int direction) {
5588 switch (direction) {
5589 case FOCUS_LEFT:
5590 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005591 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 case FOCUS_RIGHT:
5593 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005594 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005595 case FOCUS_UP:
5596 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005597 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 case FOCUS_DOWN:
5599 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005600 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005601 case FOCUS_FORWARD:
5602 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005603 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005604 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005605 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005606 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005607 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005608 @Override
5609 public boolean apply(View t) {
5610 return t.mNextFocusForwardId == id;
5611 }
5612 });
5613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614 }
5615 return null;
5616 }
5617
Jeff Brown4dfbec22011-08-15 14:55:37 -07005618 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5619 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5620 @Override
5621 public boolean apply(View t) {
5622 return t.mID == childViewId;
5623 }
5624 });
5625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 if (result == null) {
5627 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5628 + "by user for id " + childViewId);
5629 }
5630 return result;
5631 }
5632
5633 /**
5634 * Find and return all focusable views that are descendants of this view,
5635 * possibly including this view if it is focusable itself.
5636 *
5637 * @param direction The direction of the focus
5638 * @return A list of focusable views
5639 */
5640 public ArrayList<View> getFocusables(int direction) {
5641 ArrayList<View> result = new ArrayList<View>(24);
5642 addFocusables(result, direction);
5643 return result;
5644 }
5645
5646 /**
5647 * Add any focusable views that are descendants of this view (possibly
5648 * including this view if it is focusable itself) to views. If we are in touch mode,
5649 * only add views that are also focusable in touch mode.
5650 *
5651 * @param views Focusable views found so far
5652 * @param direction The direction of the focus
5653 */
5654 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005655 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657
svetoslavganov75986cf2009-05-14 22:28:01 -07005658 /**
5659 * Adds any focusable views that are descendants of this view (possibly
5660 * including this view if it is focusable itself) to views. This method
5661 * adds all focusable views regardless if we are in touch mode or
5662 * only views focusable in touch mode if we are in touch mode depending on
5663 * the focusable mode paramater.
5664 *
5665 * @param views Focusable views found so far or null if all we are interested is
5666 * the number of focusables.
5667 * @param direction The direction of the focus.
5668 * @param focusableMode The type of focusables to be added.
5669 *
5670 * @see #FOCUSABLES_ALL
5671 * @see #FOCUSABLES_TOUCH_MODE
5672 */
5673 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5674 if (!isFocusable()) {
5675 return;
5676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677
svetoslavganov75986cf2009-05-14 22:28:01 -07005678 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5679 isInTouchMode() && !isFocusableInTouchMode()) {
5680 return;
5681 }
5682
5683 if (views != null) {
5684 views.add(this);
5685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 }
5687
5688 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005689 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005690 * The search is performed by either the text that the View renders or the content
5691 * description that describes the view for accessibility purposes and the view does
5692 * not render or both. Clients can specify how the search is to be performed via
5693 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5694 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005695 *
5696 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005697 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005698 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005699 * @see #FIND_VIEWS_WITH_TEXT
5700 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5701 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005702 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005703 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005704 if (getAccessibilityNodeProvider() != null) {
5705 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5706 outViews.add(this);
5707 }
5708 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5709 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005710 String searchedLowerCase = searched.toString().toLowerCase();
5711 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5712 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5713 outViews.add(this);
5714 }
5715 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005716 }
5717
5718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719 * Find and return all touchable views that are descendants of this view,
5720 * possibly including this view if it is touchable itself.
5721 *
5722 * @return A list of touchable views
5723 */
5724 public ArrayList<View> getTouchables() {
5725 ArrayList<View> result = new ArrayList<View>();
5726 addTouchables(result);
5727 return result;
5728 }
5729
5730 /**
5731 * Add any touchable views that are descendants of this view (possibly
5732 * including this view if it is touchable itself) to views.
5733 *
5734 * @param views Touchable views found so far
5735 */
5736 public void addTouchables(ArrayList<View> views) {
5737 final int viewFlags = mViewFlags;
5738
5739 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5740 && (viewFlags & ENABLED_MASK) == ENABLED) {
5741 views.add(this);
5742 }
5743 }
5744
5745 /**
5746 * Call this to try to give focus to a specific view or to one of its
5747 * descendants.
5748 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005749 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5750 * false), or if it is focusable and it is not focusable in touch mode
5751 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005752 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005753 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 * have focus, and you want your parent to look for the next one.
5755 *
5756 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5757 * {@link #FOCUS_DOWN} and <code>null</code>.
5758 *
5759 * @return Whether this view or one of its descendants actually took focus.
5760 */
5761 public final boolean requestFocus() {
5762 return requestFocus(View.FOCUS_DOWN);
5763 }
5764
5765
5766 /**
5767 * Call this to try to give focus to a specific view or to one of its
5768 * descendants and give it a hint about what direction focus is heading.
5769 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005770 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5771 * false), or if it is focusable and it is not focusable in touch mode
5772 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005774 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 * have focus, and you want your parent to look for the next one.
5776 *
5777 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5778 * <code>null</code> set for the previously focused rectangle.
5779 *
5780 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5781 * @return Whether this view or one of its descendants actually took focus.
5782 */
5783 public final boolean requestFocus(int direction) {
5784 return requestFocus(direction, null);
5785 }
5786
5787 /**
5788 * Call this to try to give focus to a specific view or to one of its descendants
5789 * and give it hints about the direction and a specific rectangle that the focus
5790 * is coming from. The rectangle can help give larger views a finer grained hint
5791 * about where focus is coming from, and therefore, where to show selection, or
5792 * forward focus change internally.
5793 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005794 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5795 * false), or if it is focusable and it is not focusable in touch mode
5796 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 *
5798 * A View will not take focus if it is not visible.
5799 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005800 * A View will not take focus if one of its parents has
5801 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5802 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005804 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 * have focus, and you want your parent to look for the next one.
5806 *
5807 * You may wish to override this method if your custom {@link View} has an internal
5808 * {@link View} that it wishes to forward the request to.
5809 *
5810 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5811 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5812 * to give a finer grained hint about where focus is coming from. May be null
5813 * if there is no hint.
5814 * @return Whether this view or one of its descendants actually took focus.
5815 */
5816 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5817 // need to be focusable
5818 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5819 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5820 return false;
5821 }
5822
5823 // need to be focusable in touch mode if in touch mode
5824 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005825 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5826 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005827 }
5828
5829 // need to not have any parents blocking us
5830 if (hasAncestorThatBlocksDescendantFocus()) {
5831 return false;
5832 }
5833
5834 handleFocusGainInternal(direction, previouslyFocusedRect);
5835 return true;
5836 }
5837
5838 /**
5839 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5840 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5841 * touch mode to request focus when they are touched.
5842 *
5843 * @return Whether this view or one of its descendants actually took focus.
5844 *
5845 * @see #isInTouchMode()
5846 *
5847 */
5848 public final boolean requestFocusFromTouch() {
5849 // Leave touch mode if we need to
5850 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005851 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005852 if (viewRoot != null) {
5853 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 }
5855 }
5856 return requestFocus(View.FOCUS_DOWN);
5857 }
5858
5859 /**
5860 * @return Whether any ancestor of this view blocks descendant focus.
5861 */
5862 private boolean hasAncestorThatBlocksDescendantFocus() {
5863 ViewParent ancestor = mParent;
5864 while (ancestor instanceof ViewGroup) {
5865 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5866 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5867 return true;
5868 } else {
5869 ancestor = vgAncestor.getParent();
5870 }
5871 }
5872 return false;
5873 }
5874
5875 /**
Romain Guya440b002010-02-24 15:57:54 -08005876 * @hide
5877 */
5878 public void dispatchStartTemporaryDetach() {
5879 onStartTemporaryDetach();
5880 }
5881
5882 /**
5883 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5885 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005886 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 */
5888 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005889 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005890 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005891 }
5892
5893 /**
5894 * @hide
5895 */
5896 public void dispatchFinishTemporaryDetach() {
5897 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 }
Romain Guy8506ab42009-06-11 17:35:47 -07005899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 /**
5901 * Called after {@link #onStartTemporaryDetach} when the container is done
5902 * changing the view.
5903 */
5904 public void onFinishTemporaryDetach() {
5905 }
Romain Guy8506ab42009-06-11 17:35:47 -07005906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005908 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5909 * for this view's window. Returns null if the view is not currently attached
5910 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005911 * just use the standard high-level event callbacks like
5912 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005913 */
5914 public KeyEvent.DispatcherState getKeyDispatcherState() {
5915 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5916 }
Joe Malin32736f02011-01-19 16:14:20 -08005917
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005918 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 * Dispatch a key event before it is processed by any input method
5920 * associated with the view hierarchy. This can be used to intercept
5921 * key events in special situations before the IME consumes them; a
5922 * typical example would be handling the BACK key to update the application's
5923 * UI instead of allowing the IME to see it and close itself.
5924 *
5925 * @param event The key event to be dispatched.
5926 * @return True if the event was handled, false otherwise.
5927 */
5928 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5929 return onKeyPreIme(event.getKeyCode(), event);
5930 }
5931
5932 /**
5933 * Dispatch a key event to the next view on the focus path. This path runs
5934 * from the top of the view tree down to the currently focused view. If this
5935 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5936 * the next node down the focus path. This method also fires any key
5937 * listeners.
5938 *
5939 * @param event The key event to be dispatched.
5940 * @return True if the event was handled, false otherwise.
5941 */
5942 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005943 if (mInputEventConsistencyVerifier != null) {
5944 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946
Jeff Brown21bc5c92011-02-28 18:27:14 -08005947 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005948 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005949 ListenerInfo li = mListenerInfo;
5950 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5951 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 return true;
5953 }
5954
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005955 if (event.dispatch(this, mAttachInfo != null
5956 ? mAttachInfo.mKeyDispatchState : null, this)) {
5957 return true;
5958 }
5959
5960 if (mInputEventConsistencyVerifier != null) {
5961 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5962 }
5963 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 }
5965
5966 /**
5967 * Dispatches a key shortcut event.
5968 *
5969 * @param event The key event to be dispatched.
5970 * @return True if the event was handled by the view, false otherwise.
5971 */
5972 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5973 return onKeyShortcut(event.getKeyCode(), event);
5974 }
5975
5976 /**
5977 * Pass the touch screen motion event down to the target view, or this
5978 * view if it is the target.
5979 *
5980 * @param event The motion event to be dispatched.
5981 * @return True if the event was handled by the view, false otherwise.
5982 */
5983 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005984 if (mInputEventConsistencyVerifier != null) {
5985 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5986 }
5987
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005988 if (onFilterTouchEventForSecurity(event)) {
5989 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005990 ListenerInfo li = mListenerInfo;
5991 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5992 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005993 return true;
5994 }
5995
5996 if (onTouchEvent(event)) {
5997 return true;
5998 }
Jeff Brown85a31762010-09-01 17:01:00 -07005999 }
6000
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006001 if (mInputEventConsistencyVerifier != null) {
6002 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006004 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 }
6006
6007 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006008 * Filter the touch event to apply security policies.
6009 *
6010 * @param event The motion event to be filtered.
6011 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006012 *
Jeff Brown85a31762010-09-01 17:01:00 -07006013 * @see #getFilterTouchesWhenObscured
6014 */
6015 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006016 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006017 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6018 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6019 // Window is obscured, drop this touch.
6020 return false;
6021 }
6022 return true;
6023 }
6024
6025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006026 * Pass a trackball motion event down to the focused view.
6027 *
6028 * @param event The motion event to be dispatched.
6029 * @return True if the event was handled by the view, false otherwise.
6030 */
6031 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006032 if (mInputEventConsistencyVerifier != null) {
6033 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6034 }
6035
Romain Guy02ccac62011-06-24 13:20:23 -07006036 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 }
6038
6039 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006040 * Dispatch a generic motion event.
6041 * <p>
6042 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6043 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006044 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006045 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006046 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006047 *
6048 * @param event The motion event to be dispatched.
6049 * @return True if the event was handled by the view, false otherwise.
6050 */
6051 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006052 if (mInputEventConsistencyVerifier != null) {
6053 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6054 }
6055
Jeff Browna032cc02011-03-07 16:56:21 -08006056 final int source = event.getSource();
6057 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6058 final int action = event.getAction();
6059 if (action == MotionEvent.ACTION_HOVER_ENTER
6060 || action == MotionEvent.ACTION_HOVER_MOVE
6061 || action == MotionEvent.ACTION_HOVER_EXIT) {
6062 if (dispatchHoverEvent(event)) {
6063 return true;
6064 }
6065 } else if (dispatchGenericPointerEvent(event)) {
6066 return true;
6067 }
6068 } else if (dispatchGenericFocusedEvent(event)) {
6069 return true;
6070 }
6071
Jeff Brown10b62902011-06-20 16:40:37 -07006072 if (dispatchGenericMotionEventInternal(event)) {
6073 return true;
6074 }
6075
6076 if (mInputEventConsistencyVerifier != null) {
6077 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6078 }
6079 return false;
6080 }
6081
6082 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006083 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006084 ListenerInfo li = mListenerInfo;
6085 if (li != null && li.mOnGenericMotionListener != null
6086 && (mViewFlags & ENABLED_MASK) == ENABLED
6087 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006088 return true;
6089 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006090
6091 if (onGenericMotionEvent(event)) {
6092 return true;
6093 }
6094
6095 if (mInputEventConsistencyVerifier != null) {
6096 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6097 }
6098 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006099 }
6100
6101 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006102 * Dispatch a hover event.
6103 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006104 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006105 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006106 * </p>
6107 *
6108 * @param event The motion event to be dispatched.
6109 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006110 */
6111 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006112 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006113 ListenerInfo li = mListenerInfo;
6114 if (li != null && li.mOnHoverListener != null
6115 && (mViewFlags & ENABLED_MASK) == ENABLED
6116 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006117 return true;
6118 }
6119
Jeff Browna032cc02011-03-07 16:56:21 -08006120 return onHoverEvent(event);
6121 }
6122
6123 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006124 * Returns true if the view has a child to which it has recently sent
6125 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6126 * it does not have a hovered child, then it must be the innermost hovered view.
6127 * @hide
6128 */
6129 protected boolean hasHoveredChild() {
6130 return false;
6131 }
6132
6133 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006134 * Dispatch a generic motion event to the view under the first pointer.
6135 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006136 * Do not call this method directly.
6137 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006138 * </p>
6139 *
6140 * @param event The motion event to be dispatched.
6141 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006142 */
6143 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6144 return false;
6145 }
6146
6147 /**
6148 * Dispatch a generic motion event to the currently focused view.
6149 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006150 * Do not call this method directly.
6151 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006152 * </p>
6153 *
6154 * @param event The motion event to be dispatched.
6155 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006156 */
6157 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6158 return false;
6159 }
6160
6161 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006162 * Dispatch a pointer event.
6163 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006164 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6165 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6166 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006167 * and should not be expected to handle other pointing device features.
6168 * </p>
6169 *
6170 * @param event The motion event to be dispatched.
6171 * @return True if the event was handled by the view, false otherwise.
6172 * @hide
6173 */
6174 public final boolean dispatchPointerEvent(MotionEvent event) {
6175 if (event.isTouchEvent()) {
6176 return dispatchTouchEvent(event);
6177 } else {
6178 return dispatchGenericMotionEvent(event);
6179 }
6180 }
6181
6182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 * Called when the window containing this view gains or loses window focus.
6184 * ViewGroups should override to route to their children.
6185 *
6186 * @param hasFocus True if the window containing this view now has focus,
6187 * false otherwise.
6188 */
6189 public void dispatchWindowFocusChanged(boolean hasFocus) {
6190 onWindowFocusChanged(hasFocus);
6191 }
6192
6193 /**
6194 * Called when the window containing this view gains or loses focus. Note
6195 * that this is separate from view focus: to receive key events, both
6196 * your view and its window must have focus. If a window is displayed
6197 * on top of yours that takes input focus, then your own window will lose
6198 * focus but the view focus will remain unchanged.
6199 *
6200 * @param hasWindowFocus True if the window containing this view now has
6201 * focus, false otherwise.
6202 */
6203 public void onWindowFocusChanged(boolean hasWindowFocus) {
6204 InputMethodManager imm = InputMethodManager.peekInstance();
6205 if (!hasWindowFocus) {
6206 if (isPressed()) {
6207 setPressed(false);
6208 }
6209 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6210 imm.focusOut(this);
6211 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006212 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006213 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006214 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6216 imm.focusIn(this);
6217 }
6218 refreshDrawableState();
6219 }
6220
6221 /**
6222 * Returns true if this view is in a window that currently has window focus.
6223 * Note that this is not the same as the view itself having focus.
6224 *
6225 * @return True if this view is in a window that currently has window focus.
6226 */
6227 public boolean hasWindowFocus() {
6228 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6229 }
6230
6231 /**
Adam Powell326d8082009-12-09 15:10:07 -08006232 * Dispatch a view visibility change down the view hierarchy.
6233 * ViewGroups should override to route to their children.
6234 * @param changedView The view whose visibility changed. Could be 'this' or
6235 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006236 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6237 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006238 */
6239 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6240 onVisibilityChanged(changedView, visibility);
6241 }
6242
6243 /**
6244 * Called when the visibility of the view or an ancestor of the view is changed.
6245 * @param changedView The view whose visibility changed. Could be 'this' or
6246 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006247 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6248 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006249 */
6250 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006251 if (visibility == VISIBLE) {
6252 if (mAttachInfo != null) {
6253 initialAwakenScrollBars();
6254 } else {
6255 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6256 }
6257 }
Adam Powell326d8082009-12-09 15:10:07 -08006258 }
6259
6260 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006261 * Dispatch a hint about whether this view is displayed. For instance, when
6262 * a View moves out of the screen, it might receives a display hint indicating
6263 * the view is not displayed. Applications should not <em>rely</em> on this hint
6264 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006265 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006266 * @param hint A hint about whether or not this view is displayed:
6267 * {@link #VISIBLE} or {@link #INVISIBLE}.
6268 */
6269 public void dispatchDisplayHint(int hint) {
6270 onDisplayHint(hint);
6271 }
6272
6273 /**
6274 * Gives this view a hint about whether is displayed or not. For instance, when
6275 * a View moves out of the screen, it might receives a display hint indicating
6276 * the view is not displayed. Applications should not <em>rely</em> on this hint
6277 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006278 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006279 * @param hint A hint about whether or not this view is displayed:
6280 * {@link #VISIBLE} or {@link #INVISIBLE}.
6281 */
6282 protected void onDisplayHint(int hint) {
6283 }
6284
6285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 * Dispatch a window visibility change down the view hierarchy.
6287 * ViewGroups should override to route to their children.
6288 *
6289 * @param visibility The new visibility of the window.
6290 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006291 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 */
6293 public void dispatchWindowVisibilityChanged(int visibility) {
6294 onWindowVisibilityChanged(visibility);
6295 }
6296
6297 /**
6298 * Called when the window containing has change its visibility
6299 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6300 * that this tells you whether or not your window is being made visible
6301 * to the window manager; this does <em>not</em> tell you whether or not
6302 * your window is obscured by other windows on the screen, even if it
6303 * is itself visible.
6304 *
6305 * @param visibility The new visibility of the window.
6306 */
6307 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006308 if (visibility == VISIBLE) {
6309 initialAwakenScrollBars();
6310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006311 }
6312
6313 /**
6314 * Returns the current visibility of the window this view is attached to
6315 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6316 *
6317 * @return Returns the current visibility of the view's window.
6318 */
6319 public int getWindowVisibility() {
6320 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6321 }
6322
6323 /**
6324 * Retrieve the overall visible display size in which the window this view is
6325 * attached to has been positioned in. This takes into account screen
6326 * decorations above the window, for both cases where the window itself
6327 * is being position inside of them or the window is being placed under
6328 * then and covered insets are used for the window to position its content
6329 * inside. In effect, this tells you the available area where content can
6330 * be placed and remain visible to users.
6331 *
6332 * <p>This function requires an IPC back to the window manager to retrieve
6333 * the requested information, so should not be used in performance critical
6334 * code like drawing.
6335 *
6336 * @param outRect Filled in with the visible display frame. If the view
6337 * is not attached to a window, this is simply the raw display size.
6338 */
6339 public void getWindowVisibleDisplayFrame(Rect outRect) {
6340 if (mAttachInfo != null) {
6341 try {
6342 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6343 } catch (RemoteException e) {
6344 return;
6345 }
6346 // XXX This is really broken, and probably all needs to be done
6347 // in the window manager, and we need to know more about whether
6348 // we want the area behind or in front of the IME.
6349 final Rect insets = mAttachInfo.mVisibleInsets;
6350 outRect.left += insets.left;
6351 outRect.top += insets.top;
6352 outRect.right -= insets.right;
6353 outRect.bottom -= insets.bottom;
6354 return;
6355 }
6356 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006357 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358 }
6359
6360 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006361 * Dispatch a notification about a resource configuration change down
6362 * the view hierarchy.
6363 * ViewGroups should override to route to their children.
6364 *
6365 * @param newConfig The new resource configuration.
6366 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006367 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006368 */
6369 public void dispatchConfigurationChanged(Configuration newConfig) {
6370 onConfigurationChanged(newConfig);
6371 }
6372
6373 /**
6374 * Called when the current configuration of the resources being used
6375 * by the application have changed. You can use this to decide when
6376 * to reload resources that can changed based on orientation and other
6377 * configuration characterstics. You only need to use this if you are
6378 * not relying on the normal {@link android.app.Activity} mechanism of
6379 * recreating the activity instance upon a configuration change.
6380 *
6381 * @param newConfig The new resource configuration.
6382 */
6383 protected void onConfigurationChanged(Configuration newConfig) {
6384 }
6385
6386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 * Private function to aggregate all per-view attributes in to the view
6388 * root.
6389 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006390 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6391 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006392 }
6393
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006394 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6395 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006396 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006397 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006398 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006399 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006400 ListenerInfo li = mListenerInfo;
6401 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006402 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006404 }
6405 }
6406
6407 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006408 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006409 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006410 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6411 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006412 ai.mRecomputeGlobalAttributes = true;
6413 }
6414 }
6415 }
6416
6417 /**
6418 * Returns whether the device is currently in touch mode. Touch mode is entered
6419 * once the user begins interacting with the device by touch, and affects various
6420 * things like whether focus is always visible to the user.
6421 *
6422 * @return Whether the device is in touch mode.
6423 */
6424 @ViewDebug.ExportedProperty
6425 public boolean isInTouchMode() {
6426 if (mAttachInfo != null) {
6427 return mAttachInfo.mInTouchMode;
6428 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006429 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 }
6431 }
6432
6433 /**
6434 * Returns the context the view is running in, through which it can
6435 * access the current theme, resources, etc.
6436 *
6437 * @return The view's Context.
6438 */
6439 @ViewDebug.CapturedViewProperty
6440 public final Context getContext() {
6441 return mContext;
6442 }
6443
6444 /**
6445 * Handle a key event before it is processed by any input method
6446 * associated with the view hierarchy. This can be used to intercept
6447 * key events in special situations before the IME consumes them; a
6448 * typical example would be handling the BACK key to update the application's
6449 * UI instead of allowing the IME to see it and close itself.
6450 *
6451 * @param keyCode The value in event.getKeyCode().
6452 * @param event Description of the key event.
6453 * @return If you handled the event, return true. If you want to allow the
6454 * event to be handled by the next receiver, return false.
6455 */
6456 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6457 return false;
6458 }
6459
6460 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006461 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6462 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6464 * is released, if the view is enabled and clickable.
6465 *
6466 * @param keyCode A key code that represents the button pressed, from
6467 * {@link android.view.KeyEvent}.
6468 * @param event The KeyEvent object that defines the button action.
6469 */
6470 public boolean onKeyDown(int keyCode, KeyEvent event) {
6471 boolean result = false;
6472
6473 switch (keyCode) {
6474 case KeyEvent.KEYCODE_DPAD_CENTER:
6475 case KeyEvent.KEYCODE_ENTER: {
6476 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6477 return true;
6478 }
6479 // Long clickable items don't necessarily have to be clickable
6480 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6481 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6482 (event.getRepeatCount() == 0)) {
6483 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006484 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006485 return true;
6486 }
6487 break;
6488 }
6489 }
6490 return result;
6491 }
6492
6493 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006494 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6495 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6496 * the event).
6497 */
6498 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6499 return false;
6500 }
6501
6502 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006503 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6504 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6506 * {@link KeyEvent#KEYCODE_ENTER} is released.
6507 *
6508 * @param keyCode A key code that represents the button pressed, from
6509 * {@link android.view.KeyEvent}.
6510 * @param event The KeyEvent object that defines the button action.
6511 */
6512 public boolean onKeyUp(int keyCode, KeyEvent event) {
6513 boolean result = false;
6514
6515 switch (keyCode) {
6516 case KeyEvent.KEYCODE_DPAD_CENTER:
6517 case KeyEvent.KEYCODE_ENTER: {
6518 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6519 return true;
6520 }
6521 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6522 setPressed(false);
6523
6524 if (!mHasPerformedLongPress) {
6525 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006526 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527
6528 result = performClick();
6529 }
6530 }
6531 break;
6532 }
6533 }
6534 return result;
6535 }
6536
6537 /**
6538 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6539 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6540 * the event).
6541 *
6542 * @param keyCode A key code that represents the button pressed, from
6543 * {@link android.view.KeyEvent}.
6544 * @param repeatCount The number of times the action was made.
6545 * @param event The KeyEvent object that defines the button action.
6546 */
6547 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6548 return false;
6549 }
6550
6551 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006552 * Called on the focused view when a key shortcut event is not handled.
6553 * Override this method to implement local key shortcuts for the View.
6554 * Key shortcuts can also be implemented by setting the
6555 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 *
6557 * @param keyCode The value in event.getKeyCode().
6558 * @param event Description of the key event.
6559 * @return If you handled the event, return true. If you want to allow the
6560 * event to be handled by the next receiver, return false.
6561 */
6562 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6563 return false;
6564 }
6565
6566 /**
6567 * Check whether the called view is a text editor, in which case it
6568 * would make sense to automatically display a soft input window for
6569 * it. Subclasses should override this if they implement
6570 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006571 * a call on that method would return a non-null InputConnection, and
6572 * they are really a first-class editor that the user would normally
6573 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006574 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006575 * <p>The default implementation always returns false. This does
6576 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6577 * will not be called or the user can not otherwise perform edits on your
6578 * view; it is just a hint to the system that this is not the primary
6579 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006580 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 * @return Returns true if this view is a text editor, else false.
6582 */
6583 public boolean onCheckIsTextEditor() {
6584 return false;
6585 }
Romain Guy8506ab42009-06-11 17:35:47 -07006586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006587 /**
6588 * Create a new InputConnection for an InputMethod to interact
6589 * with the view. The default implementation returns null, since it doesn't
6590 * support input methods. You can override this to implement such support.
6591 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006592 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006593 * <p>When implementing this, you probably also want to implement
6594 * {@link #onCheckIsTextEditor()} to indicate you will return a
6595 * non-null InputConnection.
6596 *
6597 * @param outAttrs Fill in with attribute information about the connection.
6598 */
6599 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6600 return null;
6601 }
6602
6603 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006604 * Called by the {@link android.view.inputmethod.InputMethodManager}
6605 * when a view who is not the current
6606 * input connection target is trying to make a call on the manager. The
6607 * default implementation returns false; you can override this to return
6608 * true for certain views if you are performing InputConnection proxying
6609 * to them.
6610 * @param view The View that is making the InputMethodManager call.
6611 * @return Return true to allow the call, false to reject.
6612 */
6613 public boolean checkInputConnectionProxy(View view) {
6614 return false;
6615 }
Romain Guy8506ab42009-06-11 17:35:47 -07006616
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006617 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 * Show the context menu for this view. It is not safe to hold on to the
6619 * menu after returning from this method.
6620 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006621 * You should normally not overload this method. Overload
6622 * {@link #onCreateContextMenu(ContextMenu)} or define an
6623 * {@link OnCreateContextMenuListener} to add items to the context menu.
6624 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 * @param menu The context menu to populate
6626 */
6627 public void createContextMenu(ContextMenu menu) {
6628 ContextMenuInfo menuInfo = getContextMenuInfo();
6629
6630 // Sets the current menu info so all items added to menu will have
6631 // my extra info set.
6632 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6633
6634 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006635 ListenerInfo li = mListenerInfo;
6636 if (li != null && li.mOnCreateContextMenuListener != null) {
6637 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 }
6639
6640 // Clear the extra information so subsequent items that aren't mine don't
6641 // have my extra info.
6642 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6643
6644 if (mParent != null) {
6645 mParent.createContextMenu(menu);
6646 }
6647 }
6648
6649 /**
6650 * Views should implement this if they have extra information to associate
6651 * with the context menu. The return result is supplied as a parameter to
6652 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6653 * callback.
6654 *
6655 * @return Extra information about the item for which the context menu
6656 * should be shown. This information will vary across different
6657 * subclasses of View.
6658 */
6659 protected ContextMenuInfo getContextMenuInfo() {
6660 return null;
6661 }
6662
6663 /**
6664 * Views should implement this if the view itself is going to add items to
6665 * the context menu.
6666 *
6667 * @param menu the context menu to populate
6668 */
6669 protected void onCreateContextMenu(ContextMenu menu) {
6670 }
6671
6672 /**
6673 * Implement this method to handle trackball motion events. The
6674 * <em>relative</em> movement of the trackball since the last event
6675 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6676 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6677 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6678 * they will often be fractional values, representing the more fine-grained
6679 * movement information available from a trackball).
6680 *
6681 * @param event The motion event.
6682 * @return True if the event was handled, false otherwise.
6683 */
6684 public boolean onTrackballEvent(MotionEvent event) {
6685 return false;
6686 }
6687
6688 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006689 * Implement this method to handle generic motion events.
6690 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006691 * Generic motion events describe joystick movements, mouse hovers, track pad
6692 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006693 * {@link MotionEvent#getSource() source} of the motion event specifies
6694 * the class of input that was received. Implementations of this method
6695 * must examine the bits in the source before processing the event.
6696 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006697 * </p><p>
6698 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6699 * are delivered to the view under the pointer. All other generic motion events are
6700 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006701 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006702 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006703 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006704 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6705 * // process the joystick movement...
6706 * return true;
6707 * }
6708 * }
6709 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6710 * switch (event.getAction()) {
6711 * case MotionEvent.ACTION_HOVER_MOVE:
6712 * // process the mouse hover movement...
6713 * return true;
6714 * case MotionEvent.ACTION_SCROLL:
6715 * // process the scroll wheel movement...
6716 * return true;
6717 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006718 * }
6719 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006720 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006721 *
6722 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006723 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006724 */
6725 public boolean onGenericMotionEvent(MotionEvent event) {
6726 return false;
6727 }
6728
6729 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006730 * Implement this method to handle hover events.
6731 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006732 * This method is called whenever a pointer is hovering into, over, or out of the
6733 * bounds of a view and the view is not currently being touched.
6734 * Hover events are represented as pointer events with action
6735 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6736 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6737 * </p>
6738 * <ul>
6739 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6740 * when the pointer enters the bounds of the view.</li>
6741 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6742 * when the pointer has already entered the bounds of the view and has moved.</li>
6743 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6744 * when the pointer has exited the bounds of the view or when the pointer is
6745 * about to go down due to a button click, tap, or similar user action that
6746 * causes the view to be touched.</li>
6747 * </ul>
6748 * <p>
6749 * The view should implement this method to return true to indicate that it is
6750 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006751 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006752 * The default implementation calls {@link #setHovered} to update the hovered state
6753 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006754 * is enabled and is clickable. The default implementation also sends hover
6755 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006756 * </p>
6757 *
6758 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006759 * @return True if the view handled the hover event.
6760 *
6761 * @see #isHovered
6762 * @see #setHovered
6763 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006764 */
6765 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006766 // The root view may receive hover (or touch) events that are outside the bounds of
6767 // the window. This code ensures that we only send accessibility events for
6768 // hovers that are actually within the bounds of the root view.
6769 final int action = event.getAction();
6770 if (!mSendingHoverAccessibilityEvents) {
6771 if ((action == MotionEvent.ACTION_HOVER_ENTER
6772 || action == MotionEvent.ACTION_HOVER_MOVE)
6773 && !hasHoveredChild()
6774 && pointInView(event.getX(), event.getY())) {
6775 mSendingHoverAccessibilityEvents = true;
6776 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6777 }
6778 } else {
6779 if (action == MotionEvent.ACTION_HOVER_EXIT
6780 || (action == MotionEvent.ACTION_HOVER_MOVE
6781 && !pointInView(event.getX(), event.getY()))) {
6782 mSendingHoverAccessibilityEvents = false;
6783 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6784 }
Jeff Browna1b24182011-07-28 13:38:24 -07006785 }
6786
Jeff Brown87b7f802011-06-21 18:35:45 -07006787 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006788 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006789 case MotionEvent.ACTION_HOVER_ENTER:
6790 setHovered(true);
6791 break;
6792 case MotionEvent.ACTION_HOVER_EXIT:
6793 setHovered(false);
6794 break;
6795 }
Jeff Browna1b24182011-07-28 13:38:24 -07006796
6797 // Dispatch the event to onGenericMotionEvent before returning true.
6798 // This is to provide compatibility with existing applications that
6799 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6800 // break because of the new default handling for hoverable views
6801 // in onHoverEvent.
6802 // Note that onGenericMotionEvent will be called by default when
6803 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6804 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006805 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006806 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006807 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006808 }
6809
6810 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006811 * Returns true if the view should handle {@link #onHoverEvent}
6812 * by calling {@link #setHovered} to change its hovered state.
6813 *
6814 * @return True if the view is hoverable.
6815 */
6816 private boolean isHoverable() {
6817 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006818 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006819 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6820 return false;
6821 }
6822
6823 return (viewFlags & CLICKABLE) == CLICKABLE
6824 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6825 }
6826
6827 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006828 * Returns true if the view is currently hovered.
6829 *
6830 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006831 *
6832 * @see #setHovered
6833 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006834 */
Jeff Brown10b62902011-06-20 16:40:37 -07006835 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006836 public boolean isHovered() {
6837 return (mPrivateFlags & HOVERED) != 0;
6838 }
6839
6840 /**
6841 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006842 * <p>
6843 * Calling this method also changes the drawable state of the view. This
6844 * enables the view to react to hover by using different drawable resources
6845 * to change its appearance.
6846 * </p><p>
6847 * The {@link #onHoverChanged} method is called when the hovered state changes.
6848 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006849 *
6850 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006851 *
6852 * @see #isHovered
6853 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006854 */
6855 public void setHovered(boolean hovered) {
6856 if (hovered) {
6857 if ((mPrivateFlags & HOVERED) == 0) {
6858 mPrivateFlags |= HOVERED;
6859 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006860 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006861 }
6862 } else {
6863 if ((mPrivateFlags & HOVERED) != 0) {
6864 mPrivateFlags &= ~HOVERED;
6865 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006866 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006867 }
6868 }
6869 }
6870
6871 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006872 * Implement this method to handle hover state changes.
6873 * <p>
6874 * This method is called whenever the hover state changes as a result of a
6875 * call to {@link #setHovered}.
6876 * </p>
6877 *
6878 * @param hovered The current hover state, as returned by {@link #isHovered}.
6879 *
6880 * @see #isHovered
6881 * @see #setHovered
6882 */
6883 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006884 }
6885
6886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006887 * Implement this method to handle touch screen motion events.
6888 *
6889 * @param event The motion event.
6890 * @return True if the event was handled, false otherwise.
6891 */
6892 public boolean onTouchEvent(MotionEvent event) {
6893 final int viewFlags = mViewFlags;
6894
6895 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006896 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006897 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 // A disabled view that is clickable still consumes the touch
6900 // events, it just doesn't respond to them.
6901 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6902 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6903 }
6904
6905 if (mTouchDelegate != null) {
6906 if (mTouchDelegate.onTouchEvent(event)) {
6907 return true;
6908 }
6909 }
6910
6911 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6912 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6913 switch (event.getAction()) {
6914 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006915 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6916 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 // take focus if we don't have it already and we should in
6918 // touch mode.
6919 boolean focusTaken = false;
6920 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6921 focusTaken = requestFocus();
6922 }
6923
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006924 if (prepressed) {
6925 // The button is being released before we actually
6926 // showed it as pressed. Make it show the pressed
6927 // state now (before scheduling the click) to ensure
6928 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006929 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006930 }
Joe Malin32736f02011-01-19 16:14:20 -08006931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 if (!mHasPerformedLongPress) {
6933 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006934 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935
6936 // Only perform take click actions if we were in the pressed state
6937 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006938 // Use a Runnable and post this rather than calling
6939 // performClick directly. This lets other visual state
6940 // of the view update before click actions start.
6941 if (mPerformClick == null) {
6942 mPerformClick = new PerformClick();
6943 }
6944 if (!post(mPerformClick)) {
6945 performClick();
6946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 }
6948 }
6949
6950 if (mUnsetPressedState == null) {
6951 mUnsetPressedState = new UnsetPressedState();
6952 }
6953
Adam Powelle14579b2009-12-16 18:39:52 -08006954 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006955 postDelayed(mUnsetPressedState,
6956 ViewConfiguration.getPressedStateDuration());
6957 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 // If the post failed, unpress right now
6959 mUnsetPressedState.run();
6960 }
Adam Powelle14579b2009-12-16 18:39:52 -08006961 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 }
6963 break;
6964
6965 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006966 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006967
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006968 if (performButtonActionOnTouchDown(event)) {
6969 break;
6970 }
6971
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006972 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006973 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006974
6975 // For views inside a scrolling container, delay the pressed feedback for
6976 // a short period in case this is a scroll.
6977 if (isInScrollingContainer) {
6978 mPrivateFlags |= PREPRESSED;
6979 if (mPendingCheckForTap == null) {
6980 mPendingCheckForTap = new CheckForTap();
6981 }
6982 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6983 } else {
6984 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006985 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006986 checkForLongClick(0);
6987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 break;
6989
6990 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006991 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006992 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 break;
6994
6995 case MotionEvent.ACTION_MOVE:
6996 final int x = (int) event.getX();
6997 final int y = (int) event.getY();
6998
6999 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007000 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007002 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007003 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007004 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007005 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006
Adam Powell4d6f0662012-02-21 15:11:11 -08007007 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007009 }
7010 break;
7011 }
7012 return true;
7013 }
7014
7015 return false;
7016 }
7017
7018 /**
Adam Powell10298662011-08-14 18:26:30 -07007019 * @hide
7020 */
7021 public boolean isInScrollingContainer() {
7022 ViewParent p = getParent();
7023 while (p != null && p instanceof ViewGroup) {
7024 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7025 return true;
7026 }
7027 p = p.getParent();
7028 }
7029 return false;
7030 }
7031
7032 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007033 * Remove the longpress detection timer.
7034 */
7035 private void removeLongPressCallback() {
7036 if (mPendingCheckForLongPress != null) {
7037 removeCallbacks(mPendingCheckForLongPress);
7038 }
7039 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007040
7041 /**
7042 * Remove the pending click action
7043 */
7044 private void removePerformClickCallback() {
7045 if (mPerformClick != null) {
7046 removeCallbacks(mPerformClick);
7047 }
7048 }
7049
Adam Powelle14579b2009-12-16 18:39:52 -08007050 /**
Romain Guya440b002010-02-24 15:57:54 -08007051 * Remove the prepress detection timer.
7052 */
7053 private void removeUnsetPressCallback() {
7054 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7055 setPressed(false);
7056 removeCallbacks(mUnsetPressedState);
7057 }
7058 }
7059
7060 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007061 * Remove the tap detection timer.
7062 */
7063 private void removeTapCallback() {
7064 if (mPendingCheckForTap != null) {
7065 mPrivateFlags &= ~PREPRESSED;
7066 removeCallbacks(mPendingCheckForTap);
7067 }
7068 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007069
7070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007071 * Cancels a pending long press. Your subclass can use this if you
7072 * want the context menu to come up if the user presses and holds
7073 * at the same place, but you don't want it to come up if they press
7074 * and then move around enough to cause scrolling.
7075 */
7076 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007077 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007078
7079 /*
7080 * The prepressed state handled by the tap callback is a display
7081 * construct, but the tap callback will post a long press callback
7082 * less its own timeout. Remove it here.
7083 */
7084 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007085 }
7086
7087 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007088 * Remove the pending callback for sending a
7089 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7090 */
7091 private void removeSendViewScrolledAccessibilityEventCallback() {
7092 if (mSendViewScrolledAccessibilityEvent != null) {
7093 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7094 }
7095 }
7096
7097 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007098 * Sets the TouchDelegate for this View.
7099 */
7100 public void setTouchDelegate(TouchDelegate delegate) {
7101 mTouchDelegate = delegate;
7102 }
7103
7104 /**
7105 * Gets the TouchDelegate for this View.
7106 */
7107 public TouchDelegate getTouchDelegate() {
7108 return mTouchDelegate;
7109 }
7110
7111 /**
7112 * Set flags controlling behavior of this view.
7113 *
7114 * @param flags Constant indicating the value which should be set
7115 * @param mask Constant indicating the bit range that should be changed
7116 */
7117 void setFlags(int flags, int mask) {
7118 int old = mViewFlags;
7119 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7120
7121 int changed = mViewFlags ^ old;
7122 if (changed == 0) {
7123 return;
7124 }
7125 int privateFlags = mPrivateFlags;
7126
7127 /* Check if the FOCUSABLE bit has changed */
7128 if (((changed & FOCUSABLE_MASK) != 0) &&
7129 ((privateFlags & HAS_BOUNDS) !=0)) {
7130 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7131 && ((privateFlags & FOCUSED) != 0)) {
7132 /* Give up focus if we are no longer focusable */
7133 clearFocus();
7134 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7135 && ((privateFlags & FOCUSED) == 0)) {
7136 /*
7137 * Tell the view system that we are now available to take focus
7138 * if no one else already has it.
7139 */
7140 if (mParent != null) mParent.focusableViewAvailable(this);
7141 }
7142 }
7143
7144 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7145 if ((changed & VISIBILITY_MASK) != 0) {
7146 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007147 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007148 * it was not visible. Marking it drawn ensures that the invalidation will
7149 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007151 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007152 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153
7154 needGlobalAttributesUpdate(true);
7155
7156 // a view becoming visible is worth notifying the parent
7157 // about in case nothing has focus. even if this specific view
7158 // isn't focusable, it may contain something that is, so let
7159 // the root view try to give this focus if nothing else does.
7160 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7161 mParent.focusableViewAvailable(this);
7162 }
7163 }
7164 }
7165
7166 /* Check if the GONE bit has changed */
7167 if ((changed & GONE) != 0) {
7168 needGlobalAttributesUpdate(false);
7169 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170
Romain Guyecd80ee2009-12-03 17:13:02 -08007171 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7172 if (hasFocus()) clearFocus();
7173 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007174 if (mParent instanceof View) {
7175 // GONE views noop invalidation, so invalidate the parent
7176 ((View) mParent).invalidate(true);
7177 }
7178 // Mark the view drawn to ensure that it gets invalidated properly the next
7179 // time it is visible and gets invalidated
7180 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007181 }
7182 if (mAttachInfo != null) {
7183 mAttachInfo.mViewVisibilityChanged = true;
7184 }
7185 }
7186
7187 /* Check if the VISIBLE bit has changed */
7188 if ((changed & INVISIBLE) != 0) {
7189 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007190 /*
7191 * If this view is becoming invisible, set the DRAWN flag so that
7192 * the next invalidate() will not be skipped.
7193 */
7194 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007195
7196 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
7197 // root view becoming invisible shouldn't clear focus
7198 if (getRootView() != this) {
7199 clearFocus();
7200 }
7201 }
7202 if (mAttachInfo != null) {
7203 mAttachInfo.mViewVisibilityChanged = true;
7204 }
7205 }
7206
Adam Powell326d8082009-12-09 15:10:07 -08007207 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007208 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007209 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7210 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007211 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007212 } else if (mParent != null) {
7213 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007214 }
Adam Powell326d8082009-12-09 15:10:07 -08007215 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7216 }
7217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7219 destroyDrawingCache();
7220 }
7221
7222 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7223 destroyDrawingCache();
7224 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007225 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 }
7227
7228 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7229 destroyDrawingCache();
7230 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7231 }
7232
7233 if ((changed & DRAW_MASK) != 0) {
7234 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007235 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236 mPrivateFlags &= ~SKIP_DRAW;
7237 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7238 } else {
7239 mPrivateFlags |= SKIP_DRAW;
7240 }
7241 } else {
7242 mPrivateFlags &= ~SKIP_DRAW;
7243 }
7244 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007245 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007246 }
7247
7248 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007249 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 mParent.recomputeViewAttributes(this);
7251 }
7252 }
7253 }
7254
7255 /**
7256 * Change the view's z order in the tree, so it's on top of other sibling
7257 * views
7258 */
7259 public void bringToFront() {
7260 if (mParent != null) {
7261 mParent.bringChildToFront(this);
7262 }
7263 }
7264
7265 /**
7266 * This is called in response to an internal scroll in this view (i.e., the
7267 * view scrolled its own contents). This is typically as a result of
7268 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7269 * called.
7270 *
7271 * @param l Current horizontal scroll origin.
7272 * @param t Current vertical scroll origin.
7273 * @param oldl Previous horizontal scroll origin.
7274 * @param oldt Previous vertical scroll origin.
7275 */
7276 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007277 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7278 postSendViewScrolledAccessibilityEventCallback();
7279 }
7280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007281 mBackgroundSizeChanged = true;
7282
7283 final AttachInfo ai = mAttachInfo;
7284 if (ai != null) {
7285 ai.mViewScrollChanged = true;
7286 }
7287 }
7288
7289 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007290 * Interface definition for a callback to be invoked when the layout bounds of a view
7291 * changes due to layout processing.
7292 */
7293 public interface OnLayoutChangeListener {
7294 /**
7295 * Called when the focus state of a view has changed.
7296 *
7297 * @param v The view whose state has changed.
7298 * @param left The new value of the view's left property.
7299 * @param top The new value of the view's top property.
7300 * @param right The new value of the view's right property.
7301 * @param bottom The new value of the view's bottom property.
7302 * @param oldLeft The previous value of the view's left property.
7303 * @param oldTop The previous value of the view's top property.
7304 * @param oldRight The previous value of the view's right property.
7305 * @param oldBottom The previous value of the view's bottom property.
7306 */
7307 void onLayoutChange(View v, int left, int top, int right, int bottom,
7308 int oldLeft, int oldTop, int oldRight, int oldBottom);
7309 }
7310
7311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 * This is called during layout when the size of this view has changed. If
7313 * you were just added to the view hierarchy, you're called with the old
7314 * values of 0.
7315 *
7316 * @param w Current width of this view.
7317 * @param h Current height of this view.
7318 * @param oldw Old width of this view.
7319 * @param oldh Old height of this view.
7320 */
7321 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7322 }
7323
7324 /**
7325 * Called by draw to draw the child views. This may be overridden
7326 * by derived classes to gain control just before its children are drawn
7327 * (but after its own view has been drawn).
7328 * @param canvas the canvas on which to draw the view
7329 */
7330 protected void dispatchDraw(Canvas canvas) {
7331 }
7332
7333 /**
7334 * Gets the parent of this view. Note that the parent is a
7335 * ViewParent and not necessarily a View.
7336 *
7337 * @return Parent of this view.
7338 */
7339 public final ViewParent getParent() {
7340 return mParent;
7341 }
7342
7343 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007344 * Set the horizontal scrolled position of your view. This will cause a call to
7345 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7346 * invalidated.
7347 * @param value the x position to scroll to
7348 */
7349 public void setScrollX(int value) {
7350 scrollTo(value, mScrollY);
7351 }
7352
7353 /**
7354 * Set the vertical scrolled position of your view. This will cause a call to
7355 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7356 * invalidated.
7357 * @param value the y position to scroll to
7358 */
7359 public void setScrollY(int value) {
7360 scrollTo(mScrollX, value);
7361 }
7362
7363 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007364 * Return the scrolled left position of this view. This is the left edge of
7365 * the displayed part of your view. You do not need to draw any pixels
7366 * farther left, since those are outside of the frame of your view on
7367 * screen.
7368 *
7369 * @return The left edge of the displayed part of your view, in pixels.
7370 */
7371 public final int getScrollX() {
7372 return mScrollX;
7373 }
7374
7375 /**
7376 * Return the scrolled top position of this view. This is the top edge of
7377 * the displayed part of your view. You do not need to draw any pixels above
7378 * it, since those are outside of the frame of your view on screen.
7379 *
7380 * @return The top edge of the displayed part of your view, in pixels.
7381 */
7382 public final int getScrollY() {
7383 return mScrollY;
7384 }
7385
7386 /**
7387 * Return the width of the your view.
7388 *
7389 * @return The width of your view, in pixels.
7390 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007391 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 public final int getWidth() {
7393 return mRight - mLeft;
7394 }
7395
7396 /**
7397 * Return the height of your view.
7398 *
7399 * @return The height of your view, in pixels.
7400 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007401 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 public final int getHeight() {
7403 return mBottom - mTop;
7404 }
7405
7406 /**
7407 * Return the visible drawing bounds of your view. Fills in the output
7408 * rectangle with the values from getScrollX(), getScrollY(),
7409 * getWidth(), and getHeight().
7410 *
7411 * @param outRect The (scrolled) drawing bounds of the view.
7412 */
7413 public void getDrawingRect(Rect outRect) {
7414 outRect.left = mScrollX;
7415 outRect.top = mScrollY;
7416 outRect.right = mScrollX + (mRight - mLeft);
7417 outRect.bottom = mScrollY + (mBottom - mTop);
7418 }
7419
7420 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007421 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7422 * raw width component (that is the result is masked by
7423 * {@link #MEASURED_SIZE_MASK}).
7424 *
7425 * @return The raw measured width of this view.
7426 */
7427 public final int getMeasuredWidth() {
7428 return mMeasuredWidth & MEASURED_SIZE_MASK;
7429 }
7430
7431 /**
7432 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007433 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007434 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 * This should be used during measurement and layout calculations only. Use
7436 * {@link #getWidth()} to see how wide a view is after layout.
7437 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007438 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007439 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007440 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 return mMeasuredWidth;
7442 }
7443
7444 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007445 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7446 * raw width component (that is the result is masked by
7447 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007449 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 */
7451 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007452 return mMeasuredHeight & MEASURED_SIZE_MASK;
7453 }
7454
7455 /**
7456 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007457 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007458 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7459 * This should be used during measurement and layout calculations only. Use
7460 * {@link #getHeight()} to see how wide a view is after layout.
7461 *
7462 * @return The measured width of this view as a bit mask.
7463 */
7464 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 return mMeasuredHeight;
7466 }
7467
7468 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007469 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7470 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7471 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7472 * and the height component is at the shifted bits
7473 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7474 */
7475 public final int getMeasuredState() {
7476 return (mMeasuredWidth&MEASURED_STATE_MASK)
7477 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7478 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7479 }
7480
7481 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007482 * The transform matrix of this view, which is calculated based on the current
7483 * roation, scale, and pivot properties.
7484 *
7485 * @see #getRotation()
7486 * @see #getScaleX()
7487 * @see #getScaleY()
7488 * @see #getPivotX()
7489 * @see #getPivotY()
7490 * @return The current transform matrix for the view
7491 */
7492 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007493 if (mTransformationInfo != null) {
7494 updateMatrix();
7495 return mTransformationInfo.mMatrix;
7496 }
7497 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007498 }
7499
7500 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007501 * Utility function to determine if the value is far enough away from zero to be
7502 * considered non-zero.
7503 * @param value A floating point value to check for zero-ness
7504 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7505 */
7506 private static boolean nonzero(float value) {
7507 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7508 }
7509
7510 /**
Jeff Brown86671742010-09-30 20:00:15 -07007511 * Returns true if the transform matrix is the identity matrix.
7512 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007513 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007514 * @return True if the transform matrix is the identity matrix, false otherwise.
7515 */
Jeff Brown86671742010-09-30 20:00:15 -07007516 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007517 if (mTransformationInfo != null) {
7518 updateMatrix();
7519 return mTransformationInfo.mMatrixIsIdentity;
7520 }
7521 return true;
7522 }
7523
7524 void ensureTransformationInfo() {
7525 if (mTransformationInfo == null) {
7526 mTransformationInfo = new TransformationInfo();
7527 }
Jeff Brown86671742010-09-30 20:00:15 -07007528 }
7529
7530 /**
7531 * Recomputes the transform matrix if necessary.
7532 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007533 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007534 final TransformationInfo info = mTransformationInfo;
7535 if (info == null) {
7536 return;
7537 }
7538 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007539 // transform-related properties have changed since the last time someone
7540 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007541
7542 // Figure out if we need to update the pivot point
7543 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007544 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7545 info.mPrevWidth = mRight - mLeft;
7546 info.mPrevHeight = mBottom - mTop;
7547 info.mPivotX = info.mPrevWidth / 2f;
7548 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007549 }
7550 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007551 info.mMatrix.reset();
7552 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7553 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7554 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7555 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007556 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007557 if (info.mCamera == null) {
7558 info.mCamera = new Camera();
7559 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007560 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007561 info.mCamera.save();
7562 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7563 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7564 info.mCamera.getMatrix(info.matrix3D);
7565 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7566 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7567 info.mPivotY + info.mTranslationY);
7568 info.mMatrix.postConcat(info.matrix3D);
7569 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007570 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007571 info.mMatrixDirty = false;
7572 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7573 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007574 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007575 }
7576
7577 /**
7578 * Utility method to retrieve the inverse of the current mMatrix property.
7579 * We cache the matrix to avoid recalculating it when transform properties
7580 * have not changed.
7581 *
7582 * @return The inverse of the current matrix of this view.
7583 */
Jeff Brown86671742010-09-30 20:00:15 -07007584 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007585 final TransformationInfo info = mTransformationInfo;
7586 if (info != null) {
7587 updateMatrix();
7588 if (info.mInverseMatrixDirty) {
7589 if (info.mInverseMatrix == null) {
7590 info.mInverseMatrix = new Matrix();
7591 }
7592 info.mMatrix.invert(info.mInverseMatrix);
7593 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007594 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007595 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007596 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007597 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007598 }
7599
7600 /**
Chet Haasea1cff502012-02-21 13:43:44 -08007601 * Gets the distance along the Z axis from the camera to this view.
7602 *
7603 * @see #setCameraDistance(float)
7604 *
7605 * @return The distance along the Z axis.
7606 */
7607 public float getCameraDistance() {
7608 ensureTransformationInfo();
7609 final float dpi = mResources.getDisplayMetrics().densityDpi;
7610 final TransformationInfo info = mTransformationInfo;
7611 if (info.mCamera == null) {
7612 info.mCamera = new Camera();
7613 info.matrix3D = new Matrix();
7614 }
7615 return -(info.mCamera.getLocationZ() * dpi);
7616 }
7617
7618 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007619 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7620 * views are drawn) from the camera to this view. The camera's distance
7621 * affects 3D transformations, for instance rotations around the X and Y
7622 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07007623 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08007624 * use a camera distance that's greater than the height (X axis rotation) or
7625 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007626 *
Romain Guya5364ee2011-02-24 14:46:04 -08007627 * <p>The distance of the camera from the view plane can have an affect on the
7628 * perspective distortion of the view when it is rotated around the x or y axis.
7629 * For example, a large distance will result in a large viewing angle, and there
7630 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07007631 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08007632 * also result in some drawing artifacts if the rotated view ends up partially
7633 * behind the camera (which is why the recommendation is to use a distance at
7634 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007635 *
Romain Guya5364ee2011-02-24 14:46:04 -08007636 * <p>The distance is expressed in "depth pixels." The default distance depends
7637 * on the screen density. For instance, on a medium density display, the
7638 * default distance is 1280. On a high density display, the default distance
7639 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007640 *
Romain Guya5364ee2011-02-24 14:46:04 -08007641 * <p>If you want to specify a distance that leads to visually consistent
7642 * results across various densities, use the following formula:</p>
7643 * <pre>
7644 * float scale = context.getResources().getDisplayMetrics().density;
7645 * view.setCameraDistance(distance * scale);
7646 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07007647 *
Romain Guya5364ee2011-02-24 14:46:04 -08007648 * <p>The density scale factor of a high density display is 1.5,
7649 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007650 *
Romain Guya5364ee2011-02-24 14:46:04 -08007651 * @param distance The distance in "depth pixels", if negative the opposite
7652 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07007653 *
7654 * @see #setRotationX(float)
7655 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08007656 */
7657 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007658 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08007659
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007660 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007661 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007662 final TransformationInfo info = mTransformationInfo;
7663 if (info.mCamera == null) {
7664 info.mCamera = new Camera();
7665 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007666 }
7667
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007668 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7669 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007670
Chet Haase9d1992d2012-03-13 11:03:25 -07007671 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007672 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07007673 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08007674 }
Romain Guya5364ee2011-02-24 14:46:04 -08007675 }
7676
7677 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007678 * The degrees that the view is rotated around the pivot point.
7679 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007680 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007681 * @see #getPivotX()
7682 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007683 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007684 * @return The degrees of rotation.
7685 */
Chet Haasea5531132012-02-02 13:41:44 -08007686 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007687 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007688 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007689 }
7690
7691 /**
Chet Haase897247b2010-09-09 14:54:47 -07007692 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7693 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007694 *
7695 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007696 *
7697 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007698 * @see #getPivotX()
7699 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007700 * @see #setRotationX(float)
7701 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007702 *
7703 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007704 */
7705 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007706 ensureTransformationInfo();
7707 final TransformationInfo info = mTransformationInfo;
7708 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007709 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07007710 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007711 info.mRotation = rotation;
7712 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007713 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007714 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7715 mDisplayList.setRotation(rotation);
7716 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007717 }
7718 }
7719
7720 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007721 * The degrees that the view is rotated around the vertical axis through the pivot point.
7722 *
7723 * @see #getPivotX()
7724 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007725 * @see #setRotationY(float)
7726 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007727 * @return The degrees of Y rotation.
7728 */
Chet Haasea5531132012-02-02 13:41:44 -08007729 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007730 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007731 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007732 }
7733
7734 /**
Chet Haase897247b2010-09-09 14:54:47 -07007735 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7736 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7737 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007738 *
Romain Guya5364ee2011-02-24 14:46:04 -08007739 * When rotating large views, it is recommended to adjust the camera distance
7740 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007741 *
7742 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007743 *
7744 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007745 * @see #getPivotX()
7746 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007747 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007748 * @see #setRotationX(float)
7749 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007750 *
7751 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007752 */
7753 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007754 ensureTransformationInfo();
7755 final TransformationInfo info = mTransformationInfo;
7756 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007757 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007758 info.mRotationY = rotationY;
7759 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007760 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007761 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7762 mDisplayList.setRotationY(rotationY);
7763 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007764 }
7765 }
7766
7767 /**
7768 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7769 *
7770 * @see #getPivotX()
7771 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007772 * @see #setRotationX(float)
7773 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007774 * @return The degrees of X rotation.
7775 */
Chet Haasea5531132012-02-02 13:41:44 -08007776 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007777 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007778 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007779 }
7780
7781 /**
Chet Haase897247b2010-09-09 14:54:47 -07007782 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7783 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7784 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007785 *
Romain Guya5364ee2011-02-24 14:46:04 -08007786 * When rotating large views, it is recommended to adjust the camera distance
7787 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007788 *
7789 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007790 *
7791 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007792 * @see #getPivotX()
7793 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007794 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007795 * @see #setRotationY(float)
7796 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007797 *
7798 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007799 */
7800 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007801 ensureTransformationInfo();
7802 final TransformationInfo info = mTransformationInfo;
7803 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007804 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007805 info.mRotationX = rotationX;
7806 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007807 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007808 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7809 mDisplayList.setRotationX(rotationX);
7810 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007811 }
7812 }
7813
7814 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007815 * The amount that the view is scaled in x around the pivot point, as a proportion of
7816 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7817 *
Joe Onorato93162322010-09-16 15:42:01 -04007818 * <p>By default, this is 1.0f.
7819 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007820 * @see #getPivotX()
7821 * @see #getPivotY()
7822 * @return The scaling factor.
7823 */
Chet Haasea5531132012-02-02 13:41:44 -08007824 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007825 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007826 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007827 }
7828
7829 /**
7830 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7831 * the view's unscaled width. A value of 1 means that no scaling is applied.
7832 *
7833 * @param scaleX The scaling factor.
7834 * @see #getPivotX()
7835 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007836 *
7837 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007838 */
7839 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007840 ensureTransformationInfo();
7841 final TransformationInfo info = mTransformationInfo;
7842 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007843 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007844 info.mScaleX = scaleX;
7845 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007846 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007847 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7848 mDisplayList.setScaleX(scaleX);
7849 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007850 }
7851 }
7852
7853 /**
7854 * The amount that the view is scaled in y around the pivot point, as a proportion of
7855 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7856 *
Joe Onorato93162322010-09-16 15:42:01 -04007857 * <p>By default, this is 1.0f.
7858 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007859 * @see #getPivotX()
7860 * @see #getPivotY()
7861 * @return The scaling factor.
7862 */
Chet Haasea5531132012-02-02 13:41:44 -08007863 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007864 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007865 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007866 }
7867
7868 /**
7869 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7870 * the view's unscaled width. A value of 1 means that no scaling is applied.
7871 *
7872 * @param scaleY The scaling factor.
7873 * @see #getPivotX()
7874 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007875 *
7876 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007877 */
7878 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007879 ensureTransformationInfo();
7880 final TransformationInfo info = mTransformationInfo;
7881 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007882 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007883 info.mScaleY = scaleY;
7884 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007885 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007886 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7887 mDisplayList.setScaleY(scaleY);
7888 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007889 }
7890 }
7891
7892 /**
7893 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7894 * and {@link #setScaleX(float) scaled}.
7895 *
7896 * @see #getRotation()
7897 * @see #getScaleX()
7898 * @see #getScaleY()
7899 * @see #getPivotY()
7900 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007901 *
7902 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007903 */
Chet Haasea5531132012-02-02 13:41:44 -08007904 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007905 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007906 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007907 }
7908
7909 /**
7910 * Sets the x location of the point around which the view is
7911 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007912 * By default, the pivot point is centered on the object.
7913 * Setting this property disables this behavior and causes the view to use only the
7914 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007915 *
7916 * @param pivotX The x location of the pivot point.
7917 * @see #getRotation()
7918 * @see #getScaleX()
7919 * @see #getScaleY()
7920 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007921 *
7922 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007923 */
7924 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007925 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007926 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007927 final TransformationInfo info = mTransformationInfo;
7928 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007929 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007930 info.mPivotX = pivotX;
7931 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007932 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007933 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7934 mDisplayList.setPivotX(pivotX);
7935 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007936 }
7937 }
7938
7939 /**
7940 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7941 * and {@link #setScaleY(float) scaled}.
7942 *
7943 * @see #getRotation()
7944 * @see #getScaleX()
7945 * @see #getScaleY()
7946 * @see #getPivotY()
7947 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007948 *
7949 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007950 */
Chet Haasea5531132012-02-02 13:41:44 -08007951 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007952 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007953 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007954 }
7955
7956 /**
7957 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007958 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7959 * Setting this property disables this behavior and causes the view to use only the
7960 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007961 *
7962 * @param pivotY The y location of the pivot point.
7963 * @see #getRotation()
7964 * @see #getScaleX()
7965 * @see #getScaleY()
7966 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007967 *
7968 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007969 */
7970 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007971 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007972 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007973 final TransformationInfo info = mTransformationInfo;
7974 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007975 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007976 info.mPivotY = pivotY;
7977 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007978 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007979 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7980 mDisplayList.setPivotY(pivotY);
7981 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007982 }
7983 }
7984
7985 /**
7986 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7987 * completely transparent and 1 means the view is completely opaque.
7988 *
Joe Onorato93162322010-09-16 15:42:01 -04007989 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007990 * @return The opacity of the view.
7991 */
Chet Haasea5531132012-02-02 13:41:44 -08007992 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007993 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007994 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007995 }
7996
7997 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07007998 * Returns whether this View has content which overlaps. This function, intended to be
7999 * overridden by specific View types, is an optimization when alpha is set on a view. If
8000 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8001 * and then composited it into place, which can be expensive. If the view has no overlapping
8002 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8003 * An example of overlapping rendering is a TextView with a background image, such as a
8004 * Button. An example of non-overlapping rendering is a TextView with no background, or
8005 * an ImageView with only the foreground image. The default implementation returns true;
8006 * subclasses should override if they have cases which can be optimized.
8007 *
8008 * @return true if the content in this view might overlap, false otherwise.
8009 */
8010 public boolean hasOverlappingRendering() {
8011 return true;
8012 }
8013
8014 /**
Romain Guy171c5922011-01-06 10:04:23 -08008015 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8016 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008017 *
Romain Guy171c5922011-01-06 10:04:23 -08008018 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8019 * responsible for applying the opacity itself. Otherwise, calling this method is
8020 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008021 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008022 *
Chet Haasea5531132012-02-02 13:41:44 -08008023 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8024 * performance implications. It is generally best to use the alpha property sparingly and
8025 * transiently, as in the case of fading animations.</p>
8026 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008027 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008028 *
Joe Malin32736f02011-01-19 16:14:20 -08008029 * @see #setLayerType(int, android.graphics.Paint)
8030 *
Chet Haase73066682010-11-29 15:55:32 -08008031 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008032 */
8033 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008034 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008035 if (mTransformationInfo.mAlpha != alpha) {
8036 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008037 if (onSetAlpha((int) (alpha * 255))) {
8038 mPrivateFlags |= ALPHA_SET;
8039 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008040 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008041 invalidate(true);
8042 } else {
8043 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008044 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008045 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8046 mDisplayList.setAlpha(alpha);
8047 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008048 }
Chet Haaseed032702010-10-01 14:05:54 -07008049 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008050 }
8051
8052 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008053 * Faster version of setAlpha() which performs the same steps except there are
8054 * no calls to invalidate(). The caller of this function should perform proper invalidation
8055 * on the parent and this object. The return value indicates whether the subclass handles
8056 * alpha (the return value for onSetAlpha()).
8057 *
8058 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008059 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8060 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008061 */
8062 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008063 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008064 if (mTransformationInfo.mAlpha != alpha) {
8065 mTransformationInfo.mAlpha = alpha;
8066 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8067 if (subclassHandlesAlpha) {
8068 mPrivateFlags |= ALPHA_SET;
8069 return true;
8070 } else {
8071 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08008072 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8073 mDisplayList.setAlpha(alpha);
8074 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008075 }
Chet Haasea00f3862011-02-22 06:34:40 -08008076 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008077 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008078 }
8079
8080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008081 * Top position of this view relative to its parent.
8082 *
8083 * @return The top of this view, in pixels.
8084 */
8085 @ViewDebug.CapturedViewProperty
8086 public final int getTop() {
8087 return mTop;
8088 }
8089
8090 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008091 * Sets the top position of this view relative to its parent. This method is meant to be called
8092 * by the layout system and should not generally be called otherwise, because the property
8093 * may be changed at any time by the layout.
8094 *
8095 * @param top The top of this view, in pixels.
8096 */
8097 public final void setTop(int top) {
8098 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008099 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008100 final boolean matrixIsIdentity = mTransformationInfo == null
8101 || mTransformationInfo.mMatrixIsIdentity;
8102 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008103 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008104 int minTop;
8105 int yLoc;
8106 if (top < mTop) {
8107 minTop = top;
8108 yLoc = top - mTop;
8109 } else {
8110 minTop = mTop;
8111 yLoc = 0;
8112 }
Chet Haasee9140a72011-02-16 16:23:29 -08008113 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008114 }
8115 } else {
8116 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008117 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008118 }
8119
Chet Haaseed032702010-10-01 14:05:54 -07008120 int width = mRight - mLeft;
8121 int oldHeight = mBottom - mTop;
8122
Chet Haase21cd1382010-09-01 17:42:29 -07008123 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008124 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8125 mDisplayList.setTop(mTop);
8126 }
Chet Haase21cd1382010-09-01 17:42:29 -07008127
Chet Haaseed032702010-10-01 14:05:54 -07008128 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8129
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008130 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008131 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8132 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008133 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008134 }
Chet Haase21cd1382010-09-01 17:42:29 -07008135 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008136 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008137 }
Chet Haase55dbb652010-12-21 20:15:08 -08008138 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008139 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008140 }
8141 }
8142
8143 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 * Bottom position of this view relative to its parent.
8145 *
8146 * @return The bottom of this view, in pixels.
8147 */
8148 @ViewDebug.CapturedViewProperty
8149 public final int getBottom() {
8150 return mBottom;
8151 }
8152
8153 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008154 * True if this view has changed since the last time being drawn.
8155 *
8156 * @return The dirty state of this view.
8157 */
8158 public boolean isDirty() {
8159 return (mPrivateFlags & DIRTY_MASK) != 0;
8160 }
8161
8162 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008163 * Sets the bottom position of this view relative to its parent. This method is meant to be
8164 * called by the layout system and should not generally be called otherwise, because the
8165 * property may be changed at any time by the layout.
8166 *
8167 * @param bottom The bottom of this view, in pixels.
8168 */
8169 public final void setBottom(int bottom) {
8170 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008171 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008172 final boolean matrixIsIdentity = mTransformationInfo == null
8173 || mTransformationInfo.mMatrixIsIdentity;
8174 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008175 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008176 int maxBottom;
8177 if (bottom < mBottom) {
8178 maxBottom = mBottom;
8179 } else {
8180 maxBottom = bottom;
8181 }
Chet Haasee9140a72011-02-16 16:23:29 -08008182 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008183 }
8184 } else {
8185 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008186 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008187 }
8188
Chet Haaseed032702010-10-01 14:05:54 -07008189 int width = mRight - mLeft;
8190 int oldHeight = mBottom - mTop;
8191
Chet Haase21cd1382010-09-01 17:42:29 -07008192 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008193 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8194 mDisplayList.setBottom(mBottom);
8195 }
Chet Haase21cd1382010-09-01 17:42:29 -07008196
Chet Haaseed032702010-10-01 14:05:54 -07008197 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8198
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008199 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008200 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8201 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008202 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008203 }
Chet Haase21cd1382010-09-01 17:42:29 -07008204 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008205 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008206 }
Chet Haase55dbb652010-12-21 20:15:08 -08008207 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008208 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008209 }
8210 }
8211
8212 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 * Left position of this view relative to its parent.
8214 *
8215 * @return The left edge of this view, in pixels.
8216 */
8217 @ViewDebug.CapturedViewProperty
8218 public final int getLeft() {
8219 return mLeft;
8220 }
8221
8222 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008223 * Sets the left position of this view relative to its parent. This method is meant to be called
8224 * by the layout system and should not generally be called otherwise, because the property
8225 * may be changed at any time by the layout.
8226 *
8227 * @param left The bottom of this view, in pixels.
8228 */
8229 public final void setLeft(int left) {
8230 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008231 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008232 final boolean matrixIsIdentity = mTransformationInfo == null
8233 || mTransformationInfo.mMatrixIsIdentity;
8234 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008235 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008236 int minLeft;
8237 int xLoc;
8238 if (left < mLeft) {
8239 minLeft = left;
8240 xLoc = left - mLeft;
8241 } else {
8242 minLeft = mLeft;
8243 xLoc = 0;
8244 }
Chet Haasee9140a72011-02-16 16:23:29 -08008245 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008246 }
8247 } else {
8248 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008249 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008250 }
8251
Chet Haaseed032702010-10-01 14:05:54 -07008252 int oldWidth = mRight - mLeft;
8253 int height = mBottom - mTop;
8254
Chet Haase21cd1382010-09-01 17:42:29 -07008255 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008256 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8257 mDisplayList.setLeft(left);
8258 }
Chet Haase21cd1382010-09-01 17:42:29 -07008259
Chet Haaseed032702010-10-01 14:05:54 -07008260 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8261
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008262 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008263 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8264 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008265 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008266 }
Chet Haase21cd1382010-09-01 17:42:29 -07008267 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008268 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008269 }
Chet Haase55dbb652010-12-21 20:15:08 -08008270 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008271 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008272 if (USE_DISPLAY_LIST_PROPERTIES) {
8273
8274 }
Chet Haase21cd1382010-09-01 17:42:29 -07008275 }
8276 }
8277
8278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279 * Right position of this view relative to its parent.
8280 *
8281 * @return The right edge of this view, in pixels.
8282 */
8283 @ViewDebug.CapturedViewProperty
8284 public final int getRight() {
8285 return mRight;
8286 }
8287
8288 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008289 * Sets the right position of this view relative to its parent. This method is meant to be called
8290 * by the layout system and should not generally be called otherwise, because the property
8291 * may be changed at any time by the layout.
8292 *
8293 * @param right The bottom of this view, in pixels.
8294 */
8295 public final void setRight(int right) {
8296 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008297 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008298 final boolean matrixIsIdentity = mTransformationInfo == null
8299 || mTransformationInfo.mMatrixIsIdentity;
8300 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008301 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008302 int maxRight;
8303 if (right < mRight) {
8304 maxRight = mRight;
8305 } else {
8306 maxRight = right;
8307 }
Chet Haasee9140a72011-02-16 16:23:29 -08008308 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008309 }
8310 } else {
8311 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008312 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008313 }
8314
Chet Haaseed032702010-10-01 14:05:54 -07008315 int oldWidth = mRight - mLeft;
8316 int height = mBottom - mTop;
8317
Chet Haase21cd1382010-09-01 17:42:29 -07008318 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008319 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8320 mDisplayList.setRight(mRight);
8321 }
Chet Haase21cd1382010-09-01 17:42:29 -07008322
Chet Haaseed032702010-10-01 14:05:54 -07008323 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8324
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008325 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008326 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8327 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008328 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008329 }
Chet Haase21cd1382010-09-01 17:42:29 -07008330 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008331 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008332 }
Chet Haase55dbb652010-12-21 20:15:08 -08008333 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008334 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008335 }
8336 }
8337
8338 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008339 * The visual x position of this view, in pixels. This is equivalent to the
8340 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008341 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008342 *
Chet Haasedf030d22010-07-30 17:22:38 -07008343 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008344 */
Chet Haasea5531132012-02-02 13:41:44 -08008345 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008346 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008347 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008348 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008349
Chet Haasedf030d22010-07-30 17:22:38 -07008350 /**
8351 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8352 * {@link #setTranslationX(float) translationX} property to be the difference between
8353 * the x value passed in and the current {@link #getLeft() left} property.
8354 *
8355 * @param x The visual x position of this view, in pixels.
8356 */
8357 public void setX(float x) {
8358 setTranslationX(x - mLeft);
8359 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008360
Chet Haasedf030d22010-07-30 17:22:38 -07008361 /**
8362 * The visual y position of this view, in pixels. This is equivalent to the
8363 * {@link #setTranslationY(float) translationY} property plus the current
8364 * {@link #getTop() top} property.
8365 *
8366 * @return The visual y position of this view, in pixels.
8367 */
Chet Haasea5531132012-02-02 13:41:44 -08008368 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008369 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008370 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008371 }
8372
8373 /**
8374 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8375 * {@link #setTranslationY(float) translationY} property to be the difference between
8376 * the y value passed in and the current {@link #getTop() top} property.
8377 *
8378 * @param y The visual y position of this view, in pixels.
8379 */
8380 public void setY(float y) {
8381 setTranslationY(y - mTop);
8382 }
8383
8384
8385 /**
8386 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8387 * This position is post-layout, in addition to wherever the object's
8388 * layout placed it.
8389 *
8390 * @return The horizontal position of this view relative to its left position, in pixels.
8391 */
Chet Haasea5531132012-02-02 13:41:44 -08008392 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008393 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008394 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008395 }
8396
8397 /**
8398 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8399 * This effectively positions the object post-layout, in addition to wherever the object's
8400 * layout placed it.
8401 *
8402 * @param translationX The horizontal position of this view relative to its left position,
8403 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008404 *
8405 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008406 */
8407 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008408 ensureTransformationInfo();
8409 final TransformationInfo info = mTransformationInfo;
8410 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008411 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008412 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008413 info.mTranslationX = translationX;
8414 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008415 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008416 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8417 mDisplayList.setTranslationX(translationX);
8418 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008419 }
8420 }
8421
8422 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008423 * The horizontal location of this view relative to its {@link #getTop() top} position.
8424 * This position is post-layout, in addition to wherever the object's
8425 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008426 *
Chet Haasedf030d22010-07-30 17:22:38 -07008427 * @return The vertical position of this view relative to its top position,
8428 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008429 */
Chet Haasea5531132012-02-02 13:41:44 -08008430 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008431 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008432 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008433 }
8434
8435 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008436 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8437 * This effectively positions the object post-layout, in addition to wherever the object's
8438 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008439 *
Chet Haasedf030d22010-07-30 17:22:38 -07008440 * @param translationY The vertical position of this view relative to its top position,
8441 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008442 *
8443 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008444 */
Chet Haasedf030d22010-07-30 17:22:38 -07008445 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008446 ensureTransformationInfo();
8447 final TransformationInfo info = mTransformationInfo;
8448 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008449 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008450 info.mTranslationY = translationY;
8451 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008452 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008453 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8454 mDisplayList.setTranslationY(translationY);
8455 }
Chet Haasedf030d22010-07-30 17:22:38 -07008456 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008457 }
8458
8459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 * Hit rectangle in parent's coordinates
8461 *
8462 * @param outRect The hit rectangle of the view.
8463 */
8464 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008465 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008466 final TransformationInfo info = mTransformationInfo;
8467 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008468 outRect.set(mLeft, mTop, mRight, mBottom);
8469 } else {
8470 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008471 tmpRect.set(-info.mPivotX, -info.mPivotY,
8472 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8473 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008474 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8475 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008476 }
8477 }
8478
8479 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008480 * Determines whether the given point, in local coordinates is inside the view.
8481 */
8482 /*package*/ final boolean pointInView(float localX, float localY) {
8483 return localX >= 0 && localX < (mRight - mLeft)
8484 && localY >= 0 && localY < (mBottom - mTop);
8485 }
8486
8487 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008488 * Utility method to determine whether the given point, in local coordinates,
8489 * is inside the view, where the area of the view is expanded by the slop factor.
8490 * This method is called while processing touch-move events to determine if the event
8491 * is still within the view.
8492 */
8493 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008494 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008495 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 }
8497
8498 /**
8499 * When a view has focus and the user navigates away from it, the next view is searched for
8500 * starting from the rectangle filled in by this method.
8501 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008502 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8503 * of the view. However, if your view maintains some idea of internal selection,
8504 * such as a cursor, or a selected row or column, you should override this method and
8505 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008506 *
8507 * @param r The rectangle to fill in, in this view's coordinates.
8508 */
8509 public void getFocusedRect(Rect r) {
8510 getDrawingRect(r);
8511 }
8512
8513 /**
8514 * If some part of this view is not clipped by any of its parents, then
8515 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008516 * coordinates (without taking possible View rotations into account), offset
8517 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8518 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 *
8520 * @param r If true is returned, r holds the global coordinates of the
8521 * visible portion of this view.
8522 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8523 * between this view and its root. globalOffet may be null.
8524 * @return true if r is non-empty (i.e. part of the view is visible at the
8525 * root level.
8526 */
8527 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8528 int width = mRight - mLeft;
8529 int height = mBottom - mTop;
8530 if (width > 0 && height > 0) {
8531 r.set(0, 0, width, height);
8532 if (globalOffset != null) {
8533 globalOffset.set(-mScrollX, -mScrollY);
8534 }
8535 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8536 }
8537 return false;
8538 }
8539
8540 public final boolean getGlobalVisibleRect(Rect r) {
8541 return getGlobalVisibleRect(r, null);
8542 }
8543
8544 public final boolean getLocalVisibleRect(Rect r) {
8545 Point offset = new Point();
8546 if (getGlobalVisibleRect(r, offset)) {
8547 r.offset(-offset.x, -offset.y); // make r local
8548 return true;
8549 }
8550 return false;
8551 }
8552
8553 /**
8554 * Offset this view's vertical location by the specified number of pixels.
8555 *
8556 * @param offset the number of pixels to offset the view by
8557 */
8558 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008559 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008560 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008561 final boolean matrixIsIdentity = mTransformationInfo == null
8562 || mTransformationInfo.mMatrixIsIdentity;
8563 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008564 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8565 invalidateViewProperty(false, false);
8566 } else {
8567 final ViewParent p = mParent;
8568 if (p != null && mAttachInfo != null) {
8569 final Rect r = mAttachInfo.mTmpInvalRect;
8570 int minTop;
8571 int maxBottom;
8572 int yLoc;
8573 if (offset < 0) {
8574 minTop = mTop + offset;
8575 maxBottom = mBottom;
8576 yLoc = offset;
8577 } else {
8578 minTop = mTop;
8579 maxBottom = mBottom + offset;
8580 yLoc = 0;
8581 }
8582 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8583 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008584 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008585 }
8586 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008587 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008588 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008589
Chet Haasec3aa3612010-06-17 08:50:37 -07008590 mTop += offset;
8591 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008592 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8593 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008594 invalidateViewProperty(false, false);
8595 } else {
8596 if (!matrixIsIdentity) {
8597 invalidateViewProperty(false, true);
8598 }
8599 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008600 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008602 }
8603
8604 /**
8605 * Offset this view's horizontal location by the specified amount of pixels.
8606 *
8607 * @param offset the numer of pixels to offset the view by
8608 */
8609 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008610 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008611 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008612 final boolean matrixIsIdentity = mTransformationInfo == null
8613 || mTransformationInfo.mMatrixIsIdentity;
8614 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008615 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8616 invalidateViewProperty(false, false);
8617 } else {
8618 final ViewParent p = mParent;
8619 if (p != null && mAttachInfo != null) {
8620 final Rect r = mAttachInfo.mTmpInvalRect;
8621 int minLeft;
8622 int maxRight;
8623 if (offset < 0) {
8624 minLeft = mLeft + offset;
8625 maxRight = mRight;
8626 } else {
8627 minLeft = mLeft;
8628 maxRight = mRight + offset;
8629 }
8630 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
8631 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008632 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008633 }
8634 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008635 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008636 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008637
Chet Haasec3aa3612010-06-17 08:50:37 -07008638 mLeft += offset;
8639 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008640 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8641 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008642 invalidateViewProperty(false, false);
8643 } else {
8644 if (!matrixIsIdentity) {
8645 invalidateViewProperty(false, true);
8646 }
8647 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008648 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008650 }
8651
8652 /**
8653 * Get the LayoutParams associated with this view. All views should have
8654 * layout parameters. These supply parameters to the <i>parent</i> of this
8655 * view specifying how it should be arranged. There are many subclasses of
8656 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8657 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008658 *
8659 * This method may return null if this View is not attached to a parent
8660 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8661 * was not invoked successfully. When a View is attached to a parent
8662 * ViewGroup, this method must not return null.
8663 *
8664 * @return The LayoutParams associated with this view, or null if no
8665 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008666 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008667 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 public ViewGroup.LayoutParams getLayoutParams() {
8669 return mLayoutParams;
8670 }
8671
8672 /**
8673 * Set the layout parameters associated with this view. These supply
8674 * parameters to the <i>parent</i> of this view specifying how it should be
8675 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8676 * correspond to the different subclasses of ViewGroup that are responsible
8677 * for arranging their children.
8678 *
Romain Guy01c174b2011-02-22 11:51:06 -08008679 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008680 */
8681 public void setLayoutParams(ViewGroup.LayoutParams params) {
8682 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008683 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008684 }
8685 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008686 if (mParent instanceof ViewGroup) {
8687 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008689 requestLayout();
8690 }
8691
8692 /**
8693 * Set the scrolled position of your view. This will cause a call to
8694 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8695 * invalidated.
8696 * @param x the x position to scroll to
8697 * @param y the y position to scroll to
8698 */
8699 public void scrollTo(int x, int y) {
8700 if (mScrollX != x || mScrollY != y) {
8701 int oldX = mScrollX;
8702 int oldY = mScrollY;
8703 mScrollX = x;
8704 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008705 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008706 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008707 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008708 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 }
8711 }
8712
8713 /**
8714 * Move the scrolled position of your view. This will cause a call to
8715 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8716 * invalidated.
8717 * @param x the amount of pixels to scroll by horizontally
8718 * @param y the amount of pixels to scroll by vertically
8719 */
8720 public void scrollBy(int x, int y) {
8721 scrollTo(mScrollX + x, mScrollY + y);
8722 }
8723
8724 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008725 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8726 * animation to fade the scrollbars out after a default delay. If a subclass
8727 * provides animated scrolling, the start delay should equal the duration
8728 * of the scrolling animation.</p>
8729 *
8730 * <p>The animation starts only if at least one of the scrollbars is
8731 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8732 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8733 * this method returns true, and false otherwise. If the animation is
8734 * started, this method calls {@link #invalidate()}; in that case the
8735 * caller should not call {@link #invalidate()}.</p>
8736 *
8737 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008738 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008739 *
8740 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8741 * and {@link #scrollTo(int, int)}.</p>
8742 *
8743 * @return true if the animation is played, false otherwise
8744 *
8745 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008746 * @see #scrollBy(int, int)
8747 * @see #scrollTo(int, int)
8748 * @see #isHorizontalScrollBarEnabled()
8749 * @see #isVerticalScrollBarEnabled()
8750 * @see #setHorizontalScrollBarEnabled(boolean)
8751 * @see #setVerticalScrollBarEnabled(boolean)
8752 */
8753 protected boolean awakenScrollBars() {
8754 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008755 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008756 }
8757
8758 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008759 * Trigger the scrollbars to draw.
8760 * This method differs from awakenScrollBars() only in its default duration.
8761 * initialAwakenScrollBars() will show the scroll bars for longer than
8762 * usual to give the user more of a chance to notice them.
8763 *
8764 * @return true if the animation is played, false otherwise.
8765 */
8766 private boolean initialAwakenScrollBars() {
8767 return mScrollCache != null &&
8768 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8769 }
8770
8771 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008772 * <p>
8773 * Trigger the scrollbars to draw. When invoked this method starts an
8774 * animation to fade the scrollbars out after a fixed delay. If a subclass
8775 * provides animated scrolling, the start delay should equal the duration of
8776 * the scrolling animation.
8777 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008778 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008779 * <p>
8780 * The animation starts only if at least one of the scrollbars is enabled,
8781 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8782 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8783 * this method returns true, and false otherwise. If the animation is
8784 * started, this method calls {@link #invalidate()}; in that case the caller
8785 * should not call {@link #invalidate()}.
8786 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008787 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008788 * <p>
8789 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008790 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008791 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008792 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008793 * @param startDelay the delay, in milliseconds, after which the animation
8794 * should start; when the delay is 0, the animation starts
8795 * immediately
8796 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008797 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008798 * @see #scrollBy(int, int)
8799 * @see #scrollTo(int, int)
8800 * @see #isHorizontalScrollBarEnabled()
8801 * @see #isVerticalScrollBarEnabled()
8802 * @see #setHorizontalScrollBarEnabled(boolean)
8803 * @see #setVerticalScrollBarEnabled(boolean)
8804 */
8805 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008806 return awakenScrollBars(startDelay, true);
8807 }
Joe Malin32736f02011-01-19 16:14:20 -08008808
Mike Cleron290947b2009-09-29 18:34:32 -07008809 /**
8810 * <p>
8811 * Trigger the scrollbars to draw. When invoked this method starts an
8812 * animation to fade the scrollbars out after a fixed delay. If a subclass
8813 * provides animated scrolling, the start delay should equal the duration of
8814 * the scrolling animation.
8815 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008816 *
Mike Cleron290947b2009-09-29 18:34:32 -07008817 * <p>
8818 * The animation starts only if at least one of the scrollbars is enabled,
8819 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8820 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8821 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008822 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008823 * is set to true; in that case the caller
8824 * should not call {@link #invalidate()}.
8825 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008826 *
Mike Cleron290947b2009-09-29 18:34:32 -07008827 * <p>
8828 * This method should be invoked everytime a subclass directly updates the
8829 * scroll parameters.
8830 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008831 *
Mike Cleron290947b2009-09-29 18:34:32 -07008832 * @param startDelay the delay, in milliseconds, after which the animation
8833 * should start; when the delay is 0, the animation starts
8834 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008835 *
Mike Cleron290947b2009-09-29 18:34:32 -07008836 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008837 *
Mike Cleron290947b2009-09-29 18:34:32 -07008838 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008839 *
Mike Cleron290947b2009-09-29 18:34:32 -07008840 * @see #scrollBy(int, int)
8841 * @see #scrollTo(int, int)
8842 * @see #isHorizontalScrollBarEnabled()
8843 * @see #isVerticalScrollBarEnabled()
8844 * @see #setHorizontalScrollBarEnabled(boolean)
8845 * @see #setVerticalScrollBarEnabled(boolean)
8846 */
8847 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008848 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008849
Mike Cleronf116bf82009-09-27 19:14:12 -07008850 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8851 return false;
8852 }
8853
8854 if (scrollCache.scrollBar == null) {
8855 scrollCache.scrollBar = new ScrollBarDrawable();
8856 }
8857
8858 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8859
Mike Cleron290947b2009-09-29 18:34:32 -07008860 if (invalidate) {
8861 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008862 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008863 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008864
8865 if (scrollCache.state == ScrollabilityCache.OFF) {
8866 // FIXME: this is copied from WindowManagerService.
8867 // We should get this value from the system when it
8868 // is possible to do so.
8869 final int KEY_REPEAT_FIRST_DELAY = 750;
8870 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8871 }
8872
8873 // Tell mScrollCache when we should start fading. This may
8874 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008875 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008876 scrollCache.fadeStartTime = fadeStartTime;
8877 scrollCache.state = ScrollabilityCache.ON;
8878
8879 // Schedule our fader to run, unscheduling any old ones first
8880 if (mAttachInfo != null) {
8881 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8882 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8883 }
8884
8885 return true;
8886 }
8887
8888 return false;
8889 }
8890
8891 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008892 * Do not invalidate views which are not visible and which are not running an animation. They
8893 * will not get drawn and they should not set dirty flags as if they will be drawn
8894 */
8895 private boolean skipInvalidate() {
8896 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8897 (!(mParent instanceof ViewGroup) ||
8898 !((ViewGroup) mParent).isViewTransitioning(this));
8899 }
8900 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008901 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008902 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8903 * in the future. This must be called from a UI thread. To call from a non-UI
8904 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 *
8906 * WARNING: This method is destructive to dirty.
8907 * @param dirty the rectangle representing the bounds of the dirty region
8908 */
8909 public void invalidate(Rect dirty) {
8910 if (ViewDebug.TRACE_HIERARCHY) {
8911 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8912 }
8913
Chet Haaseaceafe62011-08-26 15:44:33 -07008914 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008915 return;
8916 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008917 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008918 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8919 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008920 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008921 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008922 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 final ViewParent p = mParent;
8924 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008925 //noinspection PointlessBooleanExpression,ConstantConditions
8926 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8927 if (p != null && ai != null && ai.mHardwareAccelerated) {
8928 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008929 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008930 p.invalidateChild(this, null);
8931 return;
8932 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 if (p != null && ai != null) {
8935 final int scrollX = mScrollX;
8936 final int scrollY = mScrollY;
8937 final Rect r = ai.mTmpInvalRect;
8938 r.set(dirty.left - scrollX, dirty.top - scrollY,
8939 dirty.right - scrollX, dirty.bottom - scrollY);
8940 mParent.invalidateChild(this, r);
8941 }
8942 }
8943 }
8944
8945 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008946 * 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 -08008947 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008948 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8949 * will be called at some point in the future. This must be called from
8950 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008951 * @param l the left position of the dirty region
8952 * @param t the top position of the dirty region
8953 * @param r the right position of the dirty region
8954 * @param b the bottom position of the dirty region
8955 */
8956 public void invalidate(int l, int t, int r, int b) {
8957 if (ViewDebug.TRACE_HIERARCHY) {
8958 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8959 }
8960
Chet Haaseaceafe62011-08-26 15:44:33 -07008961 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008962 return;
8963 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008964 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008965 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8966 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008967 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008968 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008969 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008970 final ViewParent p = mParent;
8971 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008972 //noinspection PointlessBooleanExpression,ConstantConditions
8973 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8974 if (p != null && ai != null && ai.mHardwareAccelerated) {
8975 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008976 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008977 p.invalidateChild(this, null);
8978 return;
8979 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008981 if (p != null && ai != null && l < r && t < b) {
8982 final int scrollX = mScrollX;
8983 final int scrollY = mScrollY;
8984 final Rect tmpr = ai.mTmpInvalRect;
8985 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8986 p.invalidateChild(this, tmpr);
8987 }
8988 }
8989 }
8990
8991 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008992 * Invalidate the whole view. If the view is visible,
8993 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8994 * the future. This must be called from a UI thread. To call from a non-UI thread,
8995 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008996 */
8997 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008998 invalidate(true);
8999 }
Joe Malin32736f02011-01-19 16:14:20 -08009000
Chet Haaseed032702010-10-01 14:05:54 -07009001 /**
9002 * This is where the invalidate() work actually happens. A full invalidate()
9003 * causes the drawing cache to be invalidated, but this function can be called with
9004 * invalidateCache set to false to skip that invalidation step for cases that do not
9005 * need it (for example, a component that remains at the same dimensions with the same
9006 * content).
9007 *
9008 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9009 * well. This is usually true for a full invalidate, but may be set to false if the
9010 * View's contents or dimensions have not changed.
9011 */
Romain Guy849d0a32011-02-01 17:20:48 -08009012 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009013 if (ViewDebug.TRACE_HIERARCHY) {
9014 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9015 }
9016
Chet Haaseaceafe62011-08-26 15:44:33 -07009017 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009018 return;
9019 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009020 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009021 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009022 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9023 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009024 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009025 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009026 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009027 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009028 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009030 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009031 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009032 //noinspection PointlessBooleanExpression,ConstantConditions
9033 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9034 if (p != null && ai != null && ai.mHardwareAccelerated) {
9035 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009036 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009037 p.invalidateChild(this, null);
9038 return;
9039 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009040 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009042 if (p != null && ai != null) {
9043 final Rect r = ai.mTmpInvalRect;
9044 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9045 // Don't call invalidate -- we don't want to internally scroll
9046 // our own bounds
9047 p.invalidateChild(this, r);
9048 }
9049 }
9050 }
9051
9052 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009053 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9054 * set any flags or handle all of the cases handled by the default invalidation methods.
9055 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9056 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9057 * walk up the hierarchy, transforming the dirty rect as necessary.
9058 *
9059 * The method also handles normal invalidation logic if display list properties are not
9060 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9061 * backup approach, to handle these cases used in the various property-setting methods.
9062 *
9063 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9064 * are not being used in this view
9065 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9066 * list properties are not being used in this view
9067 */
9068 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
9069 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
9070 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
9071 if (invalidateParent) {
9072 invalidateParentCaches();
9073 }
9074 if (forceRedraw) {
9075 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9076 }
9077 invalidate(false);
9078 } else {
9079 final AttachInfo ai = mAttachInfo;
9080 final ViewParent p = mParent;
9081 if (p != null && ai != null) {
9082 final Rect r = ai.mTmpInvalRect;
9083 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9084 if (mParent instanceof ViewGroup) {
9085 ((ViewGroup) mParent).invalidateChildFast(this, r);
9086 } else {
9087 mParent.invalidateChild(this, r);
9088 }
9089 }
9090 }
9091 }
9092
9093 /**
9094 * Utility method to transform a given Rect by the current matrix of this view.
9095 */
9096 void transformRect(final Rect rect) {
9097 if (!getMatrix().isIdentity()) {
9098 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9099 boundingRect.set(rect);
9100 getMatrix().mapRect(boundingRect);
9101 rect.set((int) (boundingRect.left - 0.5f),
9102 (int) (boundingRect.top - 0.5f),
9103 (int) (boundingRect.right + 0.5f),
9104 (int) (boundingRect.bottom + 0.5f));
9105 }
9106 }
9107
9108 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009109 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009110 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9111 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009112 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9113 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009114 *
9115 * @hide
9116 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009117 protected void invalidateParentCaches() {
9118 if (mParent instanceof View) {
9119 ((View) mParent).mPrivateFlags |= INVALIDATED;
9120 }
9121 }
Joe Malin32736f02011-01-19 16:14:20 -08009122
Romain Guy0fd89bf2011-01-26 15:41:30 -08009123 /**
9124 * Used to indicate that the parent of this view should be invalidated. This functionality
9125 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9126 * which is necessary when various parent-managed properties of the view change, such as
9127 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9128 * an invalidation event to the parent.
9129 *
9130 * @hide
9131 */
9132 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009133 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009134 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009135 }
9136 }
9137
9138 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009139 * Indicates whether this View is opaque. An opaque View guarantees that it will
9140 * draw all the pixels overlapping its bounds using a fully opaque color.
9141 *
9142 * Subclasses of View should override this method whenever possible to indicate
9143 * whether an instance is opaque. Opaque Views are treated in a special way by
9144 * the View hierarchy, possibly allowing it to perform optimizations during
9145 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009146 *
Romain Guy24443ea2009-05-11 11:56:30 -07009147 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009148 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009149 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009150 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009151 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009152 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9153 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009154 }
9155
Adam Powell20232d02010-12-08 21:08:53 -08009156 /**
9157 * @hide
9158 */
9159 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009160 // Opaque if:
9161 // - Has a background
9162 // - Background is opaque
9163 // - Doesn't have scrollbars or scrollbars are inside overlay
9164
Philip Milne6c8ea062012-04-03 17:38:43 -07009165 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009166 mPrivateFlags |= OPAQUE_BACKGROUND;
9167 } else {
9168 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9169 }
9170
9171 final int flags = mViewFlags;
9172 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9173 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9174 mPrivateFlags |= OPAQUE_SCROLLBARS;
9175 } else {
9176 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9177 }
9178 }
9179
9180 /**
9181 * @hide
9182 */
9183 protected boolean hasOpaqueScrollbars() {
9184 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009185 }
9186
9187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009188 * @return A handler associated with the thread running the View. This
9189 * handler can be used to pump events in the UI events queue.
9190 */
9191 public Handler getHandler() {
9192 if (mAttachInfo != null) {
9193 return mAttachInfo.mHandler;
9194 }
9195 return null;
9196 }
9197
9198 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009199 * Gets the view root associated with the View.
9200 * @return The view root, or null if none.
9201 * @hide
9202 */
9203 public ViewRootImpl getViewRootImpl() {
9204 if (mAttachInfo != null) {
9205 return mAttachInfo.mViewRootImpl;
9206 }
9207 return null;
9208 }
9209
9210 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009211 * <p>Causes the Runnable to be added to the message queue.
9212 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009213 *
Romain Guye63a4f32011-08-11 11:33:31 -07009214 * <p>This method can be invoked from outside of the UI thread
9215 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 *
9217 * @param action The Runnable that will be executed.
9218 *
9219 * @return Returns true if the Runnable was successfully placed in to the
9220 * message queue. Returns false on failure, usually because the
9221 * looper processing the message queue is exiting.
9222 */
9223 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009224 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009225 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009226 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009227 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009228 // Assume that post will succeed later
9229 ViewRootImpl.getRunQueue().post(action);
9230 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009231 }
9232
9233 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009234 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009236 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009237 *
Romain Guye63a4f32011-08-11 11:33:31 -07009238 * <p>This method can be invoked from outside of the UI thread
9239 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240 *
9241 * @param action The Runnable that will be executed.
9242 * @param delayMillis The delay (in milliseconds) until the Runnable
9243 * will be executed.
9244 *
9245 * @return true if the Runnable was successfully placed in to the
9246 * message queue. Returns false on failure, usually because the
9247 * looper processing the message queue is exiting. Note that a
9248 * result of true does not mean the Runnable will be processed --
9249 * if the looper is quit before the delivery time of the message
9250 * occurs then the message will be dropped.
9251 */
9252 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009253 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009254 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009255 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009256 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009257 // Assume that post will succeed later
9258 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9259 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260 }
9261
9262 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009263 * <p>Causes the Runnable to execute on the next animation time step.
9264 * The runnable will be run on the user interface thread.</p>
9265 *
9266 * <p>This method can be invoked from outside of the UI thread
9267 * only when this View is attached to a window.</p>
9268 *
9269 * @param action The Runnable that will be executed.
9270 *
9271 * @hide
9272 */
9273 public void postOnAnimation(Runnable action) {
9274 final AttachInfo attachInfo = mAttachInfo;
9275 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009276 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9277 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009278 } else {
9279 // Assume that post will succeed later
9280 ViewRootImpl.getRunQueue().post(action);
9281 }
9282 }
9283
9284 /**
9285 * <p>Causes the Runnable to execute on the next animation time step,
9286 * after the specified amount of time elapses.
9287 * The runnable will be run on the user interface thread.</p>
9288 *
9289 * <p>This method can be invoked from outside of the UI thread
9290 * only when this View is attached to a window.</p>
9291 *
9292 * @param action The Runnable that will be executed.
9293 * @param delayMillis The delay (in milliseconds) until the Runnable
9294 * will be executed.
9295 *
9296 * @hide
9297 */
9298 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9299 final AttachInfo attachInfo = mAttachInfo;
9300 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009301 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9302 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009303 } else {
9304 // Assume that post will succeed later
9305 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9306 }
9307 }
9308
9309 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009310 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009311 *
Romain Guye63a4f32011-08-11 11:33:31 -07009312 * <p>This method can be invoked from outside of the UI thread
9313 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 *
9315 * @param action The Runnable to remove from the message handling queue
9316 *
9317 * @return true if this view could ask the Handler to remove the Runnable,
9318 * false otherwise. When the returned value is true, the Runnable
9319 * may or may not have been actually removed from the message queue
9320 * (for instance, if the Runnable was not in the queue already.)
9321 */
9322 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009323 if (action != null) {
9324 final AttachInfo attachInfo = mAttachInfo;
9325 if (attachInfo != null) {
9326 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009327 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9328 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009329 } else {
9330 // Assume that post will succeed later
9331 ViewRootImpl.getRunQueue().removeCallbacks(action);
9332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009334 return true;
9335 }
9336
9337 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009338 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9339 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 *
Romain Guye63a4f32011-08-11 11:33:31 -07009341 * <p>This method can be invoked from outside of the UI thread
9342 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009343 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 * @see #invalidate()
9345 */
9346 public void postInvalidate() {
9347 postInvalidateDelayed(0);
9348 }
9349
9350 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009351 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9352 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009353 *
Romain Guye63a4f32011-08-11 11:33:31 -07009354 * <p>This method can be invoked from outside of the UI thread
9355 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009356 *
9357 * @param left The left coordinate of the rectangle to invalidate.
9358 * @param top The top coordinate of the rectangle to invalidate.
9359 * @param right The right coordinate of the rectangle to invalidate.
9360 * @param bottom The bottom coordinate of the rectangle to invalidate.
9361 *
9362 * @see #invalidate(int, int, int, int)
9363 * @see #invalidate(Rect)
9364 */
9365 public void postInvalidate(int left, int top, int right, int bottom) {
9366 postInvalidateDelayed(0, left, top, right, bottom);
9367 }
9368
9369 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009370 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9371 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009372 *
Romain Guye63a4f32011-08-11 11:33:31 -07009373 * <p>This method can be invoked from outside of the UI thread
9374 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009375 *
9376 * @param delayMilliseconds the duration in milliseconds to delay the
9377 * invalidation by
9378 */
9379 public void postInvalidateDelayed(long delayMilliseconds) {
9380 // We try only with the AttachInfo because there's no point in invalidating
9381 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009382 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009383 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009384 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009385 }
9386 }
9387
9388 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009389 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9390 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009391 *
Romain Guye63a4f32011-08-11 11:33:31 -07009392 * <p>This method can be invoked from outside of the UI thread
9393 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 *
9395 * @param delayMilliseconds the duration in milliseconds to delay the
9396 * invalidation by
9397 * @param left The left coordinate of the rectangle to invalidate.
9398 * @param top The top coordinate of the rectangle to invalidate.
9399 * @param right The right coordinate of the rectangle to invalidate.
9400 * @param bottom The bottom coordinate of the rectangle to invalidate.
9401 */
9402 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9403 int right, int bottom) {
9404
9405 // We try only with the AttachInfo because there's no point in invalidating
9406 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009407 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009408 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009409 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9410 info.target = this;
9411 info.left = left;
9412 info.top = top;
9413 info.right = right;
9414 info.bottom = bottom;
9415
Jeff Browna175a5b2012-02-15 19:18:31 -08009416 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 }
9418 }
9419
9420 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08009421 * <p>Cause an invalidate to happen on the next animation time step, typically the
9422 * next display frame.</p>
9423 *
9424 * <p>This method can be invoked from outside of the UI thread
9425 * only when this View is attached to a window.</p>
9426 *
9427 * @hide
9428 */
9429 public void postInvalidateOnAnimation() {
9430 // We try only with the AttachInfo because there's no point in invalidating
9431 // if we are not attached to our window
9432 final AttachInfo attachInfo = mAttachInfo;
9433 if (attachInfo != null) {
9434 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
9435 }
9436 }
9437
9438 /**
9439 * <p>Cause an invalidate of the specified area to happen on the next animation
9440 * time step, typically the next display frame.</p>
9441 *
9442 * <p>This method can be invoked from outside of the UI thread
9443 * only when this View is attached to a window.</p>
9444 *
9445 * @param left The left coordinate of the rectangle to invalidate.
9446 * @param top The top coordinate of the rectangle to invalidate.
9447 * @param right The right coordinate of the rectangle to invalidate.
9448 * @param bottom The bottom coordinate of the rectangle to invalidate.
9449 *
9450 * @hide
9451 */
9452 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9453 // We try only with the AttachInfo because there's no point in invalidating
9454 // if we are not attached to our window
9455 final AttachInfo attachInfo = mAttachInfo;
9456 if (attachInfo != null) {
9457 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9458 info.target = this;
9459 info.left = left;
9460 info.top = top;
9461 info.right = right;
9462 info.bottom = bottom;
9463
9464 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9465 }
9466 }
9467
9468 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009469 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9470 * This event is sent at most once every
9471 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9472 */
9473 private void postSendViewScrolledAccessibilityEventCallback() {
9474 if (mSendViewScrolledAccessibilityEvent == null) {
9475 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9476 }
9477 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9478 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9479 postDelayed(mSendViewScrolledAccessibilityEvent,
9480 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9481 }
9482 }
9483
9484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009485 * Called by a parent to request that a child update its values for mScrollX
9486 * and mScrollY if necessary. This will typically be done if the child is
9487 * animating a scroll using a {@link android.widget.Scroller Scroller}
9488 * object.
9489 */
9490 public void computeScroll() {
9491 }
9492
9493 /**
9494 * <p>Indicate whether the horizontal edges are faded when the view is
9495 * scrolled horizontally.</p>
9496 *
9497 * @return true if the horizontal edges should are faded on scroll, false
9498 * otherwise
9499 *
9500 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009501 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009502 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009503 */
9504 public boolean isHorizontalFadingEdgeEnabled() {
9505 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9506 }
9507
9508 /**
9509 * <p>Define whether the horizontal edges should be faded when this view
9510 * is scrolled horizontally.</p>
9511 *
9512 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9513 * be faded when the view is scrolled
9514 * horizontally
9515 *
9516 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009517 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009518 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009519 */
9520 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9521 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9522 if (horizontalFadingEdgeEnabled) {
9523 initScrollCache();
9524 }
9525
9526 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9527 }
9528 }
9529
9530 /**
9531 * <p>Indicate whether the vertical edges are faded when the view is
9532 * scrolled horizontally.</p>
9533 *
9534 * @return true if the vertical edges should are faded on scroll, false
9535 * otherwise
9536 *
9537 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009538 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009539 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009540 */
9541 public boolean isVerticalFadingEdgeEnabled() {
9542 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9543 }
9544
9545 /**
9546 * <p>Define whether the vertical edges should be faded when this view
9547 * is scrolled vertically.</p>
9548 *
9549 * @param verticalFadingEdgeEnabled true if the vertical edges should
9550 * be faded when the view is scrolled
9551 * vertically
9552 *
9553 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009554 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009555 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009556 */
9557 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9558 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9559 if (verticalFadingEdgeEnabled) {
9560 initScrollCache();
9561 }
9562
9563 mViewFlags ^= FADING_EDGE_VERTICAL;
9564 }
9565 }
9566
9567 /**
9568 * Returns the strength, or intensity, of the top faded edge. The strength is
9569 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9570 * returns 0.0 or 1.0 but no value in between.
9571 *
9572 * Subclasses should override this method to provide a smoother fade transition
9573 * when scrolling occurs.
9574 *
9575 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9576 */
9577 protected float getTopFadingEdgeStrength() {
9578 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9579 }
9580
9581 /**
9582 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9583 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9584 * returns 0.0 or 1.0 but no value in between.
9585 *
9586 * Subclasses should override this method to provide a smoother fade transition
9587 * when scrolling occurs.
9588 *
9589 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9590 */
9591 protected float getBottomFadingEdgeStrength() {
9592 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9593 computeVerticalScrollRange() ? 1.0f : 0.0f;
9594 }
9595
9596 /**
9597 * Returns the strength, or intensity, of the left faded edge. The strength is
9598 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9599 * returns 0.0 or 1.0 but no value in between.
9600 *
9601 * Subclasses should override this method to provide a smoother fade transition
9602 * when scrolling occurs.
9603 *
9604 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9605 */
9606 protected float getLeftFadingEdgeStrength() {
9607 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9608 }
9609
9610 /**
9611 * Returns the strength, or intensity, of the right faded edge. The strength is
9612 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9613 * returns 0.0 or 1.0 but no value in between.
9614 *
9615 * Subclasses should override this method to provide a smoother fade transition
9616 * when scrolling occurs.
9617 *
9618 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9619 */
9620 protected float getRightFadingEdgeStrength() {
9621 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9622 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9623 }
9624
9625 /**
9626 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9627 * scrollbar is not drawn by default.</p>
9628 *
9629 * @return true if the horizontal scrollbar should be painted, false
9630 * otherwise
9631 *
9632 * @see #setHorizontalScrollBarEnabled(boolean)
9633 */
9634 public boolean isHorizontalScrollBarEnabled() {
9635 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9636 }
9637
9638 /**
9639 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9640 * scrollbar is not drawn by default.</p>
9641 *
9642 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9643 * be painted
9644 *
9645 * @see #isHorizontalScrollBarEnabled()
9646 */
9647 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9648 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9649 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009650 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009651 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009652 }
9653 }
9654
9655 /**
9656 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9657 * scrollbar is not drawn by default.</p>
9658 *
9659 * @return true if the vertical scrollbar should be painted, false
9660 * otherwise
9661 *
9662 * @see #setVerticalScrollBarEnabled(boolean)
9663 */
9664 public boolean isVerticalScrollBarEnabled() {
9665 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9666 }
9667
9668 /**
9669 * <p>Define whether the vertical scrollbar should be drawn or not. The
9670 * scrollbar is not drawn by default.</p>
9671 *
9672 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9673 * be painted
9674 *
9675 * @see #isVerticalScrollBarEnabled()
9676 */
9677 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9678 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9679 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009680 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009681 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009682 }
9683 }
9684
Adam Powell20232d02010-12-08 21:08:53 -08009685 /**
9686 * @hide
9687 */
9688 protected void recomputePadding() {
9689 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009690 }
Joe Malin32736f02011-01-19 16:14:20 -08009691
Mike Cleronfe81d382009-09-28 14:22:16 -07009692 /**
9693 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009694 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009695 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009696 *
Philip Milne6c8ea062012-04-03 17:38:43 -07009697 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -07009698 */
9699 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9700 initScrollCache();
9701 final ScrollabilityCache scrollabilityCache = mScrollCache;
9702 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009703 if (fadeScrollbars) {
9704 scrollabilityCache.state = ScrollabilityCache.OFF;
9705 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009706 scrollabilityCache.state = ScrollabilityCache.ON;
9707 }
9708 }
Joe Malin32736f02011-01-19 16:14:20 -08009709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 /**
Joe Malin32736f02011-01-19 16:14:20 -08009711 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009712 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009713 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009714 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -07009715 *
9716 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -07009717 */
9718 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009719 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009720 }
Joe Malin32736f02011-01-19 16:14:20 -08009721
Mike Cleron52f0a642009-09-28 18:21:37 -07009722 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07009723 *
9724 * Returns the delay before scrollbars fade.
9725 *
9726 * @return the delay before scrollbars fade
9727 *
9728 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9729 */
9730 public int getScrollBarDefaultDelayBeforeFade() {
9731 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
9732 mScrollCache.scrollBarDefaultDelayBeforeFade;
9733 }
9734
9735 /**
9736 * Define the delay before scrollbars fade.
9737 *
9738 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
9739 *
9740 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9741 */
9742 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
9743 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
9744 }
9745
9746 /**
9747 *
9748 * Returns the scrollbar fade duration.
9749 *
9750 * @return the scrollbar fade duration
9751 *
9752 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9753 */
9754 public int getScrollBarFadeDuration() {
9755 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
9756 mScrollCache.scrollBarFadeDuration;
9757 }
9758
9759 /**
9760 * Define the scrollbar fade duration.
9761 *
9762 * @param scrollBarFadeDuration - the scrollbar fade duration
9763 *
9764 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9765 */
9766 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
9767 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
9768 }
9769
9770 /**
9771 *
9772 * Returns the scrollbar size.
9773 *
9774 * @return the scrollbar size
9775 *
9776 * @attr ref android.R.styleable#View_scrollbarSize
9777 */
9778 public int getScrollBarSize() {
9779 return mScrollCache == null ? ViewConfiguration.getScrollBarSize() :
9780 mScrollCache.scrollBarSize;
9781 }
9782
9783 /**
9784 * Define the scrollbar size.
9785 *
9786 * @param scrollBarSize - the scrollbar size
9787 *
9788 * @attr ref android.R.styleable#View_scrollbarSize
9789 */
9790 public void setScrollBarSize(int scrollBarSize) {
9791 getScrollCache().scrollBarSize = scrollBarSize;
9792 }
9793
9794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009795 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9796 * inset. When inset, they add to the padding of the view. And the scrollbars
9797 * can be drawn inside the padding area or on the edge of the view. For example,
9798 * if a view has a background drawable and you want to draw the scrollbars
9799 * inside the padding specified by the drawable, you can use
9800 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9801 * appear at the edge of the view, ignoring the padding, then you can use
9802 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9803 * @param style the style of the scrollbars. Should be one of
9804 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9805 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9806 * @see #SCROLLBARS_INSIDE_OVERLAY
9807 * @see #SCROLLBARS_INSIDE_INSET
9808 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9809 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009810 *
9811 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009812 */
9813 public void setScrollBarStyle(int style) {
9814 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9815 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009816 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009817 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 }
9819 }
9820
9821 /**
9822 * <p>Returns the current scrollbar style.</p>
9823 * @return the current scrollbar style
9824 * @see #SCROLLBARS_INSIDE_OVERLAY
9825 * @see #SCROLLBARS_INSIDE_INSET
9826 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9827 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009828 *
9829 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009831 @ViewDebug.ExportedProperty(mapping = {
9832 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9833 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9834 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9835 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9836 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 public int getScrollBarStyle() {
9838 return mViewFlags & SCROLLBARS_STYLE_MASK;
9839 }
9840
9841 /**
9842 * <p>Compute the horizontal range that the horizontal scrollbar
9843 * represents.</p>
9844 *
9845 * <p>The range is expressed in arbitrary units that must be the same as the
9846 * units used by {@link #computeHorizontalScrollExtent()} and
9847 * {@link #computeHorizontalScrollOffset()}.</p>
9848 *
9849 * <p>The default range is the drawing width of this view.</p>
9850 *
9851 * @return the total horizontal range represented by the horizontal
9852 * scrollbar
9853 *
9854 * @see #computeHorizontalScrollExtent()
9855 * @see #computeHorizontalScrollOffset()
9856 * @see android.widget.ScrollBarDrawable
9857 */
9858 protected int computeHorizontalScrollRange() {
9859 return getWidth();
9860 }
9861
9862 /**
9863 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9864 * within the horizontal range. This value is used to compute the position
9865 * of the thumb within the scrollbar's track.</p>
9866 *
9867 * <p>The range is expressed in arbitrary units that must be the same as the
9868 * units used by {@link #computeHorizontalScrollRange()} and
9869 * {@link #computeHorizontalScrollExtent()}.</p>
9870 *
9871 * <p>The default offset is the scroll offset of this view.</p>
9872 *
9873 * @return the horizontal offset of the scrollbar's thumb
9874 *
9875 * @see #computeHorizontalScrollRange()
9876 * @see #computeHorizontalScrollExtent()
9877 * @see android.widget.ScrollBarDrawable
9878 */
9879 protected int computeHorizontalScrollOffset() {
9880 return mScrollX;
9881 }
9882
9883 /**
9884 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9885 * within the horizontal range. This value is used to compute the length
9886 * of the thumb within the scrollbar's track.</p>
9887 *
9888 * <p>The range is expressed in arbitrary units that must be the same as the
9889 * units used by {@link #computeHorizontalScrollRange()} and
9890 * {@link #computeHorizontalScrollOffset()}.</p>
9891 *
9892 * <p>The default extent is the drawing width of this view.</p>
9893 *
9894 * @return the horizontal extent of the scrollbar's thumb
9895 *
9896 * @see #computeHorizontalScrollRange()
9897 * @see #computeHorizontalScrollOffset()
9898 * @see android.widget.ScrollBarDrawable
9899 */
9900 protected int computeHorizontalScrollExtent() {
9901 return getWidth();
9902 }
9903
9904 /**
9905 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9906 *
9907 * <p>The range is expressed in arbitrary units that must be the same as the
9908 * units used by {@link #computeVerticalScrollExtent()} and
9909 * {@link #computeVerticalScrollOffset()}.</p>
9910 *
9911 * @return the total vertical range represented by the vertical scrollbar
9912 *
9913 * <p>The default range is the drawing height of this view.</p>
9914 *
9915 * @see #computeVerticalScrollExtent()
9916 * @see #computeVerticalScrollOffset()
9917 * @see android.widget.ScrollBarDrawable
9918 */
9919 protected int computeVerticalScrollRange() {
9920 return getHeight();
9921 }
9922
9923 /**
9924 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9925 * within the horizontal range. This value is used to compute the position
9926 * of the thumb within the scrollbar's track.</p>
9927 *
9928 * <p>The range is expressed in arbitrary units that must be the same as the
9929 * units used by {@link #computeVerticalScrollRange()} and
9930 * {@link #computeVerticalScrollExtent()}.</p>
9931 *
9932 * <p>The default offset is the scroll offset of this view.</p>
9933 *
9934 * @return the vertical offset of the scrollbar's thumb
9935 *
9936 * @see #computeVerticalScrollRange()
9937 * @see #computeVerticalScrollExtent()
9938 * @see android.widget.ScrollBarDrawable
9939 */
9940 protected int computeVerticalScrollOffset() {
9941 return mScrollY;
9942 }
9943
9944 /**
9945 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9946 * within the vertical range. This value is used to compute the length
9947 * of the thumb within the scrollbar's track.</p>
9948 *
9949 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009950 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009951 * {@link #computeVerticalScrollOffset()}.</p>
9952 *
9953 * <p>The default extent is the drawing height of this view.</p>
9954 *
9955 * @return the vertical extent of the scrollbar's thumb
9956 *
9957 * @see #computeVerticalScrollRange()
9958 * @see #computeVerticalScrollOffset()
9959 * @see android.widget.ScrollBarDrawable
9960 */
9961 protected int computeVerticalScrollExtent() {
9962 return getHeight();
9963 }
9964
9965 /**
Adam Powell69159442011-06-13 17:53:06 -07009966 * Check if this view can be scrolled horizontally in a certain direction.
9967 *
9968 * @param direction Negative to check scrolling left, positive to check scrolling right.
9969 * @return true if this view can be scrolled in the specified direction, false otherwise.
9970 */
9971 public boolean canScrollHorizontally(int direction) {
9972 final int offset = computeHorizontalScrollOffset();
9973 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9974 if (range == 0) return false;
9975 if (direction < 0) {
9976 return offset > 0;
9977 } else {
9978 return offset < range - 1;
9979 }
9980 }
9981
9982 /**
9983 * Check if this view can be scrolled vertically in a certain direction.
9984 *
9985 * @param direction Negative to check scrolling up, positive to check scrolling down.
9986 * @return true if this view can be scrolled in the specified direction, false otherwise.
9987 */
9988 public boolean canScrollVertically(int direction) {
9989 final int offset = computeVerticalScrollOffset();
9990 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9991 if (range == 0) return false;
9992 if (direction < 0) {
9993 return offset > 0;
9994 } else {
9995 return offset < range - 1;
9996 }
9997 }
9998
9999 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10001 * scrollbars are painted only if they have been awakened first.</p>
10002 *
10003 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010004 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010005 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010006 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010007 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010008 // scrollbars are drawn only when the animation is running
10009 final ScrollabilityCache cache = mScrollCache;
10010 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010011
Mike Cleronf116bf82009-09-27 19:14:12 -070010012 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010013
Mike Cleronf116bf82009-09-27 19:14:12 -070010014 if (state == ScrollabilityCache.OFF) {
10015 return;
10016 }
Joe Malin32736f02011-01-19 16:14:20 -080010017
Mike Cleronf116bf82009-09-27 19:14:12 -070010018 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010019
Mike Cleronf116bf82009-09-27 19:14:12 -070010020 if (state == ScrollabilityCache.FADING) {
10021 // We're fading -- get our fade interpolation
10022 if (cache.interpolatorValues == null) {
10023 cache.interpolatorValues = new float[1];
10024 }
Joe Malin32736f02011-01-19 16:14:20 -080010025
Mike Cleronf116bf82009-09-27 19:14:12 -070010026 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010027
Mike Cleronf116bf82009-09-27 19:14:12 -070010028 // Stops the animation if we're done
10029 if (cache.scrollBarInterpolator.timeToValues(values) ==
10030 Interpolator.Result.FREEZE_END) {
10031 cache.state = ScrollabilityCache.OFF;
10032 } else {
10033 cache.scrollBar.setAlpha(Math.round(values[0]));
10034 }
Joe Malin32736f02011-01-19 16:14:20 -080010035
10036 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010037 // drawing. We only want this when we're fading so that
10038 // we prevent excessive redraws
10039 invalidate = true;
10040 } else {
10041 // We're just on -- but we may have been fading before so
10042 // reset alpha
10043 cache.scrollBar.setAlpha(255);
10044 }
10045
Joe Malin32736f02011-01-19 16:14:20 -080010046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010047 final int viewFlags = mViewFlags;
10048
10049 final boolean drawHorizontalScrollBar =
10050 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10051 final boolean drawVerticalScrollBar =
10052 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10053 && !isVerticalScrollBarHidden();
10054
10055 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10056 final int width = mRight - mLeft;
10057 final int height = mBottom - mTop;
10058
10059 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010060
Mike Reede8853fc2009-09-04 14:01:48 -040010061 final int scrollX = mScrollX;
10062 final int scrollY = mScrollY;
10063 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10064
Mike Cleronf116bf82009-09-27 19:14:12 -070010065 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010068 int size = scrollBar.getSize(false);
10069 if (size <= 0) {
10070 size = cache.scrollBarSize;
10071 }
10072
Mike Cleronf116bf82009-09-27 19:14:12 -070010073 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010074 computeHorizontalScrollOffset(),
10075 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010076 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010077 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010078 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010079 left = scrollX + (mPaddingLeft & inside);
10080 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10081 bottom = top + size;
10082 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10083 if (invalidate) {
10084 invalidate(left, top, right, bottom);
10085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010086 }
10087
10088 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010089 int size = scrollBar.getSize(true);
10090 if (size <= 0) {
10091 size = cache.scrollBarSize;
10092 }
10093
Mike Reede8853fc2009-09-04 14:01:48 -040010094 scrollBar.setParameters(computeVerticalScrollRange(),
10095 computeVerticalScrollOffset(),
10096 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010097 switch (mVerticalScrollbarPosition) {
10098 default:
10099 case SCROLLBAR_POSITION_DEFAULT:
10100 case SCROLLBAR_POSITION_RIGHT:
10101 left = scrollX + width - size - (mUserPaddingRight & inside);
10102 break;
10103 case SCROLLBAR_POSITION_LEFT:
10104 left = scrollX + (mUserPaddingLeft & inside);
10105 break;
10106 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010107 top = scrollY + (mPaddingTop & inside);
10108 right = left + size;
10109 bottom = scrollY + height - (mUserPaddingBottom & inside);
10110 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10111 if (invalidate) {
10112 invalidate(left, top, right, bottom);
10113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114 }
10115 }
10116 }
10117 }
Romain Guy8506ab42009-06-11 17:35:47 -070010118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010119 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010120 * 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 -080010121 * FastScroller is visible.
10122 * @return whether to temporarily hide the vertical scrollbar
10123 * @hide
10124 */
10125 protected boolean isVerticalScrollBarHidden() {
10126 return false;
10127 }
10128
10129 /**
10130 * <p>Draw the horizontal scrollbar if
10131 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10132 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010133 * @param canvas the canvas on which to draw the scrollbar
10134 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 *
10136 * @see #isHorizontalScrollBarEnabled()
10137 * @see #computeHorizontalScrollRange()
10138 * @see #computeHorizontalScrollExtent()
10139 * @see #computeHorizontalScrollOffset()
10140 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010141 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010142 */
Romain Guy8fb95422010-08-17 18:38:51 -070010143 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10144 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010145 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010146 scrollBar.draw(canvas);
10147 }
Mike Reede8853fc2009-09-04 14:01:48 -040010148
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10151 * returns true.</p>
10152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010153 * @param canvas the canvas on which to draw the scrollbar
10154 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 *
10156 * @see #isVerticalScrollBarEnabled()
10157 * @see #computeVerticalScrollRange()
10158 * @see #computeVerticalScrollExtent()
10159 * @see #computeVerticalScrollOffset()
10160 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010161 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010162 */
Romain Guy8fb95422010-08-17 18:38:51 -070010163 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10164 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010165 scrollBar.setBounds(l, t, r, b);
10166 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 }
10168
10169 /**
10170 * Implement this to do your drawing.
10171 *
10172 * @param canvas the canvas on which the background will be drawn
10173 */
10174 protected void onDraw(Canvas canvas) {
10175 }
10176
10177 /*
10178 * Caller is responsible for calling requestLayout if necessary.
10179 * (This allows addViewInLayout to not request a new layout.)
10180 */
10181 void assignParent(ViewParent parent) {
10182 if (mParent == null) {
10183 mParent = parent;
10184 } else if (parent == null) {
10185 mParent = null;
10186 } else {
10187 throw new RuntimeException("view " + this + " being added, but"
10188 + " it already has a parent");
10189 }
10190 }
10191
10192 /**
10193 * This is called when the view is attached to a window. At this point it
10194 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010195 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10196 * however it may be called any time before the first onDraw -- including
10197 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010198 *
10199 * @see #onDetachedFromWindow()
10200 */
10201 protected void onAttachedToWindow() {
10202 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10203 mParent.requestTransparentRegion(this);
10204 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010205 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10206 initialAwakenScrollBars();
10207 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10208 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010209 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010210 // Order is important here: LayoutDirection MUST be resolved before Padding
10211 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010212 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010213 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010214 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010215 resolveTextAlignment();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010216 if (isFocused()) {
10217 InputMethodManager imm = InputMethodManager.peekInstance();
10218 imm.focusIn(this);
10219 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010220 }
Cibu Johny86666632010-02-22 13:01:02 -080010221
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010222 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010223 * @see #onScreenStateChanged(int)
10224 */
10225 void dispatchScreenStateChanged(int screenState) {
10226 onScreenStateChanged(screenState);
10227 }
10228
10229 /**
10230 * This method is called whenever the state of the screen this view is
10231 * attached to changes. A state change will usually occurs when the screen
10232 * turns on or off (whether it happens automatically or the user does it
10233 * manually.)
10234 *
10235 * @param screenState The new state of the screen. Can be either
10236 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10237 */
10238 public void onScreenStateChanged(int screenState) {
10239 }
10240
10241 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010242 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10243 */
10244 private boolean hasRtlSupport() {
10245 return mContext.getApplicationInfo().hasRtlSupport();
10246 }
10247
10248 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010249 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10250 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010251 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010252 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010253 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010254 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010255 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010256
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010257 if (hasRtlSupport()) {
10258 // Set resolved depending on layout direction
10259 switch (getLayoutDirection()) {
10260 case LAYOUT_DIRECTION_INHERIT:
10261 // If this is root view, no need to look at parent's layout dir.
10262 if (canResolveLayoutDirection()) {
10263 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010264
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010265 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10266 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10267 }
10268 } else {
10269 // Nothing to do, LTR by default
10270 }
10271 break;
10272 case LAYOUT_DIRECTION_RTL:
10273 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10274 break;
10275 case LAYOUT_DIRECTION_LOCALE:
10276 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010277 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10278 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010279 break;
10280 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010281 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010282 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010283 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010284
10285 // Set to resolved
10286 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010287 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010288 // Resolve padding
10289 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010290 }
10291
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010292 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010293 * Called when layout direction has been resolved.
10294 *
10295 * The default implementation does nothing.
10296 */
10297 public void onResolvedLayoutDirectionChanged() {
10298 }
10299
10300 /**
10301 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010302 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010303 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010304 // If the user specified the absolute padding (either with android:padding or
10305 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10306 // use the default padding or the padding from the background drawable
10307 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010308 int resolvedLayoutDirection = getResolvedLayoutDirection();
10309 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010310 case LAYOUT_DIRECTION_RTL:
10311 // Start user padding override Right user padding. Otherwise, if Right user
10312 // padding is not defined, use the default Right padding. If Right user padding
10313 // is defined, just use it.
10314 if (mUserPaddingStart >= 0) {
10315 mUserPaddingRight = mUserPaddingStart;
10316 } else if (mUserPaddingRight < 0) {
10317 mUserPaddingRight = mPaddingRight;
10318 }
10319 if (mUserPaddingEnd >= 0) {
10320 mUserPaddingLeft = mUserPaddingEnd;
10321 } else if (mUserPaddingLeft < 0) {
10322 mUserPaddingLeft = mPaddingLeft;
10323 }
10324 break;
10325 case LAYOUT_DIRECTION_LTR:
10326 default:
10327 // Start user padding override Left user padding. Otherwise, if Left user
10328 // padding is not defined, use the default left padding. If Left user padding
10329 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010330 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010331 mUserPaddingLeft = mUserPaddingStart;
10332 } else if (mUserPaddingLeft < 0) {
10333 mUserPaddingLeft = mPaddingLeft;
10334 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010335 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010336 mUserPaddingRight = mUserPaddingEnd;
10337 } else if (mUserPaddingRight < 0) {
10338 mUserPaddingRight = mPaddingRight;
10339 }
10340 }
10341
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010342 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10343
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010344 if(isPaddingRelative()) {
10345 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10346 } else {
10347 recomputePadding();
10348 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010349 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010350 }
10351
10352 /**
10353 * Resolve padding depending on the layout direction. Subclasses that care about
10354 * padding resolution should override this method. The default implementation does
10355 * nothing.
10356 *
10357 * @param layoutDirection the direction of the layout
10358 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010359 * @see {@link #LAYOUT_DIRECTION_LTR}
10360 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010361 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010362 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010363 }
10364
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010365 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010366 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010367 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010368 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010369 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010370 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010371 switch (getLayoutDirection()) {
10372 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010373 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010374 default:
10375 return true;
10376 }
10377 }
10378
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010379 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010380 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10381 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010382 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010383 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010384 // Reset the current resolved bits
10385 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010386 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010387 // Reset also the text direction
10388 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010389 }
10390
10391 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010392 * Called during reset of resolved layout direction.
10393 *
10394 * Subclasses need to override this method to clear cached information that depends on the
10395 * resolved layout direction, or to inform child views that inherit their layout direction.
10396 *
10397 * The default implementation does nothing.
10398 */
10399 public void onResolvedLayoutDirectionReset() {
10400 }
10401
10402 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010403 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010404 *
10405 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010406 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010407 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010408 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080010409 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 }
10411
10412 /**
10413 * This is called when the view is detached from a window. At this point it
10414 * no longer has a surface for drawing.
10415 *
10416 * @see #onAttachedToWindow()
10417 */
10418 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080010419 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080010420
Romain Guya440b002010-02-24 15:57:54 -080010421 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050010422 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080010423 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070010424 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080010425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010427
Romain Guya998dff2012-03-23 18:58:36 -070010428 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010429
10430 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070010431 if (mDisplayList != null) {
10432 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
10433 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010434 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070010435 } else {
10436 if (mDisplayList != null) {
10437 // Should never happen
10438 mDisplayList.invalidate();
10439 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010440 }
10441
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080010442 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070010443
10444 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010445 resetResolvedTextAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010446 }
10447
10448 /**
10449 * @return The number of times this view has been attached to a window
10450 */
10451 protected int getWindowAttachCount() {
10452 return mWindowAttachCount;
10453 }
10454
10455 /**
10456 * Retrieve a unique token identifying the window this view is attached to.
10457 * @return Return the window's token for use in
10458 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
10459 */
10460 public IBinder getWindowToken() {
10461 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
10462 }
10463
10464 /**
10465 * Retrieve a unique token identifying the top-level "real" window of
10466 * the window that this view is attached to. That is, this is like
10467 * {@link #getWindowToken}, except if the window this view in is a panel
10468 * window (attached to another containing window), then the token of
10469 * the containing window is returned instead.
10470 *
10471 * @return Returns the associated window token, either
10472 * {@link #getWindowToken()} or the containing window's token.
10473 */
10474 public IBinder getApplicationWindowToken() {
10475 AttachInfo ai = mAttachInfo;
10476 if (ai != null) {
10477 IBinder appWindowToken = ai.mPanelParentWindowToken;
10478 if (appWindowToken == null) {
10479 appWindowToken = ai.mWindowToken;
10480 }
10481 return appWindowToken;
10482 }
10483 return null;
10484 }
10485
10486 /**
10487 * Retrieve private session object this view hierarchy is using to
10488 * communicate with the window manager.
10489 * @return the session object to communicate with the window manager
10490 */
10491 /*package*/ IWindowSession getWindowSession() {
10492 return mAttachInfo != null ? mAttachInfo.mSession : null;
10493 }
10494
10495 /**
10496 * @param info the {@link android.view.View.AttachInfo} to associated with
10497 * this view
10498 */
10499 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
10500 //System.out.println("Attached! " + this);
10501 mAttachInfo = info;
10502 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010503 // We will need to evaluate the drawable state at least once.
10504 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505 if (mFloatingTreeObserver != null) {
10506 info.mTreeObserver.merge(mFloatingTreeObserver);
10507 mFloatingTreeObserver = null;
10508 }
10509 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
10510 mAttachInfo.mScrollContainers.add(this);
10511 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
10512 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070010513 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010514 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080010515
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010516 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010517 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010518 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010519 if (listeners != null && listeners.size() > 0) {
10520 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10521 // perform the dispatching. The iterator is a safe guard against listeners that
10522 // could mutate the list by calling the various add/remove methods. This prevents
10523 // the array from being modified while we iterate it.
10524 for (OnAttachStateChangeListener listener : listeners) {
10525 listener.onViewAttachedToWindow(this);
10526 }
10527 }
10528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 int vis = info.mWindowVisibility;
10530 if (vis != GONE) {
10531 onWindowVisibilityChanged(vis);
10532 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010533 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
10534 // If nobody has evaluated the drawable state yet, then do it now.
10535 refreshDrawableState();
10536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537 }
10538
10539 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010540 AttachInfo info = mAttachInfo;
10541 if (info != null) {
10542 int vis = info.mWindowVisibility;
10543 if (vis != GONE) {
10544 onWindowVisibilityChanged(GONE);
10545 }
10546 }
10547
10548 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010549
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010550 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010551 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010552 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010553 if (listeners != null && listeners.size() > 0) {
10554 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10555 // perform the dispatching. The iterator is a safe guard against listeners that
10556 // could mutate the list by calling the various add/remove methods. This prevents
10557 // the array from being modified while we iterate it.
10558 for (OnAttachStateChangeListener listener : listeners) {
10559 listener.onViewDetachedFromWindow(this);
10560 }
10561 }
10562
Romain Guy01d5edc2011-01-28 11:28:53 -080010563 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 mAttachInfo.mScrollContainers.remove(this);
10565 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10566 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 mAttachInfo = null;
10569 }
10570
10571 /**
10572 * Store this view hierarchy's frozen state into the given container.
10573 *
10574 * @param container The SparseArray in which to save the view's state.
10575 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010576 * @see #restoreHierarchyState(android.util.SparseArray)
10577 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10578 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 */
10580 public void saveHierarchyState(SparseArray<Parcelable> container) {
10581 dispatchSaveInstanceState(container);
10582 }
10583
10584 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010585 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10586 * this view and its children. May be overridden to modify how freezing happens to a
10587 * 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 -080010588 *
10589 * @param container The SparseArray in which to save the view's state.
10590 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010591 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10592 * @see #saveHierarchyState(android.util.SparseArray)
10593 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 */
10595 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10596 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10597 mPrivateFlags &= ~SAVE_STATE_CALLED;
10598 Parcelable state = onSaveInstanceState();
10599 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10600 throw new IllegalStateException(
10601 "Derived class did not call super.onSaveInstanceState()");
10602 }
10603 if (state != null) {
10604 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10605 // + ": " + state);
10606 container.put(mID, state);
10607 }
10608 }
10609 }
10610
10611 /**
10612 * Hook allowing a view to generate a representation of its internal state
10613 * that can later be used to create a new instance with that same state.
10614 * This state should only contain information that is not persistent or can
10615 * not be reconstructed later. For example, you will never store your
10616 * current position on screen because that will be computed again when a
10617 * new instance of the view is placed in its view hierarchy.
10618 * <p>
10619 * Some examples of things you may store here: the current cursor position
10620 * in a text view (but usually not the text itself since that is stored in a
10621 * content provider or other persistent storage), the currently selected
10622 * item in a list view.
10623 *
10624 * @return Returns a Parcelable object containing the view's current dynamic
10625 * state, or null if there is nothing interesting to save. The
10626 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070010627 * @see #onRestoreInstanceState(android.os.Parcelable)
10628 * @see #saveHierarchyState(android.util.SparseArray)
10629 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 * @see #setSaveEnabled(boolean)
10631 */
10632 protected Parcelable onSaveInstanceState() {
10633 mPrivateFlags |= SAVE_STATE_CALLED;
10634 return BaseSavedState.EMPTY_STATE;
10635 }
10636
10637 /**
10638 * Restore this view hierarchy's frozen state from the given container.
10639 *
10640 * @param container The SparseArray which holds previously frozen states.
10641 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010642 * @see #saveHierarchyState(android.util.SparseArray)
10643 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10644 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010645 */
10646 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10647 dispatchRestoreInstanceState(container);
10648 }
10649
10650 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010651 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10652 * state for this view and its children. May be overridden to modify how restoring
10653 * happens to a view's children; for example, some views may want to not store state
10654 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 *
10656 * @param container The SparseArray which holds previously saved state.
10657 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010658 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10659 * @see #restoreHierarchyState(android.util.SparseArray)
10660 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010661 */
10662 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10663 if (mID != NO_ID) {
10664 Parcelable state = container.get(mID);
10665 if (state != null) {
10666 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10667 // + ": " + state);
10668 mPrivateFlags &= ~SAVE_STATE_CALLED;
10669 onRestoreInstanceState(state);
10670 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10671 throw new IllegalStateException(
10672 "Derived class did not call super.onRestoreInstanceState()");
10673 }
10674 }
10675 }
10676 }
10677
10678 /**
10679 * Hook allowing a view to re-apply a representation of its internal state that had previously
10680 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10681 * null state.
10682 *
10683 * @param state The frozen state that had previously been returned by
10684 * {@link #onSaveInstanceState}.
10685 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010686 * @see #onSaveInstanceState()
10687 * @see #restoreHierarchyState(android.util.SparseArray)
10688 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 */
10690 protected void onRestoreInstanceState(Parcelable state) {
10691 mPrivateFlags |= SAVE_STATE_CALLED;
10692 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010693 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10694 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010695 + "when two views of different type have the same id in the same hierarchy. "
10696 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010697 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 }
10699 }
10700
10701 /**
10702 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10703 *
10704 * @return the drawing start time in milliseconds
10705 */
10706 public long getDrawingTime() {
10707 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10708 }
10709
10710 /**
10711 * <p>Enables or disables the duplication of the parent's state into this view. When
10712 * duplication is enabled, this view gets its drawable state from its parent rather
10713 * than from its own internal properties.</p>
10714 *
10715 * <p>Note: in the current implementation, setting this property to true after the
10716 * view was added to a ViewGroup might have no effect at all. This property should
10717 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10718 *
10719 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10720 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010721 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010722 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10723 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010724 *
10725 * @param enabled True to enable duplication of the parent's drawable state, false
10726 * to disable it.
10727 *
10728 * @see #getDrawableState()
10729 * @see #isDuplicateParentStateEnabled()
10730 */
10731 public void setDuplicateParentStateEnabled(boolean enabled) {
10732 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10733 }
10734
10735 /**
10736 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10737 *
10738 * @return True if this view's drawable state is duplicated from the parent,
10739 * false otherwise
10740 *
10741 * @see #getDrawableState()
10742 * @see #setDuplicateParentStateEnabled(boolean)
10743 */
10744 public boolean isDuplicateParentStateEnabled() {
10745 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10746 }
10747
10748 /**
Romain Guy171c5922011-01-06 10:04:23 -080010749 * <p>Specifies the type of layer backing this view. The layer can be
10750 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10751 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010752 *
Romain Guy171c5922011-01-06 10:04:23 -080010753 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10754 * instance that controls how the layer is composed on screen. The following
10755 * properties of the paint are taken into account when composing the layer:</p>
10756 * <ul>
10757 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10758 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10759 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10760 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010761 *
Romain Guy171c5922011-01-06 10:04:23 -080010762 * <p>If this view has an alpha value set to < 1.0 by calling
10763 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10764 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10765 * equivalent to setting a hardware layer on this view and providing a paint with
10766 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010767 *
Romain Guy171c5922011-01-06 10:04:23 -080010768 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10769 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10770 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010771 *
Romain Guy171c5922011-01-06 10:04:23 -080010772 * @param layerType The ype of layer to use with this view, must be one of
10773 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10774 * {@link #LAYER_TYPE_HARDWARE}
10775 * @param paint The paint used to compose the layer. This argument is optional
10776 * and can be null. It is ignored when the layer type is
10777 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010778 *
10779 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010780 * @see #LAYER_TYPE_NONE
10781 * @see #LAYER_TYPE_SOFTWARE
10782 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010783 * @see #setAlpha(float)
10784 *
Romain Guy171c5922011-01-06 10:04:23 -080010785 * @attr ref android.R.styleable#View_layerType
10786 */
10787 public void setLayerType(int layerType, Paint paint) {
10788 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010789 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010790 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10791 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010792
Romain Guyd6cd5722011-01-17 14:42:41 -080010793 if (layerType == mLayerType) {
10794 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10795 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010796 invalidateParentCaches();
10797 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010798 }
10799 return;
10800 }
Romain Guy171c5922011-01-06 10:04:23 -080010801
10802 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010803 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010804 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070010805 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010806 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010807 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010808 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010809 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010810 default:
10811 break;
Romain Guy171c5922011-01-06 10:04:23 -080010812 }
10813
10814 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010815 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10816 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10817 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010818
Romain Guy0fd89bf2011-01-26 15:41:30 -080010819 invalidateParentCaches();
10820 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010821 }
10822
10823 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010824 * Indicates whether this view has a static layer. A view with layer type
10825 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10826 * dynamic.
10827 */
10828 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010829 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010830 }
10831
10832 /**
Romain Guy171c5922011-01-06 10:04:23 -080010833 * Indicates what type of layer is currently associated with this view. By default
10834 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10835 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10836 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010837 *
Romain Guy171c5922011-01-06 10:04:23 -080010838 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10839 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010840 *
10841 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070010842 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010843 * @see #LAYER_TYPE_NONE
10844 * @see #LAYER_TYPE_SOFTWARE
10845 * @see #LAYER_TYPE_HARDWARE
10846 */
10847 public int getLayerType() {
10848 return mLayerType;
10849 }
Joe Malin32736f02011-01-19 16:14:20 -080010850
Romain Guy6c319ca2011-01-11 14:29:25 -080010851 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010852 * Forces this view's layer to be created and this view to be rendered
10853 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10854 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070010855 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010856 * This method can for instance be used to render a view into its layer before
10857 * starting an animation. If this view is complex, rendering into the layer
10858 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070010859 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010860 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070010861 *
10862 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010863 */
10864 public void buildLayer() {
10865 if (mLayerType == LAYER_TYPE_NONE) return;
10866
10867 if (mAttachInfo == null) {
10868 throw new IllegalStateException("This view must be attached to a window first");
10869 }
10870
10871 switch (mLayerType) {
10872 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010873 if (mAttachInfo.mHardwareRenderer != null &&
10874 mAttachInfo.mHardwareRenderer.isEnabled() &&
10875 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010876 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010877 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010878 break;
10879 case LAYER_TYPE_SOFTWARE:
10880 buildDrawingCache(true);
10881 break;
10882 }
10883 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010884
Romain Guy9c4b79a2011-11-10 19:23:58 -080010885 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10886 void flushLayer() {
10887 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10888 mHardwareLayer.flush();
10889 }
10890 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010891
10892 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010893 * <p>Returns a hardware layer that can be used to draw this view again
10894 * without executing its draw method.</p>
10895 *
10896 * @return A HardwareLayer ready to render, or null if an error occurred.
10897 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010898 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010899 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10900 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010901 return null;
10902 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010903
Romain Guy9c4b79a2011-11-10 19:23:58 -080010904 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010905
10906 final int width = mRight - mLeft;
10907 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010908
Romain Guy6c319ca2011-01-11 14:29:25 -080010909 if (width == 0 || height == 0) {
10910 return null;
10911 }
10912
10913 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10914 if (mHardwareLayer == null) {
10915 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10916 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070010917 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010918 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10919 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070010920 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010921 }
10922
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010923 // The layer is not valid if the underlying GPU resources cannot be allocated
10924 if (!mHardwareLayer.isValid()) {
10925 return null;
10926 }
10927
Chet Haasea1cff502012-02-21 13:43:44 -080010928 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080010929 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010930 }
10931
10932 return mHardwareLayer;
10933 }
Romain Guy171c5922011-01-06 10:04:23 -080010934
Romain Guy589b0bb2011-10-10 13:57:47 -070010935 /**
10936 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070010937 *
Romain Guy589b0bb2011-10-10 13:57:47 -070010938 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070010939 *
10940 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070010941 * @see #LAYER_TYPE_HARDWARE
10942 */
Romain Guya998dff2012-03-23 18:58:36 -070010943 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070010944 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010945 AttachInfo info = mAttachInfo;
10946 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070010947 info.mHardwareRenderer.isEnabled() &&
10948 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010949 mHardwareLayer.destroy();
10950 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010951
Romain Guy9c4b79a2011-11-10 19:23:58 -080010952 invalidate(true);
10953 invalidateParentCaches();
10954 }
Romain Guy65b345f2011-07-27 18:51:50 -070010955 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010956 }
Romain Guy65b345f2011-07-27 18:51:50 -070010957 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010958 }
10959
Romain Guy171c5922011-01-06 10:04:23 -080010960 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010961 * Destroys all hardware rendering resources. This method is invoked
10962 * when the system needs to reclaim resources. Upon execution of this
10963 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070010964 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010965 * Note: you <strong>must</strong> call
10966 * <code>super.destroyHardwareResources()</code> when overriding
10967 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070010968 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010969 * @hide
10970 */
10971 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070010972 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010973 }
10974
10975 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010976 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10977 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10978 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10979 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10980 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10981 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010982 *
Romain Guy171c5922011-01-06 10:04:23 -080010983 * <p>Enabling the drawing cache is similar to
10984 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010985 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10986 * drawing cache has no effect on rendering because the system uses a different mechanism
10987 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10988 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10989 * for information on how to enable software and hardware layers.</p>
10990 *
10991 * <p>This API can be used to manually generate
10992 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10993 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010994 *
10995 * @param enabled true to enable the drawing cache, false otherwise
10996 *
10997 * @see #isDrawingCacheEnabled()
10998 * @see #getDrawingCache()
10999 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011000 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 */
11002 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011003 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011004 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11005 }
11006
11007 /**
11008 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11009 *
11010 * @return true if the drawing cache is enabled
11011 *
11012 * @see #setDrawingCacheEnabled(boolean)
11013 * @see #getDrawingCache()
11014 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011015 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011016 public boolean isDrawingCacheEnabled() {
11017 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11018 }
11019
11020 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011021 * Debugging utility which recursively outputs the dirty state of a view and its
11022 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011023 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011024 * @hide
11025 */
Romain Guy676b1732011-02-14 14:45:33 -080011026 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011027 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11028 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11029 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11030 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11031 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11032 if (clear) {
11033 mPrivateFlags &= clearMask;
11034 }
11035 if (this instanceof ViewGroup) {
11036 ViewGroup parent = (ViewGroup) this;
11037 final int count = parent.getChildCount();
11038 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011039 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011040 child.outputDirtyFlags(indent + " ", clear, clearMask);
11041 }
11042 }
11043 }
11044
11045 /**
11046 * This method is used by ViewGroup to cause its children to restore or recreate their
11047 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11048 * to recreate its own display list, which would happen if it went through the normal
11049 * draw/dispatchDraw mechanisms.
11050 *
11051 * @hide
11052 */
11053 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011054
11055 /**
11056 * A view that is not attached or hardware accelerated cannot create a display list.
11057 * This method checks these conditions and returns the appropriate result.
11058 *
11059 * @return true if view has the ability to create a display list, false otherwise.
11060 *
11061 * @hide
11062 */
11063 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011064 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011065 }
Joe Malin32736f02011-01-19 16:14:20 -080011066
Chet Haasedaf98e92011-01-10 14:10:36 -080011067 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011068 * @return The HardwareRenderer associated with that view or null if hardware rendering
11069 * is not supported or this this has not been attached to a window.
11070 *
11071 * @hide
11072 */
11073 public HardwareRenderer getHardwareRenderer() {
11074 if (mAttachInfo != null) {
11075 return mAttachInfo.mHardwareRenderer;
11076 }
11077 return null;
11078 }
11079
11080 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011081 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11082 * Otherwise, the same display list will be returned (after having been rendered into
11083 * along the way, depending on the invalidation state of the view).
11084 *
11085 * @param displayList The previous version of this displayList, could be null.
11086 * @param isLayer Whether the requester of the display list is a layer. If so,
11087 * the view will avoid creating a layer inside the resulting display list.
11088 * @return A new or reused DisplayList object.
11089 */
11090 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11091 if (!canHaveDisplayList()) {
11092 return null;
11093 }
11094
11095 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11096 displayList == null || !displayList.isValid() ||
11097 (!isLayer && mRecreateDisplayList))) {
11098 // Don't need to recreate the display list, just need to tell our
11099 // children to restore/recreate theirs
11100 if (displayList != null && displayList.isValid() &&
11101 !isLayer && !mRecreateDisplayList) {
11102 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11103 mPrivateFlags &= ~DIRTY_MASK;
11104 dispatchGetDisplayList();
11105
11106 return displayList;
11107 }
11108
11109 if (!isLayer) {
11110 // If we got here, we're recreating it. Mark it as such to ensure that
11111 // we copy in child display lists into ours in drawChild()
11112 mRecreateDisplayList = true;
11113 }
11114 if (displayList == null) {
11115 final String name = getClass().getSimpleName();
11116 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11117 // If we're creating a new display list, make sure our parent gets invalidated
11118 // since they will need to recreate their display list to account for this
11119 // new child display list.
11120 invalidateParentCaches();
11121 }
11122
11123 boolean caching = false;
11124 final HardwareCanvas canvas = displayList.start();
11125 int restoreCount = 0;
11126 int width = mRight - mLeft;
11127 int height = mBottom - mTop;
11128
11129 try {
11130 canvas.setViewport(width, height);
11131 // The dirty rect should always be null for a display list
11132 canvas.onPreDraw(null);
11133 int layerType = (
11134 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11135 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011136 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11137 if (layerType == LAYER_TYPE_HARDWARE) {
11138 final HardwareLayer layer = getHardwareLayer();
11139 if (layer != null && layer.isValid()) {
11140 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11141 } else {
11142 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11143 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11144 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11145 }
11146 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011147 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011148 buildDrawingCache(true);
11149 Bitmap cache = getDrawingCache(true);
11150 if (cache != null) {
11151 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11152 caching = true;
11153 }
Chet Haasea1cff502012-02-21 13:43:44 -080011154 }
Chet Haasea1cff502012-02-21 13:43:44 -080011155 } else {
11156
11157 computeScroll();
11158
11159 if (!USE_DISPLAY_LIST_PROPERTIES) {
11160 restoreCount = canvas.save();
11161 }
11162 canvas.translate(-mScrollX, -mScrollY);
11163 if (!isLayer) {
11164 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11165 mPrivateFlags &= ~DIRTY_MASK;
11166 }
11167
11168 // Fast path for layouts with no backgrounds
11169 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11170 dispatchDraw(canvas);
11171 } else {
11172 draw(canvas);
11173 }
11174 }
11175 } finally {
11176 if (USE_DISPLAY_LIST_PROPERTIES) {
11177 canvas.restoreToCount(restoreCount);
11178 }
11179 canvas.onPostDraw();
11180
11181 displayList.end();
11182 if (USE_DISPLAY_LIST_PROPERTIES) {
11183 displayList.setCaching(caching);
11184 }
11185 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11186 displayList.setLeftTopRightBottom(0, 0, width, height);
11187 } else {
11188 setDisplayListProperties(displayList);
11189 }
11190 }
11191 } else if (!isLayer) {
11192 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11193 mPrivateFlags &= ~DIRTY_MASK;
11194 }
11195
11196 return displayList;
11197 }
11198
11199 /**
11200 * Get the DisplayList for the HardwareLayer
11201 *
11202 * @param layer The HardwareLayer whose DisplayList we want
11203 * @return A DisplayList fopr the specified HardwareLayer
11204 */
11205 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11206 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11207 layer.setDisplayList(displayList);
11208 return displayList;
11209 }
11210
11211
11212 /**
Romain Guyb051e892010-09-28 19:09:36 -070011213 * <p>Returns a display list that can be used to draw this view again
11214 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011215 *
Romain Guyb051e892010-09-28 19:09:36 -070011216 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011217 *
11218 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011219 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011220 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011221 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011222 return mDisplayList;
11223 }
11224
11225 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011226 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011227 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011228 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011229 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011230 * @see #getDrawingCache(boolean)
11231 */
11232 public Bitmap getDrawingCache() {
11233 return getDrawingCache(false);
11234 }
11235
11236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011237 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11238 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11239 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11240 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11241 * request the drawing cache by calling this method and draw it on screen if the
11242 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011243 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011244 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11245 * this method will create a bitmap of the same size as this view. Because this bitmap
11246 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11247 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11248 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11249 * size than the view. This implies that your application must be able to handle this
11250 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011251 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011252 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11253 * the current density of the screen when the application is in compatibility
11254 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011255 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011256 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 * @see #setDrawingCacheEnabled(boolean)
11259 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011260 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011261 * @see #destroyDrawingCache()
11262 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011263 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011264 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11265 return null;
11266 }
11267 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011268 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011269 }
Romain Guy02890fd2010-08-06 17:58:44 -070011270 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011271 }
11272
11273 /**
11274 * <p>Frees the resources used by the drawing cache. If you call
11275 * {@link #buildDrawingCache()} manually without calling
11276 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11277 * should cleanup the cache with this method afterwards.</p>
11278 *
11279 * @see #setDrawingCacheEnabled(boolean)
11280 * @see #buildDrawingCache()
11281 * @see #getDrawingCache()
11282 */
11283 public void destroyDrawingCache() {
11284 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011285 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011286 mDrawingCache = null;
11287 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011288 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011289 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011290 mUnscaledDrawingCache = null;
11291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011292 }
11293
11294 /**
11295 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011296 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011297 * view will always be drawn on top of a solid color.
11298 *
11299 * @param color The background color to use for the drawing cache's bitmap
11300 *
11301 * @see #setDrawingCacheEnabled(boolean)
11302 * @see #buildDrawingCache()
11303 * @see #getDrawingCache()
11304 */
11305 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011306 if (color != mDrawingCacheBackgroundColor) {
11307 mDrawingCacheBackgroundColor = color;
11308 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011310 }
11311
11312 /**
11313 * @see #setDrawingCacheBackgroundColor(int)
11314 *
11315 * @return The background color to used for the drawing cache's bitmap
11316 */
11317 public int getDrawingCacheBackgroundColor() {
11318 return mDrawingCacheBackgroundColor;
11319 }
11320
11321 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011322 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011323 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011324 * @see #buildDrawingCache(boolean)
11325 */
11326 public void buildDrawingCache() {
11327 buildDrawingCache(false);
11328 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011329
Romain Guyfbd8f692009-06-26 14:51:58 -070011330 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011331 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11332 *
11333 * <p>If you call {@link #buildDrawingCache()} manually without calling
11334 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11335 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011336 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011337 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11338 * this method will create a bitmap of the same size as this view. Because this bitmap
11339 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11340 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11341 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11342 * size than the view. This implies that your application must be able to handle this
11343 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011344 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011345 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11346 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011347 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011348 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011349 *
11350 * @see #getDrawingCache()
11351 * @see #destroyDrawingCache()
11352 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011353 public void buildDrawingCache(boolean autoScale) {
11354 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011355 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011356 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011357
11358 if (ViewDebug.TRACE_HIERARCHY) {
11359 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011361
Romain Guy8506ab42009-06-11 17:35:47 -070011362 int width = mRight - mLeft;
11363 int height = mBottom - mTop;
11364
11365 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011366 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011367
Romain Guye1123222009-06-29 14:24:56 -070011368 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011369 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11370 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011372
11373 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011374 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011375 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376
11377 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011378 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011379 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011380 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11381 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011382 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011383 return;
11384 }
11385
11386 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011387 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011388
11389 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011390 Bitmap.Config quality;
11391 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011392 // Never pick ARGB_4444 because it looks awful
11393 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011394 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11395 case DRAWING_CACHE_QUALITY_AUTO:
11396 quality = Bitmap.Config.ARGB_8888;
11397 break;
11398 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011399 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011400 break;
11401 case DRAWING_CACHE_QUALITY_HIGH:
11402 quality = Bitmap.Config.ARGB_8888;
11403 break;
11404 default:
11405 quality = Bitmap.Config.ARGB_8888;
11406 break;
11407 }
11408 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070011409 // Optimization for translucent windows
11410 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080011411 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011412 }
11413
11414 // Try to cleanup memory
11415 if (bitmap != null) bitmap.recycle();
11416
11417 try {
11418 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070011419 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070011420 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070011421 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011422 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070011423 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011424 }
Adam Powell26153a32010-11-08 15:22:27 -080011425 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011426 } catch (OutOfMemoryError e) {
11427 // If there is not enough memory to create the bitmap cache, just
11428 // ignore the issue as bitmap caches are not required to draw the
11429 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070011430 if (autoScale) {
11431 mDrawingCache = null;
11432 } else {
11433 mUnscaledDrawingCache = null;
11434 }
Romain Guy0211a0a2011-02-14 16:34:59 -080011435 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011436 return;
11437 }
11438
11439 clear = drawingCacheBackgroundColor != 0;
11440 }
11441
11442 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011443 if (attachInfo != null) {
11444 canvas = attachInfo.mCanvas;
11445 if (canvas == null) {
11446 canvas = new Canvas();
11447 }
11448 canvas.setBitmap(bitmap);
11449 // Temporarily clobber the cached Canvas in case one of our children
11450 // is also using a drawing cache. Without this, the children would
11451 // steal the canvas by attaching their own bitmap to it and bad, bad
11452 // thing would happen (invisible views, corrupted drawings, etc.)
11453 attachInfo.mCanvas = null;
11454 } else {
11455 // This case should hopefully never or seldom happen
11456 canvas = new Canvas(bitmap);
11457 }
11458
11459 if (clear) {
11460 bitmap.eraseColor(drawingCacheBackgroundColor);
11461 }
11462
11463 computeScroll();
11464 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080011465
Romain Guye1123222009-06-29 14:24:56 -070011466 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011467 final float scale = attachInfo.mApplicationScale;
11468 canvas.scale(scale, scale);
11469 }
Joe Malin32736f02011-01-19 16:14:20 -080011470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011471 canvas.translate(-mScrollX, -mScrollY);
11472
Romain Guy5bcdff42009-05-14 21:27:18 -070011473 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080011474 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
11475 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070011476 mPrivateFlags |= DRAWING_CACHE_VALID;
11477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011478
11479 // Fast path for layouts with no backgrounds
11480 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11481 if (ViewDebug.TRACE_HIERARCHY) {
11482 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11483 }
Romain Guy5bcdff42009-05-14 21:27:18 -070011484 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011485 dispatchDraw(canvas);
11486 } else {
11487 draw(canvas);
11488 }
11489
11490 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011491 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011492
11493 if (attachInfo != null) {
11494 // Restore the cached Canvas for our siblings
11495 attachInfo.mCanvas = canvas;
11496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011497 }
11498 }
11499
11500 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011501 * Create a snapshot of the view into a bitmap. We should probably make
11502 * some form of this public, but should think about the API.
11503 */
Romain Guy223ff5c2010-03-02 17:07:47 -080011504 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011505 int width = mRight - mLeft;
11506 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011507
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011508 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070011509 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011510 width = (int) ((width * scale) + 0.5f);
11511 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080011512
Romain Guy8c11e312009-09-14 15:15:30 -070011513 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011514 if (bitmap == null) {
11515 throw new OutOfMemoryError();
11516 }
11517
Romain Guyc529d8d2011-09-06 15:01:39 -070011518 Resources resources = getResources();
11519 if (resources != null) {
11520 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
11521 }
Joe Malin32736f02011-01-19 16:14:20 -080011522
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011523 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011524 if (attachInfo != null) {
11525 canvas = attachInfo.mCanvas;
11526 if (canvas == null) {
11527 canvas = new Canvas();
11528 }
11529 canvas.setBitmap(bitmap);
11530 // Temporarily clobber the cached Canvas in case one of our children
11531 // is also using a drawing cache. Without this, the children would
11532 // steal the canvas by attaching their own bitmap to it and bad, bad
11533 // things would happen (invisible views, corrupted drawings, etc.)
11534 attachInfo.mCanvas = null;
11535 } else {
11536 // This case should hopefully never or seldom happen
11537 canvas = new Canvas(bitmap);
11538 }
11539
Romain Guy5bcdff42009-05-14 21:27:18 -070011540 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011541 bitmap.eraseColor(backgroundColor);
11542 }
11543
11544 computeScroll();
11545 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011546 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011547 canvas.translate(-mScrollX, -mScrollY);
11548
Romain Guy5bcdff42009-05-14 21:27:18 -070011549 // Temporarily remove the dirty mask
11550 int flags = mPrivateFlags;
11551 mPrivateFlags &= ~DIRTY_MASK;
11552
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011553 // Fast path for layouts with no backgrounds
11554 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11555 dispatchDraw(canvas);
11556 } else {
11557 draw(canvas);
11558 }
11559
Romain Guy5bcdff42009-05-14 21:27:18 -070011560 mPrivateFlags = flags;
11561
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011562 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011563 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011564
11565 if (attachInfo != null) {
11566 // Restore the cached Canvas for our siblings
11567 attachInfo.mCanvas = canvas;
11568 }
Romain Guy8506ab42009-06-11 17:35:47 -070011569
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011570 return bitmap;
11571 }
11572
11573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011574 * Indicates whether this View is currently in edit mode. A View is usually
11575 * in edit mode when displayed within a developer tool. For instance, if
11576 * this View is being drawn by a visual user interface builder, this method
11577 * should return true.
11578 *
11579 * Subclasses should check the return value of this method to provide
11580 * different behaviors if their normal behavior might interfere with the
11581 * host environment. For instance: the class spawns a thread in its
11582 * constructor, the drawing code relies on device-specific features, etc.
11583 *
11584 * This method is usually checked in the drawing code of custom widgets.
11585 *
11586 * @return True if this View is in edit mode, false otherwise.
11587 */
11588 public boolean isInEditMode() {
11589 return false;
11590 }
11591
11592 /**
11593 * If the View draws content inside its padding and enables fading edges,
11594 * it needs to support padding offsets. Padding offsets are added to the
11595 * fading edges to extend the length of the fade so that it covers pixels
11596 * drawn inside the padding.
11597 *
11598 * Subclasses of this class should override this method if they need
11599 * to draw content inside the padding.
11600 *
11601 * @return True if padding offset must be applied, false otherwise.
11602 *
11603 * @see #getLeftPaddingOffset()
11604 * @see #getRightPaddingOffset()
11605 * @see #getTopPaddingOffset()
11606 * @see #getBottomPaddingOffset()
11607 *
11608 * @since CURRENT
11609 */
11610 protected boolean isPaddingOffsetRequired() {
11611 return false;
11612 }
11613
11614 /**
11615 * Amount by which to extend the left fading region. Called only when
11616 * {@link #isPaddingOffsetRequired()} returns true.
11617 *
11618 * @return The left padding offset in pixels.
11619 *
11620 * @see #isPaddingOffsetRequired()
11621 *
11622 * @since CURRENT
11623 */
11624 protected int getLeftPaddingOffset() {
11625 return 0;
11626 }
11627
11628 /**
11629 * Amount by which to extend the right fading region. Called only when
11630 * {@link #isPaddingOffsetRequired()} returns true.
11631 *
11632 * @return The right padding offset in pixels.
11633 *
11634 * @see #isPaddingOffsetRequired()
11635 *
11636 * @since CURRENT
11637 */
11638 protected int getRightPaddingOffset() {
11639 return 0;
11640 }
11641
11642 /**
11643 * Amount by which to extend the top fading region. Called only when
11644 * {@link #isPaddingOffsetRequired()} returns true.
11645 *
11646 * @return The top padding offset in pixels.
11647 *
11648 * @see #isPaddingOffsetRequired()
11649 *
11650 * @since CURRENT
11651 */
11652 protected int getTopPaddingOffset() {
11653 return 0;
11654 }
11655
11656 /**
11657 * Amount by which to extend the bottom fading region. Called only when
11658 * {@link #isPaddingOffsetRequired()} returns true.
11659 *
11660 * @return The bottom padding offset in pixels.
11661 *
11662 * @see #isPaddingOffsetRequired()
11663 *
11664 * @since CURRENT
11665 */
11666 protected int getBottomPaddingOffset() {
11667 return 0;
11668 }
11669
11670 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011671 * @hide
11672 * @param offsetRequired
11673 */
11674 protected int getFadeTop(boolean offsetRequired) {
11675 int top = mPaddingTop;
11676 if (offsetRequired) top += getTopPaddingOffset();
11677 return top;
11678 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011679
Romain Guyf2fc4602011-07-19 15:20:03 -070011680 /**
11681 * @hide
11682 * @param offsetRequired
11683 */
11684 protected int getFadeHeight(boolean offsetRequired) {
11685 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070011686 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070011687 return mBottom - mTop - mPaddingBottom - padding;
11688 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011689
Romain Guyf2fc4602011-07-19 15:20:03 -070011690 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011691 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011692 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011693 *
Romain Guy2bffd262010-09-12 17:40:02 -070011694 * <p>Even if this method returns true, it does not mean that every call
11695 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11696 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011697 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011698 * window is hardware accelerated,
11699 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11700 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011701 *
Romain Guy2bffd262010-09-12 17:40:02 -070011702 * @return True if the view is attached to a window and the window is
11703 * hardware accelerated; false in any other case.
11704 */
11705 public boolean isHardwareAccelerated() {
11706 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11707 }
Joe Malin32736f02011-01-19 16:14:20 -080011708
Romain Guy2bffd262010-09-12 17:40:02 -070011709 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011710 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11711 * case of an active Animation being run on the view.
11712 */
11713 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11714 Animation a, boolean scalingRequired) {
11715 Transformation invalidationTransform;
11716 final int flags = parent.mGroupFlags;
11717 final boolean initialized = a.isInitialized();
11718 if (!initialized) {
11719 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11720 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11721 onAnimationStart();
11722 }
11723
11724 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11725 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11726 if (parent.mInvalidationTransformation == null) {
11727 parent.mInvalidationTransformation = new Transformation();
11728 }
11729 invalidationTransform = parent.mInvalidationTransformation;
11730 a.getTransformation(drawingTime, invalidationTransform, 1f);
11731 } else {
11732 invalidationTransform = parent.mChildTransformation;
11733 }
11734 if (more) {
11735 if (!a.willChangeBounds()) {
11736 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11737 parent.FLAG_OPTIMIZE_INVALIDATE) {
11738 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11739 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11740 // The child need to draw an animation, potentially offscreen, so
11741 // make sure we do not cancel invalidate requests
11742 parent.mPrivateFlags |= DRAW_ANIMATION;
11743 parent.invalidate(mLeft, mTop, mRight, mBottom);
11744 }
11745 } else {
11746 if (parent.mInvalidateRegion == null) {
11747 parent.mInvalidateRegion = new RectF();
11748 }
11749 final RectF region = parent.mInvalidateRegion;
11750 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11751 invalidationTransform);
11752
11753 // The child need to draw an animation, potentially offscreen, so
11754 // make sure we do not cancel invalidate requests
11755 parent.mPrivateFlags |= DRAW_ANIMATION;
11756
11757 final int left = mLeft + (int) region.left;
11758 final int top = mTop + (int) region.top;
11759 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11760 top + (int) (region.height() + .5f));
11761 }
11762 }
11763 return more;
11764 }
11765
Chet Haasea1cff502012-02-21 13:43:44 -080011766 void setDisplayListProperties() {
11767 setDisplayListProperties(mDisplayList);
11768 }
11769
11770 /**
11771 * This method is called by getDisplayList() when a display list is created or re-rendered.
11772 * It sets or resets the current value of all properties on that display list (resetting is
11773 * necessary when a display list is being re-created, because we need to make sure that
11774 * previously-set transform values
11775 */
11776 void setDisplayListProperties(DisplayList displayList) {
11777 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
11778 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070011779 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080011780 if (mParent instanceof ViewGroup) {
11781 displayList.setClipChildren(
11782 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
11783 }
Chet Haase9420abd2012-03-29 16:28:32 -070011784 float alpha = 1;
11785 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
11786 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
11787 ViewGroup parentVG = (ViewGroup) mParent;
11788 final boolean hasTransform =
11789 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
11790 if (hasTransform) {
11791 Transformation transform = parentVG.mChildTransformation;
11792 final int transformType = parentVG.mChildTransformation.getTransformationType();
11793 if (transformType != Transformation.TYPE_IDENTITY) {
11794 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
11795 alpha = transform.getAlpha();
11796 }
11797 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
11798 displayList.setStaticMatrix(transform.getMatrix());
11799 }
11800 }
11801 }
Chet Haasea1cff502012-02-21 13:43:44 -080011802 }
11803 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070011804 alpha *= mTransformationInfo.mAlpha;
11805 if (alpha < 1) {
11806 final int multipliedAlpha = (int) (255 * alpha);
11807 if (onSetAlpha(multipliedAlpha)) {
11808 alpha = 1;
11809 }
11810 }
11811 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080011812 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
11813 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
11814 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
11815 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070011816 if (mTransformationInfo.mCamera == null) {
11817 mTransformationInfo.mCamera = new Camera();
11818 mTransformationInfo.matrix3D = new Matrix();
11819 }
11820 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080011821 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
11822 displayList.setPivotX(getPivotX());
11823 displayList.setPivotY(getPivotY());
11824 }
Chet Haase9420abd2012-03-29 16:28:32 -070011825 } else if (alpha < 1) {
11826 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080011827 }
11828 }
11829 }
11830
Chet Haasebcca79a2012-02-14 08:45:14 -080011831 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011832 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11833 * This draw() method is an implementation detail and is not intended to be overridden or
11834 * to be called from anywhere else other than ViewGroup.drawChild().
11835 */
11836 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080011837 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
11838 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080011839 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011840 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011841 final int flags = parent.mGroupFlags;
11842
Chet Haasea1cff502012-02-21 13:43:44 -080011843 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080011844 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080011845 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011846 }
11847
11848 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011849 boolean concatMatrix = false;
11850
11851 boolean scalingRequired = false;
11852 boolean caching;
11853 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11854
11855 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080011856 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
11857 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011858 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070011859 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080011860 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11861 } else {
11862 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11863 }
11864
Chet Haasebcca79a2012-02-14 08:45:14 -080011865 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011866 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011867 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011868 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011869 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070011870 } else if (!useDisplayListProperties &&
11871 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011872 final boolean hasTransform =
11873 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011874 if (hasTransform) {
11875 final int transformType = parent.mChildTransformation.getTransformationType();
11876 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11877 parent.mChildTransformation : null;
11878 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11879 }
11880 }
11881
11882 concatMatrix |= !childHasIdentityMatrix;
11883
11884 // Sets the flag as early as possible to allow draw() implementations
11885 // to call invalidate() successfully when doing animations
11886 mPrivateFlags |= DRAWN;
11887
Chet Haasebcca79a2012-02-14 08:45:14 -080011888 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011889 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11890 return more;
11891 }
11892
11893 if (hardwareAccelerated) {
11894 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11895 // retain the flag's value temporarily in the mRecreateDisplayList flag
11896 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11897 mPrivateFlags &= ~INVALIDATED;
11898 }
11899
11900 computeScroll();
11901
11902 final int sx = mScrollX;
11903 final int sy = mScrollY;
11904
11905 DisplayList displayList = null;
11906 Bitmap cache = null;
11907 boolean hasDisplayList = false;
11908 if (caching) {
11909 if (!hardwareAccelerated) {
11910 if (layerType != LAYER_TYPE_NONE) {
11911 layerType = LAYER_TYPE_SOFTWARE;
11912 buildDrawingCache(true);
11913 }
11914 cache = getDrawingCache(true);
11915 } else {
11916 switch (layerType) {
11917 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070011918 if (useDisplayListProperties) {
11919 hasDisplayList = canHaveDisplayList();
11920 } else {
11921 buildDrawingCache(true);
11922 cache = getDrawingCache(true);
11923 }
Chet Haase64a48c12012-02-13 16:33:29 -080011924 break;
Chet Haasea1cff502012-02-21 13:43:44 -080011925 case LAYER_TYPE_HARDWARE:
11926 if (useDisplayListProperties) {
11927 hasDisplayList = canHaveDisplayList();
11928 }
11929 break;
Chet Haase64a48c12012-02-13 16:33:29 -080011930 case LAYER_TYPE_NONE:
11931 // Delay getting the display list until animation-driven alpha values are
11932 // set up and possibly passed on to the view
11933 hasDisplayList = canHaveDisplayList();
11934 break;
11935 }
11936 }
11937 }
Chet Haasea1cff502012-02-21 13:43:44 -080011938 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070011939 if (useDisplayListProperties) {
11940 displayList = getDisplayList();
11941 if (!displayList.isValid()) {
11942 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11943 // to getDisplayList(), the display list will be marked invalid and we should not
11944 // try to use it again.
11945 displayList = null;
11946 hasDisplayList = false;
11947 useDisplayListProperties = false;
11948 }
11949 }
Chet Haase64a48c12012-02-13 16:33:29 -080011950
11951 final boolean hasNoCache = cache == null || hasDisplayList;
11952 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11953 layerType != LAYER_TYPE_HARDWARE;
11954
Chet Haasea1cff502012-02-21 13:43:44 -080011955 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070011956 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080011957 restoreTo = canvas.save();
11958 }
Chet Haase64a48c12012-02-13 16:33:29 -080011959 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011960 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011961 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080011962 if (!useDisplayListProperties) {
11963 canvas.translate(mLeft, mTop);
11964 }
Chet Haase64a48c12012-02-13 16:33:29 -080011965 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080011966 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070011967 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080011968 restoreTo = canvas.save();
11969 }
Chet Haase64a48c12012-02-13 16:33:29 -080011970 // mAttachInfo cannot be null, otherwise scalingRequired == false
11971 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11972 canvas.scale(scale, scale);
11973 }
11974 }
11975
Chet Haasea1cff502012-02-21 13:43:44 -080011976 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070011977 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080011978 if (transformToApply != null || !childHasIdentityMatrix) {
11979 int transX = 0;
11980 int transY = 0;
11981
11982 if (offsetForScroll) {
11983 transX = -sx;
11984 transY = -sy;
11985 }
11986
11987 if (transformToApply != null) {
11988 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070011989 if (useDisplayListProperties) {
11990 displayList.setAnimationMatrix(transformToApply.getMatrix());
11991 } else {
11992 // Undo the scroll translation, apply the transformation matrix,
11993 // then redo the scroll translate to get the correct result.
11994 canvas.translate(-transX, -transY);
11995 canvas.concat(transformToApply.getMatrix());
11996 canvas.translate(transX, transY);
11997 }
Chet Haasea1cff502012-02-21 13:43:44 -080011998 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011999 }
12000
12001 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012002 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012003 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012004 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012005 }
12006 }
12007
Chet Haasea1cff502012-02-21 13:43:44 -080012008 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012009 canvas.translate(-transX, -transY);
12010 canvas.concat(getMatrix());
12011 canvas.translate(transX, transY);
12012 }
12013 }
12014
Chet Haase9420abd2012-03-29 16:28:32 -070012015 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012016 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012017 if (hasNoCache) {
12018 final int multipliedAlpha = (int) (255 * alpha);
12019 if (!onSetAlpha(multipliedAlpha)) {
12020 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012021 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012022 layerType != LAYER_TYPE_NONE) {
12023 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12024 }
Chet Haase9420abd2012-03-29 16:28:32 -070012025 if (useDisplayListProperties) {
12026 displayList.setAlpha(alpha * getAlpha());
12027 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012028 final int scrollX = hasDisplayList ? 0 : sx;
12029 final int scrollY = hasDisplayList ? 0 : sy;
12030 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12031 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012032 }
12033 } else {
12034 // Alpha is handled by the child directly, clobber the layer's alpha
12035 mPrivateFlags |= ALPHA_SET;
12036 }
12037 }
12038 }
12039 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12040 onSetAlpha(255);
12041 mPrivateFlags &= ~ALPHA_SET;
12042 }
12043
Chet Haasea1cff502012-02-21 13:43:44 -080012044 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12045 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012046 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012047 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012048 } else {
12049 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012050 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012051 } else {
12052 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12053 }
12054 }
12055 }
12056
Chet Haase9420abd2012-03-29 16:28:32 -070012057 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012058 displayList = getDisplayList();
12059 if (!displayList.isValid()) {
12060 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12061 // to getDisplayList(), the display list will be marked invalid and we should not
12062 // try to use it again.
12063 displayList = null;
12064 hasDisplayList = false;
12065 }
12066 }
12067
12068 if (hasNoCache) {
12069 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012070 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012071 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012072 if (layer != null && layer.isValid()) {
12073 mLayerPaint.setAlpha((int) (alpha * 255));
12074 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12075 layerRendered = true;
12076 } else {
12077 final int scrollX = hasDisplayList ? 0 : sx;
12078 final int scrollY = hasDisplayList ? 0 : sy;
12079 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012080 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012081 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12082 }
12083 }
12084
12085 if (!layerRendered) {
12086 if (!hasDisplayList) {
12087 // Fast path for layouts with no backgrounds
12088 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12089 if (ViewDebug.TRACE_HIERARCHY) {
12090 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12091 }
12092 mPrivateFlags &= ~DIRTY_MASK;
12093 dispatchDraw(canvas);
12094 } else {
12095 draw(canvas);
12096 }
12097 } else {
12098 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080012099 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080012100 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012101 }
12102 }
12103 } else if (cache != null) {
12104 mPrivateFlags &= ~DIRTY_MASK;
12105 Paint cachePaint;
12106
12107 if (layerType == LAYER_TYPE_NONE) {
12108 cachePaint = parent.mCachePaint;
12109 if (cachePaint == null) {
12110 cachePaint = new Paint();
12111 cachePaint.setDither(false);
12112 parent.mCachePaint = cachePaint;
12113 }
Chet Haase9420abd2012-03-29 16:28:32 -070012114 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012115 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012116 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12117 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012118 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012119 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012120 }
12121 } else {
12122 cachePaint = mLayerPaint;
12123 cachePaint.setAlpha((int) (alpha * 255));
12124 }
12125 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12126 }
12127
Chet Haasea1cff502012-02-21 13:43:44 -080012128 if (restoreTo >= 0) {
12129 canvas.restoreToCount(restoreTo);
12130 }
Chet Haase64a48c12012-02-13 16:33:29 -080012131
12132 if (a != null && !more) {
12133 if (!hardwareAccelerated && !a.getFillAfter()) {
12134 onSetAlpha(255);
12135 }
12136 parent.finishAnimatingView(this, a);
12137 }
12138
12139 if (more && hardwareAccelerated) {
12140 // invalidation is the trigger to recreate display lists, so if we're using
12141 // display lists to render, force an invalidate to allow the animation to
12142 // continue drawing another frame
12143 parent.invalidate(true);
12144 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12145 // alpha animations should cause the child to recreate its display list
12146 invalidate(true);
12147 }
12148 }
12149
12150 mRecreateDisplayList = false;
12151
12152 return more;
12153 }
12154
12155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012156 * Manually render this view (and all of its children) to the given Canvas.
12157 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012158 * called. When implementing a view, implement
12159 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12160 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012161 *
12162 * @param canvas The Canvas to which the View is rendered.
12163 */
12164 public void draw(Canvas canvas) {
12165 if (ViewDebug.TRACE_HIERARCHY) {
12166 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12167 }
12168
Romain Guy5bcdff42009-05-14 21:27:18 -070012169 final int privateFlags = mPrivateFlags;
12170 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12171 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12172 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012174 /*
12175 * Draw traversal performs several drawing steps which must be executed
12176 * in the appropriate order:
12177 *
12178 * 1. Draw the background
12179 * 2. If necessary, save the canvas' layers to prepare for fading
12180 * 3. Draw view's content
12181 * 4. Draw children
12182 * 5. If necessary, draw the fading edges and restore layers
12183 * 6. Draw decorations (scrollbars for instance)
12184 */
12185
12186 // Step 1, draw the background, if needed
12187 int saveCount;
12188
Romain Guy24443ea2009-05-11 11:56:30 -070012189 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012190 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012191 if (background != null) {
12192 final int scrollX = mScrollX;
12193 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012194
Romain Guy24443ea2009-05-11 11:56:30 -070012195 if (mBackgroundSizeChanged) {
12196 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12197 mBackgroundSizeChanged = false;
12198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012199
Romain Guy24443ea2009-05-11 11:56:30 -070012200 if ((scrollX | scrollY) == 0) {
12201 background.draw(canvas);
12202 } else {
12203 canvas.translate(scrollX, scrollY);
12204 background.draw(canvas);
12205 canvas.translate(-scrollX, -scrollY);
12206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012207 }
12208 }
12209
12210 // skip step 2 & 5 if possible (common case)
12211 final int viewFlags = mViewFlags;
12212 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12213 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12214 if (!verticalEdges && !horizontalEdges) {
12215 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012216 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012217
12218 // Step 4, draw the children
12219 dispatchDraw(canvas);
12220
12221 // Step 6, draw decorations (scrollbars)
12222 onDrawScrollBars(canvas);
12223
12224 // we're done...
12225 return;
12226 }
12227
12228 /*
12229 * Here we do the full fledged routine...
12230 * (this is an uncommon case where speed matters less,
12231 * this is why we repeat some of the tests that have been
12232 * done above)
12233 */
12234
12235 boolean drawTop = false;
12236 boolean drawBottom = false;
12237 boolean drawLeft = false;
12238 boolean drawRight = false;
12239
12240 float topFadeStrength = 0.0f;
12241 float bottomFadeStrength = 0.0f;
12242 float leftFadeStrength = 0.0f;
12243 float rightFadeStrength = 0.0f;
12244
12245 // Step 2, save the canvas' layers
12246 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012247
12248 final boolean offsetRequired = isPaddingOffsetRequired();
12249 if (offsetRequired) {
12250 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012251 }
12252
12253 int left = mScrollX + paddingLeft;
12254 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012255 int top = mScrollY + getFadeTop(offsetRequired);
12256 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012257
12258 if (offsetRequired) {
12259 right += getRightPaddingOffset();
12260 bottom += getBottomPaddingOffset();
12261 }
12262
12263 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012264 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012265 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012266
12267 // clip the fade length if top and bottom fades overlap
12268 // overlapping fades produce odd-looking artifacts
12269 if (verticalEdges && (top + length > bottom - length)) {
12270 length = (bottom - top) / 2;
12271 }
12272
12273 // also clip horizontal fades if necessary
12274 if (horizontalEdges && (left + length > right - length)) {
12275 length = (right - left) / 2;
12276 }
12277
12278 if (verticalEdges) {
12279 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012280 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012281 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012282 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012283 }
12284
12285 if (horizontalEdges) {
12286 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012287 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012288 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012289 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012290 }
12291
12292 saveCount = canvas.getSaveCount();
12293
12294 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012295 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012296 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12297
12298 if (drawTop) {
12299 canvas.saveLayer(left, top, right, top + length, null, flags);
12300 }
12301
12302 if (drawBottom) {
12303 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12304 }
12305
12306 if (drawLeft) {
12307 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12308 }
12309
12310 if (drawRight) {
12311 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12312 }
12313 } else {
12314 scrollabilityCache.setFadeColor(solidColor);
12315 }
12316
12317 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012318 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012319
12320 // Step 4, draw the children
12321 dispatchDraw(canvas);
12322
12323 // Step 5, draw the fade effect and restore layers
12324 final Paint p = scrollabilityCache.paint;
12325 final Matrix matrix = scrollabilityCache.matrix;
12326 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012327
12328 if (drawTop) {
12329 matrix.setScale(1, fadeHeight * topFadeStrength);
12330 matrix.postTranslate(left, top);
12331 fade.setLocalMatrix(matrix);
12332 canvas.drawRect(left, top, right, top + length, p);
12333 }
12334
12335 if (drawBottom) {
12336 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12337 matrix.postRotate(180);
12338 matrix.postTranslate(left, bottom);
12339 fade.setLocalMatrix(matrix);
12340 canvas.drawRect(left, bottom - length, right, bottom, p);
12341 }
12342
12343 if (drawLeft) {
12344 matrix.setScale(1, fadeHeight * leftFadeStrength);
12345 matrix.postRotate(-90);
12346 matrix.postTranslate(left, top);
12347 fade.setLocalMatrix(matrix);
12348 canvas.drawRect(left, top, left + length, bottom, p);
12349 }
12350
12351 if (drawRight) {
12352 matrix.setScale(1, fadeHeight * rightFadeStrength);
12353 matrix.postRotate(90);
12354 matrix.postTranslate(right, top);
12355 fade.setLocalMatrix(matrix);
12356 canvas.drawRect(right - length, top, right, bottom, p);
12357 }
12358
12359 canvas.restoreToCount(saveCount);
12360
12361 // Step 6, draw decorations (scrollbars)
12362 onDrawScrollBars(canvas);
12363 }
12364
12365 /**
12366 * Override this if your view is known to always be drawn on top of a solid color background,
12367 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12368 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12369 * should be set to 0xFF.
12370 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012371 * @see #setVerticalFadingEdgeEnabled(boolean)
12372 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012373 *
12374 * @return The known solid color background for this view, or 0 if the color may vary
12375 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012376 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012377 public int getSolidColor() {
12378 return 0;
12379 }
12380
12381 /**
12382 * Build a human readable string representation of the specified view flags.
12383 *
12384 * @param flags the view flags to convert to a string
12385 * @return a String representing the supplied flags
12386 */
12387 private static String printFlags(int flags) {
12388 String output = "";
12389 int numFlags = 0;
12390 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12391 output += "TAKES_FOCUS";
12392 numFlags++;
12393 }
12394
12395 switch (flags & VISIBILITY_MASK) {
12396 case INVISIBLE:
12397 if (numFlags > 0) {
12398 output += " ";
12399 }
12400 output += "INVISIBLE";
12401 // USELESS HERE numFlags++;
12402 break;
12403 case GONE:
12404 if (numFlags > 0) {
12405 output += " ";
12406 }
12407 output += "GONE";
12408 // USELESS HERE numFlags++;
12409 break;
12410 default:
12411 break;
12412 }
12413 return output;
12414 }
12415
12416 /**
12417 * Build a human readable string representation of the specified private
12418 * view flags.
12419 *
12420 * @param privateFlags the private view flags to convert to a string
12421 * @return a String representing the supplied flags
12422 */
12423 private static String printPrivateFlags(int privateFlags) {
12424 String output = "";
12425 int numFlags = 0;
12426
12427 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
12428 output += "WANTS_FOCUS";
12429 numFlags++;
12430 }
12431
12432 if ((privateFlags & FOCUSED) == FOCUSED) {
12433 if (numFlags > 0) {
12434 output += " ";
12435 }
12436 output += "FOCUSED";
12437 numFlags++;
12438 }
12439
12440 if ((privateFlags & SELECTED) == SELECTED) {
12441 if (numFlags > 0) {
12442 output += " ";
12443 }
12444 output += "SELECTED";
12445 numFlags++;
12446 }
12447
12448 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
12449 if (numFlags > 0) {
12450 output += " ";
12451 }
12452 output += "IS_ROOT_NAMESPACE";
12453 numFlags++;
12454 }
12455
12456 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
12457 if (numFlags > 0) {
12458 output += " ";
12459 }
12460 output += "HAS_BOUNDS";
12461 numFlags++;
12462 }
12463
12464 if ((privateFlags & DRAWN) == DRAWN) {
12465 if (numFlags > 0) {
12466 output += " ";
12467 }
12468 output += "DRAWN";
12469 // USELESS HERE numFlags++;
12470 }
12471 return output;
12472 }
12473
12474 /**
12475 * <p>Indicates whether or not this view's layout will be requested during
12476 * the next hierarchy layout pass.</p>
12477 *
12478 * @return true if the layout will be forced during next layout pass
12479 */
12480 public boolean isLayoutRequested() {
12481 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
12482 }
12483
12484 /**
12485 * Assign a size and position to a view and all of its
12486 * descendants
12487 *
12488 * <p>This is the second phase of the layout mechanism.
12489 * (The first is measuring). In this phase, each parent calls
12490 * layout on all of its children to position them.
12491 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080012492 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012493 *
Chet Haase9c087442011-01-12 16:20:16 -080012494 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012495 * Derived classes with children should override
12496 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080012497 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012498 *
12499 * @param l Left position, relative to parent
12500 * @param t Top position, relative to parent
12501 * @param r Right position, relative to parent
12502 * @param b Bottom position, relative to parent
12503 */
Romain Guy5429e1d2010-09-07 12:38:00 -070012504 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080012505 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070012506 int oldL = mLeft;
12507 int oldT = mTop;
12508 int oldB = mBottom;
12509 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012510 boolean changed = setFrame(l, t, r, b);
12511 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
12512 if (ViewDebug.TRACE_HIERARCHY) {
12513 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
12514 }
12515
12516 onLayout(changed, l, t, r, b);
12517 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070012518
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012519 ListenerInfo li = mListenerInfo;
12520 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070012521 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012522 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070012523 int numListeners = listenersCopy.size();
12524 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070012525 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070012526 }
12527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012528 }
12529 mPrivateFlags &= ~FORCE_LAYOUT;
12530 }
12531
12532 /**
12533 * Called from layout when this view should
12534 * assign a size and position to each of its children.
12535 *
12536 * Derived classes with children should override
12537 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070012538 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012539 * @param changed This is a new size or position for this view
12540 * @param left Left position, relative to parent
12541 * @param top Top position, relative to parent
12542 * @param right Right position, relative to parent
12543 * @param bottom Bottom position, relative to parent
12544 */
12545 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
12546 }
12547
12548 /**
12549 * Assign a size and position to this view.
12550 *
12551 * This is called from layout.
12552 *
12553 * @param left Left position, relative to parent
12554 * @param top Top position, relative to parent
12555 * @param right Right position, relative to parent
12556 * @param bottom Bottom position, relative to parent
12557 * @return true if the new size and position are different than the
12558 * previous ones
12559 * {@hide}
12560 */
12561 protected boolean setFrame(int left, int top, int right, int bottom) {
12562 boolean changed = false;
12563
12564 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070012565 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012566 + right + "," + bottom + ")");
12567 }
12568
12569 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
12570 changed = true;
12571
12572 // Remember our drawn bit
12573 int drawn = mPrivateFlags & DRAWN;
12574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012575 int oldWidth = mRight - mLeft;
12576 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070012577 int newWidth = right - left;
12578 int newHeight = bottom - top;
12579 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
12580
12581 // Invalidate our old position
12582 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012583
12584 mLeft = left;
12585 mTop = top;
12586 mRight = right;
12587 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080012588 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
12589 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
12590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012591
12592 mPrivateFlags |= HAS_BOUNDS;
12593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012594
Chet Haase75755e22011-07-18 17:48:25 -070012595 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012596 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
12597 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012598 if (mTransformationInfo != null) {
12599 mTransformationInfo.mMatrixDirty = true;
12600 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012602 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
12603 }
12604
12605 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
12606 // If we are visible, force the DRAWN bit to on so that
12607 // this invalidate will go through (at least to our parent).
12608 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012609 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012610 // the DRAWN bit.
12611 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070012612 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080012613 // parent display list may need to be recreated based on a change in the bounds
12614 // of any child
12615 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012616 }
12617
12618 // Reset drawn bit to original value (invalidate turns it off)
12619 mPrivateFlags |= drawn;
12620
12621 mBackgroundSizeChanged = true;
12622 }
12623 return changed;
12624 }
12625
12626 /**
12627 * Finalize inflating a view from XML. This is called as the last phase
12628 * of inflation, after all child views have been added.
12629 *
12630 * <p>Even if the subclass overrides onFinishInflate, they should always be
12631 * sure to call the super method, so that we get called.
12632 */
12633 protected void onFinishInflate() {
12634 }
12635
12636 /**
12637 * Returns the resources associated with this view.
12638 *
12639 * @return Resources object.
12640 */
12641 public Resources getResources() {
12642 return mResources;
12643 }
12644
12645 /**
12646 * Invalidates the specified Drawable.
12647 *
12648 * @param drawable the drawable to invalidate
12649 */
12650 public void invalidateDrawable(Drawable drawable) {
12651 if (verifyDrawable(drawable)) {
12652 final Rect dirty = drawable.getBounds();
12653 final int scrollX = mScrollX;
12654 final int scrollY = mScrollY;
12655
12656 invalidate(dirty.left + scrollX, dirty.top + scrollY,
12657 dirty.right + scrollX, dirty.bottom + scrollY);
12658 }
12659 }
12660
12661 /**
12662 * Schedules an action on a drawable to occur at a specified time.
12663 *
12664 * @param who the recipient of the action
12665 * @param what the action to run on the drawable
12666 * @param when the time at which the action must occur. Uses the
12667 * {@link SystemClock#uptimeMillis} timebase.
12668 */
12669 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080012670 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012671 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080012672 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012673 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
12674 Choreographer.CALLBACK_ANIMATION, what, who,
12675 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080012676 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012677 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080012678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012679 }
12680 }
12681
12682 /**
12683 * Cancels a scheduled action on a drawable.
12684 *
12685 * @param who the recipient of the action
12686 * @param what the action to cancel
12687 */
12688 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080012689 if (verifyDrawable(who) && what != null) {
12690 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012691 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12692 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080012693 } else {
12694 ViewRootImpl.getRunQueue().removeCallbacks(what);
12695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012696 }
12697 }
12698
12699 /**
12700 * Unschedule any events associated with the given Drawable. This can be
12701 * used when selecting a new Drawable into a view, so that the previous
12702 * one is completely unscheduled.
12703 *
12704 * @param who The Drawable to unschedule.
12705 *
12706 * @see #drawableStateChanged
12707 */
12708 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080012709 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012710 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12711 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012712 }
12713 }
12714
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012715 /**
12716 * Return the layout direction of a given Drawable.
12717 *
12718 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012719 */
12720 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012721 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070012722 }
12723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012724 /**
12725 * If your view subclass is displaying its own Drawable objects, it should
12726 * override this function and return true for any Drawable it is
12727 * displaying. This allows animations for those drawables to be
12728 * scheduled.
12729 *
12730 * <p>Be sure to call through to the super class when overriding this
12731 * function.
12732 *
12733 * @param who The Drawable to verify. Return true if it is one you are
12734 * displaying, else return the result of calling through to the
12735 * super class.
12736 *
12737 * @return boolean If true than the Drawable is being displayed in the
12738 * view; else false and it is not allowed to animate.
12739 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012740 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12741 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012742 */
12743 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012744 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012745 }
12746
12747 /**
12748 * This function is called whenever the state of the view changes in such
12749 * a way that it impacts the state of drawables being shown.
12750 *
12751 * <p>Be sure to call through to the superclass when overriding this
12752 * function.
12753 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012754 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012755 */
12756 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012757 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012758 if (d != null && d.isStateful()) {
12759 d.setState(getDrawableState());
12760 }
12761 }
12762
12763 /**
12764 * Call this to force a view to update its drawable state. This will cause
12765 * drawableStateChanged to be called on this view. Views that are interested
12766 * in the new state should call getDrawableState.
12767 *
12768 * @see #drawableStateChanged
12769 * @see #getDrawableState
12770 */
12771 public void refreshDrawableState() {
12772 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12773 drawableStateChanged();
12774
12775 ViewParent parent = mParent;
12776 if (parent != null) {
12777 parent.childDrawableStateChanged(this);
12778 }
12779 }
12780
12781 /**
12782 * Return an array of resource IDs of the drawable states representing the
12783 * current state of the view.
12784 *
12785 * @return The current drawable state
12786 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012787 * @see Drawable#setState(int[])
12788 * @see #drawableStateChanged()
12789 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012790 */
12791 public final int[] getDrawableState() {
12792 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12793 return mDrawableState;
12794 } else {
12795 mDrawableState = onCreateDrawableState(0);
12796 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12797 return mDrawableState;
12798 }
12799 }
12800
12801 /**
12802 * Generate the new {@link android.graphics.drawable.Drawable} state for
12803 * this view. This is called by the view
12804 * system when the cached Drawable state is determined to be invalid. To
12805 * retrieve the current state, you should use {@link #getDrawableState}.
12806 *
12807 * @param extraSpace if non-zero, this is the number of extra entries you
12808 * would like in the returned array in which you can place your own
12809 * states.
12810 *
12811 * @return Returns an array holding the current {@link Drawable} state of
12812 * the view.
12813 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012814 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012815 */
12816 protected int[] onCreateDrawableState(int extraSpace) {
12817 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12818 mParent instanceof View) {
12819 return ((View) mParent).onCreateDrawableState(extraSpace);
12820 }
12821
12822 int[] drawableState;
12823
12824 int privateFlags = mPrivateFlags;
12825
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012826 int viewStateIndex = 0;
12827 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12828 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12829 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012830 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012831 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12832 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012833 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12834 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012835 // This is set if HW acceleration is requested, even if the current
12836 // process doesn't allow it. This is just to allow app preview
12837 // windows to better match their app.
12838 viewStateIndex |= VIEW_STATE_ACCELERATED;
12839 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012840 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012841
Christopher Tate3d4bf172011-03-28 16:16:46 -070012842 final int privateFlags2 = mPrivateFlags2;
12843 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12844 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012846 drawableState = VIEW_STATE_SETS[viewStateIndex];
12847
12848 //noinspection ConstantIfStatement
12849 if (false) {
12850 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12851 Log.i("View", toString()
12852 + " pressed=" + ((privateFlags & PRESSED) != 0)
12853 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12854 + " fo=" + hasFocus()
12855 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012856 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012857 + ": " + Arrays.toString(drawableState));
12858 }
12859
12860 if (extraSpace == 0) {
12861 return drawableState;
12862 }
12863
12864 final int[] fullState;
12865 if (drawableState != null) {
12866 fullState = new int[drawableState.length + extraSpace];
12867 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12868 } else {
12869 fullState = new int[extraSpace];
12870 }
12871
12872 return fullState;
12873 }
12874
12875 /**
12876 * Merge your own state values in <var>additionalState</var> into the base
12877 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012878 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012879 *
12880 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012881 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012882 * own additional state values.
12883 *
12884 * @param additionalState The additional state values you would like
12885 * added to <var>baseState</var>; this array is not modified.
12886 *
12887 * @return As a convenience, the <var>baseState</var> array you originally
12888 * passed into the function is returned.
12889 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012890 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012891 */
12892 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12893 final int N = baseState.length;
12894 int i = N - 1;
12895 while (i >= 0 && baseState[i] == 0) {
12896 i--;
12897 }
12898 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12899 return baseState;
12900 }
12901
12902 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012903 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12904 * on all Drawable objects associated with this view.
12905 */
12906 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012907 if (mBackground != null) {
12908 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070012909 }
12910 }
12911
12912 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012913 * Sets the background color for this view.
12914 * @param color the color of the background
12915 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012916 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012917 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012918 if (mBackground instanceof ColorDrawable) {
12919 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070012920 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070012921 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070012922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012923 }
12924
12925 /**
12926 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012927 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012928 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070012929 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012930 * @attr ref android.R.styleable#View_background
12931 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012932 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012933 public void setBackgroundResource(int resid) {
12934 if (resid != 0 && resid == mBackgroundResource) {
12935 return;
12936 }
12937
12938 Drawable d= null;
12939 if (resid != 0) {
12940 d = mResources.getDrawable(resid);
12941 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012942 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012943
12944 mBackgroundResource = resid;
12945 }
12946
12947 /**
12948 * Set the background to a given Drawable, or remove the background. If the
12949 * background has padding, this View's padding is set to the background's
12950 * padding. However, when a background is removed, this View's padding isn't
12951 * touched. If setting the padding is desired, please use
12952 * {@link #setPadding(int, int, int, int)}.
12953 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012954 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012955 * background
12956 */
Philip Milne6c8ea062012-04-03 17:38:43 -070012957 public void setBackground(Drawable background) {
12958 setBackgroundDrawable(background);
12959 }
12960
12961 /**
12962 * @deprecated use {@link #setBackground(Drawable)} instead
12963 */
12964 @Deprecated
12965 public void setBackgroundDrawable(Drawable background) {
12966 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012967 return;
12968 }
12969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012970 boolean requestLayout = false;
12971
12972 mBackgroundResource = 0;
12973
12974 /*
12975 * Regardless of whether we're setting a new background or not, we want
12976 * to clear the previous drawable.
12977 */
Philip Milne6c8ea062012-04-03 17:38:43 -070012978 if (mBackground != null) {
12979 mBackground.setCallback(null);
12980 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012981 }
12982
Philip Milne6c8ea062012-04-03 17:38:43 -070012983 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012984 Rect padding = sThreadLocal.get();
12985 if (padding == null) {
12986 padding = new Rect();
12987 sThreadLocal.set(padding);
12988 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012989 if (background.getPadding(padding)) {
12990 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012991 case LAYOUT_DIRECTION_RTL:
12992 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12993 break;
12994 case LAYOUT_DIRECTION_LTR:
12995 default:
12996 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12997 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012998 }
12999
13000 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13001 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013002 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13003 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013004 requestLayout = true;
13005 }
13006
Philip Milne6c8ea062012-04-03 17:38:43 -070013007 background.setCallback(this);
13008 if (background.isStateful()) {
13009 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013010 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013011 background.setVisible(getVisibility() == VISIBLE, false);
13012 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013013
13014 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13015 mPrivateFlags &= ~SKIP_DRAW;
13016 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13017 requestLayout = true;
13018 }
13019 } else {
13020 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013021 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013022
13023 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13024 /*
13025 * This view ONLY drew the background before and we're removing
13026 * the background, so now it won't draw anything
13027 * (hence we SKIP_DRAW)
13028 */
13029 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13030 mPrivateFlags |= SKIP_DRAW;
13031 }
13032
13033 /*
13034 * When the background is set, we try to apply its padding to this
13035 * View. When the background is removed, we don't touch this View's
13036 * padding. This is noted in the Javadocs. Hence, we don't need to
13037 * requestLayout(), the invalidate() below is sufficient.
13038 */
13039
13040 // The old background's minimum size could have affected this
13041 // View's layout, so let's requestLayout
13042 requestLayout = true;
13043 }
13044
Romain Guy8f1344f52009-05-15 16:03:59 -070013045 computeOpaqueFlags();
13046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013047 if (requestLayout) {
13048 requestLayout();
13049 }
13050
13051 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013052 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013053 }
13054
13055 /**
13056 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013057 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013058 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013059 *
13060 * @see #setBackground(Drawable)
13061 *
13062 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013063 */
13064 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013065 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013066 }
13067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013068 /**
13069 * Sets the padding. The view may add on the space required to display
13070 * the scrollbars, depending on the style and visibility of the scrollbars.
13071 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13072 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13073 * from the values set in this call.
13074 *
13075 * @attr ref android.R.styleable#View_padding
13076 * @attr ref android.R.styleable#View_paddingBottom
13077 * @attr ref android.R.styleable#View_paddingLeft
13078 * @attr ref android.R.styleable#View_paddingRight
13079 * @attr ref android.R.styleable#View_paddingTop
13080 * @param left the left padding in pixels
13081 * @param top the top padding in pixels
13082 * @param right the right padding in pixels
13083 * @param bottom the bottom padding in pixels
13084 */
13085 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013086 mUserPaddingStart = -1;
13087 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013088 mUserPaddingRelative = false;
13089
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013090 internalSetPadding(left, top, right, bottom);
13091 }
13092
13093 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013094 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013095 mUserPaddingRight = right;
13096 mUserPaddingBottom = bottom;
13097
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013098 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013099 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013100
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013101 // Common case is there are no scroll bars.
13102 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013103 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013104 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013105 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013106 switch (mVerticalScrollbarPosition) {
13107 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013108 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13109 left += offset;
13110 } else {
13111 right += offset;
13112 }
13113 break;
Adam Powell20232d02010-12-08 21:08:53 -080013114 case SCROLLBAR_POSITION_RIGHT:
13115 right += offset;
13116 break;
13117 case SCROLLBAR_POSITION_LEFT:
13118 left += offset;
13119 break;
13120 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013121 }
Adam Powell20232d02010-12-08 21:08:53 -080013122 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013123 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13124 ? 0 : getHorizontalScrollbarHeight();
13125 }
13126 }
Romain Guy8506ab42009-06-11 17:35:47 -070013127
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013128 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013129 changed = true;
13130 mPaddingLeft = left;
13131 }
13132 if (mPaddingTop != top) {
13133 changed = true;
13134 mPaddingTop = top;
13135 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013136 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013137 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013138 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013139 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013140 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013141 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013142 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013143 }
13144
13145 if (changed) {
13146 requestLayout();
13147 }
13148 }
13149
13150 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013151 * Sets the relative padding. The view may add on the space required to display
13152 * the scrollbars, depending on the style and visibility of the scrollbars.
13153 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13154 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13155 * from the values set in this call.
13156 *
13157 * @attr ref android.R.styleable#View_padding
13158 * @attr ref android.R.styleable#View_paddingBottom
13159 * @attr ref android.R.styleable#View_paddingStart
13160 * @attr ref android.R.styleable#View_paddingEnd
13161 * @attr ref android.R.styleable#View_paddingTop
13162 * @param start the start padding in pixels
13163 * @param top the top padding in pixels
13164 * @param end the end padding in pixels
13165 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013166 */
13167 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013168 mUserPaddingStart = start;
13169 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013170 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013171
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013172 switch(getResolvedLayoutDirection()) {
13173 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013174 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013175 break;
13176 case LAYOUT_DIRECTION_LTR:
13177 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013178 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013179 }
13180 }
13181
13182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013183 * Returns the top padding of this view.
13184 *
13185 * @return the top padding in pixels
13186 */
13187 public int getPaddingTop() {
13188 return mPaddingTop;
13189 }
13190
13191 /**
13192 * Returns the bottom padding of this view. If there are inset and enabled
13193 * scrollbars, this value may include the space required to display the
13194 * scrollbars as well.
13195 *
13196 * @return the bottom padding in pixels
13197 */
13198 public int getPaddingBottom() {
13199 return mPaddingBottom;
13200 }
13201
13202 /**
13203 * Returns the left padding of this view. If there are inset and enabled
13204 * scrollbars, this value may include the space required to display the
13205 * scrollbars as well.
13206 *
13207 * @return the left padding in pixels
13208 */
13209 public int getPaddingLeft() {
13210 return mPaddingLeft;
13211 }
13212
13213 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013214 * Returns the start padding of this view depending on its resolved layout direction.
13215 * If there are inset and enabled scrollbars, this value may include the space
13216 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013217 *
13218 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013219 */
13220 public int getPaddingStart() {
13221 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13222 mPaddingRight : mPaddingLeft;
13223 }
13224
13225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013226 * Returns the right padding of this view. If there are inset and enabled
13227 * scrollbars, this value may include the space required to display the
13228 * scrollbars as well.
13229 *
13230 * @return the right padding in pixels
13231 */
13232 public int getPaddingRight() {
13233 return mPaddingRight;
13234 }
13235
13236 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013237 * Returns the end padding of this view depending on its resolved layout direction.
13238 * If there are inset and enabled scrollbars, this value may include the space
13239 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013240 *
13241 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013242 */
13243 public int getPaddingEnd() {
13244 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13245 mPaddingLeft : mPaddingRight;
13246 }
13247
13248 /**
13249 * Return if the padding as been set thru relative values
13250 * {@link #setPaddingRelative(int, int, int, int)} or thru
13251 * @attr ref android.R.styleable#View_paddingStart or
13252 * @attr ref android.R.styleable#View_paddingEnd
13253 *
13254 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013255 */
13256 public boolean isPaddingRelative() {
13257 return mUserPaddingRelative;
13258 }
13259
13260 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013261 * Changes the selection state of this view. A view can be selected or not.
13262 * Note that selection is not the same as focus. Views are typically
13263 * selected in the context of an AdapterView like ListView or GridView;
13264 * the selected view is the view that is highlighted.
13265 *
13266 * @param selected true if the view must be selected, false otherwise
13267 */
13268 public void setSelected(boolean selected) {
13269 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13270 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013271 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013272 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013273 refreshDrawableState();
13274 dispatchSetSelected(selected);
13275 }
13276 }
13277
13278 /**
13279 * Dispatch setSelected to all of this View's children.
13280 *
13281 * @see #setSelected(boolean)
13282 *
13283 * @param selected The new selected state
13284 */
13285 protected void dispatchSetSelected(boolean selected) {
13286 }
13287
13288 /**
13289 * Indicates the selection state of this view.
13290 *
13291 * @return true if the view is selected, false otherwise
13292 */
13293 @ViewDebug.ExportedProperty
13294 public boolean isSelected() {
13295 return (mPrivateFlags & SELECTED) != 0;
13296 }
13297
13298 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013299 * Changes the activated state of this view. A view can be activated or not.
13300 * Note that activation is not the same as selection. Selection is
13301 * a transient property, representing the view (hierarchy) the user is
13302 * currently interacting with. Activation is a longer-term state that the
13303 * user can move views in and out of. For example, in a list view with
13304 * single or multiple selection enabled, the views in the current selection
13305 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13306 * here.) The activated state is propagated down to children of the view it
13307 * is set on.
13308 *
13309 * @param activated true if the view must be activated, false otherwise
13310 */
13311 public void setActivated(boolean activated) {
13312 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13313 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013314 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013315 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013316 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013317 }
13318 }
13319
13320 /**
13321 * Dispatch setActivated to all of this View's children.
13322 *
13323 * @see #setActivated(boolean)
13324 *
13325 * @param activated The new activated state
13326 */
13327 protected void dispatchSetActivated(boolean activated) {
13328 }
13329
13330 /**
13331 * Indicates the activation state of this view.
13332 *
13333 * @return true if the view is activated, false otherwise
13334 */
13335 @ViewDebug.ExportedProperty
13336 public boolean isActivated() {
13337 return (mPrivateFlags & ACTIVATED) != 0;
13338 }
13339
13340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013341 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13342 * observer can be used to get notifications when global events, like
13343 * layout, happen.
13344 *
13345 * The returned ViewTreeObserver observer is not guaranteed to remain
13346 * valid for the lifetime of this View. If the caller of this method keeps
13347 * a long-lived reference to ViewTreeObserver, it should always check for
13348 * the return value of {@link ViewTreeObserver#isAlive()}.
13349 *
13350 * @return The ViewTreeObserver for this view's hierarchy.
13351 */
13352 public ViewTreeObserver getViewTreeObserver() {
13353 if (mAttachInfo != null) {
13354 return mAttachInfo.mTreeObserver;
13355 }
13356 if (mFloatingTreeObserver == null) {
13357 mFloatingTreeObserver = new ViewTreeObserver();
13358 }
13359 return mFloatingTreeObserver;
13360 }
13361
13362 /**
13363 * <p>Finds the topmost view in the current view hierarchy.</p>
13364 *
13365 * @return the topmost view containing this view
13366 */
13367 public View getRootView() {
13368 if (mAttachInfo != null) {
13369 final View v = mAttachInfo.mRootView;
13370 if (v != null) {
13371 return v;
13372 }
13373 }
Romain Guy8506ab42009-06-11 17:35:47 -070013374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013375 View parent = this;
13376
13377 while (parent.mParent != null && parent.mParent instanceof View) {
13378 parent = (View) parent.mParent;
13379 }
13380
13381 return parent;
13382 }
13383
13384 /**
13385 * <p>Computes the coordinates of this view on the screen. The argument
13386 * must be an array of two integers. After the method returns, the array
13387 * contains the x and y location in that order.</p>
13388 *
13389 * @param location an array of two integers in which to hold the coordinates
13390 */
13391 public void getLocationOnScreen(int[] location) {
13392 getLocationInWindow(location);
13393
13394 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070013395 if (info != null) {
13396 location[0] += info.mWindowLeft;
13397 location[1] += info.mWindowTop;
13398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013399 }
13400
13401 /**
13402 * <p>Computes the coordinates of this view in its window. The argument
13403 * must be an array of two integers. After the method returns, the array
13404 * contains the x and y location in that order.</p>
13405 *
13406 * @param location an array of two integers in which to hold the coordinates
13407 */
13408 public void getLocationInWindow(int[] location) {
13409 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013410 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013411 }
13412
Gilles Debunne6583ce52011-12-06 18:09:02 -080013413 if (mAttachInfo == null) {
13414 // When the view is not attached to a window, this method does not make sense
13415 location[0] = location[1] = 0;
13416 return;
13417 }
13418
Gilles Debunnecea45132011-11-24 02:19:27 +010013419 float[] position = mAttachInfo.mTmpTransformLocation;
13420 position[0] = position[1] = 0.0f;
13421
13422 if (!hasIdentityMatrix()) {
13423 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013425
Gilles Debunnecea45132011-11-24 02:19:27 +010013426 position[0] += mLeft;
13427 position[1] += mTop;
13428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013429 ViewParent viewParent = mParent;
13430 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013431 final View view = (View) viewParent;
13432
13433 position[0] -= view.mScrollX;
13434 position[1] -= view.mScrollY;
13435
13436 if (!view.hasIdentityMatrix()) {
13437 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013438 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013439
13440 position[0] += view.mLeft;
13441 position[1] += view.mTop;
13442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013443 viewParent = view.mParent;
13444 }
Romain Guy8506ab42009-06-11 17:35:47 -070013445
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013446 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013447 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010013448 final ViewRootImpl vr = (ViewRootImpl) viewParent;
13449 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013450 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013451
13452 location[0] = (int) (position[0] + 0.5f);
13453 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013454 }
13455
13456 /**
13457 * {@hide}
13458 * @param id the id of the view to be found
13459 * @return the view of the specified id, null if cannot be found
13460 */
13461 protected View findViewTraversal(int id) {
13462 if (id == mID) {
13463 return this;
13464 }
13465 return null;
13466 }
13467
13468 /**
13469 * {@hide}
13470 * @param tag the tag of the view to be found
13471 * @return the view of specified tag, null if cannot be found
13472 */
13473 protected View findViewWithTagTraversal(Object tag) {
13474 if (tag != null && tag.equals(mTag)) {
13475 return this;
13476 }
13477 return null;
13478 }
13479
13480 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013481 * {@hide}
13482 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070013483 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080013484 * @return The first view that matches the predicate or null.
13485 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070013486 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080013487 if (predicate.apply(this)) {
13488 return this;
13489 }
13490 return null;
13491 }
13492
13493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013494 * Look for a child view with the given id. If this view has the given
13495 * id, return this view.
13496 *
13497 * @param id The id to search for.
13498 * @return The view that has the given id in the hierarchy or null
13499 */
13500 public final View findViewById(int id) {
13501 if (id < 0) {
13502 return null;
13503 }
13504 return findViewTraversal(id);
13505 }
13506
13507 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070013508 * Finds a view by its unuque and stable accessibility id.
13509 *
13510 * @param accessibilityId The searched accessibility id.
13511 * @return The found view.
13512 */
13513 final View findViewByAccessibilityId(int accessibilityId) {
13514 if (accessibilityId < 0) {
13515 return null;
13516 }
13517 return findViewByAccessibilityIdTraversal(accessibilityId);
13518 }
13519
13520 /**
13521 * Performs the traversal to find a view by its unuque and stable accessibility id.
13522 *
13523 * <strong>Note:</strong>This method does not stop at the root namespace
13524 * boundary since the user can touch the screen at an arbitrary location
13525 * potentially crossing the root namespace bounday which will send an
13526 * accessibility event to accessibility services and they should be able
13527 * to obtain the event source. Also accessibility ids are guaranteed to be
13528 * unique in the window.
13529 *
13530 * @param accessibilityId The accessibility id.
13531 * @return The found view.
13532 */
13533 View findViewByAccessibilityIdTraversal(int accessibilityId) {
13534 if (getAccessibilityViewId() == accessibilityId) {
13535 return this;
13536 }
13537 return null;
13538 }
13539
13540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013541 * Look for a child view with the given tag. If this view has the given
13542 * tag, return this view.
13543 *
13544 * @param tag The tag to search for, using "tag.equals(getTag())".
13545 * @return The View that has the given tag in the hierarchy or null
13546 */
13547 public final View findViewWithTag(Object tag) {
13548 if (tag == null) {
13549 return null;
13550 }
13551 return findViewWithTagTraversal(tag);
13552 }
13553
13554 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013555 * {@hide}
13556 * Look for a child view that matches the specified predicate.
13557 * If this view matches the predicate, return this view.
13558 *
13559 * @param predicate The predicate to evaluate.
13560 * @return The first view that matches the predicate or null.
13561 */
13562 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070013563 return findViewByPredicateTraversal(predicate, null);
13564 }
13565
13566 /**
13567 * {@hide}
13568 * Look for a child view that matches the specified predicate,
13569 * starting with the specified view and its descendents and then
13570 * recusively searching the ancestors and siblings of that view
13571 * until this view is reached.
13572 *
13573 * This method is useful in cases where the predicate does not match
13574 * a single unique view (perhaps multiple views use the same id)
13575 * and we are trying to find the view that is "closest" in scope to the
13576 * starting view.
13577 *
13578 * @param start The view to start from.
13579 * @param predicate The predicate to evaluate.
13580 * @return The first view that matches the predicate or null.
13581 */
13582 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
13583 View childToSkip = null;
13584 for (;;) {
13585 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
13586 if (view != null || start == this) {
13587 return view;
13588 }
13589
13590 ViewParent parent = start.getParent();
13591 if (parent == null || !(parent instanceof View)) {
13592 return null;
13593 }
13594
13595 childToSkip = start;
13596 start = (View) parent;
13597 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080013598 }
13599
13600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013601 * Sets the identifier for this view. The identifier does not have to be
13602 * unique in this view's hierarchy. The identifier should be a positive
13603 * number.
13604 *
13605 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070013606 * @see #getId()
13607 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013608 *
13609 * @param id a number used to identify the view
13610 *
13611 * @attr ref android.R.styleable#View_id
13612 */
13613 public void setId(int id) {
13614 mID = id;
13615 }
13616
13617 /**
13618 * {@hide}
13619 *
13620 * @param isRoot true if the view belongs to the root namespace, false
13621 * otherwise
13622 */
13623 public void setIsRootNamespace(boolean isRoot) {
13624 if (isRoot) {
13625 mPrivateFlags |= IS_ROOT_NAMESPACE;
13626 } else {
13627 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
13628 }
13629 }
13630
13631 /**
13632 * {@hide}
13633 *
13634 * @return true if the view belongs to the root namespace, false otherwise
13635 */
13636 public boolean isRootNamespace() {
13637 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
13638 }
13639
13640 /**
13641 * Returns this view's identifier.
13642 *
13643 * @return a positive integer used to identify the view or {@link #NO_ID}
13644 * if the view has no ID
13645 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013646 * @see #setId(int)
13647 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013648 * @attr ref android.R.styleable#View_id
13649 */
13650 @ViewDebug.CapturedViewProperty
13651 public int getId() {
13652 return mID;
13653 }
13654
13655 /**
13656 * Returns this view's tag.
13657 *
13658 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070013659 *
13660 * @see #setTag(Object)
13661 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013662 */
13663 @ViewDebug.ExportedProperty
13664 public Object getTag() {
13665 return mTag;
13666 }
13667
13668 /**
13669 * Sets the tag associated with this view. A tag can be used to mark
13670 * a view in its hierarchy and does not have to be unique within the
13671 * hierarchy. Tags can also be used to store data within a view without
13672 * resorting to another data structure.
13673 *
13674 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070013675 *
13676 * @see #getTag()
13677 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013678 */
13679 public void setTag(final Object tag) {
13680 mTag = tag;
13681 }
13682
13683 /**
Romain Guyd90a3312009-05-06 14:54:28 -070013684 * Returns the tag associated with this view and the specified key.
13685 *
13686 * @param key The key identifying the tag
13687 *
13688 * @return the Object stored in this view as a tag
13689 *
13690 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070013691 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070013692 */
13693 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013694 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070013695 return null;
13696 }
13697
13698 /**
13699 * Sets a tag associated with this view and a key. A tag can be used
13700 * to mark a view in its hierarchy and does not have to be unique within
13701 * the hierarchy. Tags can also be used to store data within a view
13702 * without resorting to another data structure.
13703 *
13704 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070013705 * application to ensure it is unique (see the <a
13706 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
13707 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070013708 * the Android framework or not associated with any package will cause
13709 * an {@link IllegalArgumentException} to be thrown.
13710 *
13711 * @param key The key identifying the tag
13712 * @param tag An Object to tag the view with
13713 *
13714 * @throws IllegalArgumentException If they specified key is not valid
13715 *
13716 * @see #setTag(Object)
13717 * @see #getTag(int)
13718 */
13719 public void setTag(int key, final Object tag) {
13720 // If the package id is 0x00 or 0x01, it's either an undefined package
13721 // or a framework id
13722 if ((key >>> 24) < 2) {
13723 throw new IllegalArgumentException("The key must be an application-specific "
13724 + "resource id.");
13725 }
13726
Adam Powell2b2f6d62011-09-23 11:15:39 -070013727 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013728 }
13729
13730 /**
13731 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
13732 * framework id.
13733 *
13734 * @hide
13735 */
13736 public void setTagInternal(int key, Object tag) {
13737 if ((key >>> 24) != 0x1) {
13738 throw new IllegalArgumentException("The key must be a framework-specific "
13739 + "resource id.");
13740 }
13741
Adam Powell2b2f6d62011-09-23 11:15:39 -070013742 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013743 }
13744
Adam Powell2b2f6d62011-09-23 11:15:39 -070013745 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013746 if (mKeyedTags == null) {
13747 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013748 }
13749
Adam Powell7db82ac2011-09-22 19:44:04 -070013750 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013751 }
13752
13753 /**
Romain Guy13922e02009-05-12 17:56:14 -070013754 * @param consistency The type of consistency. See ViewDebug for more information.
13755 *
13756 * @hide
13757 */
13758 protected boolean dispatchConsistencyCheck(int consistency) {
13759 return onConsistencyCheck(consistency);
13760 }
13761
13762 /**
13763 * Method that subclasses should implement to check their consistency. The type of
13764 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013765 *
Romain Guy13922e02009-05-12 17:56:14 -070013766 * @param consistency The type of consistency. See ViewDebug for more information.
13767 *
13768 * @throws IllegalStateException if the view is in an inconsistent state.
13769 *
13770 * @hide
13771 */
13772 protected boolean onConsistencyCheck(int consistency) {
13773 boolean result = true;
13774
13775 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13776 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13777
13778 if (checkLayout) {
13779 if (getParent() == null) {
13780 result = false;
13781 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13782 "View " + this + " does not have a parent.");
13783 }
13784
13785 if (mAttachInfo == null) {
13786 result = false;
13787 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13788 "View " + this + " is not attached to a window.");
13789 }
13790 }
13791
13792 if (checkDrawing) {
13793 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13794 // from their draw() method
13795
13796 if ((mPrivateFlags & DRAWN) != DRAWN &&
13797 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13798 result = false;
13799 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13800 "View " + this + " was invalidated but its drawing cache is valid.");
13801 }
13802 }
13803
13804 return result;
13805 }
13806
13807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013808 * Prints information about this view in the log output, with the tag
13809 * {@link #VIEW_LOG_TAG}.
13810 *
13811 * @hide
13812 */
13813 public void debug() {
13814 debug(0);
13815 }
13816
13817 /**
13818 * Prints information about this view in the log output, with the tag
13819 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13820 * indentation defined by the <code>depth</code>.
13821 *
13822 * @param depth the indentation level
13823 *
13824 * @hide
13825 */
13826 protected void debug(int depth) {
13827 String output = debugIndent(depth - 1);
13828
13829 output += "+ " + this;
13830 int id = getId();
13831 if (id != -1) {
13832 output += " (id=" + id + ")";
13833 }
13834 Object tag = getTag();
13835 if (tag != null) {
13836 output += " (tag=" + tag + ")";
13837 }
13838 Log.d(VIEW_LOG_TAG, output);
13839
13840 if ((mPrivateFlags & FOCUSED) != 0) {
13841 output = debugIndent(depth) + " FOCUSED";
13842 Log.d(VIEW_LOG_TAG, output);
13843 }
13844
13845 output = debugIndent(depth);
13846 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13847 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13848 + "} ";
13849 Log.d(VIEW_LOG_TAG, output);
13850
13851 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13852 || mPaddingBottom != 0) {
13853 output = debugIndent(depth);
13854 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13855 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13856 Log.d(VIEW_LOG_TAG, output);
13857 }
13858
13859 output = debugIndent(depth);
13860 output += "mMeasureWidth=" + mMeasuredWidth +
13861 " mMeasureHeight=" + mMeasuredHeight;
13862 Log.d(VIEW_LOG_TAG, output);
13863
13864 output = debugIndent(depth);
13865 if (mLayoutParams == null) {
13866 output += "BAD! no layout params";
13867 } else {
13868 output = mLayoutParams.debug(output);
13869 }
13870 Log.d(VIEW_LOG_TAG, output);
13871
13872 output = debugIndent(depth);
13873 output += "flags={";
13874 output += View.printFlags(mViewFlags);
13875 output += "}";
13876 Log.d(VIEW_LOG_TAG, output);
13877
13878 output = debugIndent(depth);
13879 output += "privateFlags={";
13880 output += View.printPrivateFlags(mPrivateFlags);
13881 output += "}";
13882 Log.d(VIEW_LOG_TAG, output);
13883 }
13884
13885 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013886 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013887 *
13888 * @param depth the indentation level
13889 * @return a String containing (depth * 2 + 3) * 2 white spaces
13890 *
13891 * @hide
13892 */
13893 protected static String debugIndent(int depth) {
13894 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13895 for (int i = 0; i < (depth * 2) + 3; i++) {
13896 spaces.append(' ').append(' ');
13897 }
13898 return spaces.toString();
13899 }
13900
13901 /**
13902 * <p>Return the offset of the widget's text baseline from the widget's top
13903 * boundary. If this widget does not support baseline alignment, this
13904 * method returns -1. </p>
13905 *
13906 * @return the offset of the baseline within the widget's bounds or -1
13907 * if baseline alignment is not supported
13908 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013909 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013910 public int getBaseline() {
13911 return -1;
13912 }
13913
13914 /**
13915 * Call this when something has changed which has invalidated the
13916 * layout of this view. This will schedule a layout pass of the view
13917 * tree.
13918 */
13919 public void requestLayout() {
13920 if (ViewDebug.TRACE_HIERARCHY) {
13921 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13922 }
13923
13924 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013925 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013926
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070013927 if (mLayoutParams != null) {
13928 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
13929 }
13930
13931 if (mParent != null && !mParent.isLayoutRequested()) {
13932 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013933 }
13934 }
13935
13936 /**
13937 * Forces this view to be laid out during the next layout pass.
13938 * This method does not call requestLayout() or forceLayout()
13939 * on the parent.
13940 */
13941 public void forceLayout() {
13942 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013943 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013944 }
13945
13946 /**
13947 * <p>
13948 * This is called to find out how big a view should be. The parent
13949 * supplies constraint information in the width and height parameters.
13950 * </p>
13951 *
13952 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013953 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013954 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013955 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013956 * </p>
13957 *
13958 *
13959 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13960 * parent
13961 * @param heightMeasureSpec Vertical space requirements as imposed by the
13962 * parent
13963 *
13964 * @see #onMeasure(int, int)
13965 */
13966 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13967 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13968 widthMeasureSpec != mOldWidthMeasureSpec ||
13969 heightMeasureSpec != mOldHeightMeasureSpec) {
13970
13971 // first clears the measured dimension flag
13972 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13973
13974 if (ViewDebug.TRACE_HIERARCHY) {
13975 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13976 }
13977
13978 // measure ourselves, this should set the measured dimension flag back
13979 onMeasure(widthMeasureSpec, heightMeasureSpec);
13980
13981 // flag not set, setMeasuredDimension() was not invoked, we raise
13982 // an exception to warn the developer
13983 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13984 throw new IllegalStateException("onMeasure() did not set the"
13985 + " measured dimension by calling"
13986 + " setMeasuredDimension()");
13987 }
13988
13989 mPrivateFlags |= LAYOUT_REQUIRED;
13990 }
13991
13992 mOldWidthMeasureSpec = widthMeasureSpec;
13993 mOldHeightMeasureSpec = heightMeasureSpec;
13994 }
13995
13996 /**
13997 * <p>
13998 * Measure the view and its content to determine the measured width and the
13999 * measured height. This method is invoked by {@link #measure(int, int)} and
14000 * should be overriden by subclasses to provide accurate and efficient
14001 * measurement of their contents.
14002 * </p>
14003 *
14004 * <p>
14005 * <strong>CONTRACT:</strong> When overriding this method, you
14006 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14007 * measured width and height of this view. Failure to do so will trigger an
14008 * <code>IllegalStateException</code>, thrown by
14009 * {@link #measure(int, int)}. Calling the superclass'
14010 * {@link #onMeasure(int, int)} is a valid use.
14011 * </p>
14012 *
14013 * <p>
14014 * The base class implementation of measure defaults to the background size,
14015 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14016 * override {@link #onMeasure(int, int)} to provide better measurements of
14017 * their content.
14018 * </p>
14019 *
14020 * <p>
14021 * If this method is overridden, it is the subclass's responsibility to make
14022 * sure the measured height and width are at least the view's minimum height
14023 * and width ({@link #getSuggestedMinimumHeight()} and
14024 * {@link #getSuggestedMinimumWidth()}).
14025 * </p>
14026 *
14027 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14028 * The requirements are encoded with
14029 * {@link android.view.View.MeasureSpec}.
14030 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14031 * The requirements are encoded with
14032 * {@link android.view.View.MeasureSpec}.
14033 *
14034 * @see #getMeasuredWidth()
14035 * @see #getMeasuredHeight()
14036 * @see #setMeasuredDimension(int, int)
14037 * @see #getSuggestedMinimumHeight()
14038 * @see #getSuggestedMinimumWidth()
14039 * @see android.view.View.MeasureSpec#getMode(int)
14040 * @see android.view.View.MeasureSpec#getSize(int)
14041 */
14042 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14043 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14044 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14045 }
14046
14047 /**
14048 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14049 * measured width and measured height. Failing to do so will trigger an
14050 * exception at measurement time.</p>
14051 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014052 * @param measuredWidth The measured width of this view. May be a complex
14053 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14054 * {@link #MEASURED_STATE_TOO_SMALL}.
14055 * @param measuredHeight The measured height of this view. May be a complex
14056 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14057 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014058 */
14059 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14060 mMeasuredWidth = measuredWidth;
14061 mMeasuredHeight = measuredHeight;
14062
14063 mPrivateFlags |= MEASURED_DIMENSION_SET;
14064 }
14065
14066 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014067 * Merge two states as returned by {@link #getMeasuredState()}.
14068 * @param curState The current state as returned from a view or the result
14069 * of combining multiple views.
14070 * @param newState The new view state to combine.
14071 * @return Returns a new integer reflecting the combination of the two
14072 * states.
14073 */
14074 public static int combineMeasuredStates(int curState, int newState) {
14075 return curState | newState;
14076 }
14077
14078 /**
14079 * Version of {@link #resolveSizeAndState(int, int, int)}
14080 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14081 */
14082 public static int resolveSize(int size, int measureSpec) {
14083 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14084 }
14085
14086 /**
14087 * Utility to reconcile a desired size and state, with constraints imposed
14088 * by a MeasureSpec. Will take the desired size, unless a different size
14089 * is imposed by the constraints. The returned value is a compound integer,
14090 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14091 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14092 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014093 *
14094 * @param size How big the view wants to be
14095 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014096 * @return Size information bit mask as defined by
14097 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014098 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014099 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014100 int result = size;
14101 int specMode = MeasureSpec.getMode(measureSpec);
14102 int specSize = MeasureSpec.getSize(measureSpec);
14103 switch (specMode) {
14104 case MeasureSpec.UNSPECIFIED:
14105 result = size;
14106 break;
14107 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014108 if (specSize < size) {
14109 result = specSize | MEASURED_STATE_TOO_SMALL;
14110 } else {
14111 result = size;
14112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014113 break;
14114 case MeasureSpec.EXACTLY:
14115 result = specSize;
14116 break;
14117 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014118 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014119 }
14120
14121 /**
14122 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014123 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014124 * by the MeasureSpec.
14125 *
14126 * @param size Default size for this view
14127 * @param measureSpec Constraints imposed by the parent
14128 * @return The size this view should be.
14129 */
14130 public static int getDefaultSize(int size, int measureSpec) {
14131 int result = size;
14132 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014133 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014134
14135 switch (specMode) {
14136 case MeasureSpec.UNSPECIFIED:
14137 result = size;
14138 break;
14139 case MeasureSpec.AT_MOST:
14140 case MeasureSpec.EXACTLY:
14141 result = specSize;
14142 break;
14143 }
14144 return result;
14145 }
14146
14147 /**
14148 * Returns the suggested minimum height that the view should use. This
14149 * returns the maximum of the view's minimum height
14150 * and the background's minimum height
14151 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14152 * <p>
14153 * When being used in {@link #onMeasure(int, int)}, the caller should still
14154 * ensure the returned height is within the requirements of the parent.
14155 *
14156 * @return The suggested minimum height of the view.
14157 */
14158 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014159 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014161 }
14162
14163 /**
14164 * Returns the suggested minimum width that the view should use. This
14165 * returns the maximum of the view's minimum width)
14166 * and the background's minimum width
14167 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14168 * <p>
14169 * When being used in {@link #onMeasure(int, int)}, the caller should still
14170 * ensure the returned width is within the requirements of the parent.
14171 *
14172 * @return The suggested minimum width of the view.
14173 */
14174 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014175 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014177
Philip Milne6c8ea062012-04-03 17:38:43 -070014178 /**
14179 * Returns the minimum height of the view.
14180 *
14181 * @return the minimum height the view will try to be.
14182 *
14183 * @see #setMinimumHeight(int)
14184 *
14185 * @attr ref android.R.styleable#View_minHeight
14186 */
14187 public int getMinimumHeight() {
14188 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014189 }
14190
14191 /**
14192 * Sets the minimum height of the view. It is not guaranteed the view will
14193 * be able to achieve this minimum height (for example, if its parent layout
14194 * constrains it with less available height).
14195 *
14196 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014197 *
14198 * @see #getMinimumHeight()
14199 *
14200 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014201 */
14202 public void setMinimumHeight(int minHeight) {
14203 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014204 requestLayout();
14205 }
14206
14207 /**
14208 * Returns the minimum width of the view.
14209 *
14210 * @return the minimum width the view will try to be.
14211 *
14212 * @see #setMinimumWidth(int)
14213 *
14214 * @attr ref android.R.styleable#View_minWidth
14215 */
14216 public int getMinimumWidth() {
14217 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014218 }
14219
14220 /**
14221 * Sets the minimum width of the view. It is not guaranteed the view will
14222 * be able to achieve this minimum width (for example, if its parent layout
14223 * constrains it with less available width).
14224 *
14225 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014226 *
14227 * @see #getMinimumWidth()
14228 *
14229 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014230 */
14231 public void setMinimumWidth(int minWidth) {
14232 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014233 requestLayout();
14234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014235 }
14236
14237 /**
14238 * Get the animation currently associated with this view.
14239 *
14240 * @return The animation that is currently playing or
14241 * scheduled to play for this view.
14242 */
14243 public Animation getAnimation() {
14244 return mCurrentAnimation;
14245 }
14246
14247 /**
14248 * Start the specified animation now.
14249 *
14250 * @param animation the animation to start now
14251 */
14252 public void startAnimation(Animation animation) {
14253 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14254 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014255 invalidateParentCaches();
14256 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014257 }
14258
14259 /**
14260 * Cancels any animations for this view.
14261 */
14262 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014263 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014264 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014266 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014267 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014268 }
14269
14270 /**
14271 * Sets the next animation to play for this view.
14272 * If you want the animation to play immediately, use
14273 * startAnimation. This method provides allows fine-grained
14274 * control over the start time and invalidation, but you
14275 * must make sure that 1) the animation has a start time set, and
14276 * 2) the view will be invalidated when the animation is supposed to
14277 * start.
14278 *
14279 * @param animation The next animation, or null.
14280 */
14281 public void setAnimation(Animation animation) {
14282 mCurrentAnimation = animation;
14283 if (animation != null) {
14284 animation.reset();
14285 }
14286 }
14287
14288 /**
14289 * Invoked by a parent ViewGroup to notify the start of the animation
14290 * currently associated with this view. If you override this method,
14291 * always call super.onAnimationStart();
14292 *
14293 * @see #setAnimation(android.view.animation.Animation)
14294 * @see #getAnimation()
14295 */
14296 protected void onAnimationStart() {
14297 mPrivateFlags |= ANIMATION_STARTED;
14298 }
14299
14300 /**
14301 * Invoked by a parent ViewGroup to notify the end of the animation
14302 * currently associated with this view. If you override this method,
14303 * always call super.onAnimationEnd();
14304 *
14305 * @see #setAnimation(android.view.animation.Animation)
14306 * @see #getAnimation()
14307 */
14308 protected void onAnimationEnd() {
14309 mPrivateFlags &= ~ANIMATION_STARTED;
14310 }
14311
14312 /**
14313 * Invoked if there is a Transform that involves alpha. Subclass that can
14314 * draw themselves with the specified alpha should return true, and then
14315 * respect that alpha when their onDraw() is called. If this returns false
14316 * then the view may be redirected to draw into an offscreen buffer to
14317 * fulfill the request, which will look fine, but may be slower than if the
14318 * subclass handles it internally. The default implementation returns false.
14319 *
14320 * @param alpha The alpha (0..255) to apply to the view's drawing
14321 * @return true if the view can draw with the specified alpha.
14322 */
14323 protected boolean onSetAlpha(int alpha) {
14324 return false;
14325 }
14326
14327 /**
14328 * This is used by the RootView to perform an optimization when
14329 * the view hierarchy contains one or several SurfaceView.
14330 * SurfaceView is always considered transparent, but its children are not,
14331 * therefore all View objects remove themselves from the global transparent
14332 * region (passed as a parameter to this function).
14333 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014334 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014335 *
14336 * @return Returns true if the effective visibility of the view at this
14337 * point is opaque, regardless of the transparent region; returns false
14338 * if it is possible for underlying windows to be seen behind the view.
14339 *
14340 * {@hide}
14341 */
14342 public boolean gatherTransparentRegion(Region region) {
14343 final AttachInfo attachInfo = mAttachInfo;
14344 if (region != null && attachInfo != null) {
14345 final int pflags = mPrivateFlags;
14346 if ((pflags & SKIP_DRAW) == 0) {
14347 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14348 // remove it from the transparent region.
14349 final int[] location = attachInfo.mTransparentLocation;
14350 getLocationInWindow(location);
14351 region.op(location[0], location[1], location[0] + mRight - mLeft,
14352 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014353 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014354 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14355 // exists, so we remove the background drawable's non-transparent
14356 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014357 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014358 }
14359 }
14360 return true;
14361 }
14362
14363 /**
14364 * Play a sound effect for this view.
14365 *
14366 * <p>The framework will play sound effects for some built in actions, such as
14367 * clicking, but you may wish to play these effects in your widget,
14368 * for instance, for internal navigation.
14369 *
14370 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14371 * {@link #isSoundEffectsEnabled()} is true.
14372 *
14373 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
14374 */
14375 public void playSoundEffect(int soundConstant) {
14376 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
14377 return;
14378 }
14379 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
14380 }
14381
14382 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014383 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014384 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014385 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014386 *
14387 * <p>The framework will provide haptic feedback for some built in actions,
14388 * such as long presses, but you may wish to provide feedback for your
14389 * own widget.
14390 *
14391 * <p>The feedback will only be performed if
14392 * {@link #isHapticFeedbackEnabled()} is true.
14393 *
14394 * @param feedbackConstant One of the constants defined in
14395 * {@link HapticFeedbackConstants}
14396 */
14397 public boolean performHapticFeedback(int feedbackConstant) {
14398 return performHapticFeedback(feedbackConstant, 0);
14399 }
14400
14401 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014402 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014403 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014404 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014405 *
14406 * @param feedbackConstant One of the constants defined in
14407 * {@link HapticFeedbackConstants}
14408 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
14409 */
14410 public boolean performHapticFeedback(int feedbackConstant, int flags) {
14411 if (mAttachInfo == null) {
14412 return false;
14413 }
Romain Guyf607bdc2010-09-10 19:20:06 -070014414 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070014415 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014416 && !isHapticFeedbackEnabled()) {
14417 return false;
14418 }
Romain Guy812ccbe2010-06-01 14:07:24 -070014419 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
14420 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014421 }
14422
14423 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014424 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040014425 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14426 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014427 */
14428 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040014429 if (visibility != mSystemUiVisibility) {
14430 mSystemUiVisibility = visibility;
14431 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14432 mParent.recomputeViewAttributes(this);
14433 }
Joe Onorato664644d2011-01-23 17:53:23 -080014434 }
14435 }
14436
14437 /**
14438 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040014439 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14440 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014441 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080014442 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080014443 return mSystemUiVisibility;
14444 }
14445
Scott Mainec6331b2011-05-24 16:55:56 -070014446 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070014447 * Returns the current system UI visibility that is currently set for
14448 * the entire window. This is the combination of the
14449 * {@link #setSystemUiVisibility(int)} values supplied by all of the
14450 * views in the window.
14451 */
14452 public int getWindowSystemUiVisibility() {
14453 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
14454 }
14455
14456 /**
14457 * Override to find out when the window's requested system UI visibility
14458 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
14459 * This is different from the callbacks recieved through
14460 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
14461 * in that this is only telling you about the local request of the window,
14462 * not the actual values applied by the system.
14463 */
14464 public void onWindowSystemUiVisibilityChanged(int visible) {
14465 }
14466
14467 /**
14468 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
14469 * the view hierarchy.
14470 */
14471 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
14472 onWindowSystemUiVisibilityChanged(visible);
14473 }
14474
14475 /**
Scott Mainec6331b2011-05-24 16:55:56 -070014476 * Set a listener to receive callbacks when the visibility of the system bar changes.
14477 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
14478 */
Joe Onorato664644d2011-01-23 17:53:23 -080014479 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014480 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080014481 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14482 mParent.recomputeViewAttributes(this);
14483 }
14484 }
14485
14486 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014487 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
14488 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080014489 */
14490 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014491 ListenerInfo li = mListenerInfo;
14492 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
14493 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080014494 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080014495 }
14496 }
14497
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014498 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
14499 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
14500 if (val != mSystemUiVisibility) {
14501 setSystemUiVisibility(val);
14502 }
14503 }
14504
Joe Onorato664644d2011-01-23 17:53:23 -080014505 /**
Joe Malin32736f02011-01-19 16:14:20 -080014506 * Creates an image that the system displays during the drag and drop
14507 * operation. This is called a &quot;drag shadow&quot;. The default implementation
14508 * for a DragShadowBuilder based on a View returns an image that has exactly the same
14509 * appearance as the given View. The default also positions the center of the drag shadow
14510 * directly under the touch point. If no View is provided (the constructor with no parameters
14511 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
14512 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
14513 * default is an invisible drag shadow.
14514 * <p>
14515 * You are not required to use the View you provide to the constructor as the basis of the
14516 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
14517 * anything you want as the drag shadow.
14518 * </p>
14519 * <p>
14520 * You pass a DragShadowBuilder object to the system when you start the drag. The system
14521 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
14522 * size and position of the drag shadow. It uses this data to construct a
14523 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
14524 * so that your application can draw the shadow image in the Canvas.
14525 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070014526 *
14527 * <div class="special reference">
14528 * <h3>Developer Guides</h3>
14529 * <p>For a guide to implementing drag and drop features, read the
14530 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14531 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070014532 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014533 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070014534 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070014535
14536 /**
Joe Malin32736f02011-01-19 16:14:20 -080014537 * Constructs a shadow image builder based on a View. By default, the resulting drag
14538 * shadow will have the same appearance and dimensions as the View, with the touch point
14539 * over the center of the View.
14540 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070014541 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014542 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070014543 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070014544 }
14545
Christopher Tate17ed60c2011-01-18 12:50:26 -080014546 /**
14547 * Construct a shadow builder object with no associated View. This
14548 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
14549 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
14550 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080014551 * reference to any View object. If they are not overridden, then the result is an
14552 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080014553 */
14554 public DragShadowBuilder() {
14555 mView = new WeakReference<View>(null);
14556 }
14557
14558 /**
14559 * Returns the View object that had been passed to the
14560 * {@link #View.DragShadowBuilder(View)}
14561 * constructor. If that View parameter was {@code null} or if the
14562 * {@link #View.DragShadowBuilder()}
14563 * constructor was used to instantiate the builder object, this method will return
14564 * null.
14565 *
14566 * @return The View object associate with this builder object.
14567 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070014568 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070014569 final public View getView() {
14570 return mView.get();
14571 }
14572
Christopher Tate2c095f32010-10-04 14:13:40 -070014573 /**
Joe Malin32736f02011-01-19 16:14:20 -080014574 * Provides the metrics for the shadow image. These include the dimensions of
14575 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070014576 * be centered under the touch location while dragging.
14577 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014578 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080014579 * same as the dimensions of the View itself and centers the shadow under
14580 * the touch point.
14581 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070014582 *
Joe Malin32736f02011-01-19 16:14:20 -080014583 * @param shadowSize A {@link android.graphics.Point} containing the width and height
14584 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
14585 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
14586 * image.
14587 *
14588 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
14589 * shadow image that should be underneath the touch point during the drag and drop
14590 * operation. Your application must set {@link android.graphics.Point#x} to the
14591 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070014592 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014593 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070014594 final View view = mView.get();
14595 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014596 shadowSize.set(view.getWidth(), view.getHeight());
14597 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070014598 } else {
14599 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
14600 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014601 }
14602
14603 /**
Joe Malin32736f02011-01-19 16:14:20 -080014604 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
14605 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014606 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070014607 *
Joe Malin32736f02011-01-19 16:14:20 -080014608 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070014609 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014610 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070014611 final View view = mView.get();
14612 if (view != null) {
14613 view.draw(canvas);
14614 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014615 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070014616 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014617 }
14618 }
14619
14620 /**
Joe Malin32736f02011-01-19 16:14:20 -080014621 * Starts a drag and drop operation. When your application calls this method, it passes a
14622 * {@link android.view.View.DragShadowBuilder} object to the system. The
14623 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
14624 * to get metrics for the drag shadow, and then calls the object's
14625 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
14626 * <p>
14627 * Once the system has the drag shadow, it begins the drag and drop operation by sending
14628 * drag events to all the View objects in your application that are currently visible. It does
14629 * this either by calling the View object's drag listener (an implementation of
14630 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
14631 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
14632 * Both are passed a {@link android.view.DragEvent} object that has a
14633 * {@link android.view.DragEvent#getAction()} value of
14634 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
14635 * </p>
14636 * <p>
14637 * Your application can invoke startDrag() on any attached View object. The View object does not
14638 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
14639 * be related to the View the user selected for dragging.
14640 * </p>
14641 * @param data A {@link android.content.ClipData} object pointing to the data to be
14642 * transferred by the drag and drop operation.
14643 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
14644 * drag shadow.
14645 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
14646 * drop operation. This Object is put into every DragEvent object sent by the system during the
14647 * current drag.
14648 * <p>
14649 * myLocalState is a lightweight mechanism for the sending information from the dragged View
14650 * to the target Views. For example, it can contain flags that differentiate between a
14651 * a copy operation and a move operation.
14652 * </p>
14653 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
14654 * so the parameter should be set to 0.
14655 * @return {@code true} if the method completes successfully, or
14656 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
14657 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070014658 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014659 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014660 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070014661 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014662 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070014663 }
14664 boolean okay = false;
14665
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014666 Point shadowSize = new Point();
14667 Point shadowTouchPoint = new Point();
14668 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070014669
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014670 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
14671 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
14672 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070014673 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014674
Chris Tatea32dcf72010-10-14 12:13:50 -070014675 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014676 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
14677 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070014678 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014679 Surface surface = new Surface();
14680 try {
14681 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014682 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070014683 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070014684 + " surface=" + surface);
14685 if (token != null) {
14686 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070014687 try {
Chris Tate6b391282010-10-14 15:48:59 -070014688 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014689 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070014690 } finally {
14691 surface.unlockCanvasAndPost(canvas);
14692 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014693
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014694 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080014695
14696 // Cache the local state object for delivery with DragEvents
14697 root.setLocalDragState(myLocalState);
14698
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014699 // repurpose 'shadowSize' for the last touch point
14700 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070014701
Christopher Tatea53146c2010-09-07 11:57:52 -070014702 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014703 shadowSize.x, shadowSize.y,
14704 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070014705 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070014706
14707 // Off and running! Release our local surface instance; the drag
14708 // shadow surface is now managed by the system process.
14709 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070014710 }
14711 } catch (Exception e) {
14712 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
14713 surface.destroy();
14714 }
14715
14716 return okay;
14717 }
14718
Christopher Tatea53146c2010-09-07 11:57:52 -070014719 /**
Joe Malin32736f02011-01-19 16:14:20 -080014720 * Handles drag events sent by the system following a call to
14721 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
14722 *<p>
14723 * When the system calls this method, it passes a
14724 * {@link android.view.DragEvent} object. A call to
14725 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
14726 * in DragEvent. The method uses these to determine what is happening in the drag and drop
14727 * operation.
14728 * @param event The {@link android.view.DragEvent} sent by the system.
14729 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
14730 * in DragEvent, indicating the type of drag event represented by this object.
14731 * @return {@code true} if the method was successful, otherwise {@code false}.
14732 * <p>
14733 * The method should return {@code true} in response to an action type of
14734 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
14735 * operation.
14736 * </p>
14737 * <p>
14738 * The method should also return {@code true} in response to an action type of
14739 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
14740 * {@code false} if it didn't.
14741 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014742 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070014743 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070014744 return false;
14745 }
14746
14747 /**
Joe Malin32736f02011-01-19 16:14:20 -080014748 * Detects if this View is enabled and has a drag event listener.
14749 * If both are true, then it calls the drag event listener with the
14750 * {@link android.view.DragEvent} it received. If the drag event listener returns
14751 * {@code true}, then dispatchDragEvent() returns {@code true}.
14752 * <p>
14753 * For all other cases, the method calls the
14754 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
14755 * method and returns its result.
14756 * </p>
14757 * <p>
14758 * This ensures that a drag event is always consumed, even if the View does not have a drag
14759 * event listener. However, if the View has a listener and the listener returns true, then
14760 * onDragEvent() is not called.
14761 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014762 */
14763 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080014764 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014765 ListenerInfo li = mListenerInfo;
14766 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
14767 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070014768 return true;
14769 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014770 return onDragEvent(event);
14771 }
14772
Christopher Tate3d4bf172011-03-28 16:16:46 -070014773 boolean canAcceptDrag() {
14774 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
14775 }
14776
Christopher Tatea53146c2010-09-07 11:57:52 -070014777 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070014778 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
14779 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070014780 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070014781 */
14782 public void onCloseSystemDialogs(String reason) {
14783 }
Joe Malin32736f02011-01-19 16:14:20 -080014784
Dianne Hackbornffa42482009-09-23 22:20:11 -070014785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014786 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014787 * update a Region being computed for
14788 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014789 * that any non-transparent parts of the Drawable are removed from the
14790 * given transparent region.
14791 *
14792 * @param dr The Drawable whose transparency is to be applied to the region.
14793 * @param region A Region holding the current transparency information,
14794 * where any parts of the region that are set are considered to be
14795 * transparent. On return, this region will be modified to have the
14796 * transparency information reduced by the corresponding parts of the
14797 * Drawable that are not transparent.
14798 * {@hide}
14799 */
14800 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14801 if (DBG) {
14802 Log.i("View", "Getting transparent region for: " + this);
14803 }
14804 final Region r = dr.getTransparentRegion();
14805 final Rect db = dr.getBounds();
14806 final AttachInfo attachInfo = mAttachInfo;
14807 if (r != null && attachInfo != null) {
14808 final int w = getRight()-getLeft();
14809 final int h = getBottom()-getTop();
14810 if (db.left > 0) {
14811 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14812 r.op(0, 0, db.left, h, Region.Op.UNION);
14813 }
14814 if (db.right < w) {
14815 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14816 r.op(db.right, 0, w, h, Region.Op.UNION);
14817 }
14818 if (db.top > 0) {
14819 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14820 r.op(0, 0, w, db.top, Region.Op.UNION);
14821 }
14822 if (db.bottom < h) {
14823 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14824 r.op(0, db.bottom, w, h, Region.Op.UNION);
14825 }
14826 final int[] location = attachInfo.mTransparentLocation;
14827 getLocationInWindow(location);
14828 r.translate(location[0], location[1]);
14829 region.op(r, Region.Op.INTERSECT);
14830 } else {
14831 region.op(db, Region.Op.DIFFERENCE);
14832 }
14833 }
14834
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014835 private void checkForLongClick(int delayOffset) {
14836 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14837 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014838
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014839 if (mPendingCheckForLongPress == null) {
14840 mPendingCheckForLongPress = new CheckForLongPress();
14841 }
14842 mPendingCheckForLongPress.rememberWindowAttachCount();
14843 postDelayed(mPendingCheckForLongPress,
14844 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014846 }
14847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014848 /**
14849 * Inflate a view from an XML resource. This convenience method wraps the {@link
14850 * LayoutInflater} class, which provides a full range of options for view inflation.
14851 *
14852 * @param context The Context object for your activity or application.
14853 * @param resource The resource ID to inflate
14854 * @param root A view group that will be the parent. Used to properly inflate the
14855 * layout_* parameters.
14856 * @see LayoutInflater
14857 */
14858 public static View inflate(Context context, int resource, ViewGroup root) {
14859 LayoutInflater factory = LayoutInflater.from(context);
14860 return factory.inflate(resource, root);
14861 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014863 /**
Adam Powell637d3372010-08-25 14:37:03 -070014864 * Scroll the view with standard behavior for scrolling beyond the normal
14865 * content boundaries. Views that call this method should override
14866 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14867 * results of an over-scroll operation.
14868 *
14869 * Views can use this method to handle any touch or fling-based scrolling.
14870 *
14871 * @param deltaX Change in X in pixels
14872 * @param deltaY Change in Y in pixels
14873 * @param scrollX Current X scroll value in pixels before applying deltaX
14874 * @param scrollY Current Y scroll value in pixels before applying deltaY
14875 * @param scrollRangeX Maximum content scroll range along the X axis
14876 * @param scrollRangeY Maximum content scroll range along the Y axis
14877 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14878 * along the X axis.
14879 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14880 * along the Y axis.
14881 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14882 * @return true if scrolling was clamped to an over-scroll boundary along either
14883 * axis, false otherwise.
14884 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014885 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014886 protected boolean overScrollBy(int deltaX, int deltaY,
14887 int scrollX, int scrollY,
14888 int scrollRangeX, int scrollRangeY,
14889 int maxOverScrollX, int maxOverScrollY,
14890 boolean isTouchEvent) {
14891 final int overScrollMode = mOverScrollMode;
14892 final boolean canScrollHorizontal =
14893 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14894 final boolean canScrollVertical =
14895 computeVerticalScrollRange() > computeVerticalScrollExtent();
14896 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14897 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14898 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14899 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14900
14901 int newScrollX = scrollX + deltaX;
14902 if (!overScrollHorizontal) {
14903 maxOverScrollX = 0;
14904 }
14905
14906 int newScrollY = scrollY + deltaY;
14907 if (!overScrollVertical) {
14908 maxOverScrollY = 0;
14909 }
14910
14911 // Clamp values if at the limits and record
14912 final int left = -maxOverScrollX;
14913 final int right = maxOverScrollX + scrollRangeX;
14914 final int top = -maxOverScrollY;
14915 final int bottom = maxOverScrollY + scrollRangeY;
14916
14917 boolean clampedX = false;
14918 if (newScrollX > right) {
14919 newScrollX = right;
14920 clampedX = true;
14921 } else if (newScrollX < left) {
14922 newScrollX = left;
14923 clampedX = true;
14924 }
14925
14926 boolean clampedY = false;
14927 if (newScrollY > bottom) {
14928 newScrollY = bottom;
14929 clampedY = true;
14930 } else if (newScrollY < top) {
14931 newScrollY = top;
14932 clampedY = true;
14933 }
14934
14935 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14936
14937 return clampedX || clampedY;
14938 }
14939
14940 /**
14941 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14942 * respond to the results of an over-scroll operation.
14943 *
14944 * @param scrollX New X scroll value in pixels
14945 * @param scrollY New Y scroll value in pixels
14946 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14947 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14948 */
14949 protected void onOverScrolled(int scrollX, int scrollY,
14950 boolean clampedX, boolean clampedY) {
14951 // Intentionally empty.
14952 }
14953
14954 /**
14955 * Returns the over-scroll mode for this view. The result will be
14956 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14957 * (allow over-scrolling only if the view content is larger than the container),
14958 * or {@link #OVER_SCROLL_NEVER}.
14959 *
14960 * @return This view's over-scroll mode.
14961 */
14962 public int getOverScrollMode() {
14963 return mOverScrollMode;
14964 }
14965
14966 /**
14967 * Set the over-scroll mode for this view. Valid over-scroll modes are
14968 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14969 * (allow over-scrolling only if the view content is larger than the container),
14970 * or {@link #OVER_SCROLL_NEVER}.
14971 *
14972 * Setting the over-scroll mode of a view will have an effect only if the
14973 * view is capable of scrolling.
14974 *
14975 * @param overScrollMode The new over-scroll mode for this view.
14976 */
14977 public void setOverScrollMode(int overScrollMode) {
14978 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14979 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14980 overScrollMode != OVER_SCROLL_NEVER) {
14981 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14982 }
14983 mOverScrollMode = overScrollMode;
14984 }
14985
14986 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014987 * Gets a scale factor that determines the distance the view should scroll
14988 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14989 * @return The vertical scroll scale factor.
14990 * @hide
14991 */
14992 protected float getVerticalScrollFactor() {
14993 if (mVerticalScrollFactor == 0) {
14994 TypedValue outValue = new TypedValue();
14995 if (!mContext.getTheme().resolveAttribute(
14996 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14997 throw new IllegalStateException(
14998 "Expected theme to define listPreferredItemHeight.");
14999 }
15000 mVerticalScrollFactor = outValue.getDimension(
15001 mContext.getResources().getDisplayMetrics());
15002 }
15003 return mVerticalScrollFactor;
15004 }
15005
15006 /**
15007 * Gets a scale factor that determines the distance the view should scroll
15008 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15009 * @return The horizontal scroll scale factor.
15010 * @hide
15011 */
15012 protected float getHorizontalScrollFactor() {
15013 // TODO: Should use something else.
15014 return getVerticalScrollFactor();
15015 }
15016
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015017 /**
15018 * Return the value specifying the text direction or policy that was set with
15019 * {@link #setTextDirection(int)}.
15020 *
15021 * @return the defined text direction. It can be one of:
15022 *
15023 * {@link #TEXT_DIRECTION_INHERIT},
15024 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15025 * {@link #TEXT_DIRECTION_ANY_RTL},
15026 * {@link #TEXT_DIRECTION_LTR},
15027 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015028 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015029 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015030 @ViewDebug.ExportedProperty(category = "text", mapping = {
15031 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15032 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15033 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15034 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15035 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15036 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15037 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015038 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015039 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015040 }
15041
15042 /**
15043 * Set the text direction.
15044 *
15045 * @param textDirection the direction to set. Should be one of:
15046 *
15047 * {@link #TEXT_DIRECTION_INHERIT},
15048 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15049 * {@link #TEXT_DIRECTION_ANY_RTL},
15050 * {@link #TEXT_DIRECTION_LTR},
15051 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015052 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015053 */
15054 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015055 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015056 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015057 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015058 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015059 // Set the new text direction
15060 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015061 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015062 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015063 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015064 }
15065 }
15066
15067 /**
15068 * Return the resolved text direction.
15069 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015070 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15071 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15072 * up the parent chain of the view. if there is no parent, then it will return the default
15073 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15074 *
15075 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015076 *
Doug Feltcb3791202011-07-07 11:57:48 -070015077 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15078 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015079 * {@link #TEXT_DIRECTION_LTR},
15080 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015081 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015082 */
15083 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015084 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015085 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015086 resolveTextDirection();
15087 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015088 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015089 }
15090
15091 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015092 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15093 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015094 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015095 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015096 // Reset any previous text direction resolution
15097 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15098
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015099 if (hasRtlSupport()) {
15100 // Set resolved text direction flag depending on text direction flag
15101 final int textDirection = getTextDirection();
15102 switch(textDirection) {
15103 case TEXT_DIRECTION_INHERIT:
15104 if (canResolveTextDirection()) {
15105 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015106
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015107 // Set current resolved direction to the same value as the parent's one
15108 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15109 switch (parentResolvedDirection) {
15110 case TEXT_DIRECTION_FIRST_STRONG:
15111 case TEXT_DIRECTION_ANY_RTL:
15112 case TEXT_DIRECTION_LTR:
15113 case TEXT_DIRECTION_RTL:
15114 case TEXT_DIRECTION_LOCALE:
15115 mPrivateFlags2 |=
15116 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15117 break;
15118 default:
15119 // Default resolved direction is "first strong" heuristic
15120 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15121 }
15122 } else {
15123 // We cannot do the resolution if there is no parent, so use the default one
15124 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015125 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015126 break;
15127 case TEXT_DIRECTION_FIRST_STRONG:
15128 case TEXT_DIRECTION_ANY_RTL:
15129 case TEXT_DIRECTION_LTR:
15130 case TEXT_DIRECTION_RTL:
15131 case TEXT_DIRECTION_LOCALE:
15132 // Resolved direction is the same as text direction
15133 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15134 break;
15135 default:
15136 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015137 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015138 }
15139 } else {
15140 // Default resolved direction is "first strong" heuristic
15141 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015142 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015143
15144 // Set to resolved
15145 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015146 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015147 }
15148
15149 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015150 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015151 * resolution should override this method.
15152 *
15153 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015154 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015155 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015156 }
15157
15158 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015159 * Check if text direction resolution can be done.
15160 *
15161 * @return true if text direction resolution can be done otherwise return false.
15162 */
15163 public boolean canResolveTextDirection() {
15164 switch (getTextDirection()) {
15165 case TEXT_DIRECTION_INHERIT:
15166 return (mParent != null) && (mParent instanceof ViewGroup);
15167 default:
15168 return true;
15169 }
15170 }
15171
15172 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015173 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015174 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015175 * reset is done.
15176 */
15177 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015178 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015179 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015180 }
15181
15182 /**
15183 * Called when text direction is reset. Subclasses that care about text direction reset should
15184 * override this method and do a reset of the text direction of their children. The default
15185 * implementation does nothing.
15186 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015187 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015188 }
15189
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015190 /**
15191 * Return the value specifying the text alignment or policy that was set with
15192 * {@link #setTextAlignment(int)}.
15193 *
15194 * @return the defined text alignment. It can be one of:
15195 *
15196 * {@link #TEXT_ALIGNMENT_INHERIT},
15197 * {@link #TEXT_ALIGNMENT_GRAVITY},
15198 * {@link #TEXT_ALIGNMENT_CENTER},
15199 * {@link #TEXT_ALIGNMENT_TEXT_START},
15200 * {@link #TEXT_ALIGNMENT_TEXT_END},
15201 * {@link #TEXT_ALIGNMENT_VIEW_START},
15202 * {@link #TEXT_ALIGNMENT_VIEW_END}
15203 */
15204 @ViewDebug.ExportedProperty(category = "text", mapping = {
15205 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15206 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15207 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15208 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15209 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15210 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15211 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15212 })
15213 public int getTextAlignment() {
15214 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15215 }
15216
15217 /**
15218 * Set the text alignment.
15219 *
15220 * @param textAlignment The text alignment to set. Should be one of
15221 *
15222 * {@link #TEXT_ALIGNMENT_INHERIT},
15223 * {@link #TEXT_ALIGNMENT_GRAVITY},
15224 * {@link #TEXT_ALIGNMENT_CENTER},
15225 * {@link #TEXT_ALIGNMENT_TEXT_START},
15226 * {@link #TEXT_ALIGNMENT_TEXT_END},
15227 * {@link #TEXT_ALIGNMENT_VIEW_START},
15228 * {@link #TEXT_ALIGNMENT_VIEW_END}
15229 *
15230 * @attr ref android.R.styleable#View_textAlignment
15231 */
15232 public void setTextAlignment(int textAlignment) {
15233 if (textAlignment != getTextAlignment()) {
15234 // Reset the current and resolved text alignment
15235 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15236 resetResolvedTextAlignment();
15237 // Set the new text alignment
15238 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15239 // Refresh
15240 requestLayout();
15241 invalidate(true);
15242 }
15243 }
15244
15245 /**
15246 * Return the resolved text alignment.
15247 *
15248 * The resolved text alignment. This needs resolution if the value is
15249 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15250 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15251 *
15252 * @return the resolved text alignment. Returns one of:
15253 *
15254 * {@link #TEXT_ALIGNMENT_GRAVITY},
15255 * {@link #TEXT_ALIGNMENT_CENTER},
15256 * {@link #TEXT_ALIGNMENT_TEXT_START},
15257 * {@link #TEXT_ALIGNMENT_TEXT_END},
15258 * {@link #TEXT_ALIGNMENT_VIEW_START},
15259 * {@link #TEXT_ALIGNMENT_VIEW_END}
15260 */
15261 @ViewDebug.ExportedProperty(category = "text", mapping = {
15262 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15263 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15264 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15265 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15266 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15267 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15268 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15269 })
15270 public int getResolvedTextAlignment() {
15271 // If text alignment is not resolved, then resolve it
15272 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15273 resolveTextAlignment();
15274 }
15275 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15276 }
15277
15278 /**
15279 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15280 * resolution is done.
15281 */
15282 public void resolveTextAlignment() {
15283 // Reset any previous text alignment resolution
15284 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15285
15286 if (hasRtlSupport()) {
15287 // Set resolved text alignment flag depending on text alignment flag
15288 final int textAlignment = getTextAlignment();
15289 switch (textAlignment) {
15290 case TEXT_ALIGNMENT_INHERIT:
15291 // Check if we can resolve the text alignment
15292 if (canResolveLayoutDirection() && mParent instanceof View) {
15293 View view = (View) mParent;
15294
15295 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15296 switch (parentResolvedTextAlignment) {
15297 case TEXT_ALIGNMENT_GRAVITY:
15298 case TEXT_ALIGNMENT_TEXT_START:
15299 case TEXT_ALIGNMENT_TEXT_END:
15300 case TEXT_ALIGNMENT_CENTER:
15301 case TEXT_ALIGNMENT_VIEW_START:
15302 case TEXT_ALIGNMENT_VIEW_END:
15303 // Resolved text alignment is the same as the parent resolved
15304 // text alignment
15305 mPrivateFlags2 |=
15306 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15307 break;
15308 default:
15309 // Use default resolved text alignment
15310 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15311 }
15312 }
15313 else {
15314 // We cannot do the resolution if there is no parent so use the default
15315 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15316 }
15317 break;
15318 case TEXT_ALIGNMENT_GRAVITY:
15319 case TEXT_ALIGNMENT_TEXT_START:
15320 case TEXT_ALIGNMENT_TEXT_END:
15321 case TEXT_ALIGNMENT_CENTER:
15322 case TEXT_ALIGNMENT_VIEW_START:
15323 case TEXT_ALIGNMENT_VIEW_END:
15324 // Resolved text alignment is the same as text alignment
15325 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15326 break;
15327 default:
15328 // Use default resolved text alignment
15329 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15330 }
15331 } else {
15332 // Use default resolved text alignment
15333 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15334 }
15335
15336 // Set the resolved
15337 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
15338 onResolvedTextAlignmentChanged();
15339 }
15340
15341 /**
15342 * Check if text alignment resolution can be done.
15343 *
15344 * @return true if text alignment resolution can be done otherwise return false.
15345 */
15346 public boolean canResolveTextAlignment() {
15347 switch (getTextAlignment()) {
15348 case TEXT_DIRECTION_INHERIT:
15349 return (mParent != null);
15350 default:
15351 return true;
15352 }
15353 }
15354
15355 /**
15356 * Called when text alignment has been resolved. Subclasses that care about text alignment
15357 * resolution should override this method.
15358 *
15359 * The default implementation does nothing.
15360 */
15361 public void onResolvedTextAlignmentChanged() {
15362 }
15363
15364 /**
15365 * Reset resolved text alignment. Text alignment can be resolved with a call to
15366 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
15367 * reset is done.
15368 */
15369 public void resetResolvedTextAlignment() {
15370 // Reset any previous text alignment resolution
15371 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15372 onResolvedTextAlignmentReset();
15373 }
15374
15375 /**
15376 * Called when text alignment is reset. Subclasses that care about text alignment reset should
15377 * override this method and do a reset of the text alignment of their children. The default
15378 * implementation does nothing.
15379 */
15380 public void onResolvedTextAlignmentReset() {
15381 }
15382
Chet Haaseb39f0512011-05-24 14:36:40 -070015383 //
15384 // Properties
15385 //
15386 /**
15387 * A Property wrapper around the <code>alpha</code> functionality handled by the
15388 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
15389 */
Chet Haased47f1532011-12-16 11:18:52 -080015390 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015391 @Override
15392 public void setValue(View object, float value) {
15393 object.setAlpha(value);
15394 }
15395
15396 @Override
15397 public Float get(View object) {
15398 return object.getAlpha();
15399 }
15400 };
15401
15402 /**
15403 * A Property wrapper around the <code>translationX</code> functionality handled by the
15404 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
15405 */
Chet Haased47f1532011-12-16 11:18:52 -080015406 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015407 @Override
15408 public void setValue(View object, float value) {
15409 object.setTranslationX(value);
15410 }
15411
15412 @Override
15413 public Float get(View object) {
15414 return object.getTranslationX();
15415 }
15416 };
15417
15418 /**
15419 * A Property wrapper around the <code>translationY</code> functionality handled by the
15420 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
15421 */
Chet Haased47f1532011-12-16 11:18:52 -080015422 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015423 @Override
15424 public void setValue(View object, float value) {
15425 object.setTranslationY(value);
15426 }
15427
15428 @Override
15429 public Float get(View object) {
15430 return object.getTranslationY();
15431 }
15432 };
15433
15434 /**
15435 * A Property wrapper around the <code>x</code> functionality handled by the
15436 * {@link View#setX(float)} and {@link View#getX()} methods.
15437 */
Chet Haased47f1532011-12-16 11:18:52 -080015438 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015439 @Override
15440 public void setValue(View object, float value) {
15441 object.setX(value);
15442 }
15443
15444 @Override
15445 public Float get(View object) {
15446 return object.getX();
15447 }
15448 };
15449
15450 /**
15451 * A Property wrapper around the <code>y</code> functionality handled by the
15452 * {@link View#setY(float)} and {@link View#getY()} methods.
15453 */
Chet Haased47f1532011-12-16 11:18:52 -080015454 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015455 @Override
15456 public void setValue(View object, float value) {
15457 object.setY(value);
15458 }
15459
15460 @Override
15461 public Float get(View object) {
15462 return object.getY();
15463 }
15464 };
15465
15466 /**
15467 * A Property wrapper around the <code>rotation</code> functionality handled by the
15468 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
15469 */
Chet Haased47f1532011-12-16 11:18:52 -080015470 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015471 @Override
15472 public void setValue(View object, float value) {
15473 object.setRotation(value);
15474 }
15475
15476 @Override
15477 public Float get(View object) {
15478 return object.getRotation();
15479 }
15480 };
15481
15482 /**
15483 * A Property wrapper around the <code>rotationX</code> functionality handled by the
15484 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
15485 */
Chet Haased47f1532011-12-16 11:18:52 -080015486 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015487 @Override
15488 public void setValue(View object, float value) {
15489 object.setRotationX(value);
15490 }
15491
15492 @Override
15493 public Float get(View object) {
15494 return object.getRotationX();
15495 }
15496 };
15497
15498 /**
15499 * A Property wrapper around the <code>rotationY</code> functionality handled by the
15500 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
15501 */
Chet Haased47f1532011-12-16 11:18:52 -080015502 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015503 @Override
15504 public void setValue(View object, float value) {
15505 object.setRotationY(value);
15506 }
15507
15508 @Override
15509 public Float get(View object) {
15510 return object.getRotationY();
15511 }
15512 };
15513
15514 /**
15515 * A Property wrapper around the <code>scaleX</code> functionality handled by the
15516 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
15517 */
Chet Haased47f1532011-12-16 11:18:52 -080015518 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015519 @Override
15520 public void setValue(View object, float value) {
15521 object.setScaleX(value);
15522 }
15523
15524 @Override
15525 public Float get(View object) {
15526 return object.getScaleX();
15527 }
15528 };
15529
15530 /**
15531 * A Property wrapper around the <code>scaleY</code> functionality handled by the
15532 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
15533 */
Chet Haased47f1532011-12-16 11:18:52 -080015534 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015535 @Override
15536 public void setValue(View object, float value) {
15537 object.setScaleY(value);
15538 }
15539
15540 @Override
15541 public Float get(View object) {
15542 return object.getScaleY();
15543 }
15544 };
15545
Jeff Brown33bbfd22011-02-24 20:55:35 -080015546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015547 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
15548 * Each MeasureSpec represents a requirement for either the width or the height.
15549 * A MeasureSpec is comprised of a size and a mode. There are three possible
15550 * modes:
15551 * <dl>
15552 * <dt>UNSPECIFIED</dt>
15553 * <dd>
15554 * The parent has not imposed any constraint on the child. It can be whatever size
15555 * it wants.
15556 * </dd>
15557 *
15558 * <dt>EXACTLY</dt>
15559 * <dd>
15560 * The parent has determined an exact size for the child. The child is going to be
15561 * given those bounds regardless of how big it wants to be.
15562 * </dd>
15563 *
15564 * <dt>AT_MOST</dt>
15565 * <dd>
15566 * The child can be as large as it wants up to the specified size.
15567 * </dd>
15568 * </dl>
15569 *
15570 * MeasureSpecs are implemented as ints to reduce object allocation. This class
15571 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
15572 */
15573 public static class MeasureSpec {
15574 private static final int MODE_SHIFT = 30;
15575 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
15576
15577 /**
15578 * Measure specification mode: The parent has not imposed any constraint
15579 * on the child. It can be whatever size it wants.
15580 */
15581 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
15582
15583 /**
15584 * Measure specification mode: The parent has determined an exact size
15585 * for the child. The child is going to be given those bounds regardless
15586 * of how big it wants to be.
15587 */
15588 public static final int EXACTLY = 1 << MODE_SHIFT;
15589
15590 /**
15591 * Measure specification mode: The child can be as large as it wants up
15592 * to the specified size.
15593 */
15594 public static final int AT_MOST = 2 << MODE_SHIFT;
15595
15596 /**
15597 * Creates a measure specification based on the supplied size and mode.
15598 *
15599 * The mode must always be one of the following:
15600 * <ul>
15601 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
15602 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
15603 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
15604 * </ul>
15605 *
15606 * @param size the size of the measure specification
15607 * @param mode the mode of the measure specification
15608 * @return the measure specification based on size and mode
15609 */
15610 public static int makeMeasureSpec(int size, int mode) {
15611 return size + mode;
15612 }
15613
15614 /**
15615 * Extracts the mode from the supplied measure specification.
15616 *
15617 * @param measureSpec the measure specification to extract the mode from
15618 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
15619 * {@link android.view.View.MeasureSpec#AT_MOST} or
15620 * {@link android.view.View.MeasureSpec#EXACTLY}
15621 */
15622 public static int getMode(int measureSpec) {
15623 return (measureSpec & MODE_MASK);
15624 }
15625
15626 /**
15627 * Extracts the size from the supplied measure specification.
15628 *
15629 * @param measureSpec the measure specification to extract the size from
15630 * @return the size in pixels defined in the supplied measure specification
15631 */
15632 public static int getSize(int measureSpec) {
15633 return (measureSpec & ~MODE_MASK);
15634 }
15635
15636 /**
15637 * Returns a String representation of the specified measure
15638 * specification.
15639 *
15640 * @param measureSpec the measure specification to convert to a String
15641 * @return a String with the following format: "MeasureSpec: MODE SIZE"
15642 */
15643 public static String toString(int measureSpec) {
15644 int mode = getMode(measureSpec);
15645 int size = getSize(measureSpec);
15646
15647 StringBuilder sb = new StringBuilder("MeasureSpec: ");
15648
15649 if (mode == UNSPECIFIED)
15650 sb.append("UNSPECIFIED ");
15651 else if (mode == EXACTLY)
15652 sb.append("EXACTLY ");
15653 else if (mode == AT_MOST)
15654 sb.append("AT_MOST ");
15655 else
15656 sb.append(mode).append(" ");
15657
15658 sb.append(size);
15659 return sb.toString();
15660 }
15661 }
15662
15663 class CheckForLongPress implements Runnable {
15664
15665 private int mOriginalWindowAttachCount;
15666
15667 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070015668 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015669 && mOriginalWindowAttachCount == mWindowAttachCount) {
15670 if (performLongClick()) {
15671 mHasPerformedLongPress = true;
15672 }
15673 }
15674 }
15675
15676 public void rememberWindowAttachCount() {
15677 mOriginalWindowAttachCount = mWindowAttachCount;
15678 }
15679 }
Joe Malin32736f02011-01-19 16:14:20 -080015680
Adam Powelle14579b2009-12-16 18:39:52 -080015681 private final class CheckForTap implements Runnable {
15682 public void run() {
15683 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080015684 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015685 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080015686 }
15687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015688
Adam Powella35d7682010-03-12 14:48:13 -080015689 private final class PerformClick implements Runnable {
15690 public void run() {
15691 performClick();
15692 }
15693 }
15694
Dianne Hackborn63042d62011-01-26 18:56:29 -080015695 /** @hide */
15696 public void hackTurnOffWindowResizeAnim(boolean off) {
15697 mAttachInfo.mTurnOffWindowResizeAnim = off;
15698 }
Joe Malin32736f02011-01-19 16:14:20 -080015699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015700 /**
Chet Haasea00f3862011-02-22 06:34:40 -080015701 * This method returns a ViewPropertyAnimator object, which can be used to animate
15702 * specific properties on this View.
15703 *
15704 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
15705 */
15706 public ViewPropertyAnimator animate() {
15707 if (mAnimator == null) {
15708 mAnimator = new ViewPropertyAnimator(this);
15709 }
15710 return mAnimator;
15711 }
15712
15713 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015714 * Interface definition for a callback to be invoked when a key event is
15715 * dispatched to this view. The callback will be invoked before the key
15716 * event is given to the view.
15717 */
15718 public interface OnKeyListener {
15719 /**
15720 * Called when a key is dispatched to a view. This allows listeners to
15721 * get a chance to respond before the target view.
15722 *
15723 * @param v The view the key has been dispatched to.
15724 * @param keyCode The code for the physical key that was pressed
15725 * @param event The KeyEvent object containing full information about
15726 * the event.
15727 * @return True if the listener has consumed the event, false otherwise.
15728 */
15729 boolean onKey(View v, int keyCode, KeyEvent event);
15730 }
15731
15732 /**
15733 * Interface definition for a callback to be invoked when a touch event is
15734 * dispatched to this view. The callback will be invoked before the touch
15735 * event is given to the view.
15736 */
15737 public interface OnTouchListener {
15738 /**
15739 * Called when a touch event is dispatched to a view. This allows listeners to
15740 * get a chance to respond before the target view.
15741 *
15742 * @param v The view the touch event has been dispatched to.
15743 * @param event The MotionEvent object containing full information about
15744 * the event.
15745 * @return True if the listener has consumed the event, false otherwise.
15746 */
15747 boolean onTouch(View v, MotionEvent event);
15748 }
15749
15750 /**
Jeff Brown10b62902011-06-20 16:40:37 -070015751 * Interface definition for a callback to be invoked when a hover event is
15752 * dispatched to this view. The callback will be invoked before the hover
15753 * event is given to the view.
15754 */
15755 public interface OnHoverListener {
15756 /**
15757 * Called when a hover event is dispatched to a view. This allows listeners to
15758 * get a chance to respond before the target view.
15759 *
15760 * @param v The view the hover event has been dispatched to.
15761 * @param event The MotionEvent object containing full information about
15762 * the event.
15763 * @return True if the listener has consumed the event, false otherwise.
15764 */
15765 boolean onHover(View v, MotionEvent event);
15766 }
15767
15768 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015769 * Interface definition for a callback to be invoked when a generic motion event is
15770 * dispatched to this view. The callback will be invoked before the generic motion
15771 * event is given to the view.
15772 */
15773 public interface OnGenericMotionListener {
15774 /**
15775 * Called when a generic motion event is dispatched to a view. This allows listeners to
15776 * get a chance to respond before the target view.
15777 *
15778 * @param v The view the generic motion event has been dispatched to.
15779 * @param event The MotionEvent object containing full information about
15780 * the event.
15781 * @return True if the listener has consumed the event, false otherwise.
15782 */
15783 boolean onGenericMotion(View v, MotionEvent event);
15784 }
15785
15786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015787 * Interface definition for a callback to be invoked when a view has been clicked and held.
15788 */
15789 public interface OnLongClickListener {
15790 /**
15791 * Called when a view has been clicked and held.
15792 *
15793 * @param v The view that was clicked and held.
15794 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080015795 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015796 */
15797 boolean onLongClick(View v);
15798 }
15799
15800 /**
Chris Tate32affef2010-10-18 15:29:21 -070015801 * Interface definition for a callback to be invoked when a drag is being dispatched
15802 * to this view. The callback will be invoked before the hosting view's own
15803 * onDrag(event) method. If the listener wants to fall back to the hosting view's
15804 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070015805 *
15806 * <div class="special reference">
15807 * <h3>Developer Guides</h3>
15808 * <p>For a guide to implementing drag and drop features, read the
15809 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15810 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070015811 */
15812 public interface OnDragListener {
15813 /**
15814 * Called when a drag event is dispatched to a view. This allows listeners
15815 * to get a chance to override base View behavior.
15816 *
Joe Malin32736f02011-01-19 16:14:20 -080015817 * @param v The View that received the drag event.
15818 * @param event The {@link android.view.DragEvent} object for the drag event.
15819 * @return {@code true} if the drag event was handled successfully, or {@code false}
15820 * if the drag event was not handled. Note that {@code false} will trigger the View
15821 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070015822 */
15823 boolean onDrag(View v, DragEvent event);
15824 }
15825
15826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015827 * Interface definition for a callback to be invoked when the focus state of
15828 * a view changed.
15829 */
15830 public interface OnFocusChangeListener {
15831 /**
15832 * Called when the focus state of a view has changed.
15833 *
15834 * @param v The view whose state has changed.
15835 * @param hasFocus The new focus state of v.
15836 */
15837 void onFocusChange(View v, boolean hasFocus);
15838 }
15839
15840 /**
15841 * Interface definition for a callback to be invoked when a view is clicked.
15842 */
15843 public interface OnClickListener {
15844 /**
15845 * Called when a view has been clicked.
15846 *
15847 * @param v The view that was clicked.
15848 */
15849 void onClick(View v);
15850 }
15851
15852 /**
15853 * Interface definition for a callback to be invoked when the context menu
15854 * for this view is being built.
15855 */
15856 public interface OnCreateContextMenuListener {
15857 /**
15858 * Called when the context menu for this view is being built. It is not
15859 * safe to hold onto the menu after this method returns.
15860 *
15861 * @param menu The context menu that is being built
15862 * @param v The view for which the context menu is being built
15863 * @param menuInfo Extra information about the item for which the
15864 * context menu should be shown. This information will vary
15865 * depending on the class of v.
15866 */
15867 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
15868 }
15869
Joe Onorato664644d2011-01-23 17:53:23 -080015870 /**
15871 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015872 * visibility. This reports <strong>global</strong> changes to the system UI
15873 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080015874 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015875 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080015876 */
15877 public interface OnSystemUiVisibilityChangeListener {
15878 /**
15879 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070015880 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080015881 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040015882 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015883 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
15884 * <strong>global</strong> state of the UI visibility flags, not what your
15885 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080015886 */
15887 public void onSystemUiVisibilityChange(int visibility);
15888 }
15889
Adam Powell4afd62b2011-02-18 15:02:18 -080015890 /**
15891 * Interface definition for a callback to be invoked when this view is attached
15892 * or detached from its window.
15893 */
15894 public interface OnAttachStateChangeListener {
15895 /**
15896 * Called when the view is attached to a window.
15897 * @param v The view that was attached
15898 */
15899 public void onViewAttachedToWindow(View v);
15900 /**
15901 * Called when the view is detached from a window.
15902 * @param v The view that was detached
15903 */
15904 public void onViewDetachedFromWindow(View v);
15905 }
15906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015907 private final class UnsetPressedState implements Runnable {
15908 public void run() {
15909 setPressed(false);
15910 }
15911 }
15912
15913 /**
15914 * Base class for derived classes that want to save and restore their own
15915 * state in {@link android.view.View#onSaveInstanceState()}.
15916 */
15917 public static class BaseSavedState extends AbsSavedState {
15918 /**
15919 * Constructor used when reading from a parcel. Reads the state of the superclass.
15920 *
15921 * @param source
15922 */
15923 public BaseSavedState(Parcel source) {
15924 super(source);
15925 }
15926
15927 /**
15928 * Constructor called by derived classes when creating their SavedState objects
15929 *
15930 * @param superState The state of the superclass of this view
15931 */
15932 public BaseSavedState(Parcelable superState) {
15933 super(superState);
15934 }
15935
15936 public static final Parcelable.Creator<BaseSavedState> CREATOR =
15937 new Parcelable.Creator<BaseSavedState>() {
15938 public BaseSavedState createFromParcel(Parcel in) {
15939 return new BaseSavedState(in);
15940 }
15941
15942 public BaseSavedState[] newArray(int size) {
15943 return new BaseSavedState[size];
15944 }
15945 };
15946 }
15947
15948 /**
15949 * A set of information given to a view when it is attached to its parent
15950 * window.
15951 */
15952 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015953 interface Callbacks {
15954 void playSoundEffect(int effectId);
15955 boolean performHapticFeedback(int effectId, boolean always);
15956 }
15957
15958 /**
15959 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
15960 * to a Handler. This class contains the target (View) to invalidate and
15961 * the coordinates of the dirty rectangle.
15962 *
15963 * For performance purposes, this class also implements a pool of up to
15964 * POOL_LIMIT objects that get reused. This reduces memory allocations
15965 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015966 */
Romain Guyd928d682009-03-31 17:52:16 -070015967 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015968 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070015969 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
15970 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070015971 public InvalidateInfo newInstance() {
15972 return new InvalidateInfo();
15973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015974
Romain Guyd928d682009-03-31 17:52:16 -070015975 public void onAcquired(InvalidateInfo element) {
15976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015977
Romain Guyd928d682009-03-31 17:52:16 -070015978 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070015979 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070015980 }
15981 }, POOL_LIMIT)
15982 );
15983
15984 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015985 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015986
15987 View target;
15988
15989 int left;
15990 int top;
15991 int right;
15992 int bottom;
15993
Romain Guyd928d682009-03-31 17:52:16 -070015994 public void setNextPoolable(InvalidateInfo element) {
15995 mNext = element;
15996 }
15997
15998 public InvalidateInfo getNextPoolable() {
15999 return mNext;
16000 }
16001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016002 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016003 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016004 }
16005
16006 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016007 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016008 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016009
16010 public boolean isPooled() {
16011 return mIsPooled;
16012 }
16013
16014 public void setPooled(boolean isPooled) {
16015 mIsPooled = isPooled;
16016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016017 }
16018
16019 final IWindowSession mSession;
16020
16021 final IWindow mWindow;
16022
16023 final IBinder mWindowToken;
16024
16025 final Callbacks mRootCallbacks;
16026
Romain Guy59a12ca2011-06-09 17:48:21 -070016027 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016029 /**
16030 * The top view of the hierarchy.
16031 */
16032 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016034 IBinder mPanelParentWindowToken;
16035 Surface mSurface;
16036
Romain Guyb051e892010-09-28 19:09:36 -070016037 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016038 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016039 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016040
Romain Guy7e4e5612012-03-05 14:37:29 -080016041 boolean mScreenOn;
16042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016043 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016044 * Scale factor used by the compatibility mode
16045 */
16046 float mApplicationScale;
16047
16048 /**
16049 * Indicates whether the application is in compatibility mode
16050 */
16051 boolean mScalingRequired;
16052
16053 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016054 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016055 */
16056 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016057
Dianne Hackborn63042d62011-01-26 18:56:29 -080016058 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016059 * Left position of this view's window
16060 */
16061 int mWindowLeft;
16062
16063 /**
16064 * Top position of this view's window
16065 */
16066 int mWindowTop;
16067
16068 /**
Adam Powell26153a32010-11-08 15:22:27 -080016069 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016070 */
Adam Powell26153a32010-11-08 15:22:27 -080016071 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016072
16073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016074 * For windows that are full-screen but using insets to layout inside
16075 * of the screen decorations, these are the current insets for the
16076 * content of the window.
16077 */
16078 final Rect mContentInsets = new Rect();
16079
16080 /**
16081 * For windows that are full-screen but using insets to layout inside
16082 * of the screen decorations, these are the current insets for the
16083 * actual visible parts of the window.
16084 */
16085 final Rect mVisibleInsets = new Rect();
16086
16087 /**
16088 * The internal insets given by this window. This value is
16089 * supplied by the client (through
16090 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16091 * be given to the window manager when changed to be used in laying
16092 * out windows behind it.
16093 */
16094 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16095 = new ViewTreeObserver.InternalInsetsInfo();
16096
16097 /**
16098 * All views in the window's hierarchy that serve as scroll containers,
16099 * used to determine if the window can be resized or must be panned
16100 * to adjust for a soft input area.
16101 */
16102 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16103
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016104 final KeyEvent.DispatcherState mKeyDispatchState
16105 = new KeyEvent.DispatcherState();
16106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016107 /**
16108 * Indicates whether the view's window currently has the focus.
16109 */
16110 boolean mHasWindowFocus;
16111
16112 /**
16113 * The current visibility of the window.
16114 */
16115 int mWindowVisibility;
16116
16117 /**
16118 * Indicates the time at which drawing started to occur.
16119 */
16120 long mDrawingTime;
16121
16122 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016123 * Indicates whether or not ignoring the DIRTY_MASK flags.
16124 */
16125 boolean mIgnoreDirtyState;
16126
16127 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016128 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16129 * to avoid clearing that flag prematurely.
16130 */
16131 boolean mSetIgnoreDirtyState = false;
16132
16133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016134 * Indicates whether the view's window is currently in touch mode.
16135 */
16136 boolean mInTouchMode;
16137
16138 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016139 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016140 * the next time it performs a traversal
16141 */
16142 boolean mRecomputeGlobalAttributes;
16143
16144 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016145 * Always report new attributes at next traversal.
16146 */
16147 boolean mForceReportNewAttributes;
16148
16149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016150 * Set during a traveral if any views want to keep the screen on.
16151 */
16152 boolean mKeepScreenOn;
16153
16154 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016155 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16156 */
16157 int mSystemUiVisibility;
16158
16159 /**
16160 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16161 * attached.
16162 */
16163 boolean mHasSystemUiListeners;
16164
16165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016166 * Set if the visibility of any views has changed.
16167 */
16168 boolean mViewVisibilityChanged;
16169
16170 /**
16171 * Set to true if a view has been scrolled.
16172 */
16173 boolean mViewScrollChanged;
16174
16175 /**
16176 * Global to the view hierarchy used as a temporary for dealing with
16177 * x/y points in the transparent region computations.
16178 */
16179 final int[] mTransparentLocation = new int[2];
16180
16181 /**
16182 * Global to the view hierarchy used as a temporary for dealing with
16183 * x/y points in the ViewGroup.invalidateChild implementation.
16184 */
16185 final int[] mInvalidateChildLocation = new int[2];
16186
Chet Haasec3aa3612010-06-17 08:50:37 -070016187
16188 /**
16189 * Global to the view hierarchy used as a temporary for dealing with
16190 * x/y location when view is transformed.
16191 */
16192 final float[] mTmpTransformLocation = new float[2];
16193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016194 /**
16195 * The view tree observer used to dispatch global events like
16196 * layout, pre-draw, touch mode change, etc.
16197 */
16198 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16199
16200 /**
16201 * A Canvas used by the view hierarchy to perform bitmap caching.
16202 */
16203 Canvas mCanvas;
16204
16205 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016206 * The view root impl.
16207 */
16208 final ViewRootImpl mViewRootImpl;
16209
16210 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016211 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016212 * handler can be used to pump events in the UI events queue.
16213 */
16214 final Handler mHandler;
16215
16216 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016217 * Temporary for use in computing invalidate rectangles while
16218 * calling up the hierarchy.
16219 */
16220 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016221
16222 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016223 * Temporary for use in computing hit areas with transformed views
16224 */
16225 final RectF mTmpTransformRect = new RectF();
16226
16227 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016228 * Temporary list for use in collecting focusable descendents of a view.
16229 */
16230 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
16231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016232 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016233 * The id of the window for accessibility purposes.
16234 */
16235 int mAccessibilityWindowId = View.NO_ID;
16236
16237 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016238 * Creates a new set of attachment information with the specified
16239 * events handler and thread.
16240 *
16241 * @param handler the events handler the view must use
16242 */
16243 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016244 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016245 mSession = session;
16246 mWindow = window;
16247 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016248 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016249 mHandler = handler;
16250 mRootCallbacks = effectPlayer;
16251 }
16252 }
16253
16254 /**
16255 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16256 * is supported. This avoids keeping too many unused fields in most
16257 * instances of View.</p>
16258 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016259 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016260
Mike Cleronf116bf82009-09-27 19:14:12 -070016261 /**
16262 * Scrollbars are not visible
16263 */
16264 public static final int OFF = 0;
16265
16266 /**
16267 * Scrollbars are visible
16268 */
16269 public static final int ON = 1;
16270
16271 /**
16272 * Scrollbars are fading away
16273 */
16274 public static final int FADING = 2;
16275
16276 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016278 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016279 public int scrollBarDefaultDelayBeforeFade;
16280 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016281
16282 public int scrollBarSize;
16283 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016284 public float[] interpolatorValues;
16285 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016286
16287 public final Paint paint;
16288 public final Matrix matrix;
16289 public Shader shader;
16290
Mike Cleronf116bf82009-09-27 19:14:12 -070016291 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16292
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016293 private static final float[] OPAQUE = { 255 };
16294 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016295
Mike Cleronf116bf82009-09-27 19:14:12 -070016296 /**
16297 * When fading should start. This time moves into the future every time
16298 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16299 */
16300 public long fadeStartTime;
16301
16302
16303 /**
16304 * The current state of the scrollbars: ON, OFF, or FADING
16305 */
16306 public int state = OFF;
16307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016308 private int mLastColor;
16309
Mike Cleronf116bf82009-09-27 19:14:12 -070016310 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016311 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16312 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016313 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16314 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016315
16316 paint = new Paint();
16317 matrix = new Matrix();
16318 // use use a height of 1, and then wack the matrix each time we
16319 // actually use it.
16320 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016322 paint.setShader(shader);
16323 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070016324 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016325 }
Romain Guy8506ab42009-06-11 17:35:47 -070016326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016327 public void setFadeColor(int color) {
16328 if (color != 0 && color != mLastColor) {
16329 mLastColor = color;
16330 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070016331
Romain Guye55e1a72009-08-27 10:42:26 -070016332 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
16333 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016335 paint.setShader(shader);
16336 // Restore the default transfer mode (src_over)
16337 paint.setXfermode(null);
16338 }
16339 }
Joe Malin32736f02011-01-19 16:14:20 -080016340
Mike Cleronf116bf82009-09-27 19:14:12 -070016341 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070016342 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070016343 if (now >= fadeStartTime) {
16344
16345 // the animation fades the scrollbars out by changing
16346 // the opacity (alpha) from fully opaque to fully
16347 // transparent
16348 int nextFrame = (int) now;
16349 int framesCount = 0;
16350
16351 Interpolator interpolator = scrollBarInterpolator;
16352
16353 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016354 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070016355
16356 // End transparent
16357 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016358 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070016359
16360 state = FADING;
16361
16362 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080016363 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070016364 }
16365 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070016366 }
Mike Cleronf116bf82009-09-27 19:14:12 -070016367
Svetoslav Ganova0156172011-06-26 17:55:44 -070016368 /**
16369 * Resuable callback for sending
16370 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
16371 */
16372 private class SendViewScrolledAccessibilityEvent implements Runnable {
16373 public volatile boolean mIsPending;
16374
16375 public void run() {
16376 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
16377 mIsPending = false;
16378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016379 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016380
16381 /**
16382 * <p>
16383 * This class represents a delegate that can be registered in a {@link View}
16384 * to enhance accessibility support via composition rather via inheritance.
16385 * It is specifically targeted to widget developers that extend basic View
16386 * classes i.e. classes in package android.view, that would like their
16387 * applications to be backwards compatible.
16388 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080016389 * <div class="special reference">
16390 * <h3>Developer Guides</h3>
16391 * <p>For more information about making applications accessible, read the
16392 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
16393 * developer guide.</p>
16394 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016395 * <p>
16396 * A scenario in which a developer would like to use an accessibility delegate
16397 * is overriding a method introduced in a later API version then the minimal API
16398 * version supported by the application. For example, the method
16399 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
16400 * in API version 4 when the accessibility APIs were first introduced. If a
16401 * developer would like his application to run on API version 4 devices (assuming
16402 * all other APIs used by the application are version 4 or lower) and take advantage
16403 * of this method, instead of overriding the method which would break the application's
16404 * backwards compatibility, he can override the corresponding method in this
16405 * delegate and register the delegate in the target View if the API version of
16406 * the system is high enough i.e. the API version is same or higher to the API
16407 * version that introduced
16408 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
16409 * </p>
16410 * <p>
16411 * Here is an example implementation:
16412 * </p>
16413 * <code><pre><p>
16414 * if (Build.VERSION.SDK_INT >= 14) {
16415 * // If the API version is equal of higher than the version in
16416 * // which onInitializeAccessibilityNodeInfo was introduced we
16417 * // register a delegate with a customized implementation.
16418 * View view = findViewById(R.id.view_id);
16419 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
16420 * public void onInitializeAccessibilityNodeInfo(View host,
16421 * AccessibilityNodeInfo info) {
16422 * // Let the default implementation populate the info.
16423 * super.onInitializeAccessibilityNodeInfo(host, info);
16424 * // Set some other information.
16425 * info.setEnabled(host.isEnabled());
16426 * }
16427 * });
16428 * }
16429 * </code></pre></p>
16430 * <p>
16431 * This delegate contains methods that correspond to the accessibility methods
16432 * in View. If a delegate has been specified the implementation in View hands
16433 * off handling to the corresponding method in this delegate. The default
16434 * implementation the delegate methods behaves exactly as the corresponding
16435 * method in View for the case of no accessibility delegate been set. Hence,
16436 * to customize the behavior of a View method, clients can override only the
16437 * corresponding delegate method without altering the behavior of the rest
16438 * accessibility related methods of the host view.
16439 * </p>
16440 */
16441 public static class AccessibilityDelegate {
16442
16443 /**
16444 * Sends an accessibility event of the given type. If accessibility is not
16445 * enabled this method has no effect.
16446 * <p>
16447 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
16448 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
16449 * been set.
16450 * </p>
16451 *
16452 * @param host The View hosting the delegate.
16453 * @param eventType The type of the event to send.
16454 *
16455 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
16456 */
16457 public void sendAccessibilityEvent(View host, int eventType) {
16458 host.sendAccessibilityEventInternal(eventType);
16459 }
16460
16461 /**
16462 * Sends an accessibility event. This method behaves exactly as
16463 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
16464 * empty {@link AccessibilityEvent} and does not perform a check whether
16465 * accessibility is enabled.
16466 * <p>
16467 * The default implementation behaves as
16468 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16469 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
16470 * the case of no accessibility delegate been set.
16471 * </p>
16472 *
16473 * @param host The View hosting the delegate.
16474 * @param event The event to send.
16475 *
16476 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16477 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16478 */
16479 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
16480 host.sendAccessibilityEventUncheckedInternal(event);
16481 }
16482
16483 /**
16484 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
16485 * to its children for adding their text content to the event.
16486 * <p>
16487 * The default implementation behaves as
16488 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16489 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
16490 * the case of no accessibility delegate been set.
16491 * </p>
16492 *
16493 * @param host The View hosting the delegate.
16494 * @param event The event.
16495 * @return True if the event population was completed.
16496 *
16497 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16498 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16499 */
16500 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16501 return host.dispatchPopulateAccessibilityEventInternal(event);
16502 }
16503
16504 /**
16505 * Gives a chance to the host View to populate the accessibility event with its
16506 * text content.
16507 * <p>
16508 * The default implementation behaves as
16509 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
16510 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
16511 * the case of no accessibility delegate been set.
16512 * </p>
16513 *
16514 * @param host The View hosting the delegate.
16515 * @param event The accessibility event which to populate.
16516 *
16517 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
16518 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
16519 */
16520 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16521 host.onPopulateAccessibilityEventInternal(event);
16522 }
16523
16524 /**
16525 * Initializes an {@link AccessibilityEvent} with information about the
16526 * the host View which is the event source.
16527 * <p>
16528 * The default implementation behaves as
16529 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
16530 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
16531 * the case of no accessibility delegate been set.
16532 * </p>
16533 *
16534 * @param host The View hosting the delegate.
16535 * @param event The event to initialize.
16536 *
16537 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
16538 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
16539 */
16540 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
16541 host.onInitializeAccessibilityEventInternal(event);
16542 }
16543
16544 /**
16545 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
16546 * <p>
16547 * The default implementation behaves as
16548 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16549 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
16550 * the case of no accessibility delegate been set.
16551 * </p>
16552 *
16553 * @param host The View hosting the delegate.
16554 * @param info The instance to initialize.
16555 *
16556 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16557 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16558 */
16559 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
16560 host.onInitializeAccessibilityNodeInfoInternal(info);
16561 }
16562
16563 /**
16564 * Called when a child of the host View has requested sending an
16565 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
16566 * to augment the event.
16567 * <p>
16568 * The default implementation behaves as
16569 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16570 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
16571 * the case of no accessibility delegate been set.
16572 * </p>
16573 *
16574 * @param host The View hosting the delegate.
16575 * @param child The child which requests sending the event.
16576 * @param event The event to be sent.
16577 * @return True if the event should be sent
16578 *
16579 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16580 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16581 */
16582 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
16583 AccessibilityEvent event) {
16584 return host.onRequestSendAccessibilityEventInternal(child, event);
16585 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070016586
16587 /**
16588 * Gets the provider for managing a virtual view hierarchy rooted at this View
16589 * and reported to {@link android.accessibilityservice.AccessibilityService}s
16590 * that explore the window content.
16591 * <p>
16592 * The default implementation behaves as
16593 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
16594 * the case of no accessibility delegate been set.
16595 * </p>
16596 *
16597 * @return The provider.
16598 *
16599 * @see AccessibilityNodeProvider
16600 */
16601 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
16602 return null;
16603 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016605}