blob: d34ef3aadccf12d83ab8f66e9012b2867b2923ec [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) {
Svetoslav Ganov5012ebb2012-04-05 17:08:46 -07004062 root.requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
4064 }
4065
4066 /**
4067 * Called internally by the view system when a new view is getting focus.
4068 * This is what clears the old focus.
4069 */
4070 void unFocus() {
4071 if (DBG) {
4072 System.out.println(this + " unFocus()");
4073 }
4074
4075 if ((mPrivateFlags & FOCUSED) != 0) {
4076 mPrivateFlags &= ~FOCUSED;
4077
4078 onFocusChanged(false, 0, null);
4079 refreshDrawableState();
4080 }
4081 }
4082
4083 /**
4084 * Returns true if this view has focus iteself, or is the ancestor of the
4085 * view that has focus.
4086 *
4087 * @return True if this view has or contains focus, false otherwise.
4088 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004089 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 public boolean hasFocus() {
4091 return (mPrivateFlags & FOCUSED) != 0;
4092 }
4093
4094 /**
4095 * Returns true if this view is focusable or if it contains a reachable View
4096 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4097 * is a View whose parents do not block descendants focus.
4098 *
4099 * Only {@link #VISIBLE} views are considered focusable.
4100 *
4101 * @return True if the view is focusable or if the view contains a focusable
4102 * View, false otherwise.
4103 *
4104 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4105 */
4106 public boolean hasFocusable() {
4107 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4108 }
4109
4110 /**
4111 * Called by the view system when the focus state of this view changes.
4112 * When the focus change event is caused by directional navigation, direction
4113 * and previouslyFocusedRect provide insight into where the focus is coming from.
4114 * When overriding, be sure to call up through to the super class so that
4115 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 * @param gainFocus True if the View has focus; false otherwise.
4118 * @param direction The direction focus has moved when requestFocus()
4119 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004120 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4121 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4122 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4124 * system, of the previously focused view. If applicable, this will be
4125 * passed in as finer grained information about where the focus is coming
4126 * from (in addition to direction). Will be <code>null</code> otherwise.
4127 */
4128 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004129 if (gainFocus) {
4130 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4131 }
4132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 InputMethodManager imm = InputMethodManager.peekInstance();
4134 if (!gainFocus) {
4135 if (isPressed()) {
4136 setPressed(false);
4137 }
4138 if (imm != null && mAttachInfo != null
4139 && mAttachInfo.mHasWindowFocus) {
4140 imm.focusOut(this);
4141 }
Romain Guya2431d02009-04-30 16:30:00 -07004142 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 } else if (imm != null && mAttachInfo != null
4144 && mAttachInfo.mHasWindowFocus) {
4145 imm.focusIn(this);
4146 }
Romain Guy8506ab42009-06-11 17:35:47 -07004147
Romain Guy0fd89bf2011-01-26 15:41:30 -08004148 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004149 ListenerInfo li = mListenerInfo;
4150 if (li != null && li.mOnFocusChangeListener != null) {
4151 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 }
Joe Malin32736f02011-01-19 16:14:20 -08004153
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004154 if (mAttachInfo != null) {
4155 mAttachInfo.mKeyDispatchState.reset(this);
4156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 }
4158
4159 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004160 * Sends an accessibility event of the given type. If accessiiblity is
4161 * not enabled this method has no effect. The default implementation calls
4162 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4163 * to populate information about the event source (this View), then calls
4164 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4165 * populate the text content of the event source including its descendants,
4166 * and last calls
4167 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4168 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004169 * <p>
4170 * If an {@link AccessibilityDelegate} has been specified via calling
4171 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4172 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4173 * responsible for handling this call.
4174 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004175 *
Scott Mainb303d832011-10-12 16:45:18 -07004176 * @param eventType The type of the event to send, as defined by several types from
4177 * {@link android.view.accessibility.AccessibilityEvent}, such as
4178 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4179 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004180 *
4181 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4182 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4183 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004184 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004185 */
4186 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004187 if (mAccessibilityDelegate != null) {
4188 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4189 } else {
4190 sendAccessibilityEventInternal(eventType);
4191 }
4192 }
4193
4194 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004195 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4196 * {@link AccessibilityEvent} to make an announcement which is related to some
4197 * sort of a context change for which none of the events representing UI transitions
4198 * is a good fit. For example, announcing a new page in a book. If accessibility
4199 * is not enabled this method does nothing.
4200 *
4201 * @param text The announcement text.
4202 */
4203 public void announceForAccessibility(CharSequence text) {
4204 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4205 AccessibilityEvent event = AccessibilityEvent.obtain(
4206 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4207 event.getText().add(text);
4208 sendAccessibilityEventUnchecked(event);
4209 }
4210 }
4211
4212 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004213 * @see #sendAccessibilityEvent(int)
4214 *
4215 * Note: Called from the default {@link AccessibilityDelegate}.
4216 */
4217 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004218 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4219 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4220 }
4221 }
4222
4223 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004224 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4225 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004226 * perform a check whether accessibility is enabled.
4227 * <p>
4228 * If an {@link AccessibilityDelegate} has been specified via calling
4229 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4230 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4231 * is responsible for handling this call.
4232 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004233 *
4234 * @param event The event to send.
4235 *
4236 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004237 */
4238 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004239 if (mAccessibilityDelegate != null) {
4240 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4241 } else {
4242 sendAccessibilityEventUncheckedInternal(event);
4243 }
4244 }
4245
4246 /**
4247 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4248 *
4249 * Note: Called from the default {@link AccessibilityDelegate}.
4250 */
4251 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004252 if (!isShown()) {
4253 return;
4254 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004255 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004256 // Only a subset of accessibility events populates text content.
4257 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4258 dispatchPopulateAccessibilityEvent(event);
4259 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004260 // In the beginning we called #isShown(), so we know that getParent() is not null.
4261 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004262 }
4263
4264 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004265 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4266 * to its children for adding their text content to the event. Note that the
4267 * event text is populated in a separate dispatch path since we add to the
4268 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004269 * A typical implementation will call
4270 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4271 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4272 * on each child. Override this method if custom population of the event text
4273 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004274 * <p>
4275 * If an {@link AccessibilityDelegate} has been specified via calling
4276 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4277 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4278 * is responsible for handling this call.
4279 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004280 * <p>
4281 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4282 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4283 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004284 *
4285 * @param event The event.
4286 *
4287 * @return True if the event population was completed.
4288 */
4289 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004290 if (mAccessibilityDelegate != null) {
4291 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4292 } else {
4293 return dispatchPopulateAccessibilityEventInternal(event);
4294 }
4295 }
4296
4297 /**
4298 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4299 *
4300 * Note: Called from the default {@link AccessibilityDelegate}.
4301 */
4302 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004303 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004304 return false;
4305 }
4306
4307 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004308 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004309 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004310 * text content. While this method is free to modify event
4311 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004312 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4313 * <p>
4314 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004315 * to the text added by the super implementation:
4316 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004317 * super.onPopulateAccessibilityEvent(event);
4318 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4319 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4320 * mCurrentDate.getTimeInMillis(), flags);
4321 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004322 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004323 * <p>
4324 * If an {@link AccessibilityDelegate} has been specified via calling
4325 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4326 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4327 * is responsible for handling this call.
4328 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004329 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4330 * information to the event, in case the default implementation has basic information to add.
4331 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004332 *
4333 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004334 *
4335 * @see #sendAccessibilityEvent(int)
4336 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004337 */
4338 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004339 if (mAccessibilityDelegate != null) {
4340 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4341 } else {
4342 onPopulateAccessibilityEventInternal(event);
4343 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004344 }
4345
4346 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004347 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4348 *
4349 * Note: Called from the default {@link AccessibilityDelegate}.
4350 */
4351 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4352
4353 }
4354
4355 /**
4356 * Initializes an {@link AccessibilityEvent} with information about
4357 * this View which is the event source. In other words, the source of
4358 * an accessibility event is the view whose state change triggered firing
4359 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004360 * <p>
4361 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004362 * to properties set by the super implementation:
4363 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4364 * super.onInitializeAccessibilityEvent(event);
4365 * event.setPassword(true);
4366 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004367 * <p>
4368 * If an {@link AccessibilityDelegate} has been specified via calling
4369 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4370 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4371 * is responsible for handling this call.
4372 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004373 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4374 * information to the event, in case the default implementation has basic information to add.
4375 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004376 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004377 *
4378 * @see #sendAccessibilityEvent(int)
4379 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4380 */
4381 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004382 if (mAccessibilityDelegate != null) {
4383 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4384 } else {
4385 onInitializeAccessibilityEventInternal(event);
4386 }
4387 }
4388
4389 /**
4390 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4391 *
4392 * Note: Called from the default {@link AccessibilityDelegate}.
4393 */
4394 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004395 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004396 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004397 event.setPackageName(getContext().getPackageName());
4398 event.setEnabled(isEnabled());
4399 event.setContentDescription(mContentDescription);
4400
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004401 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4402 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4403 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4404 FOCUSABLES_ALL);
4405 event.setItemCount(focusablesTempList.size());
4406 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4407 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004408 }
4409 }
4410
4411 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004412 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4413 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4414 * This method is responsible for obtaining an accessibility node info from a
4415 * pool of reusable instances and calling
4416 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4417 * initialize the former.
4418 * <p>
4419 * Note: The client is responsible for recycling the obtained instance by calling
4420 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4421 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004422 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004423 * @return A populated {@link AccessibilityNodeInfo}.
4424 *
4425 * @see AccessibilityNodeInfo
4426 */
4427 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004428 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4429 if (provider != null) {
4430 return provider.createAccessibilityNodeInfo(View.NO_ID);
4431 } else {
4432 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4433 onInitializeAccessibilityNodeInfo(info);
4434 return info;
4435 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004436 }
4437
4438 /**
4439 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4440 * The base implementation sets:
4441 * <ul>
4442 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004443 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4444 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004445 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4446 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4447 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4448 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4449 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4450 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4451 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4452 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4453 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4454 * </ul>
4455 * <p>
4456 * Subclasses should override this method, call the super implementation,
4457 * and set additional attributes.
4458 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004459 * <p>
4460 * If an {@link AccessibilityDelegate} has been specified via calling
4461 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4462 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4463 * is responsible for handling this call.
4464 * </p>
4465 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004466 * @param info The instance to initialize.
4467 */
4468 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004469 if (mAccessibilityDelegate != null) {
4470 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4471 } else {
4472 onInitializeAccessibilityNodeInfoInternal(info);
4473 }
4474 }
4475
4476 /**
4477 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4478 *
4479 * Note: Called from the default {@link AccessibilityDelegate}.
4480 */
4481 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004482 Rect bounds = mAttachInfo.mTmpInvalRect;
4483 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004484 info.setBoundsInParent(bounds);
4485
4486 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4487 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004488 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004489 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4490 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004491
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004492 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4493 ViewParent parent = getParent();
4494 if (parent instanceof View) {
4495 View parentView = (View) parent;
4496 info.setParent(parentView);
4497 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004498 }
4499
4500 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004501 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004502 info.setContentDescription(getContentDescription());
4503
4504 info.setEnabled(isEnabled());
4505 info.setClickable(isClickable());
4506 info.setFocusable(isFocusable());
4507 info.setFocused(isFocused());
4508 info.setSelected(isSelected());
4509 info.setLongClickable(isLongClickable());
4510
4511 // TODO: These make sense only if we are in an AdapterView but all
4512 // views can be selected. Maybe from accessiiblity perspective
4513 // we should report as selectable view in an AdapterView.
4514 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4515 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4516
4517 if (isFocusable()) {
4518 if (isFocused()) {
4519 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4520 } else {
4521 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4522 }
4523 }
4524 }
4525
4526 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004527 * Sets a delegate for implementing accessibility support via compositon as
4528 * opposed to inheritance. The delegate's primary use is for implementing
4529 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4530 *
4531 * @param delegate The delegate instance.
4532 *
4533 * @see AccessibilityDelegate
4534 */
4535 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4536 mAccessibilityDelegate = delegate;
4537 }
4538
4539 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004540 * Gets the provider for managing a virtual view hierarchy rooted at this View
4541 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4542 * that explore the window content.
4543 * <p>
4544 * If this method returns an instance, this instance is responsible for managing
4545 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4546 * View including the one representing the View itself. Similarly the returned
4547 * instance is responsible for performing accessibility actions on any virtual
4548 * view or the root view itself.
4549 * </p>
4550 * <p>
4551 * If an {@link AccessibilityDelegate} has been specified via calling
4552 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4553 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4554 * is responsible for handling this call.
4555 * </p>
4556 *
4557 * @return The provider.
4558 *
4559 * @see AccessibilityNodeProvider
4560 */
4561 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4562 if (mAccessibilityDelegate != null) {
4563 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4564 } else {
4565 return null;
4566 }
4567 }
4568
4569 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004570 * Gets the unique identifier of this view on the screen for accessibility purposes.
4571 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4572 *
4573 * @return The view accessibility id.
4574 *
4575 * @hide
4576 */
4577 public int getAccessibilityViewId() {
4578 if (mAccessibilityViewId == NO_ID) {
4579 mAccessibilityViewId = sNextAccessibilityViewId++;
4580 }
4581 return mAccessibilityViewId;
4582 }
4583
4584 /**
4585 * Gets the unique identifier of the window in which this View reseides.
4586 *
4587 * @return The window accessibility id.
4588 *
4589 * @hide
4590 */
4591 public int getAccessibilityWindowId() {
4592 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4593 }
4594
4595 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004596 * Gets the {@link View} description. It briefly describes the view and is
4597 * primarily used for accessibility support. Set this property to enable
4598 * better accessibility support for your application. This is especially
4599 * true for views that do not have textual representation (For example,
4600 * ImageButton).
4601 *
4602 * @return The content descriptiopn.
4603 *
4604 * @attr ref android.R.styleable#View_contentDescription
4605 */
4606 public CharSequence getContentDescription() {
4607 return mContentDescription;
4608 }
4609
4610 /**
4611 * Sets the {@link View} description. It briefly describes the view and is
4612 * primarily used for accessibility support. Set this property to enable
4613 * better accessibility support for your application. This is especially
4614 * true for views that do not have textual representation (For example,
4615 * ImageButton).
4616 *
4617 * @param contentDescription The content description.
4618 *
4619 * @attr ref android.R.styleable#View_contentDescription
4620 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004621 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004622 public void setContentDescription(CharSequence contentDescription) {
4623 mContentDescription = contentDescription;
4624 }
4625
4626 /**
Romain Guya2431d02009-04-30 16:30:00 -07004627 * Invoked whenever this view loses focus, either by losing window focus or by losing
4628 * focus within its window. This method can be used to clear any state tied to the
4629 * focus. For instance, if a button is held pressed with the trackball and the window
4630 * loses focus, this method can be used to cancel the press.
4631 *
4632 * Subclasses of View overriding this method should always call super.onFocusLost().
4633 *
4634 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004635 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004636 *
4637 * @hide pending API council approval
4638 */
4639 protected void onFocusLost() {
4640 resetPressedState();
4641 }
4642
4643 private void resetPressedState() {
4644 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4645 return;
4646 }
4647
4648 if (isPressed()) {
4649 setPressed(false);
4650
4651 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004652 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004653 }
4654 }
4655 }
4656
4657 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 * Returns true if this view has focus
4659 *
4660 * @return True if this view has focus, false otherwise.
4661 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004662 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 public boolean isFocused() {
4664 return (mPrivateFlags & FOCUSED) != 0;
4665 }
4666
4667 /**
4668 * Find the view in the hierarchy rooted at this view that currently has
4669 * focus.
4670 *
4671 * @return The view that currently has focus, or null if no focused view can
4672 * be found.
4673 */
4674 public View findFocus() {
4675 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4676 }
4677
4678 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004679 * Indicates whether this view is one of the set of scrollable containers in
4680 * its window.
4681 *
4682 * @return whether this view is one of the set of scrollable containers in
4683 * its window
4684 *
4685 * @attr ref android.R.styleable#View_isScrollContainer
4686 */
4687 public boolean isScrollContainer() {
4688 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4689 }
4690
4691 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 * Change whether this view is one of the set of scrollable containers in
4693 * its window. This will be used to determine whether the window can
4694 * resize or must pan when a soft input area is open -- scrollable
4695 * containers allow the window to use resize mode since the container
4696 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004697 *
4698 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 */
4700 public void setScrollContainer(boolean isScrollContainer) {
4701 if (isScrollContainer) {
4702 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4703 mAttachInfo.mScrollContainers.add(this);
4704 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4705 }
4706 mPrivateFlags |= SCROLL_CONTAINER;
4707 } else {
4708 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4709 mAttachInfo.mScrollContainers.remove(this);
4710 }
4711 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4712 }
4713 }
4714
4715 /**
4716 * Returns the quality of the drawing cache.
4717 *
4718 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4719 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4720 *
4721 * @see #setDrawingCacheQuality(int)
4722 * @see #setDrawingCacheEnabled(boolean)
4723 * @see #isDrawingCacheEnabled()
4724 *
4725 * @attr ref android.R.styleable#View_drawingCacheQuality
4726 */
4727 public int getDrawingCacheQuality() {
4728 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4729 }
4730
4731 /**
4732 * Set the drawing cache quality of this view. This value is used only when the
4733 * drawing cache is enabled
4734 *
4735 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4736 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4737 *
4738 * @see #getDrawingCacheQuality()
4739 * @see #setDrawingCacheEnabled(boolean)
4740 * @see #isDrawingCacheEnabled()
4741 *
4742 * @attr ref android.R.styleable#View_drawingCacheQuality
4743 */
4744 public void setDrawingCacheQuality(int quality) {
4745 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4746 }
4747
4748 /**
4749 * Returns whether the screen should remain on, corresponding to the current
4750 * value of {@link #KEEP_SCREEN_ON}.
4751 *
4752 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4753 *
4754 * @see #setKeepScreenOn(boolean)
4755 *
4756 * @attr ref android.R.styleable#View_keepScreenOn
4757 */
4758 public boolean getKeepScreenOn() {
4759 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4760 }
4761
4762 /**
4763 * Controls whether the screen should remain on, modifying the
4764 * value of {@link #KEEP_SCREEN_ON}.
4765 *
4766 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4767 *
4768 * @see #getKeepScreenOn()
4769 *
4770 * @attr ref android.R.styleable#View_keepScreenOn
4771 */
4772 public void setKeepScreenOn(boolean keepScreenOn) {
4773 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4774 }
4775
4776 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004777 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4778 * @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 -08004779 *
4780 * @attr ref android.R.styleable#View_nextFocusLeft
4781 */
4782 public int getNextFocusLeftId() {
4783 return mNextFocusLeftId;
4784 }
4785
4786 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004787 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4788 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4789 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 *
4791 * @attr ref android.R.styleable#View_nextFocusLeft
4792 */
4793 public void setNextFocusLeftId(int nextFocusLeftId) {
4794 mNextFocusLeftId = nextFocusLeftId;
4795 }
4796
4797 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004798 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4799 * @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 -08004800 *
4801 * @attr ref android.R.styleable#View_nextFocusRight
4802 */
4803 public int getNextFocusRightId() {
4804 return mNextFocusRightId;
4805 }
4806
4807 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004808 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4809 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4810 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004811 *
4812 * @attr ref android.R.styleable#View_nextFocusRight
4813 */
4814 public void setNextFocusRightId(int nextFocusRightId) {
4815 mNextFocusRightId = nextFocusRightId;
4816 }
4817
4818 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004819 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4820 * @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 -08004821 *
4822 * @attr ref android.R.styleable#View_nextFocusUp
4823 */
4824 public int getNextFocusUpId() {
4825 return mNextFocusUpId;
4826 }
4827
4828 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004829 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4830 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4831 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 *
4833 * @attr ref android.R.styleable#View_nextFocusUp
4834 */
4835 public void setNextFocusUpId(int nextFocusUpId) {
4836 mNextFocusUpId = nextFocusUpId;
4837 }
4838
4839 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004840 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4841 * @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 -08004842 *
4843 * @attr ref android.R.styleable#View_nextFocusDown
4844 */
4845 public int getNextFocusDownId() {
4846 return mNextFocusDownId;
4847 }
4848
4849 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004850 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4851 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4852 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 *
4854 * @attr ref android.R.styleable#View_nextFocusDown
4855 */
4856 public void setNextFocusDownId(int nextFocusDownId) {
4857 mNextFocusDownId = nextFocusDownId;
4858 }
4859
4860 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004861 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4862 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4863 *
4864 * @attr ref android.R.styleable#View_nextFocusForward
4865 */
4866 public int getNextFocusForwardId() {
4867 return mNextFocusForwardId;
4868 }
4869
4870 /**
4871 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4872 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4873 * decide automatically.
4874 *
4875 * @attr ref android.R.styleable#View_nextFocusForward
4876 */
4877 public void setNextFocusForwardId(int nextFocusForwardId) {
4878 mNextFocusForwardId = nextFocusForwardId;
4879 }
4880
4881 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 * Returns the visibility of this view and all of its ancestors
4883 *
4884 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4885 */
4886 public boolean isShown() {
4887 View current = this;
4888 //noinspection ConstantConditions
4889 do {
4890 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4891 return false;
4892 }
4893 ViewParent parent = current.mParent;
4894 if (parent == null) {
4895 return false; // We are not attached to the view root
4896 }
4897 if (!(parent instanceof View)) {
4898 return true;
4899 }
4900 current = (View) parent;
4901 } while (current != null);
4902
4903 return false;
4904 }
4905
4906 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004907 * Called by the view hierarchy when the content insets for a window have
4908 * changed, to allow it to adjust its content to fit within those windows.
4909 * The content insets tell you the space that the status bar, input method,
4910 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004912 * <p>You do not normally need to deal with this function, since the default
4913 * window decoration given to applications takes care of applying it to the
4914 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
4915 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
4916 * and your content can be placed under those system elements. You can then
4917 * use this method within your view hierarchy if you have parts of your UI
4918 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004920 * <p>The default implementation of this method simply applies the content
4921 * inset's to the view's padding. This can be enabled through
4922 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
4923 * the method and handle the insets however you would like. Note that the
4924 * insets provided by the framework are always relative to the far edges
4925 * of the window, not accounting for the location of the called view within
4926 * that window. (In fact when this method is called you do not yet know
4927 * where the layout will place the view, as it is done before layout happens.)
4928 *
4929 * <p>Note: unlike many View methods, there is no dispatch phase to this
4930 * call. If you are overriding it in a ViewGroup and want to allow the
4931 * call to continue to your children, you must be sure to call the super
4932 * implementation.
4933 *
4934 * @param insets Current content insets of the window. Prior to
4935 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
4936 * the insets or else you and Android will be unhappy.
4937 *
4938 * @return Return true if this view applied the insets and it should not
4939 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 */
4941 protected boolean fitSystemWindows(Rect insets) {
4942 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004943 mUserPaddingStart = -1;
4944 mUserPaddingEnd = -1;
4945 mUserPaddingRelative = false;
4946 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
4947 || mAttachInfo == null
4948 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
4949 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
4950 return true;
4951 } else {
4952 internalSetPadding(0, 0, 0, 0);
4953 return false;
4954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 }
4956 return false;
4957 }
4958
4959 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004960 * Set whether or not this view should account for system screen decorations
4961 * such as the status bar and inset its content. This allows this view to be
4962 * positioned in absolute screen coordinates and remain visible to the user.
4963 *
4964 * <p>This should only be used by top-level window decor views.
4965 *
4966 * @param fitSystemWindows true to inset content for system screen decorations, false for
4967 * default behavior.
4968 *
4969 * @attr ref android.R.styleable#View_fitsSystemWindows
4970 */
4971 public void setFitsSystemWindows(boolean fitSystemWindows) {
4972 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4973 }
4974
4975 /**
4976 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4977 * will account for system screen decorations such as the status bar and inset its
4978 * content. This allows the view to be positioned in absolute screen coordinates
4979 * and remain visible to the user.
4980 *
4981 * @return true if this view will adjust its content bounds for system screen decorations.
4982 *
4983 * @attr ref android.R.styleable#View_fitsSystemWindows
4984 */
4985 public boolean fitsSystemWindows() {
4986 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4987 }
4988
4989 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004990 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
4991 */
4992 public void requestFitSystemWindows() {
4993 if (mParent != null) {
4994 mParent.requestFitSystemWindows();
4995 }
4996 }
4997
4998 /**
4999 * For use by PhoneWindow to make its own system window fitting optional.
5000 * @hide
5001 */
5002 public void makeOptionalFitsSystemWindows() {
5003 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5004 }
5005
5006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 * Returns the visibility status for this view.
5008 *
5009 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5010 * @attr ref android.R.styleable#View_visibility
5011 */
5012 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005013 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5014 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5015 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 })
5017 public int getVisibility() {
5018 return mViewFlags & VISIBILITY_MASK;
5019 }
5020
5021 /**
5022 * Set the enabled state of this view.
5023 *
5024 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5025 * @attr ref android.R.styleable#View_visibility
5026 */
5027 @RemotableViewMethod
5028 public void setVisibility(int visibility) {
5029 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005030 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 }
5032
5033 /**
5034 * Returns the enabled status for this view. The interpretation of the
5035 * enabled state varies by subclass.
5036 *
5037 * @return True if this view is enabled, false otherwise.
5038 */
5039 @ViewDebug.ExportedProperty
5040 public boolean isEnabled() {
5041 return (mViewFlags & ENABLED_MASK) == ENABLED;
5042 }
5043
5044 /**
5045 * Set the enabled state of this view. The interpretation of the enabled
5046 * state varies by subclass.
5047 *
5048 * @param enabled True if this view is enabled, false otherwise.
5049 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005050 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005052 if (enabled == isEnabled()) return;
5053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5055
5056 /*
5057 * The View most likely has to change its appearance, so refresh
5058 * the drawable state.
5059 */
5060 refreshDrawableState();
5061
5062 // Invalidate too, since the default behavior for views is to be
5063 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005064 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 }
5066
5067 /**
5068 * Set whether this view can receive the focus.
5069 *
5070 * Setting this to false will also ensure that this view is not focusable
5071 * in touch mode.
5072 *
5073 * @param focusable If true, this view can receive the focus.
5074 *
5075 * @see #setFocusableInTouchMode(boolean)
5076 * @attr ref android.R.styleable#View_focusable
5077 */
5078 public void setFocusable(boolean focusable) {
5079 if (!focusable) {
5080 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5081 }
5082 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5083 }
5084
5085 /**
5086 * Set whether this view can receive focus while in touch mode.
5087 *
5088 * Setting this to true will also ensure that this view is focusable.
5089 *
5090 * @param focusableInTouchMode If true, this view can receive the focus while
5091 * in touch mode.
5092 *
5093 * @see #setFocusable(boolean)
5094 * @attr ref android.R.styleable#View_focusableInTouchMode
5095 */
5096 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5097 // Focusable in touch mode should always be set before the focusable flag
5098 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5099 // which, in touch mode, will not successfully request focus on this view
5100 // because the focusable in touch mode flag is not set
5101 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5102 if (focusableInTouchMode) {
5103 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5104 }
5105 }
5106
5107 /**
5108 * Set whether this view should have sound effects enabled for events such as
5109 * clicking and touching.
5110 *
5111 * <p>You may wish to disable sound effects for a view if you already play sounds,
5112 * for instance, a dial key that plays dtmf tones.
5113 *
5114 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5115 * @see #isSoundEffectsEnabled()
5116 * @see #playSoundEffect(int)
5117 * @attr ref android.R.styleable#View_soundEffectsEnabled
5118 */
5119 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5120 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5121 }
5122
5123 /**
5124 * @return whether this view should have sound effects enabled for events such as
5125 * clicking and touching.
5126 *
5127 * @see #setSoundEffectsEnabled(boolean)
5128 * @see #playSoundEffect(int)
5129 * @attr ref android.R.styleable#View_soundEffectsEnabled
5130 */
5131 @ViewDebug.ExportedProperty
5132 public boolean isSoundEffectsEnabled() {
5133 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5134 }
5135
5136 /**
5137 * Set whether this view should have haptic feedback for events such as
5138 * long presses.
5139 *
5140 * <p>You may wish to disable haptic feedback if your view already controls
5141 * its own haptic feedback.
5142 *
5143 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5144 * @see #isHapticFeedbackEnabled()
5145 * @see #performHapticFeedback(int)
5146 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5147 */
5148 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5149 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5150 }
5151
5152 /**
5153 * @return whether this view should have haptic feedback enabled for events
5154 * long presses.
5155 *
5156 * @see #setHapticFeedbackEnabled(boolean)
5157 * @see #performHapticFeedback(int)
5158 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5159 */
5160 @ViewDebug.ExportedProperty
5161 public boolean isHapticFeedbackEnabled() {
5162 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5163 }
5164
5165 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005166 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005167 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005168 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5169 * {@link #LAYOUT_DIRECTION_RTL},
5170 * {@link #LAYOUT_DIRECTION_INHERIT} or
5171 * {@link #LAYOUT_DIRECTION_LOCALE}.
5172 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005173 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005174 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005175 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5176 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5177 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5178 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005179 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005180 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005181 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005182 }
5183
5184 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005185 * Set the layout direction for this view. This will propagate a reset of layout direction
5186 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005187 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005188 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5189 * {@link #LAYOUT_DIRECTION_RTL},
5190 * {@link #LAYOUT_DIRECTION_INHERIT} or
5191 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005192 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005193 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005194 */
5195 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005196 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005197 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005198 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005199 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005200 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005201 // Set the new layout direction (filtered) and ask for a layout pass
5202 mPrivateFlags2 |=
5203 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5204 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005205 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005206 }
5207
5208 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005209 * Returns the resolved layout direction for this view.
5210 *
5211 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005212 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005213 */
5214 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005215 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5216 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005217 })
5218 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005219 // The layout diretion will be resolved only if needed
5220 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5221 resolveLayoutDirection();
5222 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005223 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005224 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5225 }
5226
5227 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005228 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5229 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005230 *
5231 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005232 */
5233 @ViewDebug.ExportedProperty(category = "layout")
5234 public boolean isLayoutRtl() {
5235 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5236 }
5237
5238 /**
Adam Powell539ee872012-02-03 19:00:49 -08005239 * Indicates whether the view is currently tracking transient state that the
5240 * app should not need to concern itself with saving and restoring, but that
5241 * the framework should take special note to preserve when possible.
5242 *
5243 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005244 */
5245 @ViewDebug.ExportedProperty(category = "layout")
5246 public boolean hasTransientState() {
5247 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5248 }
5249
5250 /**
5251 * Set whether this view is currently tracking transient state that the
5252 * framework should attempt to preserve when possible.
5253 *
5254 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005255 */
5256 public void setHasTransientState(boolean hasTransientState) {
5257 if (hasTransientState() == hasTransientState) return;
5258
5259 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5260 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5261 if (mParent != null) {
5262 try {
5263 mParent.childHasTransientStateChanged(this, hasTransientState);
5264 } catch (AbstractMethodError e) {
5265 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5266 " does not fully implement ViewParent", e);
5267 }
5268 }
5269 }
5270
5271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 * If this view doesn't do any drawing on its own, set this flag to
5273 * allow further optimizations. By default, this flag is not set on
5274 * View, but could be set on some View subclasses such as ViewGroup.
5275 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005276 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5277 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005278 *
5279 * @param willNotDraw whether or not this View draw on its own
5280 */
5281 public void setWillNotDraw(boolean willNotDraw) {
5282 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5283 }
5284
5285 /**
5286 * Returns whether or not this View draws on its own.
5287 *
5288 * @return true if this view has nothing to draw, false otherwise
5289 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005290 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 public boolean willNotDraw() {
5292 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5293 }
5294
5295 /**
5296 * When a View's drawing cache is enabled, drawing is redirected to an
5297 * offscreen bitmap. Some views, like an ImageView, must be able to
5298 * bypass this mechanism if they already draw a single bitmap, to avoid
5299 * unnecessary usage of the memory.
5300 *
5301 * @param willNotCacheDrawing true if this view does not cache its
5302 * drawing, false otherwise
5303 */
5304 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5305 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5306 }
5307
5308 /**
5309 * Returns whether or not this View can cache its drawing or not.
5310 *
5311 * @return true if this view does not cache its drawing, false otherwise
5312 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005313 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 public boolean willNotCacheDrawing() {
5315 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5316 }
5317
5318 /**
5319 * Indicates whether this view reacts to click events or not.
5320 *
5321 * @return true if the view is clickable, false otherwise
5322 *
5323 * @see #setClickable(boolean)
5324 * @attr ref android.R.styleable#View_clickable
5325 */
5326 @ViewDebug.ExportedProperty
5327 public boolean isClickable() {
5328 return (mViewFlags & CLICKABLE) == CLICKABLE;
5329 }
5330
5331 /**
5332 * Enables or disables click events for this view. When a view
5333 * is clickable it will change its state to "pressed" on every click.
5334 * Subclasses should set the view clickable to visually react to
5335 * user's clicks.
5336 *
5337 * @param clickable true to make the view clickable, false otherwise
5338 *
5339 * @see #isClickable()
5340 * @attr ref android.R.styleable#View_clickable
5341 */
5342 public void setClickable(boolean clickable) {
5343 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5344 }
5345
5346 /**
5347 * Indicates whether this view reacts to long click events or not.
5348 *
5349 * @return true if the view is long clickable, false otherwise
5350 *
5351 * @see #setLongClickable(boolean)
5352 * @attr ref android.R.styleable#View_longClickable
5353 */
5354 public boolean isLongClickable() {
5355 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5356 }
5357
5358 /**
5359 * Enables or disables long click events for this view. When a view is long
5360 * clickable it reacts to the user holding down the button for a longer
5361 * duration than a tap. This event can either launch the listener or a
5362 * context menu.
5363 *
5364 * @param longClickable true to make the view long clickable, false otherwise
5365 * @see #isLongClickable()
5366 * @attr ref android.R.styleable#View_longClickable
5367 */
5368 public void setLongClickable(boolean longClickable) {
5369 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5370 }
5371
5372 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005373 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005374 *
5375 * @see #isClickable()
5376 * @see #setClickable(boolean)
5377 *
5378 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5379 * the View's internal state from a previously set "pressed" state.
5380 */
5381 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005382 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005384 if (pressed) {
5385 mPrivateFlags |= PRESSED;
5386 } else {
5387 mPrivateFlags &= ~PRESSED;
5388 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005389
5390 if (needsRefresh) {
5391 refreshDrawableState();
5392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 dispatchSetPressed(pressed);
5394 }
5395
5396 /**
5397 * Dispatch setPressed to all of this View's children.
5398 *
5399 * @see #setPressed(boolean)
5400 *
5401 * @param pressed The new pressed state
5402 */
5403 protected void dispatchSetPressed(boolean pressed) {
5404 }
5405
5406 /**
5407 * Indicates whether the view is currently in pressed state. Unless
5408 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5409 * the pressed state.
5410 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005411 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 * @see #isClickable()
5413 * @see #setClickable(boolean)
5414 *
5415 * @return true if the view is currently pressed, false otherwise
5416 */
5417 public boolean isPressed() {
5418 return (mPrivateFlags & PRESSED) == PRESSED;
5419 }
5420
5421 /**
5422 * Indicates whether this view will save its state (that is,
5423 * whether its {@link #onSaveInstanceState} method will be called).
5424 *
5425 * @return Returns true if the view state saving is enabled, else false.
5426 *
5427 * @see #setSaveEnabled(boolean)
5428 * @attr ref android.R.styleable#View_saveEnabled
5429 */
5430 public boolean isSaveEnabled() {
5431 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5432 }
5433
5434 /**
5435 * Controls whether the saving of this view's state is
5436 * enabled (that is, whether its {@link #onSaveInstanceState} method
5437 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005438 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 * for its state to be saved. This flag can only disable the
5440 * saving of this view; any child views may still have their state saved.
5441 *
5442 * @param enabled Set to false to <em>disable</em> state saving, or true
5443 * (the default) to allow it.
5444 *
5445 * @see #isSaveEnabled()
5446 * @see #setId(int)
5447 * @see #onSaveInstanceState()
5448 * @attr ref android.R.styleable#View_saveEnabled
5449 */
5450 public void setSaveEnabled(boolean enabled) {
5451 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5452 }
5453
Jeff Brown85a31762010-09-01 17:01:00 -07005454 /**
5455 * Gets whether the framework should discard touches when the view's
5456 * window is obscured by another visible window.
5457 * Refer to the {@link View} security documentation for more details.
5458 *
5459 * @return True if touch filtering is enabled.
5460 *
5461 * @see #setFilterTouchesWhenObscured(boolean)
5462 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5463 */
5464 @ViewDebug.ExportedProperty
5465 public boolean getFilterTouchesWhenObscured() {
5466 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5467 }
5468
5469 /**
5470 * Sets whether the framework should discard touches when the view's
5471 * window is obscured by another visible window.
5472 * Refer to the {@link View} security documentation for more details.
5473 *
5474 * @param enabled True if touch filtering should be enabled.
5475 *
5476 * @see #getFilterTouchesWhenObscured
5477 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5478 */
5479 public void setFilterTouchesWhenObscured(boolean enabled) {
5480 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5481 FILTER_TOUCHES_WHEN_OBSCURED);
5482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483
5484 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005485 * Indicates whether the entire hierarchy under this view will save its
5486 * state when a state saving traversal occurs from its parent. The default
5487 * is true; if false, these views will not be saved unless
5488 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5489 *
5490 * @return Returns true if the view state saving from parent is enabled, else false.
5491 *
5492 * @see #setSaveFromParentEnabled(boolean)
5493 */
5494 public boolean isSaveFromParentEnabled() {
5495 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5496 }
5497
5498 /**
5499 * Controls whether the entire hierarchy under this view will save its
5500 * state when a state saving traversal occurs from its parent. The default
5501 * is true; if false, these views will not be saved unless
5502 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5503 *
5504 * @param enabled Set to false to <em>disable</em> state saving, or true
5505 * (the default) to allow it.
5506 *
5507 * @see #isSaveFromParentEnabled()
5508 * @see #setId(int)
5509 * @see #onSaveInstanceState()
5510 */
5511 public void setSaveFromParentEnabled(boolean enabled) {
5512 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5513 }
5514
5515
5516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 * Returns whether this View is able to take focus.
5518 *
5519 * @return True if this view can take focus, or false otherwise.
5520 * @attr ref android.R.styleable#View_focusable
5521 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005522 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 public final boolean isFocusable() {
5524 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5525 }
5526
5527 /**
5528 * When a view is focusable, it may not want to take focus when in touch mode.
5529 * For example, a button would like focus when the user is navigating via a D-pad
5530 * so that the user can click on it, but once the user starts touching the screen,
5531 * the button shouldn't take focus
5532 * @return Whether the view is focusable in touch mode.
5533 * @attr ref android.R.styleable#View_focusableInTouchMode
5534 */
5535 @ViewDebug.ExportedProperty
5536 public final boolean isFocusableInTouchMode() {
5537 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5538 }
5539
5540 /**
5541 * Find the nearest view in the specified direction that can take focus.
5542 * This does not actually give focus to that view.
5543 *
5544 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5545 *
5546 * @return The nearest focusable in the specified direction, or null if none
5547 * can be found.
5548 */
5549 public View focusSearch(int direction) {
5550 if (mParent != null) {
5551 return mParent.focusSearch(this, direction);
5552 } else {
5553 return null;
5554 }
5555 }
5556
5557 /**
5558 * This method is the last chance for the focused view and its ancestors to
5559 * respond to an arrow key. This is called when the focused view did not
5560 * consume the key internally, nor could the view system find a new view in
5561 * the requested direction to give focus to.
5562 *
5563 * @param focused The currently focused view.
5564 * @param direction The direction focus wants to move. One of FOCUS_UP,
5565 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5566 * @return True if the this view consumed this unhandled move.
5567 */
5568 public boolean dispatchUnhandledMove(View focused, int direction) {
5569 return false;
5570 }
5571
5572 /**
5573 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005574 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005576 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5577 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005578 * @return The user specified next view, or null if there is none.
5579 */
5580 View findUserSetNextFocus(View root, int direction) {
5581 switch (direction) {
5582 case FOCUS_LEFT:
5583 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005584 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 case FOCUS_RIGHT:
5586 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005587 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 case FOCUS_UP:
5589 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005590 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 case FOCUS_DOWN:
5592 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005593 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005594 case FOCUS_FORWARD:
5595 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005596 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005597 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005598 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005599 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005600 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005601 @Override
5602 public boolean apply(View t) {
5603 return t.mNextFocusForwardId == id;
5604 }
5605 });
5606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607 }
5608 return null;
5609 }
5610
Jeff Brown4dfbec22011-08-15 14:55:37 -07005611 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5612 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5613 @Override
5614 public boolean apply(View t) {
5615 return t.mID == childViewId;
5616 }
5617 });
5618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 if (result == null) {
5620 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5621 + "by user for id " + childViewId);
5622 }
5623 return result;
5624 }
5625
5626 /**
5627 * Find and return all focusable views that are descendants of this view,
5628 * possibly including this view if it is focusable itself.
5629 *
5630 * @param direction The direction of the focus
5631 * @return A list of focusable views
5632 */
5633 public ArrayList<View> getFocusables(int direction) {
5634 ArrayList<View> result = new ArrayList<View>(24);
5635 addFocusables(result, direction);
5636 return result;
5637 }
5638
5639 /**
5640 * Add any focusable views that are descendants of this view (possibly
5641 * including this view if it is focusable itself) to views. If we are in touch mode,
5642 * only add views that are also focusable in touch mode.
5643 *
5644 * @param views Focusable views found so far
5645 * @param direction The direction of the focus
5646 */
5647 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005648 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650
svetoslavganov75986cf2009-05-14 22:28:01 -07005651 /**
5652 * Adds any focusable views that are descendants of this view (possibly
5653 * including this view if it is focusable itself) to views. This method
5654 * adds all focusable views regardless if we are in touch mode or
5655 * only views focusable in touch mode if we are in touch mode depending on
5656 * the focusable mode paramater.
5657 *
5658 * @param views Focusable views found so far or null if all we are interested is
5659 * the number of focusables.
5660 * @param direction The direction of the focus.
5661 * @param focusableMode The type of focusables to be added.
5662 *
5663 * @see #FOCUSABLES_ALL
5664 * @see #FOCUSABLES_TOUCH_MODE
5665 */
5666 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5667 if (!isFocusable()) {
5668 return;
5669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670
svetoslavganov75986cf2009-05-14 22:28:01 -07005671 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5672 isInTouchMode() && !isFocusableInTouchMode()) {
5673 return;
5674 }
5675
5676 if (views != null) {
5677 views.add(this);
5678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005679 }
5680
5681 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005682 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005683 * The search is performed by either the text that the View renders or the content
5684 * description that describes the view for accessibility purposes and the view does
5685 * not render or both. Clients can specify how the search is to be performed via
5686 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5687 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005688 *
5689 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005690 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005691 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005692 * @see #FIND_VIEWS_WITH_TEXT
5693 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5694 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005695 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005696 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005697 if (getAccessibilityNodeProvider() != null) {
5698 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5699 outViews.add(this);
5700 }
5701 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5702 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005703 String searchedLowerCase = searched.toString().toLowerCase();
5704 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5705 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5706 outViews.add(this);
5707 }
5708 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005709 }
5710
5711 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 * Find and return all touchable views that are descendants of this view,
5713 * possibly including this view if it is touchable itself.
5714 *
5715 * @return A list of touchable views
5716 */
5717 public ArrayList<View> getTouchables() {
5718 ArrayList<View> result = new ArrayList<View>();
5719 addTouchables(result);
5720 return result;
5721 }
5722
5723 /**
5724 * Add any touchable views that are descendants of this view (possibly
5725 * including this view if it is touchable itself) to views.
5726 *
5727 * @param views Touchable views found so far
5728 */
5729 public void addTouchables(ArrayList<View> views) {
5730 final int viewFlags = mViewFlags;
5731
5732 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5733 && (viewFlags & ENABLED_MASK) == ENABLED) {
5734 views.add(this);
5735 }
5736 }
5737
5738 /**
5739 * Call this to try to give focus to a specific view or to one of its
5740 * descendants.
5741 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005742 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5743 * false), or if it is focusable and it is not focusable in touch mode
5744 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005746 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 * have focus, and you want your parent to look for the next one.
5748 *
5749 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5750 * {@link #FOCUS_DOWN} and <code>null</code>.
5751 *
5752 * @return Whether this view or one of its descendants actually took focus.
5753 */
5754 public final boolean requestFocus() {
5755 return requestFocus(View.FOCUS_DOWN);
5756 }
5757
5758
5759 /**
5760 * Call this to try to give focus to a specific view or to one of its
5761 * descendants and give it a hint about what direction focus is heading.
5762 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005763 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5764 * false), or if it is focusable and it is not focusable in touch mode
5765 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005767 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 * have focus, and you want your parent to look for the next one.
5769 *
5770 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5771 * <code>null</code> set for the previously focused rectangle.
5772 *
5773 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5774 * @return Whether this view or one of its descendants actually took focus.
5775 */
5776 public final boolean requestFocus(int direction) {
5777 return requestFocus(direction, null);
5778 }
5779
5780 /**
5781 * Call this to try to give focus to a specific view or to one of its descendants
5782 * and give it hints about the direction and a specific rectangle that the focus
5783 * is coming from. The rectangle can help give larger views a finer grained hint
5784 * about where focus is coming from, and therefore, where to show selection, or
5785 * forward focus change internally.
5786 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005787 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5788 * false), or if it is focusable and it is not focusable in touch mode
5789 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 *
5791 * A View will not take focus if it is not visible.
5792 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005793 * A View will not take focus if one of its parents has
5794 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5795 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005797 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 * have focus, and you want your parent to look for the next one.
5799 *
5800 * You may wish to override this method if your custom {@link View} has an internal
5801 * {@link View} that it wishes to forward the request to.
5802 *
5803 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5804 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5805 * to give a finer grained hint about where focus is coming from. May be null
5806 * if there is no hint.
5807 * @return Whether this view or one of its descendants actually took focus.
5808 */
5809 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5810 // need to be focusable
5811 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5812 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5813 return false;
5814 }
5815
5816 // need to be focusable in touch mode if in touch mode
5817 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005818 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5819 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 }
5821
5822 // need to not have any parents blocking us
5823 if (hasAncestorThatBlocksDescendantFocus()) {
5824 return false;
5825 }
5826
5827 handleFocusGainInternal(direction, previouslyFocusedRect);
5828 return true;
5829 }
5830
5831 /**
5832 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5833 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5834 * touch mode to request focus when they are touched.
5835 *
5836 * @return Whether this view or one of its descendants actually took focus.
5837 *
5838 * @see #isInTouchMode()
5839 *
5840 */
5841 public final boolean requestFocusFromTouch() {
5842 // Leave touch mode if we need to
5843 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005844 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005845 if (viewRoot != null) {
5846 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 }
5848 }
5849 return requestFocus(View.FOCUS_DOWN);
5850 }
5851
5852 /**
5853 * @return Whether any ancestor of this view blocks descendant focus.
5854 */
5855 private boolean hasAncestorThatBlocksDescendantFocus() {
5856 ViewParent ancestor = mParent;
5857 while (ancestor instanceof ViewGroup) {
5858 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5859 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5860 return true;
5861 } else {
5862 ancestor = vgAncestor.getParent();
5863 }
5864 }
5865 return false;
5866 }
5867
5868 /**
Romain Guya440b002010-02-24 15:57:54 -08005869 * @hide
5870 */
5871 public void dispatchStartTemporaryDetach() {
5872 onStartTemporaryDetach();
5873 }
5874
5875 /**
5876 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5878 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005879 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 */
5881 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005882 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005883 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005884 }
5885
5886 /**
5887 * @hide
5888 */
5889 public void dispatchFinishTemporaryDetach() {
5890 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 }
Romain Guy8506ab42009-06-11 17:35:47 -07005892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 /**
5894 * Called after {@link #onStartTemporaryDetach} when the container is done
5895 * changing the view.
5896 */
5897 public void onFinishTemporaryDetach() {
5898 }
Romain Guy8506ab42009-06-11 17:35:47 -07005899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005901 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5902 * for this view's window. Returns null if the view is not currently attached
5903 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005904 * just use the standard high-level event callbacks like
5905 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005906 */
5907 public KeyEvent.DispatcherState getKeyDispatcherState() {
5908 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5909 }
Joe Malin32736f02011-01-19 16:14:20 -08005910
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 * Dispatch a key event before it is processed by any input method
5913 * associated with the view hierarchy. This can be used to intercept
5914 * key events in special situations before the IME consumes them; a
5915 * typical example would be handling the BACK key to update the application's
5916 * UI instead of allowing the IME to see it and close itself.
5917 *
5918 * @param event The key event to be dispatched.
5919 * @return True if the event was handled, false otherwise.
5920 */
5921 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5922 return onKeyPreIme(event.getKeyCode(), event);
5923 }
5924
5925 /**
5926 * Dispatch a key event to the next view on the focus path. This path runs
5927 * from the top of the view tree down to the currently focused view. If this
5928 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5929 * the next node down the focus path. This method also fires any key
5930 * listeners.
5931 *
5932 * @param event The key event to be dispatched.
5933 * @return True if the event was handled, false otherwise.
5934 */
5935 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005936 if (mInputEventConsistencyVerifier != null) {
5937 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005939
Jeff Brown21bc5c92011-02-28 18:27:14 -08005940 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005941 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005942 ListenerInfo li = mListenerInfo;
5943 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5944 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005945 return true;
5946 }
5947
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005948 if (event.dispatch(this, mAttachInfo != null
5949 ? mAttachInfo.mKeyDispatchState : null, this)) {
5950 return true;
5951 }
5952
5953 if (mInputEventConsistencyVerifier != null) {
5954 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5955 }
5956 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005957 }
5958
5959 /**
5960 * Dispatches a key shortcut event.
5961 *
5962 * @param event The key event to be dispatched.
5963 * @return True if the event was handled by the view, false otherwise.
5964 */
5965 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5966 return onKeyShortcut(event.getKeyCode(), event);
5967 }
5968
5969 /**
5970 * Pass the touch screen motion event down to the target view, or this
5971 * view if it is the target.
5972 *
5973 * @param event The motion event to be dispatched.
5974 * @return True if the event was handled by the view, false otherwise.
5975 */
5976 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005977 if (mInputEventConsistencyVerifier != null) {
5978 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5979 }
5980
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005981 if (onFilterTouchEventForSecurity(event)) {
5982 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005983 ListenerInfo li = mListenerInfo;
5984 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5985 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005986 return true;
5987 }
5988
5989 if (onTouchEvent(event)) {
5990 return true;
5991 }
Jeff Brown85a31762010-09-01 17:01:00 -07005992 }
5993
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005994 if (mInputEventConsistencyVerifier != null) {
5995 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005996 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005997 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998 }
5999
6000 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006001 * Filter the touch event to apply security policies.
6002 *
6003 * @param event The motion event to be filtered.
6004 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006005 *
Jeff Brown85a31762010-09-01 17:01:00 -07006006 * @see #getFilterTouchesWhenObscured
6007 */
6008 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006009 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006010 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6011 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6012 // Window is obscured, drop this touch.
6013 return false;
6014 }
6015 return true;
6016 }
6017
6018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 * Pass a trackball motion event down to the focused view.
6020 *
6021 * @param event The motion event to be dispatched.
6022 * @return True if the event was handled by the view, false otherwise.
6023 */
6024 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006025 if (mInputEventConsistencyVerifier != null) {
6026 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6027 }
6028
Romain Guy02ccac62011-06-24 13:20:23 -07006029 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 }
6031
6032 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006033 * Dispatch a generic motion event.
6034 * <p>
6035 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6036 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006037 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006038 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006039 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006040 *
6041 * @param event The motion event to be dispatched.
6042 * @return True if the event was handled by the view, false otherwise.
6043 */
6044 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006045 if (mInputEventConsistencyVerifier != null) {
6046 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6047 }
6048
Jeff Browna032cc02011-03-07 16:56:21 -08006049 final int source = event.getSource();
6050 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6051 final int action = event.getAction();
6052 if (action == MotionEvent.ACTION_HOVER_ENTER
6053 || action == MotionEvent.ACTION_HOVER_MOVE
6054 || action == MotionEvent.ACTION_HOVER_EXIT) {
6055 if (dispatchHoverEvent(event)) {
6056 return true;
6057 }
6058 } else if (dispatchGenericPointerEvent(event)) {
6059 return true;
6060 }
6061 } else if (dispatchGenericFocusedEvent(event)) {
6062 return true;
6063 }
6064
Jeff Brown10b62902011-06-20 16:40:37 -07006065 if (dispatchGenericMotionEventInternal(event)) {
6066 return true;
6067 }
6068
6069 if (mInputEventConsistencyVerifier != null) {
6070 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6071 }
6072 return false;
6073 }
6074
6075 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006076 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006077 ListenerInfo li = mListenerInfo;
6078 if (li != null && li.mOnGenericMotionListener != null
6079 && (mViewFlags & ENABLED_MASK) == ENABLED
6080 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006081 return true;
6082 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006083
6084 if (onGenericMotionEvent(event)) {
6085 return true;
6086 }
6087
6088 if (mInputEventConsistencyVerifier != null) {
6089 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6090 }
6091 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006092 }
6093
6094 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006095 * Dispatch a hover event.
6096 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006097 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006098 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006099 * </p>
6100 *
6101 * @param event The motion event to be dispatched.
6102 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006103 */
6104 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006105 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006106 ListenerInfo li = mListenerInfo;
6107 if (li != null && li.mOnHoverListener != null
6108 && (mViewFlags & ENABLED_MASK) == ENABLED
6109 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006110 return true;
6111 }
6112
Jeff Browna032cc02011-03-07 16:56:21 -08006113 return onHoverEvent(event);
6114 }
6115
6116 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006117 * Returns true if the view has a child to which it has recently sent
6118 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6119 * it does not have a hovered child, then it must be the innermost hovered view.
6120 * @hide
6121 */
6122 protected boolean hasHoveredChild() {
6123 return false;
6124 }
6125
6126 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006127 * Dispatch a generic motion event to the view under the first pointer.
6128 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006129 * Do not call this method directly.
6130 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006131 * </p>
6132 *
6133 * @param event The motion event to be dispatched.
6134 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006135 */
6136 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6137 return false;
6138 }
6139
6140 /**
6141 * Dispatch a generic motion event to the currently focused view.
6142 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006143 * Do not call this method directly.
6144 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006145 * </p>
6146 *
6147 * @param event The motion event to be dispatched.
6148 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006149 */
6150 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6151 return false;
6152 }
6153
6154 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006155 * Dispatch a pointer event.
6156 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006157 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6158 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6159 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006160 * and should not be expected to handle other pointing device features.
6161 * </p>
6162 *
6163 * @param event The motion event to be dispatched.
6164 * @return True if the event was handled by the view, false otherwise.
6165 * @hide
6166 */
6167 public final boolean dispatchPointerEvent(MotionEvent event) {
6168 if (event.isTouchEvent()) {
6169 return dispatchTouchEvent(event);
6170 } else {
6171 return dispatchGenericMotionEvent(event);
6172 }
6173 }
6174
6175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006176 * Called when the window containing this view gains or loses window focus.
6177 * ViewGroups should override to route to their children.
6178 *
6179 * @param hasFocus True if the window containing this view now has focus,
6180 * false otherwise.
6181 */
6182 public void dispatchWindowFocusChanged(boolean hasFocus) {
6183 onWindowFocusChanged(hasFocus);
6184 }
6185
6186 /**
6187 * Called when the window containing this view gains or loses focus. Note
6188 * that this is separate from view focus: to receive key events, both
6189 * your view and its window must have focus. If a window is displayed
6190 * on top of yours that takes input focus, then your own window will lose
6191 * focus but the view focus will remain unchanged.
6192 *
6193 * @param hasWindowFocus True if the window containing this view now has
6194 * focus, false otherwise.
6195 */
6196 public void onWindowFocusChanged(boolean hasWindowFocus) {
6197 InputMethodManager imm = InputMethodManager.peekInstance();
6198 if (!hasWindowFocus) {
6199 if (isPressed()) {
6200 setPressed(false);
6201 }
6202 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6203 imm.focusOut(this);
6204 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006205 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006206 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006207 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006208 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6209 imm.focusIn(this);
6210 }
6211 refreshDrawableState();
6212 }
6213
6214 /**
6215 * Returns true if this view is in a window that currently has window focus.
6216 * Note that this is not the same as the view itself having focus.
6217 *
6218 * @return True if this view is in a window that currently has window focus.
6219 */
6220 public boolean hasWindowFocus() {
6221 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6222 }
6223
6224 /**
Adam Powell326d8082009-12-09 15:10:07 -08006225 * Dispatch a view visibility change down the view hierarchy.
6226 * ViewGroups should override to route to their children.
6227 * @param changedView The view whose visibility changed. Could be 'this' or
6228 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006229 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6230 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006231 */
6232 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6233 onVisibilityChanged(changedView, visibility);
6234 }
6235
6236 /**
6237 * Called when the visibility of the view or an ancestor of the view is changed.
6238 * @param changedView The view whose visibility changed. Could be 'this' or
6239 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006240 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6241 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006242 */
6243 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006244 if (visibility == VISIBLE) {
6245 if (mAttachInfo != null) {
6246 initialAwakenScrollBars();
6247 } else {
6248 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6249 }
6250 }
Adam Powell326d8082009-12-09 15:10:07 -08006251 }
6252
6253 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006254 * Dispatch a hint about whether this view is displayed. For instance, when
6255 * a View moves out of the screen, it might receives a display hint indicating
6256 * the view is not displayed. Applications should not <em>rely</em> on this hint
6257 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006258 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006259 * @param hint A hint about whether or not this view is displayed:
6260 * {@link #VISIBLE} or {@link #INVISIBLE}.
6261 */
6262 public void dispatchDisplayHint(int hint) {
6263 onDisplayHint(hint);
6264 }
6265
6266 /**
6267 * Gives this view a hint about whether is displayed or not. For instance, when
6268 * a View moves out of the screen, it might receives a display hint indicating
6269 * the view is not displayed. Applications should not <em>rely</em> on this hint
6270 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006271 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006272 * @param hint A hint about whether or not this view is displayed:
6273 * {@link #VISIBLE} or {@link #INVISIBLE}.
6274 */
6275 protected void onDisplayHint(int hint) {
6276 }
6277
6278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 * Dispatch a window visibility change down the view hierarchy.
6280 * ViewGroups should override to route to their children.
6281 *
6282 * @param visibility The new visibility of the window.
6283 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006284 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 */
6286 public void dispatchWindowVisibilityChanged(int visibility) {
6287 onWindowVisibilityChanged(visibility);
6288 }
6289
6290 /**
6291 * Called when the window containing has change its visibility
6292 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6293 * that this tells you whether or not your window is being made visible
6294 * to the window manager; this does <em>not</em> tell you whether or not
6295 * your window is obscured by other windows on the screen, even if it
6296 * is itself visible.
6297 *
6298 * @param visibility The new visibility of the window.
6299 */
6300 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006301 if (visibility == VISIBLE) {
6302 initialAwakenScrollBars();
6303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006304 }
6305
6306 /**
6307 * Returns the current visibility of the window this view is attached to
6308 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6309 *
6310 * @return Returns the current visibility of the view's window.
6311 */
6312 public int getWindowVisibility() {
6313 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6314 }
6315
6316 /**
6317 * Retrieve the overall visible display size in which the window this view is
6318 * attached to has been positioned in. This takes into account screen
6319 * decorations above the window, for both cases where the window itself
6320 * is being position inside of them or the window is being placed under
6321 * then and covered insets are used for the window to position its content
6322 * inside. In effect, this tells you the available area where content can
6323 * be placed and remain visible to users.
6324 *
6325 * <p>This function requires an IPC back to the window manager to retrieve
6326 * the requested information, so should not be used in performance critical
6327 * code like drawing.
6328 *
6329 * @param outRect Filled in with the visible display frame. If the view
6330 * is not attached to a window, this is simply the raw display size.
6331 */
6332 public void getWindowVisibleDisplayFrame(Rect outRect) {
6333 if (mAttachInfo != null) {
6334 try {
6335 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6336 } catch (RemoteException e) {
6337 return;
6338 }
6339 // XXX This is really broken, and probably all needs to be done
6340 // in the window manager, and we need to know more about whether
6341 // we want the area behind or in front of the IME.
6342 final Rect insets = mAttachInfo.mVisibleInsets;
6343 outRect.left += insets.left;
6344 outRect.top += insets.top;
6345 outRect.right -= insets.right;
6346 outRect.bottom -= insets.bottom;
6347 return;
6348 }
6349 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006350 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 }
6352
6353 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006354 * Dispatch a notification about a resource configuration change down
6355 * the view hierarchy.
6356 * ViewGroups should override to route to their children.
6357 *
6358 * @param newConfig The new resource configuration.
6359 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006360 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006361 */
6362 public void dispatchConfigurationChanged(Configuration newConfig) {
6363 onConfigurationChanged(newConfig);
6364 }
6365
6366 /**
6367 * Called when the current configuration of the resources being used
6368 * by the application have changed. You can use this to decide when
6369 * to reload resources that can changed based on orientation and other
6370 * configuration characterstics. You only need to use this if you are
6371 * not relying on the normal {@link android.app.Activity} mechanism of
6372 * recreating the activity instance upon a configuration change.
6373 *
6374 * @param newConfig The new resource configuration.
6375 */
6376 protected void onConfigurationChanged(Configuration newConfig) {
6377 }
6378
6379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 * Private function to aggregate all per-view attributes in to the view
6381 * root.
6382 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006383 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6384 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006385 }
6386
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006387 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6388 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006389 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006390 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006391 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006392 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006393 ListenerInfo li = mListenerInfo;
6394 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006395 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 }
6398 }
6399
6400 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006401 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006403 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6404 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 ai.mRecomputeGlobalAttributes = true;
6406 }
6407 }
6408 }
6409
6410 /**
6411 * Returns whether the device is currently in touch mode. Touch mode is entered
6412 * once the user begins interacting with the device by touch, and affects various
6413 * things like whether focus is always visible to the user.
6414 *
6415 * @return Whether the device is in touch mode.
6416 */
6417 @ViewDebug.ExportedProperty
6418 public boolean isInTouchMode() {
6419 if (mAttachInfo != null) {
6420 return mAttachInfo.mInTouchMode;
6421 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006422 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006423 }
6424 }
6425
6426 /**
6427 * Returns the context the view is running in, through which it can
6428 * access the current theme, resources, etc.
6429 *
6430 * @return The view's Context.
6431 */
6432 @ViewDebug.CapturedViewProperty
6433 public final Context getContext() {
6434 return mContext;
6435 }
6436
6437 /**
6438 * Handle a key event before it is processed by any input method
6439 * associated with the view hierarchy. This can be used to intercept
6440 * key events in special situations before the IME consumes them; a
6441 * typical example would be handling the BACK key to update the application's
6442 * UI instead of allowing the IME to see it and close itself.
6443 *
6444 * @param keyCode The value in event.getKeyCode().
6445 * @param event Description of the key event.
6446 * @return If you handled the event, return true. If you want to allow the
6447 * event to be handled by the next receiver, return false.
6448 */
6449 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6450 return false;
6451 }
6452
6453 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006454 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6455 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006456 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6457 * is released, if the view is enabled and clickable.
6458 *
6459 * @param keyCode A key code that represents the button pressed, from
6460 * {@link android.view.KeyEvent}.
6461 * @param event The KeyEvent object that defines the button action.
6462 */
6463 public boolean onKeyDown(int keyCode, KeyEvent event) {
6464 boolean result = false;
6465
6466 switch (keyCode) {
6467 case KeyEvent.KEYCODE_DPAD_CENTER:
6468 case KeyEvent.KEYCODE_ENTER: {
6469 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6470 return true;
6471 }
6472 // Long clickable items don't necessarily have to be clickable
6473 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6474 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6475 (event.getRepeatCount() == 0)) {
6476 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006477 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 return true;
6479 }
6480 break;
6481 }
6482 }
6483 return result;
6484 }
6485
6486 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006487 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6488 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6489 * the event).
6490 */
6491 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6492 return false;
6493 }
6494
6495 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006496 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6497 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6499 * {@link KeyEvent#KEYCODE_ENTER} is released.
6500 *
6501 * @param keyCode A key code that represents the button pressed, from
6502 * {@link android.view.KeyEvent}.
6503 * @param event The KeyEvent object that defines the button action.
6504 */
6505 public boolean onKeyUp(int keyCode, KeyEvent event) {
6506 boolean result = false;
6507
6508 switch (keyCode) {
6509 case KeyEvent.KEYCODE_DPAD_CENTER:
6510 case KeyEvent.KEYCODE_ENTER: {
6511 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6512 return true;
6513 }
6514 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6515 setPressed(false);
6516
6517 if (!mHasPerformedLongPress) {
6518 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006519 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520
6521 result = performClick();
6522 }
6523 }
6524 break;
6525 }
6526 }
6527 return result;
6528 }
6529
6530 /**
6531 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6532 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6533 * the event).
6534 *
6535 * @param keyCode A key code that represents the button pressed, from
6536 * {@link android.view.KeyEvent}.
6537 * @param repeatCount The number of times the action was made.
6538 * @param event The KeyEvent object that defines the button action.
6539 */
6540 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6541 return false;
6542 }
6543
6544 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006545 * Called on the focused view when a key shortcut event is not handled.
6546 * Override this method to implement local key shortcuts for the View.
6547 * Key shortcuts can also be implemented by setting the
6548 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 *
6550 * @param keyCode The value in event.getKeyCode().
6551 * @param event Description of the key event.
6552 * @return If you handled the event, return true. If you want to allow the
6553 * event to be handled by the next receiver, return false.
6554 */
6555 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6556 return false;
6557 }
6558
6559 /**
6560 * Check whether the called view is a text editor, in which case it
6561 * would make sense to automatically display a soft input window for
6562 * it. Subclasses should override this if they implement
6563 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006564 * a call on that method would return a non-null InputConnection, and
6565 * they are really a first-class editor that the user would normally
6566 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006567 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006568 * <p>The default implementation always returns false. This does
6569 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6570 * will not be called or the user can not otherwise perform edits on your
6571 * view; it is just a hint to the system that this is not the primary
6572 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006573 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 * @return Returns true if this view is a text editor, else false.
6575 */
6576 public boolean onCheckIsTextEditor() {
6577 return false;
6578 }
Romain Guy8506ab42009-06-11 17:35:47 -07006579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 /**
6581 * Create a new InputConnection for an InputMethod to interact
6582 * with the view. The default implementation returns null, since it doesn't
6583 * support input methods. You can override this to implement such support.
6584 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006585 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 * <p>When implementing this, you probably also want to implement
6587 * {@link #onCheckIsTextEditor()} to indicate you will return a
6588 * non-null InputConnection.
6589 *
6590 * @param outAttrs Fill in with attribute information about the connection.
6591 */
6592 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6593 return null;
6594 }
6595
6596 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006597 * Called by the {@link android.view.inputmethod.InputMethodManager}
6598 * when a view who is not the current
6599 * input connection target is trying to make a call on the manager. The
6600 * default implementation returns false; you can override this to return
6601 * true for certain views if you are performing InputConnection proxying
6602 * to them.
6603 * @param view The View that is making the InputMethodManager call.
6604 * @return Return true to allow the call, false to reject.
6605 */
6606 public boolean checkInputConnectionProxy(View view) {
6607 return false;
6608 }
Romain Guy8506ab42009-06-11 17:35:47 -07006609
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611 * Show the context menu for this view. It is not safe to hold on to the
6612 * menu after returning from this method.
6613 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006614 * You should normally not overload this method. Overload
6615 * {@link #onCreateContextMenu(ContextMenu)} or define an
6616 * {@link OnCreateContextMenuListener} to add items to the context menu.
6617 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 * @param menu The context menu to populate
6619 */
6620 public void createContextMenu(ContextMenu menu) {
6621 ContextMenuInfo menuInfo = getContextMenuInfo();
6622
6623 // Sets the current menu info so all items added to menu will have
6624 // my extra info set.
6625 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6626
6627 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006628 ListenerInfo li = mListenerInfo;
6629 if (li != null && li.mOnCreateContextMenuListener != null) {
6630 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 }
6632
6633 // Clear the extra information so subsequent items that aren't mine don't
6634 // have my extra info.
6635 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6636
6637 if (mParent != null) {
6638 mParent.createContextMenu(menu);
6639 }
6640 }
6641
6642 /**
6643 * Views should implement this if they have extra information to associate
6644 * with the context menu. The return result is supplied as a parameter to
6645 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6646 * callback.
6647 *
6648 * @return Extra information about the item for which the context menu
6649 * should be shown. This information will vary across different
6650 * subclasses of View.
6651 */
6652 protected ContextMenuInfo getContextMenuInfo() {
6653 return null;
6654 }
6655
6656 /**
6657 * Views should implement this if the view itself is going to add items to
6658 * the context menu.
6659 *
6660 * @param menu the context menu to populate
6661 */
6662 protected void onCreateContextMenu(ContextMenu menu) {
6663 }
6664
6665 /**
6666 * Implement this method to handle trackball motion events. The
6667 * <em>relative</em> movement of the trackball since the last event
6668 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6669 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6670 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6671 * they will often be fractional values, representing the more fine-grained
6672 * movement information available from a trackball).
6673 *
6674 * @param event The motion event.
6675 * @return True if the event was handled, false otherwise.
6676 */
6677 public boolean onTrackballEvent(MotionEvent event) {
6678 return false;
6679 }
6680
6681 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006682 * Implement this method to handle generic motion events.
6683 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006684 * Generic motion events describe joystick movements, mouse hovers, track pad
6685 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006686 * {@link MotionEvent#getSource() source} of the motion event specifies
6687 * the class of input that was received. Implementations of this method
6688 * must examine the bits in the source before processing the event.
6689 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006690 * </p><p>
6691 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6692 * are delivered to the view under the pointer. All other generic motion events are
6693 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006694 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006695 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006696 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006697 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6698 * // process the joystick movement...
6699 * return true;
6700 * }
6701 * }
6702 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6703 * switch (event.getAction()) {
6704 * case MotionEvent.ACTION_HOVER_MOVE:
6705 * // process the mouse hover movement...
6706 * return true;
6707 * case MotionEvent.ACTION_SCROLL:
6708 * // process the scroll wheel movement...
6709 * return true;
6710 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006711 * }
6712 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006713 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006714 *
6715 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006716 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006717 */
6718 public boolean onGenericMotionEvent(MotionEvent event) {
6719 return false;
6720 }
6721
6722 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006723 * Implement this method to handle hover events.
6724 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006725 * This method is called whenever a pointer is hovering into, over, or out of the
6726 * bounds of a view and the view is not currently being touched.
6727 * Hover events are represented as pointer events with action
6728 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6729 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6730 * </p>
6731 * <ul>
6732 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6733 * when the pointer enters the bounds of the view.</li>
6734 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6735 * when the pointer has already entered the bounds of the view and has moved.</li>
6736 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6737 * when the pointer has exited the bounds of the view or when the pointer is
6738 * about to go down due to a button click, tap, or similar user action that
6739 * causes the view to be touched.</li>
6740 * </ul>
6741 * <p>
6742 * The view should implement this method to return true to indicate that it is
6743 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006744 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006745 * The default implementation calls {@link #setHovered} to update the hovered state
6746 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006747 * is enabled and is clickable. The default implementation also sends hover
6748 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006749 * </p>
6750 *
6751 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006752 * @return True if the view handled the hover event.
6753 *
6754 * @see #isHovered
6755 * @see #setHovered
6756 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006757 */
6758 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006759 // The root view may receive hover (or touch) events that are outside the bounds of
6760 // the window. This code ensures that we only send accessibility events for
6761 // hovers that are actually within the bounds of the root view.
6762 final int action = event.getAction();
6763 if (!mSendingHoverAccessibilityEvents) {
6764 if ((action == MotionEvent.ACTION_HOVER_ENTER
6765 || action == MotionEvent.ACTION_HOVER_MOVE)
6766 && !hasHoveredChild()
6767 && pointInView(event.getX(), event.getY())) {
6768 mSendingHoverAccessibilityEvents = true;
6769 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6770 }
6771 } else {
6772 if (action == MotionEvent.ACTION_HOVER_EXIT
6773 || (action == MotionEvent.ACTION_HOVER_MOVE
6774 && !pointInView(event.getX(), event.getY()))) {
6775 mSendingHoverAccessibilityEvents = false;
6776 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6777 }
Jeff Browna1b24182011-07-28 13:38:24 -07006778 }
6779
Jeff Brown87b7f802011-06-21 18:35:45 -07006780 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006781 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006782 case MotionEvent.ACTION_HOVER_ENTER:
6783 setHovered(true);
6784 break;
6785 case MotionEvent.ACTION_HOVER_EXIT:
6786 setHovered(false);
6787 break;
6788 }
Jeff Browna1b24182011-07-28 13:38:24 -07006789
6790 // Dispatch the event to onGenericMotionEvent before returning true.
6791 // This is to provide compatibility with existing applications that
6792 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6793 // break because of the new default handling for hoverable views
6794 // in onHoverEvent.
6795 // Note that onGenericMotionEvent will be called by default when
6796 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6797 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006798 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006799 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006800 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006801 }
6802
6803 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006804 * Returns true if the view should handle {@link #onHoverEvent}
6805 * by calling {@link #setHovered} to change its hovered state.
6806 *
6807 * @return True if the view is hoverable.
6808 */
6809 private boolean isHoverable() {
6810 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006811 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006812 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6813 return false;
6814 }
6815
6816 return (viewFlags & CLICKABLE) == CLICKABLE
6817 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6818 }
6819
6820 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006821 * Returns true if the view is currently hovered.
6822 *
6823 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006824 *
6825 * @see #setHovered
6826 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006827 */
Jeff Brown10b62902011-06-20 16:40:37 -07006828 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006829 public boolean isHovered() {
6830 return (mPrivateFlags & HOVERED) != 0;
6831 }
6832
6833 /**
6834 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006835 * <p>
6836 * Calling this method also changes the drawable state of the view. This
6837 * enables the view to react to hover by using different drawable resources
6838 * to change its appearance.
6839 * </p><p>
6840 * The {@link #onHoverChanged} method is called when the hovered state changes.
6841 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006842 *
6843 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006844 *
6845 * @see #isHovered
6846 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006847 */
6848 public void setHovered(boolean hovered) {
6849 if (hovered) {
6850 if ((mPrivateFlags & HOVERED) == 0) {
6851 mPrivateFlags |= HOVERED;
6852 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006853 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006854 }
6855 } else {
6856 if ((mPrivateFlags & HOVERED) != 0) {
6857 mPrivateFlags &= ~HOVERED;
6858 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006859 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006860 }
6861 }
6862 }
6863
6864 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006865 * Implement this method to handle hover state changes.
6866 * <p>
6867 * This method is called whenever the hover state changes as a result of a
6868 * call to {@link #setHovered}.
6869 * </p>
6870 *
6871 * @param hovered The current hover state, as returned by {@link #isHovered}.
6872 *
6873 * @see #isHovered
6874 * @see #setHovered
6875 */
6876 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006877 }
6878
6879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 * Implement this method to handle touch screen motion events.
6881 *
6882 * @param event The motion event.
6883 * @return True if the event was handled, false otherwise.
6884 */
6885 public boolean onTouchEvent(MotionEvent event) {
6886 final int viewFlags = mViewFlags;
6887
6888 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006889 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006890 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006892 // A disabled view that is clickable still consumes the touch
6893 // events, it just doesn't respond to them.
6894 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6895 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6896 }
6897
6898 if (mTouchDelegate != null) {
6899 if (mTouchDelegate.onTouchEvent(event)) {
6900 return true;
6901 }
6902 }
6903
6904 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6905 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6906 switch (event.getAction()) {
6907 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006908 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6909 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 // take focus if we don't have it already and we should in
6911 // touch mode.
6912 boolean focusTaken = false;
6913 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6914 focusTaken = requestFocus();
6915 }
6916
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006917 if (prepressed) {
6918 // The button is being released before we actually
6919 // showed it as pressed. Make it show the pressed
6920 // state now (before scheduling the click) to ensure
6921 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006922 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006923 }
Joe Malin32736f02011-01-19 16:14:20 -08006924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 if (!mHasPerformedLongPress) {
6926 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006927 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928
6929 // Only perform take click actions if we were in the pressed state
6930 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006931 // Use a Runnable and post this rather than calling
6932 // performClick directly. This lets other visual state
6933 // of the view update before click actions start.
6934 if (mPerformClick == null) {
6935 mPerformClick = new PerformClick();
6936 }
6937 if (!post(mPerformClick)) {
6938 performClick();
6939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940 }
6941 }
6942
6943 if (mUnsetPressedState == null) {
6944 mUnsetPressedState = new UnsetPressedState();
6945 }
6946
Adam Powelle14579b2009-12-16 18:39:52 -08006947 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006948 postDelayed(mUnsetPressedState,
6949 ViewConfiguration.getPressedStateDuration());
6950 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006951 // If the post failed, unpress right now
6952 mUnsetPressedState.run();
6953 }
Adam Powelle14579b2009-12-16 18:39:52 -08006954 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 }
6956 break;
6957
6958 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006959 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006960
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006961 if (performButtonActionOnTouchDown(event)) {
6962 break;
6963 }
6964
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006965 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006966 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006967
6968 // For views inside a scrolling container, delay the pressed feedback for
6969 // a short period in case this is a scroll.
6970 if (isInScrollingContainer) {
6971 mPrivateFlags |= PREPRESSED;
6972 if (mPendingCheckForTap == null) {
6973 mPendingCheckForTap = new CheckForTap();
6974 }
6975 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6976 } else {
6977 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006978 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006979 checkForLongClick(0);
6980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 break;
6982
6983 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006984 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006985 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 break;
6987
6988 case MotionEvent.ACTION_MOVE:
6989 final int x = (int) event.getX();
6990 final int y = (int) event.getY();
6991
6992 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006993 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006994 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006995 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006997 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006998 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999
Adam Powell4d6f0662012-02-21 15:11:11 -08007000 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 }
7003 break;
7004 }
7005 return true;
7006 }
7007
7008 return false;
7009 }
7010
7011 /**
Adam Powell10298662011-08-14 18:26:30 -07007012 * @hide
7013 */
7014 public boolean isInScrollingContainer() {
7015 ViewParent p = getParent();
7016 while (p != null && p instanceof ViewGroup) {
7017 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7018 return true;
7019 }
7020 p = p.getParent();
7021 }
7022 return false;
7023 }
7024
7025 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007026 * Remove the longpress detection timer.
7027 */
7028 private void removeLongPressCallback() {
7029 if (mPendingCheckForLongPress != null) {
7030 removeCallbacks(mPendingCheckForLongPress);
7031 }
7032 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007033
7034 /**
7035 * Remove the pending click action
7036 */
7037 private void removePerformClickCallback() {
7038 if (mPerformClick != null) {
7039 removeCallbacks(mPerformClick);
7040 }
7041 }
7042
Adam Powelle14579b2009-12-16 18:39:52 -08007043 /**
Romain Guya440b002010-02-24 15:57:54 -08007044 * Remove the prepress detection timer.
7045 */
7046 private void removeUnsetPressCallback() {
7047 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7048 setPressed(false);
7049 removeCallbacks(mUnsetPressedState);
7050 }
7051 }
7052
7053 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007054 * Remove the tap detection timer.
7055 */
7056 private void removeTapCallback() {
7057 if (mPendingCheckForTap != null) {
7058 mPrivateFlags &= ~PREPRESSED;
7059 removeCallbacks(mPendingCheckForTap);
7060 }
7061 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007062
7063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 * Cancels a pending long press. Your subclass can use this if you
7065 * want the context menu to come up if the user presses and holds
7066 * at the same place, but you don't want it to come up if they press
7067 * and then move around enough to cause scrolling.
7068 */
7069 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007070 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007071
7072 /*
7073 * The prepressed state handled by the tap callback is a display
7074 * construct, but the tap callback will post a long press callback
7075 * less its own timeout. Remove it here.
7076 */
7077 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078 }
7079
7080 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007081 * Remove the pending callback for sending a
7082 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7083 */
7084 private void removeSendViewScrolledAccessibilityEventCallback() {
7085 if (mSendViewScrolledAccessibilityEvent != null) {
7086 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7087 }
7088 }
7089
7090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 * Sets the TouchDelegate for this View.
7092 */
7093 public void setTouchDelegate(TouchDelegate delegate) {
7094 mTouchDelegate = delegate;
7095 }
7096
7097 /**
7098 * Gets the TouchDelegate for this View.
7099 */
7100 public TouchDelegate getTouchDelegate() {
7101 return mTouchDelegate;
7102 }
7103
7104 /**
7105 * Set flags controlling behavior of this view.
7106 *
7107 * @param flags Constant indicating the value which should be set
7108 * @param mask Constant indicating the bit range that should be changed
7109 */
7110 void setFlags(int flags, int mask) {
7111 int old = mViewFlags;
7112 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7113
7114 int changed = mViewFlags ^ old;
7115 if (changed == 0) {
7116 return;
7117 }
7118 int privateFlags = mPrivateFlags;
7119
7120 /* Check if the FOCUSABLE bit has changed */
7121 if (((changed & FOCUSABLE_MASK) != 0) &&
7122 ((privateFlags & HAS_BOUNDS) !=0)) {
7123 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7124 && ((privateFlags & FOCUSED) != 0)) {
7125 /* Give up focus if we are no longer focusable */
7126 clearFocus();
7127 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7128 && ((privateFlags & FOCUSED) == 0)) {
7129 /*
7130 * Tell the view system that we are now available to take focus
7131 * if no one else already has it.
7132 */
7133 if (mParent != null) mParent.focusableViewAvailable(this);
7134 }
7135 }
7136
7137 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7138 if ((changed & VISIBILITY_MASK) != 0) {
7139 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007140 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007141 * it was not visible. Marking it drawn ensures that the invalidation will
7142 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007144 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007145 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146
7147 needGlobalAttributesUpdate(true);
7148
7149 // a view becoming visible is worth notifying the parent
7150 // about in case nothing has focus. even if this specific view
7151 // isn't focusable, it may contain something that is, so let
7152 // the root view try to give this focus if nothing else does.
7153 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7154 mParent.focusableViewAvailable(this);
7155 }
7156 }
7157 }
7158
7159 /* Check if the GONE bit has changed */
7160 if ((changed & GONE) != 0) {
7161 needGlobalAttributesUpdate(false);
7162 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007163
Romain Guyecd80ee2009-12-03 17:13:02 -08007164 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7165 if (hasFocus()) clearFocus();
7166 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007167 if (mParent instanceof View) {
7168 // GONE views noop invalidation, so invalidate the parent
7169 ((View) mParent).invalidate(true);
7170 }
7171 // Mark the view drawn to ensure that it gets invalidated properly the next
7172 // time it is visible and gets invalidated
7173 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007174 }
7175 if (mAttachInfo != null) {
7176 mAttachInfo.mViewVisibilityChanged = true;
7177 }
7178 }
7179
7180 /* Check if the VISIBLE bit has changed */
7181 if ((changed & INVISIBLE) != 0) {
7182 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007183 /*
7184 * If this view is becoming invisible, set the DRAWN flag so that
7185 * the next invalidate() will not be skipped.
7186 */
7187 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188
7189 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
7190 // root view becoming invisible shouldn't clear focus
7191 if (getRootView() != this) {
7192 clearFocus();
7193 }
7194 }
7195 if (mAttachInfo != null) {
7196 mAttachInfo.mViewVisibilityChanged = true;
7197 }
7198 }
7199
Adam Powell326d8082009-12-09 15:10:07 -08007200 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007201 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007202 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7203 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007204 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007205 } else if (mParent != null) {
7206 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007207 }
Adam Powell326d8082009-12-09 15:10:07 -08007208 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7209 }
7210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7212 destroyDrawingCache();
7213 }
7214
7215 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7216 destroyDrawingCache();
7217 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007218 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 }
7220
7221 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7222 destroyDrawingCache();
7223 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7224 }
7225
7226 if ((changed & DRAW_MASK) != 0) {
7227 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007228 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007229 mPrivateFlags &= ~SKIP_DRAW;
7230 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7231 } else {
7232 mPrivateFlags |= SKIP_DRAW;
7233 }
7234 } else {
7235 mPrivateFlags &= ~SKIP_DRAW;
7236 }
7237 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007238 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007239 }
7240
7241 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007242 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007243 mParent.recomputeViewAttributes(this);
7244 }
7245 }
7246 }
7247
7248 /**
7249 * Change the view's z order in the tree, so it's on top of other sibling
7250 * views
7251 */
7252 public void bringToFront() {
7253 if (mParent != null) {
7254 mParent.bringChildToFront(this);
7255 }
7256 }
7257
7258 /**
7259 * This is called in response to an internal scroll in this view (i.e., the
7260 * view scrolled its own contents). This is typically as a result of
7261 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7262 * called.
7263 *
7264 * @param l Current horizontal scroll origin.
7265 * @param t Current vertical scroll origin.
7266 * @param oldl Previous horizontal scroll origin.
7267 * @param oldt Previous vertical scroll origin.
7268 */
7269 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007270 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7271 postSendViewScrolledAccessibilityEventCallback();
7272 }
7273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 mBackgroundSizeChanged = true;
7275
7276 final AttachInfo ai = mAttachInfo;
7277 if (ai != null) {
7278 ai.mViewScrollChanged = true;
7279 }
7280 }
7281
7282 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007283 * Interface definition for a callback to be invoked when the layout bounds of a view
7284 * changes due to layout processing.
7285 */
7286 public interface OnLayoutChangeListener {
7287 /**
7288 * Called when the focus state of a view has changed.
7289 *
7290 * @param v The view whose state has changed.
7291 * @param left The new value of the view's left property.
7292 * @param top The new value of the view's top property.
7293 * @param right The new value of the view's right property.
7294 * @param bottom The new value of the view's bottom property.
7295 * @param oldLeft The previous value of the view's left property.
7296 * @param oldTop The previous value of the view's top property.
7297 * @param oldRight The previous value of the view's right property.
7298 * @param oldBottom The previous value of the view's bottom property.
7299 */
7300 void onLayoutChange(View v, int left, int top, int right, int bottom,
7301 int oldLeft, int oldTop, int oldRight, int oldBottom);
7302 }
7303
7304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 * This is called during layout when the size of this view has changed. If
7306 * you were just added to the view hierarchy, you're called with the old
7307 * values of 0.
7308 *
7309 * @param w Current width of this view.
7310 * @param h Current height of this view.
7311 * @param oldw Old width of this view.
7312 * @param oldh Old height of this view.
7313 */
7314 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7315 }
7316
7317 /**
7318 * Called by draw to draw the child views. This may be overridden
7319 * by derived classes to gain control just before its children are drawn
7320 * (but after its own view has been drawn).
7321 * @param canvas the canvas on which to draw the view
7322 */
7323 protected void dispatchDraw(Canvas canvas) {
7324 }
7325
7326 /**
7327 * Gets the parent of this view. Note that the parent is a
7328 * ViewParent and not necessarily a View.
7329 *
7330 * @return Parent of this view.
7331 */
7332 public final ViewParent getParent() {
7333 return mParent;
7334 }
7335
7336 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007337 * Set the horizontal scrolled position of your view. This will cause a call to
7338 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7339 * invalidated.
7340 * @param value the x position to scroll to
7341 */
7342 public void setScrollX(int value) {
7343 scrollTo(value, mScrollY);
7344 }
7345
7346 /**
7347 * Set the vertical scrolled position of your view. This will cause a call to
7348 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7349 * invalidated.
7350 * @param value the y position to scroll to
7351 */
7352 public void setScrollY(int value) {
7353 scrollTo(mScrollX, value);
7354 }
7355
7356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 * Return the scrolled left position of this view. This is the left edge of
7358 * the displayed part of your view. You do not need to draw any pixels
7359 * farther left, since those are outside of the frame of your view on
7360 * screen.
7361 *
7362 * @return The left edge of the displayed part of your view, in pixels.
7363 */
7364 public final int getScrollX() {
7365 return mScrollX;
7366 }
7367
7368 /**
7369 * Return the scrolled top position of this view. This is the top edge of
7370 * the displayed part of your view. You do not need to draw any pixels above
7371 * it, since those are outside of the frame of your view on screen.
7372 *
7373 * @return The top edge of the displayed part of your view, in pixels.
7374 */
7375 public final int getScrollY() {
7376 return mScrollY;
7377 }
7378
7379 /**
7380 * Return the width of the your view.
7381 *
7382 * @return The width of your view, in pixels.
7383 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007384 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 public final int getWidth() {
7386 return mRight - mLeft;
7387 }
7388
7389 /**
7390 * Return the height of your view.
7391 *
7392 * @return The height of your view, in pixels.
7393 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007394 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 public final int getHeight() {
7396 return mBottom - mTop;
7397 }
7398
7399 /**
7400 * Return the visible drawing bounds of your view. Fills in the output
7401 * rectangle with the values from getScrollX(), getScrollY(),
7402 * getWidth(), and getHeight().
7403 *
7404 * @param outRect The (scrolled) drawing bounds of the view.
7405 */
7406 public void getDrawingRect(Rect outRect) {
7407 outRect.left = mScrollX;
7408 outRect.top = mScrollY;
7409 outRect.right = mScrollX + (mRight - mLeft);
7410 outRect.bottom = mScrollY + (mBottom - mTop);
7411 }
7412
7413 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007414 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7415 * raw width component (that is the result is masked by
7416 * {@link #MEASURED_SIZE_MASK}).
7417 *
7418 * @return The raw measured width of this view.
7419 */
7420 public final int getMeasuredWidth() {
7421 return mMeasuredWidth & MEASURED_SIZE_MASK;
7422 }
7423
7424 /**
7425 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007426 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007427 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 * This should be used during measurement and layout calculations only. Use
7429 * {@link #getWidth()} to see how wide a view is after layout.
7430 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007431 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007432 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007433 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007434 return mMeasuredWidth;
7435 }
7436
7437 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007438 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7439 * raw width component (that is the result is masked by
7440 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007442 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007443 */
7444 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007445 return mMeasuredHeight & MEASURED_SIZE_MASK;
7446 }
7447
7448 /**
7449 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007450 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007451 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7452 * This should be used during measurement and layout calculations only. Use
7453 * {@link #getHeight()} to see how wide a view is after layout.
7454 *
7455 * @return The measured width of this view as a bit mask.
7456 */
7457 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007458 return mMeasuredHeight;
7459 }
7460
7461 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007462 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7463 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7464 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7465 * and the height component is at the shifted bits
7466 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7467 */
7468 public final int getMeasuredState() {
7469 return (mMeasuredWidth&MEASURED_STATE_MASK)
7470 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7471 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7472 }
7473
7474 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007475 * The transform matrix of this view, which is calculated based on the current
7476 * roation, scale, and pivot properties.
7477 *
7478 * @see #getRotation()
7479 * @see #getScaleX()
7480 * @see #getScaleY()
7481 * @see #getPivotX()
7482 * @see #getPivotY()
7483 * @return The current transform matrix for the view
7484 */
7485 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007486 if (mTransformationInfo != null) {
7487 updateMatrix();
7488 return mTransformationInfo.mMatrix;
7489 }
7490 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007491 }
7492
7493 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007494 * Utility function to determine if the value is far enough away from zero to be
7495 * considered non-zero.
7496 * @param value A floating point value to check for zero-ness
7497 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7498 */
7499 private static boolean nonzero(float value) {
7500 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7501 }
7502
7503 /**
Jeff Brown86671742010-09-30 20:00:15 -07007504 * Returns true if the transform matrix is the identity matrix.
7505 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007506 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007507 * @return True if the transform matrix is the identity matrix, false otherwise.
7508 */
Jeff Brown86671742010-09-30 20:00:15 -07007509 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007510 if (mTransformationInfo != null) {
7511 updateMatrix();
7512 return mTransformationInfo.mMatrixIsIdentity;
7513 }
7514 return true;
7515 }
7516
7517 void ensureTransformationInfo() {
7518 if (mTransformationInfo == null) {
7519 mTransformationInfo = new TransformationInfo();
7520 }
Jeff Brown86671742010-09-30 20:00:15 -07007521 }
7522
7523 /**
7524 * Recomputes the transform matrix if necessary.
7525 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007526 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007527 final TransformationInfo info = mTransformationInfo;
7528 if (info == null) {
7529 return;
7530 }
7531 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007532 // transform-related properties have changed since the last time someone
7533 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007534
7535 // Figure out if we need to update the pivot point
7536 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007537 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7538 info.mPrevWidth = mRight - mLeft;
7539 info.mPrevHeight = mBottom - mTop;
7540 info.mPivotX = info.mPrevWidth / 2f;
7541 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007542 }
7543 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007544 info.mMatrix.reset();
7545 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7546 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7547 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7548 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007549 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007550 if (info.mCamera == null) {
7551 info.mCamera = new Camera();
7552 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007553 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007554 info.mCamera.save();
7555 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7556 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7557 info.mCamera.getMatrix(info.matrix3D);
7558 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7559 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7560 info.mPivotY + info.mTranslationY);
7561 info.mMatrix.postConcat(info.matrix3D);
7562 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007563 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007564 info.mMatrixDirty = false;
7565 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7566 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007567 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007568 }
7569
7570 /**
7571 * Utility method to retrieve the inverse of the current mMatrix property.
7572 * We cache the matrix to avoid recalculating it when transform properties
7573 * have not changed.
7574 *
7575 * @return The inverse of the current matrix of this view.
7576 */
Jeff Brown86671742010-09-30 20:00:15 -07007577 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007578 final TransformationInfo info = mTransformationInfo;
7579 if (info != null) {
7580 updateMatrix();
7581 if (info.mInverseMatrixDirty) {
7582 if (info.mInverseMatrix == null) {
7583 info.mInverseMatrix = new Matrix();
7584 }
7585 info.mMatrix.invert(info.mInverseMatrix);
7586 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007587 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007588 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007589 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007590 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007591 }
7592
7593 /**
Chet Haasea1cff502012-02-21 13:43:44 -08007594 * Gets the distance along the Z axis from the camera to this view.
7595 *
7596 * @see #setCameraDistance(float)
7597 *
7598 * @return The distance along the Z axis.
7599 */
7600 public float getCameraDistance() {
7601 ensureTransformationInfo();
7602 final float dpi = mResources.getDisplayMetrics().densityDpi;
7603 final TransformationInfo info = mTransformationInfo;
7604 if (info.mCamera == null) {
7605 info.mCamera = new Camera();
7606 info.matrix3D = new Matrix();
7607 }
7608 return -(info.mCamera.getLocationZ() * dpi);
7609 }
7610
7611 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007612 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7613 * views are drawn) from the camera to this view. The camera's distance
7614 * affects 3D transformations, for instance rotations around the X and Y
7615 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07007616 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08007617 * use a camera distance that's greater than the height (X axis rotation) or
7618 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007619 *
Romain Guya5364ee2011-02-24 14:46:04 -08007620 * <p>The distance of the camera from the view plane can have an affect on the
7621 * perspective distortion of the view when it is rotated around the x or y axis.
7622 * For example, a large distance will result in a large viewing angle, and there
7623 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07007624 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08007625 * also result in some drawing artifacts if the rotated view ends up partially
7626 * behind the camera (which is why the recommendation is to use a distance at
7627 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007628 *
Romain Guya5364ee2011-02-24 14:46:04 -08007629 * <p>The distance is expressed in "depth pixels." The default distance depends
7630 * on the screen density. For instance, on a medium density display, the
7631 * default distance is 1280. On a high density display, the default distance
7632 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007633 *
Romain Guya5364ee2011-02-24 14:46:04 -08007634 * <p>If you want to specify a distance that leads to visually consistent
7635 * results across various densities, use the following formula:</p>
7636 * <pre>
7637 * float scale = context.getResources().getDisplayMetrics().density;
7638 * view.setCameraDistance(distance * scale);
7639 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07007640 *
Romain Guya5364ee2011-02-24 14:46:04 -08007641 * <p>The density scale factor of a high density display is 1.5,
7642 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007643 *
Romain Guya5364ee2011-02-24 14:46:04 -08007644 * @param distance The distance in "depth pixels", if negative the opposite
7645 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07007646 *
7647 * @see #setRotationX(float)
7648 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08007649 */
7650 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007651 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08007652
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007653 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007654 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007655 final TransformationInfo info = mTransformationInfo;
7656 if (info.mCamera == null) {
7657 info.mCamera = new Camera();
7658 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007659 }
7660
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007661 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7662 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007663
Chet Haase9d1992d2012-03-13 11:03:25 -07007664 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007665 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07007666 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08007667 }
Romain Guya5364ee2011-02-24 14:46:04 -08007668 }
7669
7670 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007671 * The degrees that the view is rotated around the pivot point.
7672 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007673 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007674 * @see #getPivotX()
7675 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007676 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007677 * @return The degrees of rotation.
7678 */
Chet Haasea5531132012-02-02 13:41:44 -08007679 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007680 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007681 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007682 }
7683
7684 /**
Chet Haase897247b2010-09-09 14:54:47 -07007685 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7686 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007687 *
7688 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007689 *
7690 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007691 * @see #getPivotX()
7692 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007693 * @see #setRotationX(float)
7694 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007695 *
7696 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007697 */
7698 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007699 ensureTransformationInfo();
7700 final TransformationInfo info = mTransformationInfo;
7701 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007702 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07007703 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007704 info.mRotation = rotation;
7705 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007706 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007707 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7708 mDisplayList.setRotation(rotation);
7709 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007710 }
7711 }
7712
7713 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007714 * The degrees that the view is rotated around the vertical axis through the pivot point.
7715 *
7716 * @see #getPivotX()
7717 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007718 * @see #setRotationY(float)
7719 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007720 * @return The degrees of Y rotation.
7721 */
Chet Haasea5531132012-02-02 13:41:44 -08007722 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007723 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007724 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007725 }
7726
7727 /**
Chet Haase897247b2010-09-09 14:54:47 -07007728 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7729 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7730 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007731 *
Romain Guya5364ee2011-02-24 14:46:04 -08007732 * When rotating large views, it is recommended to adjust the camera distance
7733 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007734 *
7735 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007736 *
7737 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007738 * @see #getPivotX()
7739 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007740 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007741 * @see #setRotationX(float)
7742 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007743 *
7744 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007745 */
7746 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007747 ensureTransformationInfo();
7748 final TransformationInfo info = mTransformationInfo;
7749 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007750 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007751 info.mRotationY = rotationY;
7752 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007753 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007754 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7755 mDisplayList.setRotationY(rotationY);
7756 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007757 }
7758 }
7759
7760 /**
7761 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7762 *
7763 * @see #getPivotX()
7764 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007765 * @see #setRotationX(float)
7766 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007767 * @return The degrees of X rotation.
7768 */
Chet Haasea5531132012-02-02 13:41:44 -08007769 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007770 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007771 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007772 }
7773
7774 /**
Chet Haase897247b2010-09-09 14:54:47 -07007775 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7776 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7777 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007778 *
Romain Guya5364ee2011-02-24 14:46:04 -08007779 * When rotating large views, it is recommended to adjust the camera distance
7780 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007781 *
7782 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007783 *
7784 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007785 * @see #getPivotX()
7786 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007787 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007788 * @see #setRotationY(float)
7789 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007790 *
7791 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007792 */
7793 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007794 ensureTransformationInfo();
7795 final TransformationInfo info = mTransformationInfo;
7796 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007797 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007798 info.mRotationX = rotationX;
7799 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007800 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007801 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7802 mDisplayList.setRotationX(rotationX);
7803 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007804 }
7805 }
7806
7807 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007808 * The amount that the view is scaled in x around the pivot point, as a proportion of
7809 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7810 *
Joe Onorato93162322010-09-16 15:42:01 -04007811 * <p>By default, this is 1.0f.
7812 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007813 * @see #getPivotX()
7814 * @see #getPivotY()
7815 * @return The scaling factor.
7816 */
Chet Haasea5531132012-02-02 13:41:44 -08007817 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007818 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007819 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007820 }
7821
7822 /**
7823 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7824 * the view's unscaled width. A value of 1 means that no scaling is applied.
7825 *
7826 * @param scaleX The scaling factor.
7827 * @see #getPivotX()
7828 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007829 *
7830 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007831 */
7832 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007833 ensureTransformationInfo();
7834 final TransformationInfo info = mTransformationInfo;
7835 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007836 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007837 info.mScaleX = scaleX;
7838 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007839 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007840 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7841 mDisplayList.setScaleX(scaleX);
7842 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007843 }
7844 }
7845
7846 /**
7847 * The amount that the view is scaled in y around the pivot point, as a proportion of
7848 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7849 *
Joe Onorato93162322010-09-16 15:42:01 -04007850 * <p>By default, this is 1.0f.
7851 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007852 * @see #getPivotX()
7853 * @see #getPivotY()
7854 * @return The scaling factor.
7855 */
Chet Haasea5531132012-02-02 13:41:44 -08007856 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007857 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007858 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007859 }
7860
7861 /**
7862 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7863 * the view's unscaled width. A value of 1 means that no scaling is applied.
7864 *
7865 * @param scaleY The scaling factor.
7866 * @see #getPivotX()
7867 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007868 *
7869 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007870 */
7871 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007872 ensureTransformationInfo();
7873 final TransformationInfo info = mTransformationInfo;
7874 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007875 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007876 info.mScaleY = scaleY;
7877 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007878 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007879 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7880 mDisplayList.setScaleY(scaleY);
7881 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007882 }
7883 }
7884
7885 /**
7886 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7887 * and {@link #setScaleX(float) scaled}.
7888 *
7889 * @see #getRotation()
7890 * @see #getScaleX()
7891 * @see #getScaleY()
7892 * @see #getPivotY()
7893 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007894 *
7895 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007896 */
Chet Haasea5531132012-02-02 13:41:44 -08007897 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007898 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007899 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007900 }
7901
7902 /**
7903 * Sets the x location of the point around which the view is
7904 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007905 * By default, the pivot point is centered on the object.
7906 * Setting this property disables this behavior and causes the view to use only the
7907 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007908 *
7909 * @param pivotX The x location of the pivot point.
7910 * @see #getRotation()
7911 * @see #getScaleX()
7912 * @see #getScaleY()
7913 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007914 *
7915 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007916 */
7917 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007918 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007919 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007920 final TransformationInfo info = mTransformationInfo;
7921 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007922 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007923 info.mPivotX = pivotX;
7924 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007925 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007926 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7927 mDisplayList.setPivotX(pivotX);
7928 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007929 }
7930 }
7931
7932 /**
7933 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7934 * and {@link #setScaleY(float) scaled}.
7935 *
7936 * @see #getRotation()
7937 * @see #getScaleX()
7938 * @see #getScaleY()
7939 * @see #getPivotY()
7940 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007941 *
7942 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007943 */
Chet Haasea5531132012-02-02 13:41:44 -08007944 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007945 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007946 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007947 }
7948
7949 /**
7950 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007951 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7952 * Setting this property disables this behavior and causes the view to use only the
7953 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007954 *
7955 * @param pivotY The y location of the pivot point.
7956 * @see #getRotation()
7957 * @see #getScaleX()
7958 * @see #getScaleY()
7959 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007960 *
7961 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007962 */
7963 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007964 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007965 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007966 final TransformationInfo info = mTransformationInfo;
7967 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007968 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007969 info.mPivotY = pivotY;
7970 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007971 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007972 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7973 mDisplayList.setPivotY(pivotY);
7974 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007975 }
7976 }
7977
7978 /**
7979 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7980 * completely transparent and 1 means the view is completely opaque.
7981 *
Joe Onorato93162322010-09-16 15:42:01 -04007982 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007983 * @return The opacity of the view.
7984 */
Chet Haasea5531132012-02-02 13:41:44 -08007985 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007986 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007987 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007988 }
7989
7990 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07007991 * Returns whether this View has content which overlaps. This function, intended to be
7992 * overridden by specific View types, is an optimization when alpha is set on a view. If
7993 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
7994 * and then composited it into place, which can be expensive. If the view has no overlapping
7995 * rendering, the view can draw each primitive with the appropriate alpha value directly.
7996 * An example of overlapping rendering is a TextView with a background image, such as a
7997 * Button. An example of non-overlapping rendering is a TextView with no background, or
7998 * an ImageView with only the foreground image. The default implementation returns true;
7999 * subclasses should override if they have cases which can be optimized.
8000 *
8001 * @return true if the content in this view might overlap, false otherwise.
8002 */
8003 public boolean hasOverlappingRendering() {
8004 return true;
8005 }
8006
8007 /**
Romain Guy171c5922011-01-06 10:04:23 -08008008 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8009 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008010 *
Romain Guy171c5922011-01-06 10:04:23 -08008011 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8012 * responsible for applying the opacity itself. Otherwise, calling this method is
8013 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008014 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008015 *
Chet Haasea5531132012-02-02 13:41:44 -08008016 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8017 * performance implications. It is generally best to use the alpha property sparingly and
8018 * transiently, as in the case of fading animations.</p>
8019 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008020 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008021 *
Joe Malin32736f02011-01-19 16:14:20 -08008022 * @see #setLayerType(int, android.graphics.Paint)
8023 *
Chet Haase73066682010-11-29 15:55:32 -08008024 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008025 */
8026 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008027 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008028 if (mTransformationInfo.mAlpha != alpha) {
8029 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008030 if (onSetAlpha((int) (alpha * 255))) {
8031 mPrivateFlags |= ALPHA_SET;
8032 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008033 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008034 invalidate(true);
8035 } else {
8036 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008037 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008038 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8039 mDisplayList.setAlpha(alpha);
8040 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008041 }
Chet Haaseed032702010-10-01 14:05:54 -07008042 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008043 }
8044
8045 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008046 * Faster version of setAlpha() which performs the same steps except there are
8047 * no calls to invalidate(). The caller of this function should perform proper invalidation
8048 * on the parent and this object. The return value indicates whether the subclass handles
8049 * alpha (the return value for onSetAlpha()).
8050 *
8051 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008052 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8053 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008054 */
8055 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008056 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008057 if (mTransformationInfo.mAlpha != alpha) {
8058 mTransformationInfo.mAlpha = alpha;
8059 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8060 if (subclassHandlesAlpha) {
8061 mPrivateFlags |= ALPHA_SET;
8062 return true;
8063 } else {
8064 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08008065 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8066 mDisplayList.setAlpha(alpha);
8067 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008068 }
Chet Haasea00f3862011-02-22 06:34:40 -08008069 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008070 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008071 }
8072
8073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008074 * Top position of this view relative to its parent.
8075 *
8076 * @return The top of this view, in pixels.
8077 */
8078 @ViewDebug.CapturedViewProperty
8079 public final int getTop() {
8080 return mTop;
8081 }
8082
8083 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008084 * Sets the top position of this view relative to its parent. This method is meant to be called
8085 * by the layout system and should not generally be called otherwise, because the property
8086 * may be changed at any time by the layout.
8087 *
8088 * @param top The top of this view, in pixels.
8089 */
8090 public final void setTop(int top) {
8091 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008092 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008093 final boolean matrixIsIdentity = mTransformationInfo == null
8094 || mTransformationInfo.mMatrixIsIdentity;
8095 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008096 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008097 int minTop;
8098 int yLoc;
8099 if (top < mTop) {
8100 minTop = top;
8101 yLoc = top - mTop;
8102 } else {
8103 minTop = mTop;
8104 yLoc = 0;
8105 }
Chet Haasee9140a72011-02-16 16:23:29 -08008106 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008107 }
8108 } else {
8109 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008110 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008111 }
8112
Chet Haaseed032702010-10-01 14:05:54 -07008113 int width = mRight - mLeft;
8114 int oldHeight = mBottom - mTop;
8115
Chet Haase21cd1382010-09-01 17:42:29 -07008116 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008117 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8118 mDisplayList.setTop(mTop);
8119 }
Chet Haase21cd1382010-09-01 17:42:29 -07008120
Chet Haaseed032702010-10-01 14:05:54 -07008121 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8122
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008123 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008124 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8125 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008126 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008127 }
Chet Haase21cd1382010-09-01 17:42:29 -07008128 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008129 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008130 }
Chet Haase55dbb652010-12-21 20:15:08 -08008131 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008132 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008133 }
8134 }
8135
8136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 * Bottom position of this view relative to its parent.
8138 *
8139 * @return The bottom of this view, in pixels.
8140 */
8141 @ViewDebug.CapturedViewProperty
8142 public final int getBottom() {
8143 return mBottom;
8144 }
8145
8146 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008147 * True if this view has changed since the last time being drawn.
8148 *
8149 * @return The dirty state of this view.
8150 */
8151 public boolean isDirty() {
8152 return (mPrivateFlags & DIRTY_MASK) != 0;
8153 }
8154
8155 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008156 * Sets the bottom position of this view relative to its parent. This method is meant to be
8157 * called by the layout system and should not generally be called otherwise, because the
8158 * property may be changed at any time by the layout.
8159 *
8160 * @param bottom The bottom of this view, in pixels.
8161 */
8162 public final void setBottom(int bottom) {
8163 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008164 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008165 final boolean matrixIsIdentity = mTransformationInfo == null
8166 || mTransformationInfo.mMatrixIsIdentity;
8167 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008168 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008169 int maxBottom;
8170 if (bottom < mBottom) {
8171 maxBottom = mBottom;
8172 } else {
8173 maxBottom = bottom;
8174 }
Chet Haasee9140a72011-02-16 16:23:29 -08008175 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008176 }
8177 } else {
8178 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008179 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008180 }
8181
Chet Haaseed032702010-10-01 14:05:54 -07008182 int width = mRight - mLeft;
8183 int oldHeight = mBottom - mTop;
8184
Chet Haase21cd1382010-09-01 17:42:29 -07008185 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008186 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8187 mDisplayList.setBottom(mBottom);
8188 }
Chet Haase21cd1382010-09-01 17:42:29 -07008189
Chet Haaseed032702010-10-01 14:05:54 -07008190 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8191
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008192 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008193 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8194 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008195 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008196 }
Chet Haase21cd1382010-09-01 17:42:29 -07008197 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008198 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008199 }
Chet Haase55dbb652010-12-21 20:15:08 -08008200 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008201 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008202 }
8203 }
8204
8205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008206 * Left position of this view relative to its parent.
8207 *
8208 * @return The left edge of this view, in pixels.
8209 */
8210 @ViewDebug.CapturedViewProperty
8211 public final int getLeft() {
8212 return mLeft;
8213 }
8214
8215 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008216 * Sets the left position of this view relative to its parent. This method is meant to be called
8217 * by the layout system and should not generally be called otherwise, because the property
8218 * may be changed at any time by the layout.
8219 *
8220 * @param left The bottom of this view, in pixels.
8221 */
8222 public final void setLeft(int left) {
8223 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008224 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008225 final boolean matrixIsIdentity = mTransformationInfo == null
8226 || mTransformationInfo.mMatrixIsIdentity;
8227 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008228 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008229 int minLeft;
8230 int xLoc;
8231 if (left < mLeft) {
8232 minLeft = left;
8233 xLoc = left - mLeft;
8234 } else {
8235 minLeft = mLeft;
8236 xLoc = 0;
8237 }
Chet Haasee9140a72011-02-16 16:23:29 -08008238 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008239 }
8240 } else {
8241 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008242 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008243 }
8244
Chet Haaseed032702010-10-01 14:05:54 -07008245 int oldWidth = mRight - mLeft;
8246 int height = mBottom - mTop;
8247
Chet Haase21cd1382010-09-01 17:42:29 -07008248 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008249 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8250 mDisplayList.setLeft(left);
8251 }
Chet Haase21cd1382010-09-01 17:42:29 -07008252
Chet Haaseed032702010-10-01 14:05:54 -07008253 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8254
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008255 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008256 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8257 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008258 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008259 }
Chet Haase21cd1382010-09-01 17:42:29 -07008260 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008261 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008262 }
Chet Haase55dbb652010-12-21 20:15:08 -08008263 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008264 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008265 if (USE_DISPLAY_LIST_PROPERTIES) {
8266
8267 }
Chet Haase21cd1382010-09-01 17:42:29 -07008268 }
8269 }
8270
8271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008272 * Right position of this view relative to its parent.
8273 *
8274 * @return The right edge of this view, in pixels.
8275 */
8276 @ViewDebug.CapturedViewProperty
8277 public final int getRight() {
8278 return mRight;
8279 }
8280
8281 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008282 * Sets the right position of this view relative to its parent. This method is meant to be called
8283 * by the layout system and should not generally be called otherwise, because the property
8284 * may be changed at any time by the layout.
8285 *
8286 * @param right The bottom of this view, in pixels.
8287 */
8288 public final void setRight(int right) {
8289 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008290 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008291 final boolean matrixIsIdentity = mTransformationInfo == null
8292 || mTransformationInfo.mMatrixIsIdentity;
8293 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008294 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008295 int maxRight;
8296 if (right < mRight) {
8297 maxRight = mRight;
8298 } else {
8299 maxRight = right;
8300 }
Chet Haasee9140a72011-02-16 16:23:29 -08008301 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008302 }
8303 } else {
8304 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008305 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008306 }
8307
Chet Haaseed032702010-10-01 14:05:54 -07008308 int oldWidth = mRight - mLeft;
8309 int height = mBottom - mTop;
8310
Chet Haase21cd1382010-09-01 17:42:29 -07008311 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008312 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8313 mDisplayList.setRight(mRight);
8314 }
Chet Haase21cd1382010-09-01 17:42:29 -07008315
Chet Haaseed032702010-10-01 14:05:54 -07008316 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8317
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008318 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008319 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8320 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008321 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008322 }
Chet Haase21cd1382010-09-01 17:42:29 -07008323 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008324 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008325 }
Chet Haase55dbb652010-12-21 20:15:08 -08008326 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008327 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008328 }
8329 }
8330
8331 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008332 * The visual x position of this view, in pixels. This is equivalent to the
8333 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008334 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008335 *
Chet Haasedf030d22010-07-30 17:22:38 -07008336 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008337 */
Chet Haasea5531132012-02-02 13:41:44 -08008338 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008339 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008340 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008341 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008342
Chet Haasedf030d22010-07-30 17:22:38 -07008343 /**
8344 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8345 * {@link #setTranslationX(float) translationX} property to be the difference between
8346 * the x value passed in and the current {@link #getLeft() left} property.
8347 *
8348 * @param x The visual x position of this view, in pixels.
8349 */
8350 public void setX(float x) {
8351 setTranslationX(x - mLeft);
8352 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008353
Chet Haasedf030d22010-07-30 17:22:38 -07008354 /**
8355 * The visual y position of this view, in pixels. This is equivalent to the
8356 * {@link #setTranslationY(float) translationY} property plus the current
8357 * {@link #getTop() top} property.
8358 *
8359 * @return The visual y position of this view, in pixels.
8360 */
Chet Haasea5531132012-02-02 13:41:44 -08008361 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008362 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008363 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008364 }
8365
8366 /**
8367 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8368 * {@link #setTranslationY(float) translationY} property to be the difference between
8369 * the y value passed in and the current {@link #getTop() top} property.
8370 *
8371 * @param y The visual y position of this view, in pixels.
8372 */
8373 public void setY(float y) {
8374 setTranslationY(y - mTop);
8375 }
8376
8377
8378 /**
8379 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8380 * This position is post-layout, in addition to wherever the object's
8381 * layout placed it.
8382 *
8383 * @return The horizontal position of this view relative to its left position, in pixels.
8384 */
Chet Haasea5531132012-02-02 13:41:44 -08008385 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008386 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008387 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008388 }
8389
8390 /**
8391 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8392 * This effectively positions the object post-layout, in addition to wherever the object's
8393 * layout placed it.
8394 *
8395 * @param translationX The horizontal position of this view relative to its left position,
8396 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008397 *
8398 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008399 */
8400 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008401 ensureTransformationInfo();
8402 final TransformationInfo info = mTransformationInfo;
8403 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008404 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008405 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008406 info.mTranslationX = translationX;
8407 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008408 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008409 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8410 mDisplayList.setTranslationX(translationX);
8411 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008412 }
8413 }
8414
8415 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008416 * The horizontal location of this view relative to its {@link #getTop() top} position.
8417 * This position is post-layout, in addition to wherever the object's
8418 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008419 *
Chet Haasedf030d22010-07-30 17:22:38 -07008420 * @return The vertical position of this view relative to its top position,
8421 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008422 */
Chet Haasea5531132012-02-02 13:41:44 -08008423 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008424 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008425 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008426 }
8427
8428 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008429 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8430 * This effectively positions the object post-layout, in addition to wherever the object's
8431 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008432 *
Chet Haasedf030d22010-07-30 17:22:38 -07008433 * @param translationY The vertical position of this view relative to its top position,
8434 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008435 *
8436 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008437 */
Chet Haasedf030d22010-07-30 17:22:38 -07008438 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008439 ensureTransformationInfo();
8440 final TransformationInfo info = mTransformationInfo;
8441 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008442 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008443 info.mTranslationY = translationY;
8444 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008445 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008446 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8447 mDisplayList.setTranslationY(translationY);
8448 }
Chet Haasedf030d22010-07-30 17:22:38 -07008449 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008450 }
8451
8452 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008453 * Hit rectangle in parent's coordinates
8454 *
8455 * @param outRect The hit rectangle of the view.
8456 */
8457 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008458 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008459 final TransformationInfo info = mTransformationInfo;
8460 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008461 outRect.set(mLeft, mTop, mRight, mBottom);
8462 } else {
8463 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008464 tmpRect.set(-info.mPivotX, -info.mPivotY,
8465 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8466 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008467 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8468 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008469 }
8470 }
8471
8472 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008473 * Determines whether the given point, in local coordinates is inside the view.
8474 */
8475 /*package*/ final boolean pointInView(float localX, float localY) {
8476 return localX >= 0 && localX < (mRight - mLeft)
8477 && localY >= 0 && localY < (mBottom - mTop);
8478 }
8479
8480 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008481 * Utility method to determine whether the given point, in local coordinates,
8482 * is inside the view, where the area of the view is expanded by the slop factor.
8483 * This method is called while processing touch-move events to determine if the event
8484 * is still within the view.
8485 */
8486 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008487 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008488 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008489 }
8490
8491 /**
8492 * When a view has focus and the user navigates away from it, the next view is searched for
8493 * starting from the rectangle filled in by this method.
8494 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008495 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8496 * of the view. However, if your view maintains some idea of internal selection,
8497 * such as a cursor, or a selected row or column, you should override this method and
8498 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 *
8500 * @param r The rectangle to fill in, in this view's coordinates.
8501 */
8502 public void getFocusedRect(Rect r) {
8503 getDrawingRect(r);
8504 }
8505
8506 /**
8507 * If some part of this view is not clipped by any of its parents, then
8508 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008509 * coordinates (without taking possible View rotations into account), offset
8510 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8511 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 *
8513 * @param r If true is returned, r holds the global coordinates of the
8514 * visible portion of this view.
8515 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8516 * between this view and its root. globalOffet may be null.
8517 * @return true if r is non-empty (i.e. part of the view is visible at the
8518 * root level.
8519 */
8520 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8521 int width = mRight - mLeft;
8522 int height = mBottom - mTop;
8523 if (width > 0 && height > 0) {
8524 r.set(0, 0, width, height);
8525 if (globalOffset != null) {
8526 globalOffset.set(-mScrollX, -mScrollY);
8527 }
8528 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8529 }
8530 return false;
8531 }
8532
8533 public final boolean getGlobalVisibleRect(Rect r) {
8534 return getGlobalVisibleRect(r, null);
8535 }
8536
8537 public final boolean getLocalVisibleRect(Rect r) {
8538 Point offset = new Point();
8539 if (getGlobalVisibleRect(r, offset)) {
8540 r.offset(-offset.x, -offset.y); // make r local
8541 return true;
8542 }
8543 return false;
8544 }
8545
8546 /**
8547 * Offset this view's vertical location by the specified number of pixels.
8548 *
8549 * @param offset the number of pixels to offset the view by
8550 */
8551 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008552 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008553 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008554 final boolean matrixIsIdentity = mTransformationInfo == null
8555 || mTransformationInfo.mMatrixIsIdentity;
8556 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008557 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8558 invalidateViewProperty(false, false);
8559 } else {
8560 final ViewParent p = mParent;
8561 if (p != null && mAttachInfo != null) {
8562 final Rect r = mAttachInfo.mTmpInvalRect;
8563 int minTop;
8564 int maxBottom;
8565 int yLoc;
8566 if (offset < 0) {
8567 minTop = mTop + offset;
8568 maxBottom = mBottom;
8569 yLoc = offset;
8570 } else {
8571 minTop = mTop;
8572 maxBottom = mBottom + offset;
8573 yLoc = 0;
8574 }
8575 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8576 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008577 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008578 }
8579 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008580 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008581 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008582
Chet Haasec3aa3612010-06-17 08:50:37 -07008583 mTop += offset;
8584 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008585 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8586 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008587 invalidateViewProperty(false, false);
8588 } else {
8589 if (!matrixIsIdentity) {
8590 invalidateViewProperty(false, true);
8591 }
8592 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008593 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008595 }
8596
8597 /**
8598 * Offset this view's horizontal location by the specified amount of pixels.
8599 *
8600 * @param offset the numer of pixels to offset the view by
8601 */
8602 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008603 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008604 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008605 final boolean matrixIsIdentity = mTransformationInfo == null
8606 || mTransformationInfo.mMatrixIsIdentity;
8607 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008608 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8609 invalidateViewProperty(false, false);
8610 } else {
8611 final ViewParent p = mParent;
8612 if (p != null && mAttachInfo != null) {
8613 final Rect r = mAttachInfo.mTmpInvalRect;
8614 int minLeft;
8615 int maxRight;
8616 if (offset < 0) {
8617 minLeft = mLeft + offset;
8618 maxRight = mRight;
8619 } else {
8620 minLeft = mLeft;
8621 maxRight = mRight + offset;
8622 }
8623 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
8624 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008625 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008626 }
8627 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008628 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008629 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008630
Chet Haasec3aa3612010-06-17 08:50:37 -07008631 mLeft += offset;
8632 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008633 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8634 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008635 invalidateViewProperty(false, false);
8636 } else {
8637 if (!matrixIsIdentity) {
8638 invalidateViewProperty(false, true);
8639 }
8640 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008641 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 }
8644
8645 /**
8646 * Get the LayoutParams associated with this view. All views should have
8647 * layout parameters. These supply parameters to the <i>parent</i> of this
8648 * view specifying how it should be arranged. There are many subclasses of
8649 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8650 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008651 *
8652 * This method may return null if this View is not attached to a parent
8653 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8654 * was not invoked successfully. When a View is attached to a parent
8655 * ViewGroup, this method must not return null.
8656 *
8657 * @return The LayoutParams associated with this view, or null if no
8658 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008660 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 public ViewGroup.LayoutParams getLayoutParams() {
8662 return mLayoutParams;
8663 }
8664
8665 /**
8666 * Set the layout parameters associated with this view. These supply
8667 * parameters to the <i>parent</i> of this view specifying how it should be
8668 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8669 * correspond to the different subclasses of ViewGroup that are responsible
8670 * for arranging their children.
8671 *
Romain Guy01c174b2011-02-22 11:51:06 -08008672 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 */
8674 public void setLayoutParams(ViewGroup.LayoutParams params) {
8675 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008676 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008677 }
8678 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008679 if (mParent instanceof ViewGroup) {
8680 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 requestLayout();
8683 }
8684
8685 /**
8686 * Set the scrolled position of your view. This will cause a call to
8687 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8688 * invalidated.
8689 * @param x the x position to scroll to
8690 * @param y the y position to scroll to
8691 */
8692 public void scrollTo(int x, int y) {
8693 if (mScrollX != x || mScrollY != y) {
8694 int oldX = mScrollX;
8695 int oldY = mScrollY;
8696 mScrollX = x;
8697 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008698 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008700 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008701 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 }
8704 }
8705
8706 /**
8707 * Move the scrolled position of your view. This will cause a call to
8708 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8709 * invalidated.
8710 * @param x the amount of pixels to scroll by horizontally
8711 * @param y the amount of pixels to scroll by vertically
8712 */
8713 public void scrollBy(int x, int y) {
8714 scrollTo(mScrollX + x, mScrollY + y);
8715 }
8716
8717 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008718 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8719 * animation to fade the scrollbars out after a default delay. If a subclass
8720 * provides animated scrolling, the start delay should equal the duration
8721 * of the scrolling animation.</p>
8722 *
8723 * <p>The animation starts only if at least one of the scrollbars is
8724 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8725 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8726 * this method returns true, and false otherwise. If the animation is
8727 * started, this method calls {@link #invalidate()}; in that case the
8728 * caller should not call {@link #invalidate()}.</p>
8729 *
8730 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008731 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008732 *
8733 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8734 * and {@link #scrollTo(int, int)}.</p>
8735 *
8736 * @return true if the animation is played, false otherwise
8737 *
8738 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008739 * @see #scrollBy(int, int)
8740 * @see #scrollTo(int, int)
8741 * @see #isHorizontalScrollBarEnabled()
8742 * @see #isVerticalScrollBarEnabled()
8743 * @see #setHorizontalScrollBarEnabled(boolean)
8744 * @see #setVerticalScrollBarEnabled(boolean)
8745 */
8746 protected boolean awakenScrollBars() {
8747 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008748 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008749 }
8750
8751 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008752 * Trigger the scrollbars to draw.
8753 * This method differs from awakenScrollBars() only in its default duration.
8754 * initialAwakenScrollBars() will show the scroll bars for longer than
8755 * usual to give the user more of a chance to notice them.
8756 *
8757 * @return true if the animation is played, false otherwise.
8758 */
8759 private boolean initialAwakenScrollBars() {
8760 return mScrollCache != null &&
8761 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8762 }
8763
8764 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008765 * <p>
8766 * Trigger the scrollbars to draw. When invoked this method starts an
8767 * animation to fade the scrollbars out after a fixed delay. If a subclass
8768 * provides animated scrolling, the start delay should equal the duration of
8769 * the scrolling animation.
8770 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008771 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008772 * <p>
8773 * The animation starts only if at least one of the scrollbars is enabled,
8774 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8775 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8776 * this method returns true, and false otherwise. If the animation is
8777 * started, this method calls {@link #invalidate()}; in that case the caller
8778 * should not call {@link #invalidate()}.
8779 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008780 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008781 * <p>
8782 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008783 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008784 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008785 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008786 * @param startDelay the delay, in milliseconds, after which the animation
8787 * should start; when the delay is 0, the animation starts
8788 * immediately
8789 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008790 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008791 * @see #scrollBy(int, int)
8792 * @see #scrollTo(int, int)
8793 * @see #isHorizontalScrollBarEnabled()
8794 * @see #isVerticalScrollBarEnabled()
8795 * @see #setHorizontalScrollBarEnabled(boolean)
8796 * @see #setVerticalScrollBarEnabled(boolean)
8797 */
8798 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008799 return awakenScrollBars(startDelay, true);
8800 }
Joe Malin32736f02011-01-19 16:14:20 -08008801
Mike Cleron290947b2009-09-29 18:34:32 -07008802 /**
8803 * <p>
8804 * Trigger the scrollbars to draw. When invoked this method starts an
8805 * animation to fade the scrollbars out after a fixed delay. If a subclass
8806 * provides animated scrolling, the start delay should equal the duration of
8807 * the scrolling animation.
8808 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008809 *
Mike Cleron290947b2009-09-29 18:34:32 -07008810 * <p>
8811 * The animation starts only if at least one of the scrollbars is enabled,
8812 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8813 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8814 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008815 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008816 * is set to true; in that case the caller
8817 * should not call {@link #invalidate()}.
8818 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008819 *
Mike Cleron290947b2009-09-29 18:34:32 -07008820 * <p>
8821 * This method should be invoked everytime a subclass directly updates the
8822 * scroll parameters.
8823 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008824 *
Mike Cleron290947b2009-09-29 18:34:32 -07008825 * @param startDelay the delay, in milliseconds, after which the animation
8826 * should start; when the delay is 0, the animation starts
8827 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008828 *
Mike Cleron290947b2009-09-29 18:34:32 -07008829 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008830 *
Mike Cleron290947b2009-09-29 18:34:32 -07008831 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008832 *
Mike Cleron290947b2009-09-29 18:34:32 -07008833 * @see #scrollBy(int, int)
8834 * @see #scrollTo(int, int)
8835 * @see #isHorizontalScrollBarEnabled()
8836 * @see #isVerticalScrollBarEnabled()
8837 * @see #setHorizontalScrollBarEnabled(boolean)
8838 * @see #setVerticalScrollBarEnabled(boolean)
8839 */
8840 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008841 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008842
Mike Cleronf116bf82009-09-27 19:14:12 -07008843 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8844 return false;
8845 }
8846
8847 if (scrollCache.scrollBar == null) {
8848 scrollCache.scrollBar = new ScrollBarDrawable();
8849 }
8850
8851 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8852
Mike Cleron290947b2009-09-29 18:34:32 -07008853 if (invalidate) {
8854 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008855 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008856 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008857
8858 if (scrollCache.state == ScrollabilityCache.OFF) {
8859 // FIXME: this is copied from WindowManagerService.
8860 // We should get this value from the system when it
8861 // is possible to do so.
8862 final int KEY_REPEAT_FIRST_DELAY = 750;
8863 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8864 }
8865
8866 // Tell mScrollCache when we should start fading. This may
8867 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008868 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008869 scrollCache.fadeStartTime = fadeStartTime;
8870 scrollCache.state = ScrollabilityCache.ON;
8871
8872 // Schedule our fader to run, unscheduling any old ones first
8873 if (mAttachInfo != null) {
8874 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8875 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8876 }
8877
8878 return true;
8879 }
8880
8881 return false;
8882 }
8883
8884 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008885 * Do not invalidate views which are not visible and which are not running an animation. They
8886 * will not get drawn and they should not set dirty flags as if they will be drawn
8887 */
8888 private boolean skipInvalidate() {
8889 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8890 (!(mParent instanceof ViewGroup) ||
8891 !((ViewGroup) mParent).isViewTransitioning(this));
8892 }
8893 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008894 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008895 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8896 * in the future. This must be called from a UI thread. To call from a non-UI
8897 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008898 *
8899 * WARNING: This method is destructive to dirty.
8900 * @param dirty the rectangle representing the bounds of the dirty region
8901 */
8902 public void invalidate(Rect dirty) {
8903 if (ViewDebug.TRACE_HIERARCHY) {
8904 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8905 }
8906
Chet Haaseaceafe62011-08-26 15:44:33 -07008907 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008908 return;
8909 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008910 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008911 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8912 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008914 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008915 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 final ViewParent p = mParent;
8917 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008918 //noinspection PointlessBooleanExpression,ConstantConditions
8919 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8920 if (p != null && ai != null && ai.mHardwareAccelerated) {
8921 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008922 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008923 p.invalidateChild(this, null);
8924 return;
8925 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008927 if (p != null && ai != null) {
8928 final int scrollX = mScrollX;
8929 final int scrollY = mScrollY;
8930 final Rect r = ai.mTmpInvalRect;
8931 r.set(dirty.left - scrollX, dirty.top - scrollY,
8932 dirty.right - scrollX, dirty.bottom - scrollY);
8933 mParent.invalidateChild(this, r);
8934 }
8935 }
8936 }
8937
8938 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008939 * 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 -08008940 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008941 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8942 * will be called at some point in the future. This must be called from
8943 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008944 * @param l the left position of the dirty region
8945 * @param t the top position of the dirty region
8946 * @param r the right position of the dirty region
8947 * @param b the bottom position of the dirty region
8948 */
8949 public void invalidate(int l, int t, int r, int b) {
8950 if (ViewDebug.TRACE_HIERARCHY) {
8951 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8952 }
8953
Chet Haaseaceafe62011-08-26 15:44:33 -07008954 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008955 return;
8956 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008957 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008958 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8959 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008961 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008962 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 final ViewParent p = mParent;
8964 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008965 //noinspection PointlessBooleanExpression,ConstantConditions
8966 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8967 if (p != null && ai != null && ai.mHardwareAccelerated) {
8968 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008969 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008970 p.invalidateChild(this, null);
8971 return;
8972 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008974 if (p != null && ai != null && l < r && t < b) {
8975 final int scrollX = mScrollX;
8976 final int scrollY = mScrollY;
8977 final Rect tmpr = ai.mTmpInvalRect;
8978 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8979 p.invalidateChild(this, tmpr);
8980 }
8981 }
8982 }
8983
8984 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008985 * Invalidate the whole view. If the view is visible,
8986 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8987 * the future. This must be called from a UI thread. To call from a non-UI thread,
8988 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989 */
8990 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008991 invalidate(true);
8992 }
Joe Malin32736f02011-01-19 16:14:20 -08008993
Chet Haaseed032702010-10-01 14:05:54 -07008994 /**
8995 * This is where the invalidate() work actually happens. A full invalidate()
8996 * causes the drawing cache to be invalidated, but this function can be called with
8997 * invalidateCache set to false to skip that invalidation step for cases that do not
8998 * need it (for example, a component that remains at the same dimensions with the same
8999 * content).
9000 *
9001 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9002 * well. This is usually true for a full invalidate, but may be set to false if the
9003 * View's contents or dimensions have not changed.
9004 */
Romain Guy849d0a32011-02-01 17:20:48 -08009005 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 if (ViewDebug.TRACE_HIERARCHY) {
9007 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9008 }
9009
Chet Haaseaceafe62011-08-26 15:44:33 -07009010 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009011 return;
9012 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009013 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009014 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009015 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9016 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009017 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009018 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009019 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009020 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009021 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009023 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009024 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009025 //noinspection PointlessBooleanExpression,ConstantConditions
9026 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9027 if (p != null && ai != null && ai.mHardwareAccelerated) {
9028 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009029 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009030 p.invalidateChild(this, null);
9031 return;
9032 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009033 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009035 if (p != null && ai != null) {
9036 final Rect r = ai.mTmpInvalRect;
9037 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9038 // Don't call invalidate -- we don't want to internally scroll
9039 // our own bounds
9040 p.invalidateChild(this, r);
9041 }
9042 }
9043 }
9044
9045 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009046 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9047 * set any flags or handle all of the cases handled by the default invalidation methods.
9048 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9049 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9050 * walk up the hierarchy, transforming the dirty rect as necessary.
9051 *
9052 * The method also handles normal invalidation logic if display list properties are not
9053 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9054 * backup approach, to handle these cases used in the various property-setting methods.
9055 *
9056 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9057 * are not being used in this view
9058 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9059 * list properties are not being used in this view
9060 */
9061 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
9062 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
9063 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
9064 if (invalidateParent) {
9065 invalidateParentCaches();
9066 }
9067 if (forceRedraw) {
9068 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9069 }
9070 invalidate(false);
9071 } else {
9072 final AttachInfo ai = mAttachInfo;
9073 final ViewParent p = mParent;
9074 if (p != null && ai != null) {
9075 final Rect r = ai.mTmpInvalRect;
9076 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9077 if (mParent instanceof ViewGroup) {
9078 ((ViewGroup) mParent).invalidateChildFast(this, r);
9079 } else {
9080 mParent.invalidateChild(this, r);
9081 }
9082 }
9083 }
9084 }
9085
9086 /**
9087 * Utility method to transform a given Rect by the current matrix of this view.
9088 */
9089 void transformRect(final Rect rect) {
9090 if (!getMatrix().isIdentity()) {
9091 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9092 boundingRect.set(rect);
9093 getMatrix().mapRect(boundingRect);
9094 rect.set((int) (boundingRect.left - 0.5f),
9095 (int) (boundingRect.top - 0.5f),
9096 (int) (boundingRect.right + 0.5f),
9097 (int) (boundingRect.bottom + 0.5f));
9098 }
9099 }
9100
9101 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009102 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009103 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9104 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009105 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9106 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009107 *
9108 * @hide
9109 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009110 protected void invalidateParentCaches() {
9111 if (mParent instanceof View) {
9112 ((View) mParent).mPrivateFlags |= INVALIDATED;
9113 }
9114 }
Joe Malin32736f02011-01-19 16:14:20 -08009115
Romain Guy0fd89bf2011-01-26 15:41:30 -08009116 /**
9117 * Used to indicate that the parent of this view should be invalidated. This functionality
9118 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9119 * which is necessary when various parent-managed properties of the view change, such as
9120 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9121 * an invalidation event to the parent.
9122 *
9123 * @hide
9124 */
9125 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009126 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009127 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009128 }
9129 }
9130
9131 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009132 * Indicates whether this View is opaque. An opaque View guarantees that it will
9133 * draw all the pixels overlapping its bounds using a fully opaque color.
9134 *
9135 * Subclasses of View should override this method whenever possible to indicate
9136 * whether an instance is opaque. Opaque Views are treated in a special way by
9137 * the View hierarchy, possibly allowing it to perform optimizations during
9138 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009139 *
Romain Guy24443ea2009-05-11 11:56:30 -07009140 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009141 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009142 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009143 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009144 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009145 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9146 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009147 }
9148
Adam Powell20232d02010-12-08 21:08:53 -08009149 /**
9150 * @hide
9151 */
9152 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009153 // Opaque if:
9154 // - Has a background
9155 // - Background is opaque
9156 // - Doesn't have scrollbars or scrollbars are inside overlay
9157
Philip Milne6c8ea062012-04-03 17:38:43 -07009158 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009159 mPrivateFlags |= OPAQUE_BACKGROUND;
9160 } else {
9161 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9162 }
9163
9164 final int flags = mViewFlags;
9165 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9166 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9167 mPrivateFlags |= OPAQUE_SCROLLBARS;
9168 } else {
9169 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9170 }
9171 }
9172
9173 /**
9174 * @hide
9175 */
9176 protected boolean hasOpaqueScrollbars() {
9177 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009178 }
9179
9180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009181 * @return A handler associated with the thread running the View. This
9182 * handler can be used to pump events in the UI events queue.
9183 */
9184 public Handler getHandler() {
9185 if (mAttachInfo != null) {
9186 return mAttachInfo.mHandler;
9187 }
9188 return null;
9189 }
9190
9191 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009192 * Gets the view root associated with the View.
9193 * @return The view root, or null if none.
9194 * @hide
9195 */
9196 public ViewRootImpl getViewRootImpl() {
9197 if (mAttachInfo != null) {
9198 return mAttachInfo.mViewRootImpl;
9199 }
9200 return null;
9201 }
9202
9203 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009204 * <p>Causes the Runnable to be added to the message queue.
9205 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009206 *
Romain Guye63a4f32011-08-11 11:33:31 -07009207 * <p>This method can be invoked from outside of the UI thread
9208 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009209 *
9210 * @param action The Runnable that will be executed.
9211 *
9212 * @return Returns true if the Runnable was successfully placed in to the
9213 * message queue. Returns false on failure, usually because the
9214 * looper processing the message queue is exiting.
9215 */
9216 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009217 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009218 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009219 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009220 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009221 // Assume that post will succeed later
9222 ViewRootImpl.getRunQueue().post(action);
9223 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009224 }
9225
9226 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009227 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009229 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009230 *
Romain Guye63a4f32011-08-11 11:33:31 -07009231 * <p>This method can be invoked from outside of the UI thread
9232 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 *
9234 * @param action The Runnable that will be executed.
9235 * @param delayMillis The delay (in milliseconds) until the Runnable
9236 * will be executed.
9237 *
9238 * @return true if the Runnable was successfully placed in to the
9239 * message queue. Returns false on failure, usually because the
9240 * looper processing the message queue is exiting. Note that a
9241 * result of true does not mean the Runnable will be processed --
9242 * if the looper is quit before the delivery time of the message
9243 * occurs then the message will be dropped.
9244 */
9245 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009246 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009247 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009248 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009249 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009250 // Assume that post will succeed later
9251 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9252 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009253 }
9254
9255 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009256 * <p>Causes the Runnable to execute on the next animation time step.
9257 * The runnable will be run on the user interface thread.</p>
9258 *
9259 * <p>This method can be invoked from outside of the UI thread
9260 * only when this View is attached to a window.</p>
9261 *
9262 * @param action The Runnable that will be executed.
9263 *
9264 * @hide
9265 */
9266 public void postOnAnimation(Runnable action) {
9267 final AttachInfo attachInfo = mAttachInfo;
9268 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009269 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9270 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009271 } else {
9272 // Assume that post will succeed later
9273 ViewRootImpl.getRunQueue().post(action);
9274 }
9275 }
9276
9277 /**
9278 * <p>Causes the Runnable to execute on the next animation time step,
9279 * after the specified amount of time elapses.
9280 * The runnable will be run on the user interface thread.</p>
9281 *
9282 * <p>This method can be invoked from outside of the UI thread
9283 * only when this View is attached to a window.</p>
9284 *
9285 * @param action The Runnable that will be executed.
9286 * @param delayMillis The delay (in milliseconds) until the Runnable
9287 * will be executed.
9288 *
9289 * @hide
9290 */
9291 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9292 final AttachInfo attachInfo = mAttachInfo;
9293 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009294 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9295 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009296 } else {
9297 // Assume that post will succeed later
9298 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9299 }
9300 }
9301
9302 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009303 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009304 *
Romain Guye63a4f32011-08-11 11:33:31 -07009305 * <p>This method can be invoked from outside of the UI thread
9306 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 *
9308 * @param action The Runnable to remove from the message handling queue
9309 *
9310 * @return true if this view could ask the Handler to remove the Runnable,
9311 * false otherwise. When the returned value is true, the Runnable
9312 * may or may not have been actually removed from the message queue
9313 * (for instance, if the Runnable was not in the queue already.)
9314 */
9315 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009316 if (action != null) {
9317 final AttachInfo attachInfo = mAttachInfo;
9318 if (attachInfo != null) {
9319 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009320 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9321 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009322 } else {
9323 // Assume that post will succeed later
9324 ViewRootImpl.getRunQueue().removeCallbacks(action);
9325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 return true;
9328 }
9329
9330 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009331 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9332 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009333 *
Romain Guye63a4f32011-08-11 11:33:31 -07009334 * <p>This method can be invoked from outside of the UI thread
9335 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009336 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009337 * @see #invalidate()
9338 */
9339 public void postInvalidate() {
9340 postInvalidateDelayed(0);
9341 }
9342
9343 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009344 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9345 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009346 *
Romain Guye63a4f32011-08-11 11:33:31 -07009347 * <p>This method can be invoked from outside of the UI thread
9348 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009349 *
9350 * @param left The left coordinate of the rectangle to invalidate.
9351 * @param top The top coordinate of the rectangle to invalidate.
9352 * @param right The right coordinate of the rectangle to invalidate.
9353 * @param bottom The bottom coordinate of the rectangle to invalidate.
9354 *
9355 * @see #invalidate(int, int, int, int)
9356 * @see #invalidate(Rect)
9357 */
9358 public void postInvalidate(int left, int top, int right, int bottom) {
9359 postInvalidateDelayed(0, left, top, right, bottom);
9360 }
9361
9362 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009363 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9364 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009365 *
Romain Guye63a4f32011-08-11 11:33:31 -07009366 * <p>This method can be invoked from outside of the UI thread
9367 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009368 *
9369 * @param delayMilliseconds the duration in milliseconds to delay the
9370 * invalidation by
9371 */
9372 public void postInvalidateDelayed(long delayMilliseconds) {
9373 // We try only with the AttachInfo because there's no point in invalidating
9374 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009375 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009376 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009377 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009378 }
9379 }
9380
9381 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009382 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9383 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009384 *
Romain Guye63a4f32011-08-11 11:33:31 -07009385 * <p>This method can be invoked from outside of the UI thread
9386 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009387 *
9388 * @param delayMilliseconds the duration in milliseconds to delay the
9389 * invalidation by
9390 * @param left The left coordinate of the rectangle to invalidate.
9391 * @param top The top coordinate of the rectangle to invalidate.
9392 * @param right The right coordinate of the rectangle to invalidate.
9393 * @param bottom The bottom coordinate of the rectangle to invalidate.
9394 */
9395 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9396 int right, int bottom) {
9397
9398 // We try only with the AttachInfo because there's no point in invalidating
9399 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009400 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009401 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009402 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9403 info.target = this;
9404 info.left = left;
9405 info.top = top;
9406 info.right = right;
9407 info.bottom = bottom;
9408
Jeff Browna175a5b2012-02-15 19:18:31 -08009409 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009410 }
9411 }
9412
9413 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08009414 * <p>Cause an invalidate to happen on the next animation time step, typically the
9415 * next display frame.</p>
9416 *
9417 * <p>This method can be invoked from outside of the UI thread
9418 * only when this View is attached to a window.</p>
9419 *
9420 * @hide
9421 */
9422 public void postInvalidateOnAnimation() {
9423 // We try only with the AttachInfo because there's no point in invalidating
9424 // if we are not attached to our window
9425 final AttachInfo attachInfo = mAttachInfo;
9426 if (attachInfo != null) {
9427 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
9428 }
9429 }
9430
9431 /**
9432 * <p>Cause an invalidate of the specified area to happen on the next animation
9433 * time step, typically the next display frame.</p>
9434 *
9435 * <p>This method can be invoked from outside of the UI thread
9436 * only when this View is attached to a window.</p>
9437 *
9438 * @param left The left coordinate of the rectangle to invalidate.
9439 * @param top The top coordinate of the rectangle to invalidate.
9440 * @param right The right coordinate of the rectangle to invalidate.
9441 * @param bottom The bottom coordinate of the rectangle to invalidate.
9442 *
9443 * @hide
9444 */
9445 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9446 // We try only with the AttachInfo because there's no point in invalidating
9447 // if we are not attached to our window
9448 final AttachInfo attachInfo = mAttachInfo;
9449 if (attachInfo != null) {
9450 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9451 info.target = this;
9452 info.left = left;
9453 info.top = top;
9454 info.right = right;
9455 info.bottom = bottom;
9456
9457 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9458 }
9459 }
9460
9461 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009462 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9463 * This event is sent at most once every
9464 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9465 */
9466 private void postSendViewScrolledAccessibilityEventCallback() {
9467 if (mSendViewScrolledAccessibilityEvent == null) {
9468 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9469 }
9470 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9471 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9472 postDelayed(mSendViewScrolledAccessibilityEvent,
9473 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9474 }
9475 }
9476
9477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009478 * Called by a parent to request that a child update its values for mScrollX
9479 * and mScrollY if necessary. This will typically be done if the child is
9480 * animating a scroll using a {@link android.widget.Scroller Scroller}
9481 * object.
9482 */
9483 public void computeScroll() {
9484 }
9485
9486 /**
9487 * <p>Indicate whether the horizontal edges are faded when the view is
9488 * scrolled horizontally.</p>
9489 *
9490 * @return true if the horizontal edges should are faded on scroll, false
9491 * otherwise
9492 *
9493 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009494 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009495 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009496 */
9497 public boolean isHorizontalFadingEdgeEnabled() {
9498 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9499 }
9500
9501 /**
9502 * <p>Define whether the horizontal edges should be faded when this view
9503 * is scrolled horizontally.</p>
9504 *
9505 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9506 * be faded when the view is scrolled
9507 * horizontally
9508 *
9509 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009510 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009511 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009512 */
9513 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9514 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9515 if (horizontalFadingEdgeEnabled) {
9516 initScrollCache();
9517 }
9518
9519 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9520 }
9521 }
9522
9523 /**
9524 * <p>Indicate whether the vertical edges are faded when the view is
9525 * scrolled horizontally.</p>
9526 *
9527 * @return true if the vertical edges should are faded on scroll, false
9528 * otherwise
9529 *
9530 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009531 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009532 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 */
9534 public boolean isVerticalFadingEdgeEnabled() {
9535 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9536 }
9537
9538 /**
9539 * <p>Define whether the vertical edges should be faded when this view
9540 * is scrolled vertically.</p>
9541 *
9542 * @param verticalFadingEdgeEnabled true if the vertical edges should
9543 * be faded when the view is scrolled
9544 * vertically
9545 *
9546 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009547 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009548 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009549 */
9550 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9551 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9552 if (verticalFadingEdgeEnabled) {
9553 initScrollCache();
9554 }
9555
9556 mViewFlags ^= FADING_EDGE_VERTICAL;
9557 }
9558 }
9559
9560 /**
9561 * Returns the strength, or intensity, of the top faded edge. The strength is
9562 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9563 * returns 0.0 or 1.0 but no value in between.
9564 *
9565 * Subclasses should override this method to provide a smoother fade transition
9566 * when scrolling occurs.
9567 *
9568 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9569 */
9570 protected float getTopFadingEdgeStrength() {
9571 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9572 }
9573
9574 /**
9575 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9576 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9577 * returns 0.0 or 1.0 but no value in between.
9578 *
9579 * Subclasses should override this method to provide a smoother fade transition
9580 * when scrolling occurs.
9581 *
9582 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9583 */
9584 protected float getBottomFadingEdgeStrength() {
9585 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9586 computeVerticalScrollRange() ? 1.0f : 0.0f;
9587 }
9588
9589 /**
9590 * Returns the strength, or intensity, of the left faded edge. The strength is
9591 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9592 * returns 0.0 or 1.0 but no value in between.
9593 *
9594 * Subclasses should override this method to provide a smoother fade transition
9595 * when scrolling occurs.
9596 *
9597 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9598 */
9599 protected float getLeftFadingEdgeStrength() {
9600 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9601 }
9602
9603 /**
9604 * Returns the strength, or intensity, of the right faded edge. The strength is
9605 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9606 * returns 0.0 or 1.0 but no value in between.
9607 *
9608 * Subclasses should override this method to provide a smoother fade transition
9609 * when scrolling occurs.
9610 *
9611 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9612 */
9613 protected float getRightFadingEdgeStrength() {
9614 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9615 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9616 }
9617
9618 /**
9619 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9620 * scrollbar is not drawn by default.</p>
9621 *
9622 * @return true if the horizontal scrollbar should be painted, false
9623 * otherwise
9624 *
9625 * @see #setHorizontalScrollBarEnabled(boolean)
9626 */
9627 public boolean isHorizontalScrollBarEnabled() {
9628 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9629 }
9630
9631 /**
9632 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9633 * scrollbar is not drawn by default.</p>
9634 *
9635 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9636 * be painted
9637 *
9638 * @see #isHorizontalScrollBarEnabled()
9639 */
9640 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9641 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9642 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009643 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009644 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009645 }
9646 }
9647
9648 /**
9649 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9650 * scrollbar is not drawn by default.</p>
9651 *
9652 * @return true if the vertical scrollbar should be painted, false
9653 * otherwise
9654 *
9655 * @see #setVerticalScrollBarEnabled(boolean)
9656 */
9657 public boolean isVerticalScrollBarEnabled() {
9658 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9659 }
9660
9661 /**
9662 * <p>Define whether the vertical scrollbar should be drawn or not. The
9663 * scrollbar is not drawn by default.</p>
9664 *
9665 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9666 * be painted
9667 *
9668 * @see #isVerticalScrollBarEnabled()
9669 */
9670 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9671 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9672 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009673 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009674 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009675 }
9676 }
9677
Adam Powell20232d02010-12-08 21:08:53 -08009678 /**
9679 * @hide
9680 */
9681 protected void recomputePadding() {
9682 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009683 }
Joe Malin32736f02011-01-19 16:14:20 -08009684
Mike Cleronfe81d382009-09-28 14:22:16 -07009685 /**
9686 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009687 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009688 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009689 *
Philip Milne6c8ea062012-04-03 17:38:43 -07009690 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -07009691 */
9692 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9693 initScrollCache();
9694 final ScrollabilityCache scrollabilityCache = mScrollCache;
9695 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009696 if (fadeScrollbars) {
9697 scrollabilityCache.state = ScrollabilityCache.OFF;
9698 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009699 scrollabilityCache.state = ScrollabilityCache.ON;
9700 }
9701 }
Joe Malin32736f02011-01-19 16:14:20 -08009702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 /**
Joe Malin32736f02011-01-19 16:14:20 -08009704 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009705 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009706 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009707 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -07009708 *
9709 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -07009710 */
9711 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009712 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009713 }
Joe Malin32736f02011-01-19 16:14:20 -08009714
Mike Cleron52f0a642009-09-28 18:21:37 -07009715 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07009716 *
9717 * Returns the delay before scrollbars fade.
9718 *
9719 * @return the delay before scrollbars fade
9720 *
9721 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9722 */
9723 public int getScrollBarDefaultDelayBeforeFade() {
9724 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
9725 mScrollCache.scrollBarDefaultDelayBeforeFade;
9726 }
9727
9728 /**
9729 * Define the delay before scrollbars fade.
9730 *
9731 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
9732 *
9733 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9734 */
9735 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
9736 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
9737 }
9738
9739 /**
9740 *
9741 * Returns the scrollbar fade duration.
9742 *
9743 * @return the scrollbar fade duration
9744 *
9745 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9746 */
9747 public int getScrollBarFadeDuration() {
9748 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
9749 mScrollCache.scrollBarFadeDuration;
9750 }
9751
9752 /**
9753 * Define the scrollbar fade duration.
9754 *
9755 * @param scrollBarFadeDuration - the scrollbar fade duration
9756 *
9757 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9758 */
9759 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
9760 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
9761 }
9762
9763 /**
9764 *
9765 * Returns the scrollbar size.
9766 *
9767 * @return the scrollbar size
9768 *
9769 * @attr ref android.R.styleable#View_scrollbarSize
9770 */
9771 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -07009772 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -07009773 mScrollCache.scrollBarSize;
9774 }
9775
9776 /**
9777 * Define the scrollbar size.
9778 *
9779 * @param scrollBarSize - the scrollbar size
9780 *
9781 * @attr ref android.R.styleable#View_scrollbarSize
9782 */
9783 public void setScrollBarSize(int scrollBarSize) {
9784 getScrollCache().scrollBarSize = scrollBarSize;
9785 }
9786
9787 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009788 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9789 * inset. When inset, they add to the padding of the view. And the scrollbars
9790 * can be drawn inside the padding area or on the edge of the view. For example,
9791 * if a view has a background drawable and you want to draw the scrollbars
9792 * inside the padding specified by the drawable, you can use
9793 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9794 * appear at the edge of the view, ignoring the padding, then you can use
9795 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9796 * @param style the style of the scrollbars. Should be one of
9797 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9798 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9799 * @see #SCROLLBARS_INSIDE_OVERLAY
9800 * @see #SCROLLBARS_INSIDE_INSET
9801 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9802 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009803 *
9804 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 */
9806 public void setScrollBarStyle(int style) {
9807 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9808 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009809 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009810 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 }
9812 }
9813
9814 /**
9815 * <p>Returns the current scrollbar style.</p>
9816 * @return the current scrollbar style
9817 * @see #SCROLLBARS_INSIDE_OVERLAY
9818 * @see #SCROLLBARS_INSIDE_INSET
9819 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9820 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009821 *
9822 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009823 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009824 @ViewDebug.ExportedProperty(mapping = {
9825 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9826 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9827 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9828 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9829 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 public int getScrollBarStyle() {
9831 return mViewFlags & SCROLLBARS_STYLE_MASK;
9832 }
9833
9834 /**
9835 * <p>Compute the horizontal range that the horizontal scrollbar
9836 * represents.</p>
9837 *
9838 * <p>The range is expressed in arbitrary units that must be the same as the
9839 * units used by {@link #computeHorizontalScrollExtent()} and
9840 * {@link #computeHorizontalScrollOffset()}.</p>
9841 *
9842 * <p>The default range is the drawing width of this view.</p>
9843 *
9844 * @return the total horizontal range represented by the horizontal
9845 * scrollbar
9846 *
9847 * @see #computeHorizontalScrollExtent()
9848 * @see #computeHorizontalScrollOffset()
9849 * @see android.widget.ScrollBarDrawable
9850 */
9851 protected int computeHorizontalScrollRange() {
9852 return getWidth();
9853 }
9854
9855 /**
9856 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9857 * within the horizontal range. This value is used to compute the position
9858 * of the thumb within the scrollbar's track.</p>
9859 *
9860 * <p>The range is expressed in arbitrary units that must be the same as the
9861 * units used by {@link #computeHorizontalScrollRange()} and
9862 * {@link #computeHorizontalScrollExtent()}.</p>
9863 *
9864 * <p>The default offset is the scroll offset of this view.</p>
9865 *
9866 * @return the horizontal offset of the scrollbar's thumb
9867 *
9868 * @see #computeHorizontalScrollRange()
9869 * @see #computeHorizontalScrollExtent()
9870 * @see android.widget.ScrollBarDrawable
9871 */
9872 protected int computeHorizontalScrollOffset() {
9873 return mScrollX;
9874 }
9875
9876 /**
9877 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9878 * within the horizontal range. This value is used to compute the length
9879 * of the thumb within the scrollbar's track.</p>
9880 *
9881 * <p>The range is expressed in arbitrary units that must be the same as the
9882 * units used by {@link #computeHorizontalScrollRange()} and
9883 * {@link #computeHorizontalScrollOffset()}.</p>
9884 *
9885 * <p>The default extent is the drawing width of this view.</p>
9886 *
9887 * @return the horizontal extent of the scrollbar's thumb
9888 *
9889 * @see #computeHorizontalScrollRange()
9890 * @see #computeHorizontalScrollOffset()
9891 * @see android.widget.ScrollBarDrawable
9892 */
9893 protected int computeHorizontalScrollExtent() {
9894 return getWidth();
9895 }
9896
9897 /**
9898 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9899 *
9900 * <p>The range is expressed in arbitrary units that must be the same as the
9901 * units used by {@link #computeVerticalScrollExtent()} and
9902 * {@link #computeVerticalScrollOffset()}.</p>
9903 *
9904 * @return the total vertical range represented by the vertical scrollbar
9905 *
9906 * <p>The default range is the drawing height of this view.</p>
9907 *
9908 * @see #computeVerticalScrollExtent()
9909 * @see #computeVerticalScrollOffset()
9910 * @see android.widget.ScrollBarDrawable
9911 */
9912 protected int computeVerticalScrollRange() {
9913 return getHeight();
9914 }
9915
9916 /**
9917 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9918 * within the horizontal range. This value is used to compute the position
9919 * of the thumb within the scrollbar's track.</p>
9920 *
9921 * <p>The range is expressed in arbitrary units that must be the same as the
9922 * units used by {@link #computeVerticalScrollRange()} and
9923 * {@link #computeVerticalScrollExtent()}.</p>
9924 *
9925 * <p>The default offset is the scroll offset of this view.</p>
9926 *
9927 * @return the vertical offset of the scrollbar's thumb
9928 *
9929 * @see #computeVerticalScrollRange()
9930 * @see #computeVerticalScrollExtent()
9931 * @see android.widget.ScrollBarDrawable
9932 */
9933 protected int computeVerticalScrollOffset() {
9934 return mScrollY;
9935 }
9936
9937 /**
9938 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9939 * within the vertical range. This value is used to compute the length
9940 * of the thumb within the scrollbar's track.</p>
9941 *
9942 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009943 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009944 * {@link #computeVerticalScrollOffset()}.</p>
9945 *
9946 * <p>The default extent is the drawing height of this view.</p>
9947 *
9948 * @return the vertical extent of the scrollbar's thumb
9949 *
9950 * @see #computeVerticalScrollRange()
9951 * @see #computeVerticalScrollOffset()
9952 * @see android.widget.ScrollBarDrawable
9953 */
9954 protected int computeVerticalScrollExtent() {
9955 return getHeight();
9956 }
9957
9958 /**
Adam Powell69159442011-06-13 17:53:06 -07009959 * Check if this view can be scrolled horizontally in a certain direction.
9960 *
9961 * @param direction Negative to check scrolling left, positive to check scrolling right.
9962 * @return true if this view can be scrolled in the specified direction, false otherwise.
9963 */
9964 public boolean canScrollHorizontally(int direction) {
9965 final int offset = computeHorizontalScrollOffset();
9966 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9967 if (range == 0) return false;
9968 if (direction < 0) {
9969 return offset > 0;
9970 } else {
9971 return offset < range - 1;
9972 }
9973 }
9974
9975 /**
9976 * Check if this view can be scrolled vertically in a certain direction.
9977 *
9978 * @param direction Negative to check scrolling up, positive to check scrolling down.
9979 * @return true if this view can be scrolled in the specified direction, false otherwise.
9980 */
9981 public boolean canScrollVertically(int direction) {
9982 final int offset = computeVerticalScrollOffset();
9983 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9984 if (range == 0) return false;
9985 if (direction < 0) {
9986 return offset > 0;
9987 } else {
9988 return offset < range - 1;
9989 }
9990 }
9991
9992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009993 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9994 * scrollbars are painted only if they have been awakened first.</p>
9995 *
9996 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009997 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009998 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010000 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010001 // scrollbars are drawn only when the animation is running
10002 final ScrollabilityCache cache = mScrollCache;
10003 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010004
Mike Cleronf116bf82009-09-27 19:14:12 -070010005 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010006
Mike Cleronf116bf82009-09-27 19:14:12 -070010007 if (state == ScrollabilityCache.OFF) {
10008 return;
10009 }
Joe Malin32736f02011-01-19 16:14:20 -080010010
Mike Cleronf116bf82009-09-27 19:14:12 -070010011 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010012
Mike Cleronf116bf82009-09-27 19:14:12 -070010013 if (state == ScrollabilityCache.FADING) {
10014 // We're fading -- get our fade interpolation
10015 if (cache.interpolatorValues == null) {
10016 cache.interpolatorValues = new float[1];
10017 }
Joe Malin32736f02011-01-19 16:14:20 -080010018
Mike Cleronf116bf82009-09-27 19:14:12 -070010019 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010020
Mike Cleronf116bf82009-09-27 19:14:12 -070010021 // Stops the animation if we're done
10022 if (cache.scrollBarInterpolator.timeToValues(values) ==
10023 Interpolator.Result.FREEZE_END) {
10024 cache.state = ScrollabilityCache.OFF;
10025 } else {
10026 cache.scrollBar.setAlpha(Math.round(values[0]));
10027 }
Joe Malin32736f02011-01-19 16:14:20 -080010028
10029 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010030 // drawing. We only want this when we're fading so that
10031 // we prevent excessive redraws
10032 invalidate = true;
10033 } else {
10034 // We're just on -- but we may have been fading before so
10035 // reset alpha
10036 cache.scrollBar.setAlpha(255);
10037 }
10038
Joe Malin32736f02011-01-19 16:14:20 -080010039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010040 final int viewFlags = mViewFlags;
10041
10042 final boolean drawHorizontalScrollBar =
10043 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10044 final boolean drawVerticalScrollBar =
10045 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10046 && !isVerticalScrollBarHidden();
10047
10048 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10049 final int width = mRight - mLeft;
10050 final int height = mBottom - mTop;
10051
10052 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010053
Mike Reede8853fc2009-09-04 14:01:48 -040010054 final int scrollX = mScrollX;
10055 final int scrollY = mScrollY;
10056 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10057
Mike Cleronf116bf82009-09-27 19:14:12 -070010058 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010060 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010061 int size = scrollBar.getSize(false);
10062 if (size <= 0) {
10063 size = cache.scrollBarSize;
10064 }
10065
Mike Cleronf116bf82009-09-27 19:14:12 -070010066 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010067 computeHorizontalScrollOffset(),
10068 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010069 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010070 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010071 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010072 left = scrollX + (mPaddingLeft & inside);
10073 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10074 bottom = top + size;
10075 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10076 if (invalidate) {
10077 invalidate(left, top, right, bottom);
10078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010079 }
10080
10081 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010082 int size = scrollBar.getSize(true);
10083 if (size <= 0) {
10084 size = cache.scrollBarSize;
10085 }
10086
Mike Reede8853fc2009-09-04 14:01:48 -040010087 scrollBar.setParameters(computeVerticalScrollRange(),
10088 computeVerticalScrollOffset(),
10089 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010090 switch (mVerticalScrollbarPosition) {
10091 default:
10092 case SCROLLBAR_POSITION_DEFAULT:
10093 case SCROLLBAR_POSITION_RIGHT:
10094 left = scrollX + width - size - (mUserPaddingRight & inside);
10095 break;
10096 case SCROLLBAR_POSITION_LEFT:
10097 left = scrollX + (mUserPaddingLeft & inside);
10098 break;
10099 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010100 top = scrollY + (mPaddingTop & inside);
10101 right = left + size;
10102 bottom = scrollY + height - (mUserPaddingBottom & inside);
10103 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10104 if (invalidate) {
10105 invalidate(left, top, right, bottom);
10106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 }
10108 }
10109 }
10110 }
Romain Guy8506ab42009-06-11 17:35:47 -070010111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010112 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010113 * 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 -080010114 * FastScroller is visible.
10115 * @return whether to temporarily hide the vertical scrollbar
10116 * @hide
10117 */
10118 protected boolean isVerticalScrollBarHidden() {
10119 return false;
10120 }
10121
10122 /**
10123 * <p>Draw the horizontal scrollbar if
10124 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10125 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010126 * @param canvas the canvas on which to draw the scrollbar
10127 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010128 *
10129 * @see #isHorizontalScrollBarEnabled()
10130 * @see #computeHorizontalScrollRange()
10131 * @see #computeHorizontalScrollExtent()
10132 * @see #computeHorizontalScrollOffset()
10133 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010134 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010135 */
Romain Guy8fb95422010-08-17 18:38:51 -070010136 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10137 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010138 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010139 scrollBar.draw(canvas);
10140 }
Mike Reede8853fc2009-09-04 14:01:48 -040010141
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010143 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10144 * returns true.</p>
10145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 * @param canvas the canvas on which to draw the scrollbar
10147 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 *
10149 * @see #isVerticalScrollBarEnabled()
10150 * @see #computeVerticalScrollRange()
10151 * @see #computeVerticalScrollExtent()
10152 * @see #computeVerticalScrollOffset()
10153 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010154 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 */
Romain Guy8fb95422010-08-17 18:38:51 -070010156 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10157 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010158 scrollBar.setBounds(l, t, r, b);
10159 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010160 }
10161
10162 /**
10163 * Implement this to do your drawing.
10164 *
10165 * @param canvas the canvas on which the background will be drawn
10166 */
10167 protected void onDraw(Canvas canvas) {
10168 }
10169
10170 /*
10171 * Caller is responsible for calling requestLayout if necessary.
10172 * (This allows addViewInLayout to not request a new layout.)
10173 */
10174 void assignParent(ViewParent parent) {
10175 if (mParent == null) {
10176 mParent = parent;
10177 } else if (parent == null) {
10178 mParent = null;
10179 } else {
10180 throw new RuntimeException("view " + this + " being added, but"
10181 + " it already has a parent");
10182 }
10183 }
10184
10185 /**
10186 * This is called when the view is attached to a window. At this point it
10187 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010188 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10189 * however it may be called any time before the first onDraw -- including
10190 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191 *
10192 * @see #onDetachedFromWindow()
10193 */
10194 protected void onAttachedToWindow() {
10195 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10196 mParent.requestTransparentRegion(this);
10197 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010198 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10199 initialAwakenScrollBars();
10200 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10201 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010202 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010203 // Order is important here: LayoutDirection MUST be resolved before Padding
10204 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010205 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010206 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010207 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010208 resolveTextAlignment();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010209 if (isFocused()) {
10210 InputMethodManager imm = InputMethodManager.peekInstance();
10211 imm.focusIn(this);
10212 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010213 }
Cibu Johny86666632010-02-22 13:01:02 -080010214
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010215 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010216 * @see #onScreenStateChanged(int)
10217 */
10218 void dispatchScreenStateChanged(int screenState) {
10219 onScreenStateChanged(screenState);
10220 }
10221
10222 /**
10223 * This method is called whenever the state of the screen this view is
10224 * attached to changes. A state change will usually occurs when the screen
10225 * turns on or off (whether it happens automatically or the user does it
10226 * manually.)
10227 *
10228 * @param screenState The new state of the screen. Can be either
10229 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10230 */
10231 public void onScreenStateChanged(int screenState) {
10232 }
10233
10234 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010235 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10236 */
10237 private boolean hasRtlSupport() {
10238 return mContext.getApplicationInfo().hasRtlSupport();
10239 }
10240
10241 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010242 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10243 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010244 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010245 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010246 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010247 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010248 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010249
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010250 if (hasRtlSupport()) {
10251 // Set resolved depending on layout direction
10252 switch (getLayoutDirection()) {
10253 case LAYOUT_DIRECTION_INHERIT:
10254 // If this is root view, no need to look at parent's layout dir.
10255 if (canResolveLayoutDirection()) {
10256 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010257
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010258 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10259 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10260 }
10261 } else {
10262 // Nothing to do, LTR by default
10263 }
10264 break;
10265 case LAYOUT_DIRECTION_RTL:
10266 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10267 break;
10268 case LAYOUT_DIRECTION_LOCALE:
10269 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010270 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10271 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010272 break;
10273 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010274 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010275 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010276 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010277
10278 // Set to resolved
10279 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010280 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010281 // Resolve padding
10282 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010283 }
10284
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010285 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010286 * Called when layout direction has been resolved.
10287 *
10288 * The default implementation does nothing.
10289 */
10290 public void onResolvedLayoutDirectionChanged() {
10291 }
10292
10293 /**
10294 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010295 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010296 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010297 // If the user specified the absolute padding (either with android:padding or
10298 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10299 // use the default padding or the padding from the background drawable
10300 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010301 int resolvedLayoutDirection = getResolvedLayoutDirection();
10302 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010303 case LAYOUT_DIRECTION_RTL:
10304 // Start user padding override Right user padding. Otherwise, if Right user
10305 // padding is not defined, use the default Right padding. If Right user padding
10306 // is defined, just use it.
10307 if (mUserPaddingStart >= 0) {
10308 mUserPaddingRight = mUserPaddingStart;
10309 } else if (mUserPaddingRight < 0) {
10310 mUserPaddingRight = mPaddingRight;
10311 }
10312 if (mUserPaddingEnd >= 0) {
10313 mUserPaddingLeft = mUserPaddingEnd;
10314 } else if (mUserPaddingLeft < 0) {
10315 mUserPaddingLeft = mPaddingLeft;
10316 }
10317 break;
10318 case LAYOUT_DIRECTION_LTR:
10319 default:
10320 // Start user padding override Left user padding. Otherwise, if Left user
10321 // padding is not defined, use the default left padding. If Left user padding
10322 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010323 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010324 mUserPaddingLeft = mUserPaddingStart;
10325 } else if (mUserPaddingLeft < 0) {
10326 mUserPaddingLeft = mPaddingLeft;
10327 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010328 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010329 mUserPaddingRight = mUserPaddingEnd;
10330 } else if (mUserPaddingRight < 0) {
10331 mUserPaddingRight = mPaddingRight;
10332 }
10333 }
10334
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010335 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10336
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010337 if(isPaddingRelative()) {
10338 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10339 } else {
10340 recomputePadding();
10341 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010342 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010343 }
10344
10345 /**
10346 * Resolve padding depending on the layout direction. Subclasses that care about
10347 * padding resolution should override this method. The default implementation does
10348 * nothing.
10349 *
10350 * @param layoutDirection the direction of the layout
10351 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010352 * @see {@link #LAYOUT_DIRECTION_LTR}
10353 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010354 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010355 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010356 }
10357
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010358 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010359 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010360 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010361 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010362 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010363 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010364 switch (getLayoutDirection()) {
10365 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010366 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010367 default:
10368 return true;
10369 }
10370 }
10371
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010372 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010373 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10374 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010375 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010376 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010377 // Reset the current resolved bits
10378 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010379 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010380 // Reset also the text direction
10381 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010382 }
10383
10384 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010385 * Called during reset of resolved layout direction.
10386 *
10387 * Subclasses need to override this method to clear cached information that depends on the
10388 * resolved layout direction, or to inform child views that inherit their layout direction.
10389 *
10390 * The default implementation does nothing.
10391 */
10392 public void onResolvedLayoutDirectionReset() {
10393 }
10394
10395 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010396 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010397 *
10398 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010399 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010400 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010401 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080010402 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010403 }
10404
10405 /**
10406 * This is called when the view is detached from a window. At this point it
10407 * no longer has a surface for drawing.
10408 *
10409 * @see #onAttachedToWindow()
10410 */
10411 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080010412 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080010413
Romain Guya440b002010-02-24 15:57:54 -080010414 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050010415 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080010416 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070010417 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080010418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010419 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010420
Romain Guya998dff2012-03-23 18:58:36 -070010421 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010422
10423 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070010424 if (mDisplayList != null) {
10425 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
10426 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010427 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070010428 } else {
10429 if (mDisplayList != null) {
10430 // Should never happen
10431 mDisplayList.invalidate();
10432 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010433 }
10434
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080010435 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070010436
10437 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010438 resetResolvedTextAlignment();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010439 }
10440
10441 /**
10442 * @return The number of times this view has been attached to a window
10443 */
10444 protected int getWindowAttachCount() {
10445 return mWindowAttachCount;
10446 }
10447
10448 /**
10449 * Retrieve a unique token identifying the window this view is attached to.
10450 * @return Return the window's token for use in
10451 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
10452 */
10453 public IBinder getWindowToken() {
10454 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
10455 }
10456
10457 /**
10458 * Retrieve a unique token identifying the top-level "real" window of
10459 * the window that this view is attached to. That is, this is like
10460 * {@link #getWindowToken}, except if the window this view in is a panel
10461 * window (attached to another containing window), then the token of
10462 * the containing window is returned instead.
10463 *
10464 * @return Returns the associated window token, either
10465 * {@link #getWindowToken()} or the containing window's token.
10466 */
10467 public IBinder getApplicationWindowToken() {
10468 AttachInfo ai = mAttachInfo;
10469 if (ai != null) {
10470 IBinder appWindowToken = ai.mPanelParentWindowToken;
10471 if (appWindowToken == null) {
10472 appWindowToken = ai.mWindowToken;
10473 }
10474 return appWindowToken;
10475 }
10476 return null;
10477 }
10478
10479 /**
10480 * Retrieve private session object this view hierarchy is using to
10481 * communicate with the window manager.
10482 * @return the session object to communicate with the window manager
10483 */
10484 /*package*/ IWindowSession getWindowSession() {
10485 return mAttachInfo != null ? mAttachInfo.mSession : null;
10486 }
10487
10488 /**
10489 * @param info the {@link android.view.View.AttachInfo} to associated with
10490 * this view
10491 */
10492 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
10493 //System.out.println("Attached! " + this);
10494 mAttachInfo = info;
10495 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010496 // We will need to evaluate the drawable state at least once.
10497 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 if (mFloatingTreeObserver != null) {
10499 info.mTreeObserver.merge(mFloatingTreeObserver);
10500 mFloatingTreeObserver = null;
10501 }
10502 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
10503 mAttachInfo.mScrollContainers.add(this);
10504 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
10505 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070010506 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080010508
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010509 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010510 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010511 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010512 if (listeners != null && listeners.size() > 0) {
10513 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10514 // perform the dispatching. The iterator is a safe guard against listeners that
10515 // could mutate the list by calling the various add/remove methods. This prevents
10516 // the array from being modified while we iterate it.
10517 for (OnAttachStateChangeListener listener : listeners) {
10518 listener.onViewAttachedToWindow(this);
10519 }
10520 }
10521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 int vis = info.mWindowVisibility;
10523 if (vis != GONE) {
10524 onWindowVisibilityChanged(vis);
10525 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010526 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
10527 // If nobody has evaluated the drawable state yet, then do it now.
10528 refreshDrawableState();
10529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 }
10531
10532 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 AttachInfo info = mAttachInfo;
10534 if (info != null) {
10535 int vis = info.mWindowVisibility;
10536 if (vis != GONE) {
10537 onWindowVisibilityChanged(GONE);
10538 }
10539 }
10540
10541 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010542
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010543 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010544 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010545 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010546 if (listeners != null && listeners.size() > 0) {
10547 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10548 // perform the dispatching. The iterator is a safe guard against listeners that
10549 // could mutate the list by calling the various add/remove methods. This prevents
10550 // the array from being modified while we iterate it.
10551 for (OnAttachStateChangeListener listener : listeners) {
10552 listener.onViewDetachedFromWindow(this);
10553 }
10554 }
10555
Romain Guy01d5edc2011-01-28 11:28:53 -080010556 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 mAttachInfo.mScrollContainers.remove(this);
10558 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10559 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010561 mAttachInfo = null;
10562 }
10563
10564 /**
10565 * Store this view hierarchy's frozen state into the given container.
10566 *
10567 * @param container The SparseArray in which to save the view's state.
10568 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010569 * @see #restoreHierarchyState(android.util.SparseArray)
10570 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10571 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 */
10573 public void saveHierarchyState(SparseArray<Parcelable> container) {
10574 dispatchSaveInstanceState(container);
10575 }
10576
10577 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010578 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10579 * this view and its children. May be overridden to modify how freezing happens to a
10580 * 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 -080010581 *
10582 * @param container The SparseArray in which to save the view's state.
10583 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010584 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10585 * @see #saveHierarchyState(android.util.SparseArray)
10586 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010587 */
10588 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10589 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10590 mPrivateFlags &= ~SAVE_STATE_CALLED;
10591 Parcelable state = onSaveInstanceState();
10592 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10593 throw new IllegalStateException(
10594 "Derived class did not call super.onSaveInstanceState()");
10595 }
10596 if (state != null) {
10597 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10598 // + ": " + state);
10599 container.put(mID, state);
10600 }
10601 }
10602 }
10603
10604 /**
10605 * Hook allowing a view to generate a representation of its internal state
10606 * that can later be used to create a new instance with that same state.
10607 * This state should only contain information that is not persistent or can
10608 * not be reconstructed later. For example, you will never store your
10609 * current position on screen because that will be computed again when a
10610 * new instance of the view is placed in its view hierarchy.
10611 * <p>
10612 * Some examples of things you may store here: the current cursor position
10613 * in a text view (but usually not the text itself since that is stored in a
10614 * content provider or other persistent storage), the currently selected
10615 * item in a list view.
10616 *
10617 * @return Returns a Parcelable object containing the view's current dynamic
10618 * state, or null if there is nothing interesting to save. The
10619 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070010620 * @see #onRestoreInstanceState(android.os.Parcelable)
10621 * @see #saveHierarchyState(android.util.SparseArray)
10622 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010623 * @see #setSaveEnabled(boolean)
10624 */
10625 protected Parcelable onSaveInstanceState() {
10626 mPrivateFlags |= SAVE_STATE_CALLED;
10627 return BaseSavedState.EMPTY_STATE;
10628 }
10629
10630 /**
10631 * Restore this view hierarchy's frozen state from the given container.
10632 *
10633 * @param container The SparseArray which holds previously frozen states.
10634 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010635 * @see #saveHierarchyState(android.util.SparseArray)
10636 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10637 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638 */
10639 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10640 dispatchRestoreInstanceState(container);
10641 }
10642
10643 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010644 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10645 * state for this view and its children. May be overridden to modify how restoring
10646 * happens to a view's children; for example, some views may want to not store state
10647 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 *
10649 * @param container The SparseArray which holds previously saved state.
10650 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010651 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10652 * @see #restoreHierarchyState(android.util.SparseArray)
10653 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 */
10655 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10656 if (mID != NO_ID) {
10657 Parcelable state = container.get(mID);
10658 if (state != null) {
10659 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10660 // + ": " + state);
10661 mPrivateFlags &= ~SAVE_STATE_CALLED;
10662 onRestoreInstanceState(state);
10663 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10664 throw new IllegalStateException(
10665 "Derived class did not call super.onRestoreInstanceState()");
10666 }
10667 }
10668 }
10669 }
10670
10671 /**
10672 * Hook allowing a view to re-apply a representation of its internal state that had previously
10673 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10674 * null state.
10675 *
10676 * @param state The frozen state that had previously been returned by
10677 * {@link #onSaveInstanceState}.
10678 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010679 * @see #onSaveInstanceState()
10680 * @see #restoreHierarchyState(android.util.SparseArray)
10681 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 */
10683 protected void onRestoreInstanceState(Parcelable state) {
10684 mPrivateFlags |= SAVE_STATE_CALLED;
10685 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010686 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10687 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010688 + "when two views of different type have the same id in the same hierarchy. "
10689 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010690 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 }
10692 }
10693
10694 /**
10695 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10696 *
10697 * @return the drawing start time in milliseconds
10698 */
10699 public long getDrawingTime() {
10700 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10701 }
10702
10703 /**
10704 * <p>Enables or disables the duplication of the parent's state into this view. When
10705 * duplication is enabled, this view gets its drawable state from its parent rather
10706 * than from its own internal properties.</p>
10707 *
10708 * <p>Note: in the current implementation, setting this property to true after the
10709 * view was added to a ViewGroup might have no effect at all. This property should
10710 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10711 *
10712 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10713 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010714 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010715 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10716 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010717 *
10718 * @param enabled True to enable duplication of the parent's drawable state, false
10719 * to disable it.
10720 *
10721 * @see #getDrawableState()
10722 * @see #isDuplicateParentStateEnabled()
10723 */
10724 public void setDuplicateParentStateEnabled(boolean enabled) {
10725 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10726 }
10727
10728 /**
10729 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10730 *
10731 * @return True if this view's drawable state is duplicated from the parent,
10732 * false otherwise
10733 *
10734 * @see #getDrawableState()
10735 * @see #setDuplicateParentStateEnabled(boolean)
10736 */
10737 public boolean isDuplicateParentStateEnabled() {
10738 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10739 }
10740
10741 /**
Romain Guy171c5922011-01-06 10:04:23 -080010742 * <p>Specifies the type of layer backing this view. The layer can be
10743 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10744 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010745 *
Romain Guy171c5922011-01-06 10:04:23 -080010746 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10747 * instance that controls how the layer is composed on screen. The following
10748 * properties of the paint are taken into account when composing the layer:</p>
10749 * <ul>
10750 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10751 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10752 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10753 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010754 *
Romain Guy171c5922011-01-06 10:04:23 -080010755 * <p>If this view has an alpha value set to < 1.0 by calling
10756 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10757 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10758 * equivalent to setting a hardware layer on this view and providing a paint with
10759 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010760 *
Romain Guy171c5922011-01-06 10:04:23 -080010761 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10762 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10763 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010764 *
Romain Guy171c5922011-01-06 10:04:23 -080010765 * @param layerType The ype of layer to use with this view, must be one of
10766 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10767 * {@link #LAYER_TYPE_HARDWARE}
10768 * @param paint The paint used to compose the layer. This argument is optional
10769 * and can be null. It is ignored when the layer type is
10770 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010771 *
10772 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010773 * @see #LAYER_TYPE_NONE
10774 * @see #LAYER_TYPE_SOFTWARE
10775 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010776 * @see #setAlpha(float)
10777 *
Romain Guy171c5922011-01-06 10:04:23 -080010778 * @attr ref android.R.styleable#View_layerType
10779 */
10780 public void setLayerType(int layerType, Paint paint) {
10781 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010782 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010783 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10784 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010785
Romain Guyd6cd5722011-01-17 14:42:41 -080010786 if (layerType == mLayerType) {
10787 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10788 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010789 invalidateParentCaches();
10790 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010791 }
10792 return;
10793 }
Romain Guy171c5922011-01-06 10:04:23 -080010794
10795 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010796 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010797 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070010798 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010799 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010800 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010801 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010802 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010803 default:
10804 break;
Romain Guy171c5922011-01-06 10:04:23 -080010805 }
10806
10807 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010808 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10809 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10810 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010811
Romain Guy0fd89bf2011-01-26 15:41:30 -080010812 invalidateParentCaches();
10813 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010814 }
10815
10816 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010817 * Indicates whether this view has a static layer. A view with layer type
10818 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10819 * dynamic.
10820 */
10821 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010822 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010823 }
10824
10825 /**
Romain Guy171c5922011-01-06 10:04:23 -080010826 * Indicates what type of layer is currently associated with this view. By default
10827 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10828 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10829 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010830 *
Romain Guy171c5922011-01-06 10:04:23 -080010831 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10832 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010833 *
10834 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070010835 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010836 * @see #LAYER_TYPE_NONE
10837 * @see #LAYER_TYPE_SOFTWARE
10838 * @see #LAYER_TYPE_HARDWARE
10839 */
10840 public int getLayerType() {
10841 return mLayerType;
10842 }
Joe Malin32736f02011-01-19 16:14:20 -080010843
Romain Guy6c319ca2011-01-11 14:29:25 -080010844 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010845 * Forces this view's layer to be created and this view to be rendered
10846 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10847 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070010848 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010849 * This method can for instance be used to render a view into its layer before
10850 * starting an animation. If this view is complex, rendering into the layer
10851 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070010852 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010853 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070010854 *
10855 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010856 */
10857 public void buildLayer() {
10858 if (mLayerType == LAYER_TYPE_NONE) return;
10859
10860 if (mAttachInfo == null) {
10861 throw new IllegalStateException("This view must be attached to a window first");
10862 }
10863
10864 switch (mLayerType) {
10865 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010866 if (mAttachInfo.mHardwareRenderer != null &&
10867 mAttachInfo.mHardwareRenderer.isEnabled() &&
10868 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010869 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010870 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010871 break;
10872 case LAYER_TYPE_SOFTWARE:
10873 buildDrawingCache(true);
10874 break;
10875 }
10876 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010877
Romain Guy9c4b79a2011-11-10 19:23:58 -080010878 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10879 void flushLayer() {
10880 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10881 mHardwareLayer.flush();
10882 }
10883 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010884
10885 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010886 * <p>Returns a hardware layer that can be used to draw this view again
10887 * without executing its draw method.</p>
10888 *
10889 * @return A HardwareLayer ready to render, or null if an error occurred.
10890 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010891 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010892 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10893 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010894 return null;
10895 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010896
Romain Guy9c4b79a2011-11-10 19:23:58 -080010897 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010898
10899 final int width = mRight - mLeft;
10900 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010901
Romain Guy6c319ca2011-01-11 14:29:25 -080010902 if (width == 0 || height == 0) {
10903 return null;
10904 }
10905
10906 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10907 if (mHardwareLayer == null) {
10908 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10909 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070010910 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010911 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10912 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070010913 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010914 }
10915
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010916 // The layer is not valid if the underlying GPU resources cannot be allocated
10917 if (!mHardwareLayer.isValid()) {
10918 return null;
10919 }
10920
Chet Haasea1cff502012-02-21 13:43:44 -080010921 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080010922 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010923 }
10924
10925 return mHardwareLayer;
10926 }
Romain Guy171c5922011-01-06 10:04:23 -080010927
Romain Guy589b0bb2011-10-10 13:57:47 -070010928 /**
10929 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070010930 *
Romain Guy589b0bb2011-10-10 13:57:47 -070010931 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070010932 *
10933 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070010934 * @see #LAYER_TYPE_HARDWARE
10935 */
Romain Guya998dff2012-03-23 18:58:36 -070010936 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070010937 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010938 AttachInfo info = mAttachInfo;
10939 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070010940 info.mHardwareRenderer.isEnabled() &&
10941 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010942 mHardwareLayer.destroy();
10943 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010944
Romain Guy9c4b79a2011-11-10 19:23:58 -080010945 invalidate(true);
10946 invalidateParentCaches();
10947 }
Romain Guy65b345f2011-07-27 18:51:50 -070010948 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010949 }
Romain Guy65b345f2011-07-27 18:51:50 -070010950 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010951 }
10952
Romain Guy171c5922011-01-06 10:04:23 -080010953 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010954 * Destroys all hardware rendering resources. This method is invoked
10955 * when the system needs to reclaim resources. Upon execution of this
10956 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070010957 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010958 * Note: you <strong>must</strong> call
10959 * <code>super.destroyHardwareResources()</code> when overriding
10960 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070010961 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010962 * @hide
10963 */
10964 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070010965 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010966 }
10967
10968 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010969 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10970 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10971 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10972 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10973 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10974 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010975 *
Romain Guy171c5922011-01-06 10:04:23 -080010976 * <p>Enabling the drawing cache is similar to
10977 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010978 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10979 * drawing cache has no effect on rendering because the system uses a different mechanism
10980 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10981 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10982 * for information on how to enable software and hardware layers.</p>
10983 *
10984 * <p>This API can be used to manually generate
10985 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10986 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010987 *
10988 * @param enabled true to enable the drawing cache, false otherwise
10989 *
10990 * @see #isDrawingCacheEnabled()
10991 * @see #getDrawingCache()
10992 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010993 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010994 */
10995 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010996 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010997 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10998 }
10999
11000 /**
11001 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11002 *
11003 * @return true if the drawing cache is enabled
11004 *
11005 * @see #setDrawingCacheEnabled(boolean)
11006 * @see #getDrawingCache()
11007 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011008 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011009 public boolean isDrawingCacheEnabled() {
11010 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11011 }
11012
11013 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011014 * Debugging utility which recursively outputs the dirty state of a view and its
11015 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011016 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011017 * @hide
11018 */
Romain Guy676b1732011-02-14 14:45:33 -080011019 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011020 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11021 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11022 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11023 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11024 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11025 if (clear) {
11026 mPrivateFlags &= clearMask;
11027 }
11028 if (this instanceof ViewGroup) {
11029 ViewGroup parent = (ViewGroup) this;
11030 final int count = parent.getChildCount();
11031 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011032 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011033 child.outputDirtyFlags(indent + " ", clear, clearMask);
11034 }
11035 }
11036 }
11037
11038 /**
11039 * This method is used by ViewGroup to cause its children to restore or recreate their
11040 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11041 * to recreate its own display list, which would happen if it went through the normal
11042 * draw/dispatchDraw mechanisms.
11043 *
11044 * @hide
11045 */
11046 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011047
11048 /**
11049 * A view that is not attached or hardware accelerated cannot create a display list.
11050 * This method checks these conditions and returns the appropriate result.
11051 *
11052 * @return true if view has the ability to create a display list, false otherwise.
11053 *
11054 * @hide
11055 */
11056 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011057 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011058 }
Joe Malin32736f02011-01-19 16:14:20 -080011059
Chet Haasedaf98e92011-01-10 14:10:36 -080011060 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011061 * @return The HardwareRenderer associated with that view or null if hardware rendering
11062 * is not supported or this this has not been attached to a window.
11063 *
11064 * @hide
11065 */
11066 public HardwareRenderer getHardwareRenderer() {
11067 if (mAttachInfo != null) {
11068 return mAttachInfo.mHardwareRenderer;
11069 }
11070 return null;
11071 }
11072
11073 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011074 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11075 * Otherwise, the same display list will be returned (after having been rendered into
11076 * along the way, depending on the invalidation state of the view).
11077 *
11078 * @param displayList The previous version of this displayList, could be null.
11079 * @param isLayer Whether the requester of the display list is a layer. If so,
11080 * the view will avoid creating a layer inside the resulting display list.
11081 * @return A new or reused DisplayList object.
11082 */
11083 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11084 if (!canHaveDisplayList()) {
11085 return null;
11086 }
11087
11088 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11089 displayList == null || !displayList.isValid() ||
11090 (!isLayer && mRecreateDisplayList))) {
11091 // Don't need to recreate the display list, just need to tell our
11092 // children to restore/recreate theirs
11093 if (displayList != null && displayList.isValid() &&
11094 !isLayer && !mRecreateDisplayList) {
11095 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11096 mPrivateFlags &= ~DIRTY_MASK;
11097 dispatchGetDisplayList();
11098
11099 return displayList;
11100 }
11101
11102 if (!isLayer) {
11103 // If we got here, we're recreating it. Mark it as such to ensure that
11104 // we copy in child display lists into ours in drawChild()
11105 mRecreateDisplayList = true;
11106 }
11107 if (displayList == null) {
11108 final String name = getClass().getSimpleName();
11109 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11110 // If we're creating a new display list, make sure our parent gets invalidated
11111 // since they will need to recreate their display list to account for this
11112 // new child display list.
11113 invalidateParentCaches();
11114 }
11115
11116 boolean caching = false;
11117 final HardwareCanvas canvas = displayList.start();
11118 int restoreCount = 0;
11119 int width = mRight - mLeft;
11120 int height = mBottom - mTop;
11121
11122 try {
11123 canvas.setViewport(width, height);
11124 // The dirty rect should always be null for a display list
11125 canvas.onPreDraw(null);
11126 int layerType = (
11127 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11128 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011129 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11130 if (layerType == LAYER_TYPE_HARDWARE) {
11131 final HardwareLayer layer = getHardwareLayer();
11132 if (layer != null && layer.isValid()) {
11133 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11134 } else {
11135 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11136 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11137 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11138 }
11139 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011140 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011141 buildDrawingCache(true);
11142 Bitmap cache = getDrawingCache(true);
11143 if (cache != null) {
11144 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11145 caching = true;
11146 }
Chet Haasea1cff502012-02-21 13:43:44 -080011147 }
Chet Haasea1cff502012-02-21 13:43:44 -080011148 } else {
11149
11150 computeScroll();
11151
11152 if (!USE_DISPLAY_LIST_PROPERTIES) {
11153 restoreCount = canvas.save();
11154 }
11155 canvas.translate(-mScrollX, -mScrollY);
11156 if (!isLayer) {
11157 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11158 mPrivateFlags &= ~DIRTY_MASK;
11159 }
11160
11161 // Fast path for layouts with no backgrounds
11162 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11163 dispatchDraw(canvas);
11164 } else {
11165 draw(canvas);
11166 }
11167 }
11168 } finally {
11169 if (USE_DISPLAY_LIST_PROPERTIES) {
11170 canvas.restoreToCount(restoreCount);
11171 }
11172 canvas.onPostDraw();
11173
11174 displayList.end();
11175 if (USE_DISPLAY_LIST_PROPERTIES) {
11176 displayList.setCaching(caching);
11177 }
11178 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11179 displayList.setLeftTopRightBottom(0, 0, width, height);
11180 } else {
11181 setDisplayListProperties(displayList);
11182 }
11183 }
11184 } else if (!isLayer) {
11185 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11186 mPrivateFlags &= ~DIRTY_MASK;
11187 }
11188
11189 return displayList;
11190 }
11191
11192 /**
11193 * Get the DisplayList for the HardwareLayer
11194 *
11195 * @param layer The HardwareLayer whose DisplayList we want
11196 * @return A DisplayList fopr the specified HardwareLayer
11197 */
11198 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11199 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11200 layer.setDisplayList(displayList);
11201 return displayList;
11202 }
11203
11204
11205 /**
Romain Guyb051e892010-09-28 19:09:36 -070011206 * <p>Returns a display list that can be used to draw this view again
11207 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011208 *
Romain Guyb051e892010-09-28 19:09:36 -070011209 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011210 *
11211 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011212 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011213 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011214 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011215 return mDisplayList;
11216 }
11217
11218 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011219 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011220 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011221 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011222 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011223 * @see #getDrawingCache(boolean)
11224 */
11225 public Bitmap getDrawingCache() {
11226 return getDrawingCache(false);
11227 }
11228
11229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011230 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11231 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11232 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11233 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11234 * request the drawing cache by calling this method and draw it on screen if the
11235 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011236 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011237 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11238 * this method will create a bitmap of the same size as this view. Because this bitmap
11239 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11240 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11241 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11242 * size than the view. This implies that your application must be able to handle this
11243 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011244 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011245 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11246 * the current density of the screen when the application is in compatibility
11247 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011248 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011249 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011251 * @see #setDrawingCacheEnabled(boolean)
11252 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011253 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011254 * @see #destroyDrawingCache()
11255 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011256 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011257 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11258 return null;
11259 }
11260 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011261 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011262 }
Romain Guy02890fd2010-08-06 17:58:44 -070011263 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011264 }
11265
11266 /**
11267 * <p>Frees the resources used by the drawing cache. If you call
11268 * {@link #buildDrawingCache()} manually without calling
11269 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11270 * should cleanup the cache with this method afterwards.</p>
11271 *
11272 * @see #setDrawingCacheEnabled(boolean)
11273 * @see #buildDrawingCache()
11274 * @see #getDrawingCache()
11275 */
11276 public void destroyDrawingCache() {
11277 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011278 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011279 mDrawingCache = null;
11280 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011281 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011282 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011283 mUnscaledDrawingCache = null;
11284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011285 }
11286
11287 /**
11288 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011289 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011290 * view will always be drawn on top of a solid color.
11291 *
11292 * @param color The background color to use for the drawing cache's bitmap
11293 *
11294 * @see #setDrawingCacheEnabled(boolean)
11295 * @see #buildDrawingCache()
11296 * @see #getDrawingCache()
11297 */
11298 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011299 if (color != mDrawingCacheBackgroundColor) {
11300 mDrawingCacheBackgroundColor = color;
11301 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011303 }
11304
11305 /**
11306 * @see #setDrawingCacheBackgroundColor(int)
11307 *
11308 * @return The background color to used for the drawing cache's bitmap
11309 */
11310 public int getDrawingCacheBackgroundColor() {
11311 return mDrawingCacheBackgroundColor;
11312 }
11313
11314 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011315 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011316 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011317 * @see #buildDrawingCache(boolean)
11318 */
11319 public void buildDrawingCache() {
11320 buildDrawingCache(false);
11321 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011322
Romain Guyfbd8f692009-06-26 14:51:58 -070011323 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011324 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11325 *
11326 * <p>If you call {@link #buildDrawingCache()} manually without calling
11327 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11328 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011329 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011330 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11331 * this method will create a bitmap of the same size as this view. Because this bitmap
11332 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11333 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11334 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11335 * size than the view. This implies that your application must be able to handle this
11336 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011337 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011338 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11339 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011340 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011341 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011342 *
11343 * @see #getDrawingCache()
11344 * @see #destroyDrawingCache()
11345 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011346 public void buildDrawingCache(boolean autoScale) {
11347 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011348 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011349 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011350
11351 if (ViewDebug.TRACE_HIERARCHY) {
11352 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011354
Romain Guy8506ab42009-06-11 17:35:47 -070011355 int width = mRight - mLeft;
11356 int height = mBottom - mTop;
11357
11358 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011359 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011360
Romain Guye1123222009-06-29 14:24:56 -070011361 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011362 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11363 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365
11366 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011367 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011368 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011369
11370 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011371 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011372 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011373 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11374 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011375 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376 return;
11377 }
11378
11379 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011380 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011381
11382 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011383 Bitmap.Config quality;
11384 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011385 // Never pick ARGB_4444 because it looks awful
11386 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11388 case DRAWING_CACHE_QUALITY_AUTO:
11389 quality = Bitmap.Config.ARGB_8888;
11390 break;
11391 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011392 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011393 break;
11394 case DRAWING_CACHE_QUALITY_HIGH:
11395 quality = Bitmap.Config.ARGB_8888;
11396 break;
11397 default:
11398 quality = Bitmap.Config.ARGB_8888;
11399 break;
11400 }
11401 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070011402 // Optimization for translucent windows
11403 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080011404 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011405 }
11406
11407 // Try to cleanup memory
11408 if (bitmap != null) bitmap.recycle();
11409
11410 try {
11411 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070011412 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070011413 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070011414 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011415 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070011416 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011417 }
Adam Powell26153a32010-11-08 15:22:27 -080011418 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011419 } catch (OutOfMemoryError e) {
11420 // If there is not enough memory to create the bitmap cache, just
11421 // ignore the issue as bitmap caches are not required to draw the
11422 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070011423 if (autoScale) {
11424 mDrawingCache = null;
11425 } else {
11426 mUnscaledDrawingCache = null;
11427 }
Romain Guy0211a0a2011-02-14 16:34:59 -080011428 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011429 return;
11430 }
11431
11432 clear = drawingCacheBackgroundColor != 0;
11433 }
11434
11435 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011436 if (attachInfo != null) {
11437 canvas = attachInfo.mCanvas;
11438 if (canvas == null) {
11439 canvas = new Canvas();
11440 }
11441 canvas.setBitmap(bitmap);
11442 // Temporarily clobber the cached Canvas in case one of our children
11443 // is also using a drawing cache. Without this, the children would
11444 // steal the canvas by attaching their own bitmap to it and bad, bad
11445 // thing would happen (invisible views, corrupted drawings, etc.)
11446 attachInfo.mCanvas = null;
11447 } else {
11448 // This case should hopefully never or seldom happen
11449 canvas = new Canvas(bitmap);
11450 }
11451
11452 if (clear) {
11453 bitmap.eraseColor(drawingCacheBackgroundColor);
11454 }
11455
11456 computeScroll();
11457 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080011458
Romain Guye1123222009-06-29 14:24:56 -070011459 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011460 final float scale = attachInfo.mApplicationScale;
11461 canvas.scale(scale, scale);
11462 }
Joe Malin32736f02011-01-19 16:14:20 -080011463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011464 canvas.translate(-mScrollX, -mScrollY);
11465
Romain Guy5bcdff42009-05-14 21:27:18 -070011466 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080011467 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
11468 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070011469 mPrivateFlags |= DRAWING_CACHE_VALID;
11470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011471
11472 // Fast path for layouts with no backgrounds
11473 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11474 if (ViewDebug.TRACE_HIERARCHY) {
11475 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11476 }
Romain Guy5bcdff42009-05-14 21:27:18 -070011477 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011478 dispatchDraw(canvas);
11479 } else {
11480 draw(canvas);
11481 }
11482
11483 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011484 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011485
11486 if (attachInfo != null) {
11487 // Restore the cached Canvas for our siblings
11488 attachInfo.mCanvas = canvas;
11489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011490 }
11491 }
11492
11493 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011494 * Create a snapshot of the view into a bitmap. We should probably make
11495 * some form of this public, but should think about the API.
11496 */
Romain Guy223ff5c2010-03-02 17:07:47 -080011497 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011498 int width = mRight - mLeft;
11499 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011500
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011501 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070011502 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011503 width = (int) ((width * scale) + 0.5f);
11504 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080011505
Romain Guy8c11e312009-09-14 15:15:30 -070011506 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011507 if (bitmap == null) {
11508 throw new OutOfMemoryError();
11509 }
11510
Romain Guyc529d8d2011-09-06 15:01:39 -070011511 Resources resources = getResources();
11512 if (resources != null) {
11513 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
11514 }
Joe Malin32736f02011-01-19 16:14:20 -080011515
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011516 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011517 if (attachInfo != null) {
11518 canvas = attachInfo.mCanvas;
11519 if (canvas == null) {
11520 canvas = new Canvas();
11521 }
11522 canvas.setBitmap(bitmap);
11523 // Temporarily clobber the cached Canvas in case one of our children
11524 // is also using a drawing cache. Without this, the children would
11525 // steal the canvas by attaching their own bitmap to it and bad, bad
11526 // things would happen (invisible views, corrupted drawings, etc.)
11527 attachInfo.mCanvas = null;
11528 } else {
11529 // This case should hopefully never or seldom happen
11530 canvas = new Canvas(bitmap);
11531 }
11532
Romain Guy5bcdff42009-05-14 21:27:18 -070011533 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011534 bitmap.eraseColor(backgroundColor);
11535 }
11536
11537 computeScroll();
11538 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011539 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011540 canvas.translate(-mScrollX, -mScrollY);
11541
Romain Guy5bcdff42009-05-14 21:27:18 -070011542 // Temporarily remove the dirty mask
11543 int flags = mPrivateFlags;
11544 mPrivateFlags &= ~DIRTY_MASK;
11545
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011546 // Fast path for layouts with no backgrounds
11547 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11548 dispatchDraw(canvas);
11549 } else {
11550 draw(canvas);
11551 }
11552
Romain Guy5bcdff42009-05-14 21:27:18 -070011553 mPrivateFlags = flags;
11554
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011555 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011556 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011557
11558 if (attachInfo != null) {
11559 // Restore the cached Canvas for our siblings
11560 attachInfo.mCanvas = canvas;
11561 }
Romain Guy8506ab42009-06-11 17:35:47 -070011562
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011563 return bitmap;
11564 }
11565
11566 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011567 * Indicates whether this View is currently in edit mode. A View is usually
11568 * in edit mode when displayed within a developer tool. For instance, if
11569 * this View is being drawn by a visual user interface builder, this method
11570 * should return true.
11571 *
11572 * Subclasses should check the return value of this method to provide
11573 * different behaviors if their normal behavior might interfere with the
11574 * host environment. For instance: the class spawns a thread in its
11575 * constructor, the drawing code relies on device-specific features, etc.
11576 *
11577 * This method is usually checked in the drawing code of custom widgets.
11578 *
11579 * @return True if this View is in edit mode, false otherwise.
11580 */
11581 public boolean isInEditMode() {
11582 return false;
11583 }
11584
11585 /**
11586 * If the View draws content inside its padding and enables fading edges,
11587 * it needs to support padding offsets. Padding offsets are added to the
11588 * fading edges to extend the length of the fade so that it covers pixels
11589 * drawn inside the padding.
11590 *
11591 * Subclasses of this class should override this method if they need
11592 * to draw content inside the padding.
11593 *
11594 * @return True if padding offset must be applied, false otherwise.
11595 *
11596 * @see #getLeftPaddingOffset()
11597 * @see #getRightPaddingOffset()
11598 * @see #getTopPaddingOffset()
11599 * @see #getBottomPaddingOffset()
11600 *
11601 * @since CURRENT
11602 */
11603 protected boolean isPaddingOffsetRequired() {
11604 return false;
11605 }
11606
11607 /**
11608 * Amount by which to extend the left fading region. Called only when
11609 * {@link #isPaddingOffsetRequired()} returns true.
11610 *
11611 * @return The left padding offset in pixels.
11612 *
11613 * @see #isPaddingOffsetRequired()
11614 *
11615 * @since CURRENT
11616 */
11617 protected int getLeftPaddingOffset() {
11618 return 0;
11619 }
11620
11621 /**
11622 * Amount by which to extend the right fading region. Called only when
11623 * {@link #isPaddingOffsetRequired()} returns true.
11624 *
11625 * @return The right padding offset in pixels.
11626 *
11627 * @see #isPaddingOffsetRequired()
11628 *
11629 * @since CURRENT
11630 */
11631 protected int getRightPaddingOffset() {
11632 return 0;
11633 }
11634
11635 /**
11636 * Amount by which to extend the top fading region. Called only when
11637 * {@link #isPaddingOffsetRequired()} returns true.
11638 *
11639 * @return The top padding offset in pixels.
11640 *
11641 * @see #isPaddingOffsetRequired()
11642 *
11643 * @since CURRENT
11644 */
11645 protected int getTopPaddingOffset() {
11646 return 0;
11647 }
11648
11649 /**
11650 * Amount by which to extend the bottom fading region. Called only when
11651 * {@link #isPaddingOffsetRequired()} returns true.
11652 *
11653 * @return The bottom padding offset in pixels.
11654 *
11655 * @see #isPaddingOffsetRequired()
11656 *
11657 * @since CURRENT
11658 */
11659 protected int getBottomPaddingOffset() {
11660 return 0;
11661 }
11662
11663 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011664 * @hide
11665 * @param offsetRequired
11666 */
11667 protected int getFadeTop(boolean offsetRequired) {
11668 int top = mPaddingTop;
11669 if (offsetRequired) top += getTopPaddingOffset();
11670 return top;
11671 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011672
Romain Guyf2fc4602011-07-19 15:20:03 -070011673 /**
11674 * @hide
11675 * @param offsetRequired
11676 */
11677 protected int getFadeHeight(boolean offsetRequired) {
11678 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070011679 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070011680 return mBottom - mTop - mPaddingBottom - padding;
11681 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011682
Romain Guyf2fc4602011-07-19 15:20:03 -070011683 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011684 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011685 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011686 *
Romain Guy2bffd262010-09-12 17:40:02 -070011687 * <p>Even if this method returns true, it does not mean that every call
11688 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11689 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011690 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011691 * window is hardware accelerated,
11692 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11693 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011694 *
Romain Guy2bffd262010-09-12 17:40:02 -070011695 * @return True if the view is attached to a window and the window is
11696 * hardware accelerated; false in any other case.
11697 */
11698 public boolean isHardwareAccelerated() {
11699 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11700 }
Joe Malin32736f02011-01-19 16:14:20 -080011701
Romain Guy2bffd262010-09-12 17:40:02 -070011702 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011703 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11704 * case of an active Animation being run on the view.
11705 */
11706 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11707 Animation a, boolean scalingRequired) {
11708 Transformation invalidationTransform;
11709 final int flags = parent.mGroupFlags;
11710 final boolean initialized = a.isInitialized();
11711 if (!initialized) {
11712 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11713 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11714 onAnimationStart();
11715 }
11716
11717 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11718 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11719 if (parent.mInvalidationTransformation == null) {
11720 parent.mInvalidationTransformation = new Transformation();
11721 }
11722 invalidationTransform = parent.mInvalidationTransformation;
11723 a.getTransformation(drawingTime, invalidationTransform, 1f);
11724 } else {
11725 invalidationTransform = parent.mChildTransformation;
11726 }
11727 if (more) {
11728 if (!a.willChangeBounds()) {
11729 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11730 parent.FLAG_OPTIMIZE_INVALIDATE) {
11731 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11732 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11733 // The child need to draw an animation, potentially offscreen, so
11734 // make sure we do not cancel invalidate requests
11735 parent.mPrivateFlags |= DRAW_ANIMATION;
11736 parent.invalidate(mLeft, mTop, mRight, mBottom);
11737 }
11738 } else {
11739 if (parent.mInvalidateRegion == null) {
11740 parent.mInvalidateRegion = new RectF();
11741 }
11742 final RectF region = parent.mInvalidateRegion;
11743 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11744 invalidationTransform);
11745
11746 // The child need to draw an animation, potentially offscreen, so
11747 // make sure we do not cancel invalidate requests
11748 parent.mPrivateFlags |= DRAW_ANIMATION;
11749
11750 final int left = mLeft + (int) region.left;
11751 final int top = mTop + (int) region.top;
11752 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11753 top + (int) (region.height() + .5f));
11754 }
11755 }
11756 return more;
11757 }
11758
Chet Haasea1cff502012-02-21 13:43:44 -080011759 void setDisplayListProperties() {
11760 setDisplayListProperties(mDisplayList);
11761 }
11762
11763 /**
11764 * This method is called by getDisplayList() when a display list is created or re-rendered.
11765 * It sets or resets the current value of all properties on that display list (resetting is
11766 * necessary when a display list is being re-created, because we need to make sure that
11767 * previously-set transform values
11768 */
11769 void setDisplayListProperties(DisplayList displayList) {
11770 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
11771 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070011772 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080011773 if (mParent instanceof ViewGroup) {
11774 displayList.setClipChildren(
11775 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
11776 }
Chet Haase9420abd2012-03-29 16:28:32 -070011777 float alpha = 1;
11778 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
11779 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
11780 ViewGroup parentVG = (ViewGroup) mParent;
11781 final boolean hasTransform =
11782 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
11783 if (hasTransform) {
11784 Transformation transform = parentVG.mChildTransformation;
11785 final int transformType = parentVG.mChildTransformation.getTransformationType();
11786 if (transformType != Transformation.TYPE_IDENTITY) {
11787 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
11788 alpha = transform.getAlpha();
11789 }
11790 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
11791 displayList.setStaticMatrix(transform.getMatrix());
11792 }
11793 }
11794 }
Chet Haasea1cff502012-02-21 13:43:44 -080011795 }
11796 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070011797 alpha *= mTransformationInfo.mAlpha;
11798 if (alpha < 1) {
11799 final int multipliedAlpha = (int) (255 * alpha);
11800 if (onSetAlpha(multipliedAlpha)) {
11801 alpha = 1;
11802 }
11803 }
11804 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080011805 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
11806 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
11807 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
11808 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070011809 if (mTransformationInfo.mCamera == null) {
11810 mTransformationInfo.mCamera = new Camera();
11811 mTransformationInfo.matrix3D = new Matrix();
11812 }
11813 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080011814 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
11815 displayList.setPivotX(getPivotX());
11816 displayList.setPivotY(getPivotY());
11817 }
Chet Haase9420abd2012-03-29 16:28:32 -070011818 } else if (alpha < 1) {
11819 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080011820 }
11821 }
11822 }
11823
Chet Haasebcca79a2012-02-14 08:45:14 -080011824 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011825 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11826 * This draw() method is an implementation detail and is not intended to be overridden or
11827 * to be called from anywhere else other than ViewGroup.drawChild().
11828 */
11829 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080011830 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
11831 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080011832 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011833 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011834 final int flags = parent.mGroupFlags;
11835
Chet Haasea1cff502012-02-21 13:43:44 -080011836 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080011837 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080011838 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011839 }
11840
11841 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011842 boolean concatMatrix = false;
11843
11844 boolean scalingRequired = false;
11845 boolean caching;
11846 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11847
11848 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080011849 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
11850 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011851 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070011852 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080011853 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11854 } else {
11855 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11856 }
11857
Chet Haasebcca79a2012-02-14 08:45:14 -080011858 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011859 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011860 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011861 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011862 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070011863 } else if (!useDisplayListProperties &&
11864 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011865 final boolean hasTransform =
11866 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011867 if (hasTransform) {
11868 final int transformType = parent.mChildTransformation.getTransformationType();
11869 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11870 parent.mChildTransformation : null;
11871 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11872 }
11873 }
11874
11875 concatMatrix |= !childHasIdentityMatrix;
11876
11877 // Sets the flag as early as possible to allow draw() implementations
11878 // to call invalidate() successfully when doing animations
11879 mPrivateFlags |= DRAWN;
11880
Chet Haasebcca79a2012-02-14 08:45:14 -080011881 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011882 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11883 return more;
11884 }
11885
11886 if (hardwareAccelerated) {
11887 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11888 // retain the flag's value temporarily in the mRecreateDisplayList flag
11889 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11890 mPrivateFlags &= ~INVALIDATED;
11891 }
11892
11893 computeScroll();
11894
11895 final int sx = mScrollX;
11896 final int sy = mScrollY;
11897
11898 DisplayList displayList = null;
11899 Bitmap cache = null;
11900 boolean hasDisplayList = false;
11901 if (caching) {
11902 if (!hardwareAccelerated) {
11903 if (layerType != LAYER_TYPE_NONE) {
11904 layerType = LAYER_TYPE_SOFTWARE;
11905 buildDrawingCache(true);
11906 }
11907 cache = getDrawingCache(true);
11908 } else {
11909 switch (layerType) {
11910 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070011911 if (useDisplayListProperties) {
11912 hasDisplayList = canHaveDisplayList();
11913 } else {
11914 buildDrawingCache(true);
11915 cache = getDrawingCache(true);
11916 }
Chet Haase64a48c12012-02-13 16:33:29 -080011917 break;
Chet Haasea1cff502012-02-21 13:43:44 -080011918 case LAYER_TYPE_HARDWARE:
11919 if (useDisplayListProperties) {
11920 hasDisplayList = canHaveDisplayList();
11921 }
11922 break;
Chet Haase64a48c12012-02-13 16:33:29 -080011923 case LAYER_TYPE_NONE:
11924 // Delay getting the display list until animation-driven alpha values are
11925 // set up and possibly passed on to the view
11926 hasDisplayList = canHaveDisplayList();
11927 break;
11928 }
11929 }
11930 }
Chet Haasea1cff502012-02-21 13:43:44 -080011931 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070011932 if (useDisplayListProperties) {
11933 displayList = getDisplayList();
11934 if (!displayList.isValid()) {
11935 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11936 // to getDisplayList(), the display list will be marked invalid and we should not
11937 // try to use it again.
11938 displayList = null;
11939 hasDisplayList = false;
11940 useDisplayListProperties = false;
11941 }
11942 }
Chet Haase64a48c12012-02-13 16:33:29 -080011943
11944 final boolean hasNoCache = cache == null || hasDisplayList;
11945 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11946 layerType != LAYER_TYPE_HARDWARE;
11947
Chet Haasea1cff502012-02-21 13:43:44 -080011948 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070011949 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080011950 restoreTo = canvas.save();
11951 }
Chet Haase64a48c12012-02-13 16:33:29 -080011952 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011953 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011954 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080011955 if (!useDisplayListProperties) {
11956 canvas.translate(mLeft, mTop);
11957 }
Chet Haase64a48c12012-02-13 16:33:29 -080011958 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080011959 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070011960 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080011961 restoreTo = canvas.save();
11962 }
Chet Haase64a48c12012-02-13 16:33:29 -080011963 // mAttachInfo cannot be null, otherwise scalingRequired == false
11964 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11965 canvas.scale(scale, scale);
11966 }
11967 }
11968
Chet Haasea1cff502012-02-21 13:43:44 -080011969 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070011970 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080011971 if (transformToApply != null || !childHasIdentityMatrix) {
11972 int transX = 0;
11973 int transY = 0;
11974
11975 if (offsetForScroll) {
11976 transX = -sx;
11977 transY = -sy;
11978 }
11979
11980 if (transformToApply != null) {
11981 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070011982 if (useDisplayListProperties) {
11983 displayList.setAnimationMatrix(transformToApply.getMatrix());
11984 } else {
11985 // Undo the scroll translation, apply the transformation matrix,
11986 // then redo the scroll translate to get the correct result.
11987 canvas.translate(-transX, -transY);
11988 canvas.concat(transformToApply.getMatrix());
11989 canvas.translate(transX, transY);
11990 }
Chet Haasea1cff502012-02-21 13:43:44 -080011991 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011992 }
11993
11994 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070011995 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080011996 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080011997 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011998 }
11999 }
12000
Chet Haasea1cff502012-02-21 13:43:44 -080012001 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012002 canvas.translate(-transX, -transY);
12003 canvas.concat(getMatrix());
12004 canvas.translate(transX, transY);
12005 }
12006 }
12007
Chet Haase9420abd2012-03-29 16:28:32 -070012008 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012009 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012010 if (hasNoCache) {
12011 final int multipliedAlpha = (int) (255 * alpha);
12012 if (!onSetAlpha(multipliedAlpha)) {
12013 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012014 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012015 layerType != LAYER_TYPE_NONE) {
12016 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12017 }
Chet Haase9420abd2012-03-29 16:28:32 -070012018 if (useDisplayListProperties) {
12019 displayList.setAlpha(alpha * getAlpha());
12020 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012021 final int scrollX = hasDisplayList ? 0 : sx;
12022 final int scrollY = hasDisplayList ? 0 : sy;
12023 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12024 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012025 }
12026 } else {
12027 // Alpha is handled by the child directly, clobber the layer's alpha
12028 mPrivateFlags |= ALPHA_SET;
12029 }
12030 }
12031 }
12032 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12033 onSetAlpha(255);
12034 mPrivateFlags &= ~ALPHA_SET;
12035 }
12036
Chet Haasea1cff502012-02-21 13:43:44 -080012037 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12038 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012039 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012040 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012041 } else {
12042 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012043 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012044 } else {
12045 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12046 }
12047 }
12048 }
12049
Chet Haase9420abd2012-03-29 16:28:32 -070012050 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012051 displayList = getDisplayList();
12052 if (!displayList.isValid()) {
12053 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12054 // to getDisplayList(), the display list will be marked invalid and we should not
12055 // try to use it again.
12056 displayList = null;
12057 hasDisplayList = false;
12058 }
12059 }
12060
12061 if (hasNoCache) {
12062 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012063 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012064 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012065 if (layer != null && layer.isValid()) {
12066 mLayerPaint.setAlpha((int) (alpha * 255));
12067 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12068 layerRendered = true;
12069 } else {
12070 final int scrollX = hasDisplayList ? 0 : sx;
12071 final int scrollY = hasDisplayList ? 0 : sy;
12072 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012073 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012074 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12075 }
12076 }
12077
12078 if (!layerRendered) {
12079 if (!hasDisplayList) {
12080 // Fast path for layouts with no backgrounds
12081 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12082 if (ViewDebug.TRACE_HIERARCHY) {
12083 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12084 }
12085 mPrivateFlags &= ~DIRTY_MASK;
12086 dispatchDraw(canvas);
12087 } else {
12088 draw(canvas);
12089 }
12090 } else {
12091 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080012092 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080012093 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012094 }
12095 }
12096 } else if (cache != null) {
12097 mPrivateFlags &= ~DIRTY_MASK;
12098 Paint cachePaint;
12099
12100 if (layerType == LAYER_TYPE_NONE) {
12101 cachePaint = parent.mCachePaint;
12102 if (cachePaint == null) {
12103 cachePaint = new Paint();
12104 cachePaint.setDither(false);
12105 parent.mCachePaint = cachePaint;
12106 }
Chet Haase9420abd2012-03-29 16:28:32 -070012107 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012108 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012109 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12110 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012111 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012112 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012113 }
12114 } else {
12115 cachePaint = mLayerPaint;
12116 cachePaint.setAlpha((int) (alpha * 255));
12117 }
12118 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12119 }
12120
Chet Haasea1cff502012-02-21 13:43:44 -080012121 if (restoreTo >= 0) {
12122 canvas.restoreToCount(restoreTo);
12123 }
Chet Haase64a48c12012-02-13 16:33:29 -080012124
12125 if (a != null && !more) {
12126 if (!hardwareAccelerated && !a.getFillAfter()) {
12127 onSetAlpha(255);
12128 }
12129 parent.finishAnimatingView(this, a);
12130 }
12131
12132 if (more && hardwareAccelerated) {
12133 // invalidation is the trigger to recreate display lists, so if we're using
12134 // display lists to render, force an invalidate to allow the animation to
12135 // continue drawing another frame
12136 parent.invalidate(true);
12137 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12138 // alpha animations should cause the child to recreate its display list
12139 invalidate(true);
12140 }
12141 }
12142
12143 mRecreateDisplayList = false;
12144
12145 return more;
12146 }
12147
12148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012149 * Manually render this view (and all of its children) to the given Canvas.
12150 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012151 * called. When implementing a view, implement
12152 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12153 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012154 *
12155 * @param canvas The Canvas to which the View is rendered.
12156 */
12157 public void draw(Canvas canvas) {
12158 if (ViewDebug.TRACE_HIERARCHY) {
12159 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12160 }
12161
Romain Guy5bcdff42009-05-14 21:27:18 -070012162 final int privateFlags = mPrivateFlags;
12163 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12164 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12165 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012167 /*
12168 * Draw traversal performs several drawing steps which must be executed
12169 * in the appropriate order:
12170 *
12171 * 1. Draw the background
12172 * 2. If necessary, save the canvas' layers to prepare for fading
12173 * 3. Draw view's content
12174 * 4. Draw children
12175 * 5. If necessary, draw the fading edges and restore layers
12176 * 6. Draw decorations (scrollbars for instance)
12177 */
12178
12179 // Step 1, draw the background, if needed
12180 int saveCount;
12181
Romain Guy24443ea2009-05-11 11:56:30 -070012182 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012183 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012184 if (background != null) {
12185 final int scrollX = mScrollX;
12186 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012187
Romain Guy24443ea2009-05-11 11:56:30 -070012188 if (mBackgroundSizeChanged) {
12189 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12190 mBackgroundSizeChanged = false;
12191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012192
Romain Guy24443ea2009-05-11 11:56:30 -070012193 if ((scrollX | scrollY) == 0) {
12194 background.draw(canvas);
12195 } else {
12196 canvas.translate(scrollX, scrollY);
12197 background.draw(canvas);
12198 canvas.translate(-scrollX, -scrollY);
12199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012200 }
12201 }
12202
12203 // skip step 2 & 5 if possible (common case)
12204 final int viewFlags = mViewFlags;
12205 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12206 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12207 if (!verticalEdges && !horizontalEdges) {
12208 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012209 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012210
12211 // Step 4, draw the children
12212 dispatchDraw(canvas);
12213
12214 // Step 6, draw decorations (scrollbars)
12215 onDrawScrollBars(canvas);
12216
12217 // we're done...
12218 return;
12219 }
12220
12221 /*
12222 * Here we do the full fledged routine...
12223 * (this is an uncommon case where speed matters less,
12224 * this is why we repeat some of the tests that have been
12225 * done above)
12226 */
12227
12228 boolean drawTop = false;
12229 boolean drawBottom = false;
12230 boolean drawLeft = false;
12231 boolean drawRight = false;
12232
12233 float topFadeStrength = 0.0f;
12234 float bottomFadeStrength = 0.0f;
12235 float leftFadeStrength = 0.0f;
12236 float rightFadeStrength = 0.0f;
12237
12238 // Step 2, save the canvas' layers
12239 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012240
12241 final boolean offsetRequired = isPaddingOffsetRequired();
12242 if (offsetRequired) {
12243 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012244 }
12245
12246 int left = mScrollX + paddingLeft;
12247 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012248 int top = mScrollY + getFadeTop(offsetRequired);
12249 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012250
12251 if (offsetRequired) {
12252 right += getRightPaddingOffset();
12253 bottom += getBottomPaddingOffset();
12254 }
12255
12256 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012257 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012258 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012259
12260 // clip the fade length if top and bottom fades overlap
12261 // overlapping fades produce odd-looking artifacts
12262 if (verticalEdges && (top + length > bottom - length)) {
12263 length = (bottom - top) / 2;
12264 }
12265
12266 // also clip horizontal fades if necessary
12267 if (horizontalEdges && (left + length > right - length)) {
12268 length = (right - left) / 2;
12269 }
12270
12271 if (verticalEdges) {
12272 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012273 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012274 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012275 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012276 }
12277
12278 if (horizontalEdges) {
12279 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012280 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012281 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012282 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012283 }
12284
12285 saveCount = canvas.getSaveCount();
12286
12287 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012288 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012289 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12290
12291 if (drawTop) {
12292 canvas.saveLayer(left, top, right, top + length, null, flags);
12293 }
12294
12295 if (drawBottom) {
12296 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12297 }
12298
12299 if (drawLeft) {
12300 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12301 }
12302
12303 if (drawRight) {
12304 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12305 }
12306 } else {
12307 scrollabilityCache.setFadeColor(solidColor);
12308 }
12309
12310 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012311 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012312
12313 // Step 4, draw the children
12314 dispatchDraw(canvas);
12315
12316 // Step 5, draw the fade effect and restore layers
12317 final Paint p = scrollabilityCache.paint;
12318 final Matrix matrix = scrollabilityCache.matrix;
12319 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012320
12321 if (drawTop) {
12322 matrix.setScale(1, fadeHeight * topFadeStrength);
12323 matrix.postTranslate(left, top);
12324 fade.setLocalMatrix(matrix);
12325 canvas.drawRect(left, top, right, top + length, p);
12326 }
12327
12328 if (drawBottom) {
12329 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12330 matrix.postRotate(180);
12331 matrix.postTranslate(left, bottom);
12332 fade.setLocalMatrix(matrix);
12333 canvas.drawRect(left, bottom - length, right, bottom, p);
12334 }
12335
12336 if (drawLeft) {
12337 matrix.setScale(1, fadeHeight * leftFadeStrength);
12338 matrix.postRotate(-90);
12339 matrix.postTranslate(left, top);
12340 fade.setLocalMatrix(matrix);
12341 canvas.drawRect(left, top, left + length, bottom, p);
12342 }
12343
12344 if (drawRight) {
12345 matrix.setScale(1, fadeHeight * rightFadeStrength);
12346 matrix.postRotate(90);
12347 matrix.postTranslate(right, top);
12348 fade.setLocalMatrix(matrix);
12349 canvas.drawRect(right - length, top, right, bottom, p);
12350 }
12351
12352 canvas.restoreToCount(saveCount);
12353
12354 // Step 6, draw decorations (scrollbars)
12355 onDrawScrollBars(canvas);
12356 }
12357
12358 /**
12359 * Override this if your view is known to always be drawn on top of a solid color background,
12360 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12361 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12362 * should be set to 0xFF.
12363 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012364 * @see #setVerticalFadingEdgeEnabled(boolean)
12365 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012366 *
12367 * @return The known solid color background for this view, or 0 if the color may vary
12368 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012369 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012370 public int getSolidColor() {
12371 return 0;
12372 }
12373
12374 /**
12375 * Build a human readable string representation of the specified view flags.
12376 *
12377 * @param flags the view flags to convert to a string
12378 * @return a String representing the supplied flags
12379 */
12380 private static String printFlags(int flags) {
12381 String output = "";
12382 int numFlags = 0;
12383 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12384 output += "TAKES_FOCUS";
12385 numFlags++;
12386 }
12387
12388 switch (flags & VISIBILITY_MASK) {
12389 case INVISIBLE:
12390 if (numFlags > 0) {
12391 output += " ";
12392 }
12393 output += "INVISIBLE";
12394 // USELESS HERE numFlags++;
12395 break;
12396 case GONE:
12397 if (numFlags > 0) {
12398 output += " ";
12399 }
12400 output += "GONE";
12401 // USELESS HERE numFlags++;
12402 break;
12403 default:
12404 break;
12405 }
12406 return output;
12407 }
12408
12409 /**
12410 * Build a human readable string representation of the specified private
12411 * view flags.
12412 *
12413 * @param privateFlags the private view flags to convert to a string
12414 * @return a String representing the supplied flags
12415 */
12416 private static String printPrivateFlags(int privateFlags) {
12417 String output = "";
12418 int numFlags = 0;
12419
12420 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
12421 output += "WANTS_FOCUS";
12422 numFlags++;
12423 }
12424
12425 if ((privateFlags & FOCUSED) == FOCUSED) {
12426 if (numFlags > 0) {
12427 output += " ";
12428 }
12429 output += "FOCUSED";
12430 numFlags++;
12431 }
12432
12433 if ((privateFlags & SELECTED) == SELECTED) {
12434 if (numFlags > 0) {
12435 output += " ";
12436 }
12437 output += "SELECTED";
12438 numFlags++;
12439 }
12440
12441 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
12442 if (numFlags > 0) {
12443 output += " ";
12444 }
12445 output += "IS_ROOT_NAMESPACE";
12446 numFlags++;
12447 }
12448
12449 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
12450 if (numFlags > 0) {
12451 output += " ";
12452 }
12453 output += "HAS_BOUNDS";
12454 numFlags++;
12455 }
12456
12457 if ((privateFlags & DRAWN) == DRAWN) {
12458 if (numFlags > 0) {
12459 output += " ";
12460 }
12461 output += "DRAWN";
12462 // USELESS HERE numFlags++;
12463 }
12464 return output;
12465 }
12466
12467 /**
12468 * <p>Indicates whether or not this view's layout will be requested during
12469 * the next hierarchy layout pass.</p>
12470 *
12471 * @return true if the layout will be forced during next layout pass
12472 */
12473 public boolean isLayoutRequested() {
12474 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
12475 }
12476
12477 /**
12478 * Assign a size and position to a view and all of its
12479 * descendants
12480 *
12481 * <p>This is the second phase of the layout mechanism.
12482 * (The first is measuring). In this phase, each parent calls
12483 * layout on all of its children to position them.
12484 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080012485 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012486 *
Chet Haase9c087442011-01-12 16:20:16 -080012487 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012488 * Derived classes with children should override
12489 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080012490 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012491 *
12492 * @param l Left position, relative to parent
12493 * @param t Top position, relative to parent
12494 * @param r Right position, relative to parent
12495 * @param b Bottom position, relative to parent
12496 */
Romain Guy5429e1d2010-09-07 12:38:00 -070012497 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080012498 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070012499 int oldL = mLeft;
12500 int oldT = mTop;
12501 int oldB = mBottom;
12502 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012503 boolean changed = setFrame(l, t, r, b);
12504 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
12505 if (ViewDebug.TRACE_HIERARCHY) {
12506 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
12507 }
12508
12509 onLayout(changed, l, t, r, b);
12510 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070012511
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012512 ListenerInfo li = mListenerInfo;
12513 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070012514 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012515 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070012516 int numListeners = listenersCopy.size();
12517 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070012518 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070012519 }
12520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012521 }
12522 mPrivateFlags &= ~FORCE_LAYOUT;
12523 }
12524
12525 /**
12526 * Called from layout when this view should
12527 * assign a size and position to each of its children.
12528 *
12529 * Derived classes with children should override
12530 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070012531 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012532 * @param changed This is a new size or position for this view
12533 * @param left Left position, relative to parent
12534 * @param top Top position, relative to parent
12535 * @param right Right position, relative to parent
12536 * @param bottom Bottom position, relative to parent
12537 */
12538 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
12539 }
12540
12541 /**
12542 * Assign a size and position to this view.
12543 *
12544 * This is called from layout.
12545 *
12546 * @param left Left position, relative to parent
12547 * @param top Top position, relative to parent
12548 * @param right Right position, relative to parent
12549 * @param bottom Bottom position, relative to parent
12550 * @return true if the new size and position are different than the
12551 * previous ones
12552 * {@hide}
12553 */
12554 protected boolean setFrame(int left, int top, int right, int bottom) {
12555 boolean changed = false;
12556
12557 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070012558 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012559 + right + "," + bottom + ")");
12560 }
12561
12562 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
12563 changed = true;
12564
12565 // Remember our drawn bit
12566 int drawn = mPrivateFlags & DRAWN;
12567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012568 int oldWidth = mRight - mLeft;
12569 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070012570 int newWidth = right - left;
12571 int newHeight = bottom - top;
12572 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
12573
12574 // Invalidate our old position
12575 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012576
12577 mLeft = left;
12578 mTop = top;
12579 mRight = right;
12580 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080012581 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
12582 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
12583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012584
12585 mPrivateFlags |= HAS_BOUNDS;
12586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012587
Chet Haase75755e22011-07-18 17:48:25 -070012588 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012589 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
12590 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012591 if (mTransformationInfo != null) {
12592 mTransformationInfo.mMatrixDirty = true;
12593 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012595 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
12596 }
12597
12598 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
12599 // If we are visible, force the DRAWN bit to on so that
12600 // this invalidate will go through (at least to our parent).
12601 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012602 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012603 // the DRAWN bit.
12604 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070012605 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080012606 // parent display list may need to be recreated based on a change in the bounds
12607 // of any child
12608 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012609 }
12610
12611 // Reset drawn bit to original value (invalidate turns it off)
12612 mPrivateFlags |= drawn;
12613
12614 mBackgroundSizeChanged = true;
12615 }
12616 return changed;
12617 }
12618
12619 /**
12620 * Finalize inflating a view from XML. This is called as the last phase
12621 * of inflation, after all child views have been added.
12622 *
12623 * <p>Even if the subclass overrides onFinishInflate, they should always be
12624 * sure to call the super method, so that we get called.
12625 */
12626 protected void onFinishInflate() {
12627 }
12628
12629 /**
12630 * Returns the resources associated with this view.
12631 *
12632 * @return Resources object.
12633 */
12634 public Resources getResources() {
12635 return mResources;
12636 }
12637
12638 /**
12639 * Invalidates the specified Drawable.
12640 *
12641 * @param drawable the drawable to invalidate
12642 */
12643 public void invalidateDrawable(Drawable drawable) {
12644 if (verifyDrawable(drawable)) {
12645 final Rect dirty = drawable.getBounds();
12646 final int scrollX = mScrollX;
12647 final int scrollY = mScrollY;
12648
12649 invalidate(dirty.left + scrollX, dirty.top + scrollY,
12650 dirty.right + scrollX, dirty.bottom + scrollY);
12651 }
12652 }
12653
12654 /**
12655 * Schedules an action on a drawable to occur at a specified time.
12656 *
12657 * @param who the recipient of the action
12658 * @param what the action to run on the drawable
12659 * @param when the time at which the action must occur. Uses the
12660 * {@link SystemClock#uptimeMillis} timebase.
12661 */
12662 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080012663 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012664 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080012665 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012666 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
12667 Choreographer.CALLBACK_ANIMATION, what, who,
12668 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080012669 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012670 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080012671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012672 }
12673 }
12674
12675 /**
12676 * Cancels a scheduled action on a drawable.
12677 *
12678 * @param who the recipient of the action
12679 * @param what the action to cancel
12680 */
12681 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080012682 if (verifyDrawable(who) && what != null) {
12683 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012684 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12685 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080012686 } else {
12687 ViewRootImpl.getRunQueue().removeCallbacks(what);
12688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012689 }
12690 }
12691
12692 /**
12693 * Unschedule any events associated with the given Drawable. This can be
12694 * used when selecting a new Drawable into a view, so that the previous
12695 * one is completely unscheduled.
12696 *
12697 * @param who The Drawable to unschedule.
12698 *
12699 * @see #drawableStateChanged
12700 */
12701 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080012702 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012703 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12704 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012705 }
12706 }
12707
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012708 /**
12709 * Return the layout direction of a given Drawable.
12710 *
12711 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012712 */
12713 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012714 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070012715 }
12716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012717 /**
12718 * If your view subclass is displaying its own Drawable objects, it should
12719 * override this function and return true for any Drawable it is
12720 * displaying. This allows animations for those drawables to be
12721 * scheduled.
12722 *
12723 * <p>Be sure to call through to the super class when overriding this
12724 * function.
12725 *
12726 * @param who The Drawable to verify. Return true if it is one you are
12727 * displaying, else return the result of calling through to the
12728 * super class.
12729 *
12730 * @return boolean If true than the Drawable is being displayed in the
12731 * view; else false and it is not allowed to animate.
12732 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012733 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12734 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012735 */
12736 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012737 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012738 }
12739
12740 /**
12741 * This function is called whenever the state of the view changes in such
12742 * a way that it impacts the state of drawables being shown.
12743 *
12744 * <p>Be sure to call through to the superclass when overriding this
12745 * function.
12746 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012747 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012748 */
12749 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012750 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012751 if (d != null && d.isStateful()) {
12752 d.setState(getDrawableState());
12753 }
12754 }
12755
12756 /**
12757 * Call this to force a view to update its drawable state. This will cause
12758 * drawableStateChanged to be called on this view. Views that are interested
12759 * in the new state should call getDrawableState.
12760 *
12761 * @see #drawableStateChanged
12762 * @see #getDrawableState
12763 */
12764 public void refreshDrawableState() {
12765 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12766 drawableStateChanged();
12767
12768 ViewParent parent = mParent;
12769 if (parent != null) {
12770 parent.childDrawableStateChanged(this);
12771 }
12772 }
12773
12774 /**
12775 * Return an array of resource IDs of the drawable states representing the
12776 * current state of the view.
12777 *
12778 * @return The current drawable state
12779 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012780 * @see Drawable#setState(int[])
12781 * @see #drawableStateChanged()
12782 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012783 */
12784 public final int[] getDrawableState() {
12785 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12786 return mDrawableState;
12787 } else {
12788 mDrawableState = onCreateDrawableState(0);
12789 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12790 return mDrawableState;
12791 }
12792 }
12793
12794 /**
12795 * Generate the new {@link android.graphics.drawable.Drawable} state for
12796 * this view. This is called by the view
12797 * system when the cached Drawable state is determined to be invalid. To
12798 * retrieve the current state, you should use {@link #getDrawableState}.
12799 *
12800 * @param extraSpace if non-zero, this is the number of extra entries you
12801 * would like in the returned array in which you can place your own
12802 * states.
12803 *
12804 * @return Returns an array holding the current {@link Drawable} state of
12805 * the view.
12806 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012807 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012808 */
12809 protected int[] onCreateDrawableState(int extraSpace) {
12810 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12811 mParent instanceof View) {
12812 return ((View) mParent).onCreateDrawableState(extraSpace);
12813 }
12814
12815 int[] drawableState;
12816
12817 int privateFlags = mPrivateFlags;
12818
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012819 int viewStateIndex = 0;
12820 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12821 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12822 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012823 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012824 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12825 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012826 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12827 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012828 // This is set if HW acceleration is requested, even if the current
12829 // process doesn't allow it. This is just to allow app preview
12830 // windows to better match their app.
12831 viewStateIndex |= VIEW_STATE_ACCELERATED;
12832 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012833 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012834
Christopher Tate3d4bf172011-03-28 16:16:46 -070012835 final int privateFlags2 = mPrivateFlags2;
12836 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12837 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012839 drawableState = VIEW_STATE_SETS[viewStateIndex];
12840
12841 //noinspection ConstantIfStatement
12842 if (false) {
12843 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12844 Log.i("View", toString()
12845 + " pressed=" + ((privateFlags & PRESSED) != 0)
12846 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12847 + " fo=" + hasFocus()
12848 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012849 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012850 + ": " + Arrays.toString(drawableState));
12851 }
12852
12853 if (extraSpace == 0) {
12854 return drawableState;
12855 }
12856
12857 final int[] fullState;
12858 if (drawableState != null) {
12859 fullState = new int[drawableState.length + extraSpace];
12860 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12861 } else {
12862 fullState = new int[extraSpace];
12863 }
12864
12865 return fullState;
12866 }
12867
12868 /**
12869 * Merge your own state values in <var>additionalState</var> into the base
12870 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012871 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012872 *
12873 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012874 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012875 * own additional state values.
12876 *
12877 * @param additionalState The additional state values you would like
12878 * added to <var>baseState</var>; this array is not modified.
12879 *
12880 * @return As a convenience, the <var>baseState</var> array you originally
12881 * passed into the function is returned.
12882 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012883 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012884 */
12885 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12886 final int N = baseState.length;
12887 int i = N - 1;
12888 while (i >= 0 && baseState[i] == 0) {
12889 i--;
12890 }
12891 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12892 return baseState;
12893 }
12894
12895 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012896 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12897 * on all Drawable objects associated with this view.
12898 */
12899 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012900 if (mBackground != null) {
12901 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070012902 }
12903 }
12904
12905 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012906 * Sets the background color for this view.
12907 * @param color the color of the background
12908 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012909 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012910 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012911 if (mBackground instanceof ColorDrawable) {
12912 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070012913 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070012914 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070012915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012916 }
12917
12918 /**
12919 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012920 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012921 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070012922 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012923 * @attr ref android.R.styleable#View_background
12924 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012925 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012926 public void setBackgroundResource(int resid) {
12927 if (resid != 0 && resid == mBackgroundResource) {
12928 return;
12929 }
12930
12931 Drawable d= null;
12932 if (resid != 0) {
12933 d = mResources.getDrawable(resid);
12934 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012935 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012936
12937 mBackgroundResource = resid;
12938 }
12939
12940 /**
12941 * Set the background to a given Drawable, or remove the background. If the
12942 * background has padding, this View's padding is set to the background's
12943 * padding. However, when a background is removed, this View's padding isn't
12944 * touched. If setting the padding is desired, please use
12945 * {@link #setPadding(int, int, int, int)}.
12946 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012947 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012948 * background
12949 */
Philip Milne6c8ea062012-04-03 17:38:43 -070012950 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070012951 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070012952 setBackgroundDrawable(background);
12953 }
12954
12955 /**
12956 * @deprecated use {@link #setBackground(Drawable)} instead
12957 */
12958 @Deprecated
12959 public void setBackgroundDrawable(Drawable background) {
12960 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012961 return;
12962 }
12963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012964 boolean requestLayout = false;
12965
12966 mBackgroundResource = 0;
12967
12968 /*
12969 * Regardless of whether we're setting a new background or not, we want
12970 * to clear the previous drawable.
12971 */
Philip Milne6c8ea062012-04-03 17:38:43 -070012972 if (mBackground != null) {
12973 mBackground.setCallback(null);
12974 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012975 }
12976
Philip Milne6c8ea062012-04-03 17:38:43 -070012977 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012978 Rect padding = sThreadLocal.get();
12979 if (padding == null) {
12980 padding = new Rect();
12981 sThreadLocal.set(padding);
12982 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012983 if (background.getPadding(padding)) {
12984 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012985 case LAYOUT_DIRECTION_RTL:
12986 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12987 break;
12988 case LAYOUT_DIRECTION_LTR:
12989 default:
12990 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012992 }
12993
12994 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12995 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070012996 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
12997 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012998 requestLayout = true;
12999 }
13000
Philip Milne6c8ea062012-04-03 17:38:43 -070013001 background.setCallback(this);
13002 if (background.isStateful()) {
13003 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013004 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013005 background.setVisible(getVisibility() == VISIBLE, false);
13006 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013007
13008 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13009 mPrivateFlags &= ~SKIP_DRAW;
13010 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13011 requestLayout = true;
13012 }
13013 } else {
13014 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013015 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013016
13017 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13018 /*
13019 * This view ONLY drew the background before and we're removing
13020 * the background, so now it won't draw anything
13021 * (hence we SKIP_DRAW)
13022 */
13023 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13024 mPrivateFlags |= SKIP_DRAW;
13025 }
13026
13027 /*
13028 * When the background is set, we try to apply its padding to this
13029 * View. When the background is removed, we don't touch this View's
13030 * padding. This is noted in the Javadocs. Hence, we don't need to
13031 * requestLayout(), the invalidate() below is sufficient.
13032 */
13033
13034 // The old background's minimum size could have affected this
13035 // View's layout, so let's requestLayout
13036 requestLayout = true;
13037 }
13038
Romain Guy8f1344f52009-05-15 16:03:59 -070013039 computeOpaqueFlags();
13040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013041 if (requestLayout) {
13042 requestLayout();
13043 }
13044
13045 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013046 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013047 }
13048
13049 /**
13050 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013051 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013052 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013053 *
13054 * @see #setBackground(Drawable)
13055 *
13056 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013057 */
13058 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013059 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013060 }
13061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013062 /**
13063 * Sets the padding. The view may add on the space required to display
13064 * the scrollbars, depending on the style and visibility of the scrollbars.
13065 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13066 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13067 * from the values set in this call.
13068 *
13069 * @attr ref android.R.styleable#View_padding
13070 * @attr ref android.R.styleable#View_paddingBottom
13071 * @attr ref android.R.styleable#View_paddingLeft
13072 * @attr ref android.R.styleable#View_paddingRight
13073 * @attr ref android.R.styleable#View_paddingTop
13074 * @param left the left padding in pixels
13075 * @param top the top padding in pixels
13076 * @param right the right padding in pixels
13077 * @param bottom the bottom padding in pixels
13078 */
13079 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013080 mUserPaddingStart = -1;
13081 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013082 mUserPaddingRelative = false;
13083
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013084 internalSetPadding(left, top, right, bottom);
13085 }
13086
13087 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013088 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013089 mUserPaddingRight = right;
13090 mUserPaddingBottom = bottom;
13091
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013092 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013093 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013094
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013095 // Common case is there are no scroll bars.
13096 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013097 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013098 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013099 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013100 switch (mVerticalScrollbarPosition) {
13101 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013102 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13103 left += offset;
13104 } else {
13105 right += offset;
13106 }
13107 break;
Adam Powell20232d02010-12-08 21:08:53 -080013108 case SCROLLBAR_POSITION_RIGHT:
13109 right += offset;
13110 break;
13111 case SCROLLBAR_POSITION_LEFT:
13112 left += offset;
13113 break;
13114 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013115 }
Adam Powell20232d02010-12-08 21:08:53 -080013116 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013117 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13118 ? 0 : getHorizontalScrollbarHeight();
13119 }
13120 }
Romain Guy8506ab42009-06-11 17:35:47 -070013121
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013122 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013123 changed = true;
13124 mPaddingLeft = left;
13125 }
13126 if (mPaddingTop != top) {
13127 changed = true;
13128 mPaddingTop = top;
13129 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013130 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013131 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013132 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013133 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013134 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013135 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013136 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013137 }
13138
13139 if (changed) {
13140 requestLayout();
13141 }
13142 }
13143
13144 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013145 * Sets the relative padding. The view may add on the space required to display
13146 * the scrollbars, depending on the style and visibility of the scrollbars.
13147 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13148 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13149 * from the values set in this call.
13150 *
13151 * @attr ref android.R.styleable#View_padding
13152 * @attr ref android.R.styleable#View_paddingBottom
13153 * @attr ref android.R.styleable#View_paddingStart
13154 * @attr ref android.R.styleable#View_paddingEnd
13155 * @attr ref android.R.styleable#View_paddingTop
13156 * @param start the start padding in pixels
13157 * @param top the top padding in pixels
13158 * @param end the end padding in pixels
13159 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013160 */
13161 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013162 mUserPaddingStart = start;
13163 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013164 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013165
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013166 switch(getResolvedLayoutDirection()) {
13167 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013168 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013169 break;
13170 case LAYOUT_DIRECTION_LTR:
13171 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013172 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013173 }
13174 }
13175
13176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013177 * Returns the top padding of this view.
13178 *
13179 * @return the top padding in pixels
13180 */
13181 public int getPaddingTop() {
13182 return mPaddingTop;
13183 }
13184
13185 /**
13186 * Returns the bottom padding of this view. If there are inset and enabled
13187 * scrollbars, this value may include the space required to display the
13188 * scrollbars as well.
13189 *
13190 * @return the bottom padding in pixels
13191 */
13192 public int getPaddingBottom() {
13193 return mPaddingBottom;
13194 }
13195
13196 /**
13197 * Returns the left padding of this view. If there are inset and enabled
13198 * scrollbars, this value may include the space required to display the
13199 * scrollbars as well.
13200 *
13201 * @return the left padding in pixels
13202 */
13203 public int getPaddingLeft() {
13204 return mPaddingLeft;
13205 }
13206
13207 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013208 * Returns the start padding of this view depending on its resolved layout direction.
13209 * If there are inset and enabled scrollbars, this value may include the space
13210 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013211 *
13212 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013213 */
13214 public int getPaddingStart() {
13215 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13216 mPaddingRight : mPaddingLeft;
13217 }
13218
13219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013220 * Returns the right padding of this view. If there are inset and enabled
13221 * scrollbars, this value may include the space required to display the
13222 * scrollbars as well.
13223 *
13224 * @return the right padding in pixels
13225 */
13226 public int getPaddingRight() {
13227 return mPaddingRight;
13228 }
13229
13230 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013231 * Returns the end padding of this view depending on its resolved layout direction.
13232 * If there are inset and enabled scrollbars, this value may include the space
13233 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013234 *
13235 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013236 */
13237 public int getPaddingEnd() {
13238 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13239 mPaddingLeft : mPaddingRight;
13240 }
13241
13242 /**
13243 * Return if the padding as been set thru relative values
13244 * {@link #setPaddingRelative(int, int, int, int)} or thru
13245 * @attr ref android.R.styleable#View_paddingStart or
13246 * @attr ref android.R.styleable#View_paddingEnd
13247 *
13248 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013249 */
13250 public boolean isPaddingRelative() {
13251 return mUserPaddingRelative;
13252 }
13253
13254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013255 * Changes the selection state of this view. A view can be selected or not.
13256 * Note that selection is not the same as focus. Views are typically
13257 * selected in the context of an AdapterView like ListView or GridView;
13258 * the selected view is the view that is highlighted.
13259 *
13260 * @param selected true if the view must be selected, false otherwise
13261 */
13262 public void setSelected(boolean selected) {
13263 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13264 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013265 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013266 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013267 refreshDrawableState();
13268 dispatchSetSelected(selected);
13269 }
13270 }
13271
13272 /**
13273 * Dispatch setSelected to all of this View's children.
13274 *
13275 * @see #setSelected(boolean)
13276 *
13277 * @param selected The new selected state
13278 */
13279 protected void dispatchSetSelected(boolean selected) {
13280 }
13281
13282 /**
13283 * Indicates the selection state of this view.
13284 *
13285 * @return true if the view is selected, false otherwise
13286 */
13287 @ViewDebug.ExportedProperty
13288 public boolean isSelected() {
13289 return (mPrivateFlags & SELECTED) != 0;
13290 }
13291
13292 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013293 * Changes the activated state of this view. A view can be activated or not.
13294 * Note that activation is not the same as selection. Selection is
13295 * a transient property, representing the view (hierarchy) the user is
13296 * currently interacting with. Activation is a longer-term state that the
13297 * user can move views in and out of. For example, in a list view with
13298 * single or multiple selection enabled, the views in the current selection
13299 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13300 * here.) The activated state is propagated down to children of the view it
13301 * is set on.
13302 *
13303 * @param activated true if the view must be activated, false otherwise
13304 */
13305 public void setActivated(boolean activated) {
13306 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13307 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013308 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013309 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013310 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013311 }
13312 }
13313
13314 /**
13315 * Dispatch setActivated to all of this View's children.
13316 *
13317 * @see #setActivated(boolean)
13318 *
13319 * @param activated The new activated state
13320 */
13321 protected void dispatchSetActivated(boolean activated) {
13322 }
13323
13324 /**
13325 * Indicates the activation state of this view.
13326 *
13327 * @return true if the view is activated, false otherwise
13328 */
13329 @ViewDebug.ExportedProperty
13330 public boolean isActivated() {
13331 return (mPrivateFlags & ACTIVATED) != 0;
13332 }
13333
13334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013335 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13336 * observer can be used to get notifications when global events, like
13337 * layout, happen.
13338 *
13339 * The returned ViewTreeObserver observer is not guaranteed to remain
13340 * valid for the lifetime of this View. If the caller of this method keeps
13341 * a long-lived reference to ViewTreeObserver, it should always check for
13342 * the return value of {@link ViewTreeObserver#isAlive()}.
13343 *
13344 * @return The ViewTreeObserver for this view's hierarchy.
13345 */
13346 public ViewTreeObserver getViewTreeObserver() {
13347 if (mAttachInfo != null) {
13348 return mAttachInfo.mTreeObserver;
13349 }
13350 if (mFloatingTreeObserver == null) {
13351 mFloatingTreeObserver = new ViewTreeObserver();
13352 }
13353 return mFloatingTreeObserver;
13354 }
13355
13356 /**
13357 * <p>Finds the topmost view in the current view hierarchy.</p>
13358 *
13359 * @return the topmost view containing this view
13360 */
13361 public View getRootView() {
13362 if (mAttachInfo != null) {
13363 final View v = mAttachInfo.mRootView;
13364 if (v != null) {
13365 return v;
13366 }
13367 }
Romain Guy8506ab42009-06-11 17:35:47 -070013368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013369 View parent = this;
13370
13371 while (parent.mParent != null && parent.mParent instanceof View) {
13372 parent = (View) parent.mParent;
13373 }
13374
13375 return parent;
13376 }
13377
13378 /**
13379 * <p>Computes the coordinates of this view on the screen. The argument
13380 * must be an array of two integers. After the method returns, the array
13381 * contains the x and y location in that order.</p>
13382 *
13383 * @param location an array of two integers in which to hold the coordinates
13384 */
13385 public void getLocationOnScreen(int[] location) {
13386 getLocationInWindow(location);
13387
13388 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070013389 if (info != null) {
13390 location[0] += info.mWindowLeft;
13391 location[1] += info.mWindowTop;
13392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013393 }
13394
13395 /**
13396 * <p>Computes the coordinates of this view in its window. The argument
13397 * must be an array of two integers. After the method returns, the array
13398 * contains the x and y location in that order.</p>
13399 *
13400 * @param location an array of two integers in which to hold the coordinates
13401 */
13402 public void getLocationInWindow(int[] location) {
13403 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013404 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013405 }
13406
Gilles Debunne6583ce52011-12-06 18:09:02 -080013407 if (mAttachInfo == null) {
13408 // When the view is not attached to a window, this method does not make sense
13409 location[0] = location[1] = 0;
13410 return;
13411 }
13412
Gilles Debunnecea45132011-11-24 02:19:27 +010013413 float[] position = mAttachInfo.mTmpTransformLocation;
13414 position[0] = position[1] = 0.0f;
13415
13416 if (!hasIdentityMatrix()) {
13417 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013419
Gilles Debunnecea45132011-11-24 02:19:27 +010013420 position[0] += mLeft;
13421 position[1] += mTop;
13422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013423 ViewParent viewParent = mParent;
13424 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013425 final View view = (View) viewParent;
13426
13427 position[0] -= view.mScrollX;
13428 position[1] -= view.mScrollY;
13429
13430 if (!view.hasIdentityMatrix()) {
13431 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013432 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013433
13434 position[0] += view.mLeft;
13435 position[1] += view.mTop;
13436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013437 viewParent = view.mParent;
13438 }
Romain Guy8506ab42009-06-11 17:35:47 -070013439
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013440 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013441 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010013442 final ViewRootImpl vr = (ViewRootImpl) viewParent;
13443 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013444 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013445
13446 location[0] = (int) (position[0] + 0.5f);
13447 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013448 }
13449
13450 /**
13451 * {@hide}
13452 * @param id the id of the view to be found
13453 * @return the view of the specified id, null if cannot be found
13454 */
13455 protected View findViewTraversal(int id) {
13456 if (id == mID) {
13457 return this;
13458 }
13459 return null;
13460 }
13461
13462 /**
13463 * {@hide}
13464 * @param tag the tag of the view to be found
13465 * @return the view of specified tag, null if cannot be found
13466 */
13467 protected View findViewWithTagTraversal(Object tag) {
13468 if (tag != null && tag.equals(mTag)) {
13469 return this;
13470 }
13471 return null;
13472 }
13473
13474 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013475 * {@hide}
13476 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070013477 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080013478 * @return The first view that matches the predicate or null.
13479 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070013480 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080013481 if (predicate.apply(this)) {
13482 return this;
13483 }
13484 return null;
13485 }
13486
13487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013488 * Look for a child view with the given id. If this view has the given
13489 * id, return this view.
13490 *
13491 * @param id The id to search for.
13492 * @return The view that has the given id in the hierarchy or null
13493 */
13494 public final View findViewById(int id) {
13495 if (id < 0) {
13496 return null;
13497 }
13498 return findViewTraversal(id);
13499 }
13500
13501 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070013502 * Finds a view by its unuque and stable accessibility id.
13503 *
13504 * @param accessibilityId The searched accessibility id.
13505 * @return The found view.
13506 */
13507 final View findViewByAccessibilityId(int accessibilityId) {
13508 if (accessibilityId < 0) {
13509 return null;
13510 }
13511 return findViewByAccessibilityIdTraversal(accessibilityId);
13512 }
13513
13514 /**
13515 * Performs the traversal to find a view by its unuque and stable accessibility id.
13516 *
13517 * <strong>Note:</strong>This method does not stop at the root namespace
13518 * boundary since the user can touch the screen at an arbitrary location
13519 * potentially crossing the root namespace bounday which will send an
13520 * accessibility event to accessibility services and they should be able
13521 * to obtain the event source. Also accessibility ids are guaranteed to be
13522 * unique in the window.
13523 *
13524 * @param accessibilityId The accessibility id.
13525 * @return The found view.
13526 */
13527 View findViewByAccessibilityIdTraversal(int accessibilityId) {
13528 if (getAccessibilityViewId() == accessibilityId) {
13529 return this;
13530 }
13531 return null;
13532 }
13533
13534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013535 * Look for a child view with the given tag. If this view has the given
13536 * tag, return this view.
13537 *
13538 * @param tag The tag to search for, using "tag.equals(getTag())".
13539 * @return The View that has the given tag in the hierarchy or null
13540 */
13541 public final View findViewWithTag(Object tag) {
13542 if (tag == null) {
13543 return null;
13544 }
13545 return findViewWithTagTraversal(tag);
13546 }
13547
13548 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013549 * {@hide}
13550 * Look for a child view that matches the specified predicate.
13551 * If this view matches the predicate, return this view.
13552 *
13553 * @param predicate The predicate to evaluate.
13554 * @return The first view that matches the predicate or null.
13555 */
13556 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070013557 return findViewByPredicateTraversal(predicate, null);
13558 }
13559
13560 /**
13561 * {@hide}
13562 * Look for a child view that matches the specified predicate,
13563 * starting with the specified view and its descendents and then
13564 * recusively searching the ancestors and siblings of that view
13565 * until this view is reached.
13566 *
13567 * This method is useful in cases where the predicate does not match
13568 * a single unique view (perhaps multiple views use the same id)
13569 * and we are trying to find the view that is "closest" in scope to the
13570 * starting view.
13571 *
13572 * @param start The view to start from.
13573 * @param predicate The predicate to evaluate.
13574 * @return The first view that matches the predicate or null.
13575 */
13576 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
13577 View childToSkip = null;
13578 for (;;) {
13579 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
13580 if (view != null || start == this) {
13581 return view;
13582 }
13583
13584 ViewParent parent = start.getParent();
13585 if (parent == null || !(parent instanceof View)) {
13586 return null;
13587 }
13588
13589 childToSkip = start;
13590 start = (View) parent;
13591 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080013592 }
13593
13594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013595 * Sets the identifier for this view. The identifier does not have to be
13596 * unique in this view's hierarchy. The identifier should be a positive
13597 * number.
13598 *
13599 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070013600 * @see #getId()
13601 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013602 *
13603 * @param id a number used to identify the view
13604 *
13605 * @attr ref android.R.styleable#View_id
13606 */
13607 public void setId(int id) {
13608 mID = id;
13609 }
13610
13611 /**
13612 * {@hide}
13613 *
13614 * @param isRoot true if the view belongs to the root namespace, false
13615 * otherwise
13616 */
13617 public void setIsRootNamespace(boolean isRoot) {
13618 if (isRoot) {
13619 mPrivateFlags |= IS_ROOT_NAMESPACE;
13620 } else {
13621 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
13622 }
13623 }
13624
13625 /**
13626 * {@hide}
13627 *
13628 * @return true if the view belongs to the root namespace, false otherwise
13629 */
13630 public boolean isRootNamespace() {
13631 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
13632 }
13633
13634 /**
13635 * Returns this view's identifier.
13636 *
13637 * @return a positive integer used to identify the view or {@link #NO_ID}
13638 * if the view has no ID
13639 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013640 * @see #setId(int)
13641 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013642 * @attr ref android.R.styleable#View_id
13643 */
13644 @ViewDebug.CapturedViewProperty
13645 public int getId() {
13646 return mID;
13647 }
13648
13649 /**
13650 * Returns this view's tag.
13651 *
13652 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070013653 *
13654 * @see #setTag(Object)
13655 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013656 */
13657 @ViewDebug.ExportedProperty
13658 public Object getTag() {
13659 return mTag;
13660 }
13661
13662 /**
13663 * Sets the tag associated with this view. A tag can be used to mark
13664 * a view in its hierarchy and does not have to be unique within the
13665 * hierarchy. Tags can also be used to store data within a view without
13666 * resorting to another data structure.
13667 *
13668 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070013669 *
13670 * @see #getTag()
13671 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013672 */
13673 public void setTag(final Object tag) {
13674 mTag = tag;
13675 }
13676
13677 /**
Romain Guyd90a3312009-05-06 14:54:28 -070013678 * Returns the tag associated with this view and the specified key.
13679 *
13680 * @param key The key identifying the tag
13681 *
13682 * @return the Object stored in this view as a tag
13683 *
13684 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070013685 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070013686 */
13687 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013688 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070013689 return null;
13690 }
13691
13692 /**
13693 * Sets a tag associated with this view and a key. A tag can be used
13694 * to mark a view in its hierarchy and does not have to be unique within
13695 * the hierarchy. Tags can also be used to store data within a view
13696 * without resorting to another data structure.
13697 *
13698 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070013699 * application to ensure it is unique (see the <a
13700 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
13701 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070013702 * the Android framework or not associated with any package will cause
13703 * an {@link IllegalArgumentException} to be thrown.
13704 *
13705 * @param key The key identifying the tag
13706 * @param tag An Object to tag the view with
13707 *
13708 * @throws IllegalArgumentException If they specified key is not valid
13709 *
13710 * @see #setTag(Object)
13711 * @see #getTag(int)
13712 */
13713 public void setTag(int key, final Object tag) {
13714 // If the package id is 0x00 or 0x01, it's either an undefined package
13715 // or a framework id
13716 if ((key >>> 24) < 2) {
13717 throw new IllegalArgumentException("The key must be an application-specific "
13718 + "resource id.");
13719 }
13720
Adam Powell2b2f6d62011-09-23 11:15:39 -070013721 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013722 }
13723
13724 /**
13725 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
13726 * framework id.
13727 *
13728 * @hide
13729 */
13730 public void setTagInternal(int key, Object tag) {
13731 if ((key >>> 24) != 0x1) {
13732 throw new IllegalArgumentException("The key must be a framework-specific "
13733 + "resource id.");
13734 }
13735
Adam Powell2b2f6d62011-09-23 11:15:39 -070013736 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013737 }
13738
Adam Powell2b2f6d62011-09-23 11:15:39 -070013739 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013740 if (mKeyedTags == null) {
13741 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013742 }
13743
Adam Powell7db82ac2011-09-22 19:44:04 -070013744 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013745 }
13746
13747 /**
Romain Guy13922e02009-05-12 17:56:14 -070013748 * @param consistency The type of consistency. See ViewDebug for more information.
13749 *
13750 * @hide
13751 */
13752 protected boolean dispatchConsistencyCheck(int consistency) {
13753 return onConsistencyCheck(consistency);
13754 }
13755
13756 /**
13757 * Method that subclasses should implement to check their consistency. The type of
13758 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013759 *
Romain Guy13922e02009-05-12 17:56:14 -070013760 * @param consistency The type of consistency. See ViewDebug for more information.
13761 *
13762 * @throws IllegalStateException if the view is in an inconsistent state.
13763 *
13764 * @hide
13765 */
13766 protected boolean onConsistencyCheck(int consistency) {
13767 boolean result = true;
13768
13769 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13770 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13771
13772 if (checkLayout) {
13773 if (getParent() == null) {
13774 result = false;
13775 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13776 "View " + this + " does not have a parent.");
13777 }
13778
13779 if (mAttachInfo == null) {
13780 result = false;
13781 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13782 "View " + this + " is not attached to a window.");
13783 }
13784 }
13785
13786 if (checkDrawing) {
13787 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13788 // from their draw() method
13789
13790 if ((mPrivateFlags & DRAWN) != DRAWN &&
13791 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13792 result = false;
13793 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13794 "View " + this + " was invalidated but its drawing cache is valid.");
13795 }
13796 }
13797
13798 return result;
13799 }
13800
13801 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013802 * Prints information about this view in the log output, with the tag
13803 * {@link #VIEW_LOG_TAG}.
13804 *
13805 * @hide
13806 */
13807 public void debug() {
13808 debug(0);
13809 }
13810
13811 /**
13812 * Prints information about this view in the log output, with the tag
13813 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13814 * indentation defined by the <code>depth</code>.
13815 *
13816 * @param depth the indentation level
13817 *
13818 * @hide
13819 */
13820 protected void debug(int depth) {
13821 String output = debugIndent(depth - 1);
13822
13823 output += "+ " + this;
13824 int id = getId();
13825 if (id != -1) {
13826 output += " (id=" + id + ")";
13827 }
13828 Object tag = getTag();
13829 if (tag != null) {
13830 output += " (tag=" + tag + ")";
13831 }
13832 Log.d(VIEW_LOG_TAG, output);
13833
13834 if ((mPrivateFlags & FOCUSED) != 0) {
13835 output = debugIndent(depth) + " FOCUSED";
13836 Log.d(VIEW_LOG_TAG, output);
13837 }
13838
13839 output = debugIndent(depth);
13840 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13841 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13842 + "} ";
13843 Log.d(VIEW_LOG_TAG, output);
13844
13845 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13846 || mPaddingBottom != 0) {
13847 output = debugIndent(depth);
13848 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13849 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13850 Log.d(VIEW_LOG_TAG, output);
13851 }
13852
13853 output = debugIndent(depth);
13854 output += "mMeasureWidth=" + mMeasuredWidth +
13855 " mMeasureHeight=" + mMeasuredHeight;
13856 Log.d(VIEW_LOG_TAG, output);
13857
13858 output = debugIndent(depth);
13859 if (mLayoutParams == null) {
13860 output += "BAD! no layout params";
13861 } else {
13862 output = mLayoutParams.debug(output);
13863 }
13864 Log.d(VIEW_LOG_TAG, output);
13865
13866 output = debugIndent(depth);
13867 output += "flags={";
13868 output += View.printFlags(mViewFlags);
13869 output += "}";
13870 Log.d(VIEW_LOG_TAG, output);
13871
13872 output = debugIndent(depth);
13873 output += "privateFlags={";
13874 output += View.printPrivateFlags(mPrivateFlags);
13875 output += "}";
13876 Log.d(VIEW_LOG_TAG, output);
13877 }
13878
13879 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013880 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013881 *
13882 * @param depth the indentation level
13883 * @return a String containing (depth * 2 + 3) * 2 white spaces
13884 *
13885 * @hide
13886 */
13887 protected static String debugIndent(int depth) {
13888 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13889 for (int i = 0; i < (depth * 2) + 3; i++) {
13890 spaces.append(' ').append(' ');
13891 }
13892 return spaces.toString();
13893 }
13894
13895 /**
13896 * <p>Return the offset of the widget's text baseline from the widget's top
13897 * boundary. If this widget does not support baseline alignment, this
13898 * method returns -1. </p>
13899 *
13900 * @return the offset of the baseline within the widget's bounds or -1
13901 * if baseline alignment is not supported
13902 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013903 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013904 public int getBaseline() {
13905 return -1;
13906 }
13907
13908 /**
13909 * Call this when something has changed which has invalidated the
13910 * layout of this view. This will schedule a layout pass of the view
13911 * tree.
13912 */
13913 public void requestLayout() {
13914 if (ViewDebug.TRACE_HIERARCHY) {
13915 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13916 }
13917
13918 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013919 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013920
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070013921 if (mLayoutParams != null) {
13922 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
13923 }
13924
13925 if (mParent != null && !mParent.isLayoutRequested()) {
13926 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013927 }
13928 }
13929
13930 /**
13931 * Forces this view to be laid out during the next layout pass.
13932 * This method does not call requestLayout() or forceLayout()
13933 * on the parent.
13934 */
13935 public void forceLayout() {
13936 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013937 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013938 }
13939
13940 /**
13941 * <p>
13942 * This is called to find out how big a view should be. The parent
13943 * supplies constraint information in the width and height parameters.
13944 * </p>
13945 *
13946 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013947 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013948 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013949 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013950 * </p>
13951 *
13952 *
13953 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13954 * parent
13955 * @param heightMeasureSpec Vertical space requirements as imposed by the
13956 * parent
13957 *
13958 * @see #onMeasure(int, int)
13959 */
13960 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13961 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13962 widthMeasureSpec != mOldWidthMeasureSpec ||
13963 heightMeasureSpec != mOldHeightMeasureSpec) {
13964
13965 // first clears the measured dimension flag
13966 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13967
13968 if (ViewDebug.TRACE_HIERARCHY) {
13969 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13970 }
13971
13972 // measure ourselves, this should set the measured dimension flag back
13973 onMeasure(widthMeasureSpec, heightMeasureSpec);
13974
13975 // flag not set, setMeasuredDimension() was not invoked, we raise
13976 // an exception to warn the developer
13977 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13978 throw new IllegalStateException("onMeasure() did not set the"
13979 + " measured dimension by calling"
13980 + " setMeasuredDimension()");
13981 }
13982
13983 mPrivateFlags |= LAYOUT_REQUIRED;
13984 }
13985
13986 mOldWidthMeasureSpec = widthMeasureSpec;
13987 mOldHeightMeasureSpec = heightMeasureSpec;
13988 }
13989
13990 /**
13991 * <p>
13992 * Measure the view and its content to determine the measured width and the
13993 * measured height. This method is invoked by {@link #measure(int, int)} and
13994 * should be overriden by subclasses to provide accurate and efficient
13995 * measurement of their contents.
13996 * </p>
13997 *
13998 * <p>
13999 * <strong>CONTRACT:</strong> When overriding this method, you
14000 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14001 * measured width and height of this view. Failure to do so will trigger an
14002 * <code>IllegalStateException</code>, thrown by
14003 * {@link #measure(int, int)}. Calling the superclass'
14004 * {@link #onMeasure(int, int)} is a valid use.
14005 * </p>
14006 *
14007 * <p>
14008 * The base class implementation of measure defaults to the background size,
14009 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14010 * override {@link #onMeasure(int, int)} to provide better measurements of
14011 * their content.
14012 * </p>
14013 *
14014 * <p>
14015 * If this method is overridden, it is the subclass's responsibility to make
14016 * sure the measured height and width are at least the view's minimum height
14017 * and width ({@link #getSuggestedMinimumHeight()} and
14018 * {@link #getSuggestedMinimumWidth()}).
14019 * </p>
14020 *
14021 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14022 * The requirements are encoded with
14023 * {@link android.view.View.MeasureSpec}.
14024 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14025 * The requirements are encoded with
14026 * {@link android.view.View.MeasureSpec}.
14027 *
14028 * @see #getMeasuredWidth()
14029 * @see #getMeasuredHeight()
14030 * @see #setMeasuredDimension(int, int)
14031 * @see #getSuggestedMinimumHeight()
14032 * @see #getSuggestedMinimumWidth()
14033 * @see android.view.View.MeasureSpec#getMode(int)
14034 * @see android.view.View.MeasureSpec#getSize(int)
14035 */
14036 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14037 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14038 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14039 }
14040
14041 /**
14042 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14043 * measured width and measured height. Failing to do so will trigger an
14044 * exception at measurement time.</p>
14045 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014046 * @param measuredWidth The measured width of this view. May be a complex
14047 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14048 * {@link #MEASURED_STATE_TOO_SMALL}.
14049 * @param measuredHeight The measured height of this view. May be a complex
14050 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14051 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052 */
14053 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14054 mMeasuredWidth = measuredWidth;
14055 mMeasuredHeight = measuredHeight;
14056
14057 mPrivateFlags |= MEASURED_DIMENSION_SET;
14058 }
14059
14060 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014061 * Merge two states as returned by {@link #getMeasuredState()}.
14062 * @param curState The current state as returned from a view or the result
14063 * of combining multiple views.
14064 * @param newState The new view state to combine.
14065 * @return Returns a new integer reflecting the combination of the two
14066 * states.
14067 */
14068 public static int combineMeasuredStates(int curState, int newState) {
14069 return curState | newState;
14070 }
14071
14072 /**
14073 * Version of {@link #resolveSizeAndState(int, int, int)}
14074 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14075 */
14076 public static int resolveSize(int size, int measureSpec) {
14077 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14078 }
14079
14080 /**
14081 * Utility to reconcile a desired size and state, with constraints imposed
14082 * by a MeasureSpec. Will take the desired size, unless a different size
14083 * is imposed by the constraints. The returned value is a compound integer,
14084 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14085 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14086 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014087 *
14088 * @param size How big the view wants to be
14089 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014090 * @return Size information bit mask as defined by
14091 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014092 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014093 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014094 int result = size;
14095 int specMode = MeasureSpec.getMode(measureSpec);
14096 int specSize = MeasureSpec.getSize(measureSpec);
14097 switch (specMode) {
14098 case MeasureSpec.UNSPECIFIED:
14099 result = size;
14100 break;
14101 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014102 if (specSize < size) {
14103 result = specSize | MEASURED_STATE_TOO_SMALL;
14104 } else {
14105 result = size;
14106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014107 break;
14108 case MeasureSpec.EXACTLY:
14109 result = specSize;
14110 break;
14111 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014112 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014113 }
14114
14115 /**
14116 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014117 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014118 * by the MeasureSpec.
14119 *
14120 * @param size Default size for this view
14121 * @param measureSpec Constraints imposed by the parent
14122 * @return The size this view should be.
14123 */
14124 public static int getDefaultSize(int size, int measureSpec) {
14125 int result = size;
14126 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014127 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014128
14129 switch (specMode) {
14130 case MeasureSpec.UNSPECIFIED:
14131 result = size;
14132 break;
14133 case MeasureSpec.AT_MOST:
14134 case MeasureSpec.EXACTLY:
14135 result = specSize;
14136 break;
14137 }
14138 return result;
14139 }
14140
14141 /**
14142 * Returns the suggested minimum height that the view should use. This
14143 * returns the maximum of the view's minimum height
14144 * and the background's minimum height
14145 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14146 * <p>
14147 * When being used in {@link #onMeasure(int, int)}, the caller should still
14148 * ensure the returned height is within the requirements of the parent.
14149 *
14150 * @return The suggested minimum height of the view.
14151 */
14152 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014153 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014155 }
14156
14157 /**
14158 * Returns the suggested minimum width that the view should use. This
14159 * returns the maximum of the view's minimum width)
14160 * and the background's minimum width
14161 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14162 * <p>
14163 * When being used in {@link #onMeasure(int, int)}, the caller should still
14164 * ensure the returned width is within the requirements of the parent.
14165 *
14166 * @return The suggested minimum width of the view.
14167 */
14168 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014169 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014171
Philip Milne6c8ea062012-04-03 17:38:43 -070014172 /**
14173 * Returns the minimum height of the view.
14174 *
14175 * @return the minimum height the view will try to be.
14176 *
14177 * @see #setMinimumHeight(int)
14178 *
14179 * @attr ref android.R.styleable#View_minHeight
14180 */
14181 public int getMinimumHeight() {
14182 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014183 }
14184
14185 /**
14186 * Sets the minimum height of the view. It is not guaranteed the view will
14187 * be able to achieve this minimum height (for example, if its parent layout
14188 * constrains it with less available height).
14189 *
14190 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014191 *
14192 * @see #getMinimumHeight()
14193 *
14194 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014195 */
14196 public void setMinimumHeight(int minHeight) {
14197 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014198 requestLayout();
14199 }
14200
14201 /**
14202 * Returns the minimum width of the view.
14203 *
14204 * @return the minimum width the view will try to be.
14205 *
14206 * @see #setMinimumWidth(int)
14207 *
14208 * @attr ref android.R.styleable#View_minWidth
14209 */
14210 public int getMinimumWidth() {
14211 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014212 }
14213
14214 /**
14215 * Sets the minimum width of the view. It is not guaranteed the view will
14216 * be able to achieve this minimum width (for example, if its parent layout
14217 * constrains it with less available width).
14218 *
14219 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014220 *
14221 * @see #getMinimumWidth()
14222 *
14223 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014224 */
14225 public void setMinimumWidth(int minWidth) {
14226 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014227 requestLayout();
14228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014229 }
14230
14231 /**
14232 * Get the animation currently associated with this view.
14233 *
14234 * @return The animation that is currently playing or
14235 * scheduled to play for this view.
14236 */
14237 public Animation getAnimation() {
14238 return mCurrentAnimation;
14239 }
14240
14241 /**
14242 * Start the specified animation now.
14243 *
14244 * @param animation the animation to start now
14245 */
14246 public void startAnimation(Animation animation) {
14247 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14248 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014249 invalidateParentCaches();
14250 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014251 }
14252
14253 /**
14254 * Cancels any animations for this view.
14255 */
14256 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014257 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014258 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014260 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014261 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014262 }
14263
14264 /**
14265 * Sets the next animation to play for this view.
14266 * If you want the animation to play immediately, use
14267 * startAnimation. This method provides allows fine-grained
14268 * control over the start time and invalidation, but you
14269 * must make sure that 1) the animation has a start time set, and
14270 * 2) the view will be invalidated when the animation is supposed to
14271 * start.
14272 *
14273 * @param animation The next animation, or null.
14274 */
14275 public void setAnimation(Animation animation) {
14276 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014278 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014279 // If the screen is off assume the animation start time is now instead of
14280 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14281 // would cause the animation to start when the screen turns back on
14282 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14283 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14284 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014286 animation.reset();
14287 }
14288 }
14289
14290 /**
14291 * Invoked by a parent ViewGroup to notify the start of the animation
14292 * currently associated with this view. If you override this method,
14293 * always call super.onAnimationStart();
14294 *
14295 * @see #setAnimation(android.view.animation.Animation)
14296 * @see #getAnimation()
14297 */
14298 protected void onAnimationStart() {
14299 mPrivateFlags |= ANIMATION_STARTED;
14300 }
14301
14302 /**
14303 * Invoked by a parent ViewGroup to notify the end of the animation
14304 * currently associated with this view. If you override this method,
14305 * always call super.onAnimationEnd();
14306 *
14307 * @see #setAnimation(android.view.animation.Animation)
14308 * @see #getAnimation()
14309 */
14310 protected void onAnimationEnd() {
14311 mPrivateFlags &= ~ANIMATION_STARTED;
14312 }
14313
14314 /**
14315 * Invoked if there is a Transform that involves alpha. Subclass that can
14316 * draw themselves with the specified alpha should return true, and then
14317 * respect that alpha when their onDraw() is called. If this returns false
14318 * then the view may be redirected to draw into an offscreen buffer to
14319 * fulfill the request, which will look fine, but may be slower than if the
14320 * subclass handles it internally. The default implementation returns false.
14321 *
14322 * @param alpha The alpha (0..255) to apply to the view's drawing
14323 * @return true if the view can draw with the specified alpha.
14324 */
14325 protected boolean onSetAlpha(int alpha) {
14326 return false;
14327 }
14328
14329 /**
14330 * This is used by the RootView to perform an optimization when
14331 * the view hierarchy contains one or several SurfaceView.
14332 * SurfaceView is always considered transparent, but its children are not,
14333 * therefore all View objects remove themselves from the global transparent
14334 * region (passed as a parameter to this function).
14335 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014336 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014337 *
14338 * @return Returns true if the effective visibility of the view at this
14339 * point is opaque, regardless of the transparent region; returns false
14340 * if it is possible for underlying windows to be seen behind the view.
14341 *
14342 * {@hide}
14343 */
14344 public boolean gatherTransparentRegion(Region region) {
14345 final AttachInfo attachInfo = mAttachInfo;
14346 if (region != null && attachInfo != null) {
14347 final int pflags = mPrivateFlags;
14348 if ((pflags & SKIP_DRAW) == 0) {
14349 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14350 // remove it from the transparent region.
14351 final int[] location = attachInfo.mTransparentLocation;
14352 getLocationInWindow(location);
14353 region.op(location[0], location[1], location[0] + mRight - mLeft,
14354 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014355 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014356 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14357 // exists, so we remove the background drawable's non-transparent
14358 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014359 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014360 }
14361 }
14362 return true;
14363 }
14364
14365 /**
14366 * Play a sound effect for this view.
14367 *
14368 * <p>The framework will play sound effects for some built in actions, such as
14369 * clicking, but you may wish to play these effects in your widget,
14370 * for instance, for internal navigation.
14371 *
14372 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14373 * {@link #isSoundEffectsEnabled()} is true.
14374 *
14375 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
14376 */
14377 public void playSoundEffect(int soundConstant) {
14378 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
14379 return;
14380 }
14381 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
14382 }
14383
14384 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014385 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014386 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014387 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014388 *
14389 * <p>The framework will provide haptic feedback for some built in actions,
14390 * such as long presses, but you may wish to provide feedback for your
14391 * own widget.
14392 *
14393 * <p>The feedback will only be performed if
14394 * {@link #isHapticFeedbackEnabled()} is true.
14395 *
14396 * @param feedbackConstant One of the constants defined in
14397 * {@link HapticFeedbackConstants}
14398 */
14399 public boolean performHapticFeedback(int feedbackConstant) {
14400 return performHapticFeedback(feedbackConstant, 0);
14401 }
14402
14403 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014404 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014405 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014406 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014407 *
14408 * @param feedbackConstant One of the constants defined in
14409 * {@link HapticFeedbackConstants}
14410 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
14411 */
14412 public boolean performHapticFeedback(int feedbackConstant, int flags) {
14413 if (mAttachInfo == null) {
14414 return false;
14415 }
Romain Guyf607bdc2010-09-10 19:20:06 -070014416 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070014417 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014418 && !isHapticFeedbackEnabled()) {
14419 return false;
14420 }
Romain Guy812ccbe2010-06-01 14:07:24 -070014421 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
14422 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014423 }
14424
14425 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070014426 * Request that the visibility of the status bar or other screen/window
14427 * decorations be changed.
14428 *
14429 * <p>This method is used to put the over device UI into temporary modes
14430 * where the user's attention is focused more on the application content,
14431 * by dimming or hiding surrounding system affordances. This is typically
14432 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
14433 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
14434 * to be placed behind the action bar (and with these flags other system
14435 * affordances) so that smooth transitions between hiding and showing them
14436 * can be done.
14437 *
14438 * <p>Two representative examples of the use of system UI visibility is
14439 * implementing a content browsing application (like a magazine reader)
14440 * and a video playing application.
14441 *
14442 * <p>The first code shows a typical implementation of a View in a content
14443 * browsing application. In this implementation, the application goes
14444 * into a content-oriented mode by hiding the status bar and action bar,
14445 * and putting the navigation elements into lights out mode. The user can
14446 * then interact with content while in this mode. Such an application should
14447 * provide an easy way for the user to toggle out of the mode (such as to
14448 * check information in the status bar or access notifications). In the
14449 * implementation here, this is done simply by tapping on the content.
14450 *
14451 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
14452 * content}
14453 *
14454 * <p>This second code sample shows a typical implementation of a View
14455 * in a video playing application. In this situation, while the video is
14456 * playing the application would like to go into a complete full-screen mode,
14457 * to use as much of the display as possible for the video. When in this state
14458 * the user can not interact with the application; the system intercepts
14459 * touching on the screen to pop the UI out of full screen mode.
14460 *
14461 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
14462 * content}
14463 *
14464 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
14465 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
14466 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
14467 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080014468 */
14469 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040014470 if (visibility != mSystemUiVisibility) {
14471 mSystemUiVisibility = visibility;
14472 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14473 mParent.recomputeViewAttributes(this);
14474 }
Joe Onorato664644d2011-01-23 17:53:23 -080014475 }
14476 }
14477
14478 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070014479 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
14480 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
14481 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
14482 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
14483 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080014484 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080014485 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080014486 return mSystemUiVisibility;
14487 }
14488
Scott Mainec6331b2011-05-24 16:55:56 -070014489 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070014490 * Returns the current system UI visibility that is currently set for
14491 * the entire window. This is the combination of the
14492 * {@link #setSystemUiVisibility(int)} values supplied by all of the
14493 * views in the window.
14494 */
14495 public int getWindowSystemUiVisibility() {
14496 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
14497 }
14498
14499 /**
14500 * Override to find out when the window's requested system UI visibility
14501 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
14502 * This is different from the callbacks recieved through
14503 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
14504 * in that this is only telling you about the local request of the window,
14505 * not the actual values applied by the system.
14506 */
14507 public void onWindowSystemUiVisibilityChanged(int visible) {
14508 }
14509
14510 /**
14511 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
14512 * the view hierarchy.
14513 */
14514 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
14515 onWindowSystemUiVisibilityChanged(visible);
14516 }
14517
14518 /**
Scott Mainec6331b2011-05-24 16:55:56 -070014519 * Set a listener to receive callbacks when the visibility of the system bar changes.
14520 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
14521 */
Joe Onorato664644d2011-01-23 17:53:23 -080014522 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014523 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080014524 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14525 mParent.recomputeViewAttributes(this);
14526 }
14527 }
14528
14529 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014530 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
14531 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080014532 */
14533 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014534 ListenerInfo li = mListenerInfo;
14535 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
14536 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080014537 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080014538 }
14539 }
14540
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014541 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
14542 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
14543 if (val != mSystemUiVisibility) {
14544 setSystemUiVisibility(val);
14545 }
14546 }
14547
Joe Onorato664644d2011-01-23 17:53:23 -080014548 /**
Joe Malin32736f02011-01-19 16:14:20 -080014549 * Creates an image that the system displays during the drag and drop
14550 * operation. This is called a &quot;drag shadow&quot;. The default implementation
14551 * for a DragShadowBuilder based on a View returns an image that has exactly the same
14552 * appearance as the given View. The default also positions the center of the drag shadow
14553 * directly under the touch point. If no View is provided (the constructor with no parameters
14554 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
14555 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
14556 * default is an invisible drag shadow.
14557 * <p>
14558 * You are not required to use the View you provide to the constructor as the basis of the
14559 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
14560 * anything you want as the drag shadow.
14561 * </p>
14562 * <p>
14563 * You pass a DragShadowBuilder object to the system when you start the drag. The system
14564 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
14565 * size and position of the drag shadow. It uses this data to construct a
14566 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
14567 * so that your application can draw the shadow image in the Canvas.
14568 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070014569 *
14570 * <div class="special reference">
14571 * <h3>Developer Guides</h3>
14572 * <p>For a guide to implementing drag and drop features, read the
14573 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14574 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070014575 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014576 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070014577 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070014578
14579 /**
Joe Malin32736f02011-01-19 16:14:20 -080014580 * Constructs a shadow image builder based on a View. By default, the resulting drag
14581 * shadow will have the same appearance and dimensions as the View, with the touch point
14582 * over the center of the View.
14583 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070014584 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014585 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070014586 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070014587 }
14588
Christopher Tate17ed60c2011-01-18 12:50:26 -080014589 /**
14590 * Construct a shadow builder object with no associated View. This
14591 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
14592 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
14593 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080014594 * reference to any View object. If they are not overridden, then the result is an
14595 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080014596 */
14597 public DragShadowBuilder() {
14598 mView = new WeakReference<View>(null);
14599 }
14600
14601 /**
14602 * Returns the View object that had been passed to the
14603 * {@link #View.DragShadowBuilder(View)}
14604 * constructor. If that View parameter was {@code null} or if the
14605 * {@link #View.DragShadowBuilder()}
14606 * constructor was used to instantiate the builder object, this method will return
14607 * null.
14608 *
14609 * @return The View object associate with this builder object.
14610 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070014611 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070014612 final public View getView() {
14613 return mView.get();
14614 }
14615
Christopher Tate2c095f32010-10-04 14:13:40 -070014616 /**
Joe Malin32736f02011-01-19 16:14:20 -080014617 * Provides the metrics for the shadow image. These include the dimensions of
14618 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070014619 * be centered under the touch location while dragging.
14620 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014621 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080014622 * same as the dimensions of the View itself and centers the shadow under
14623 * the touch point.
14624 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070014625 *
Joe Malin32736f02011-01-19 16:14:20 -080014626 * @param shadowSize A {@link android.graphics.Point} containing the width and height
14627 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
14628 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
14629 * image.
14630 *
14631 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
14632 * shadow image that should be underneath the touch point during the drag and drop
14633 * operation. Your application must set {@link android.graphics.Point#x} to the
14634 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070014635 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014636 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070014637 final View view = mView.get();
14638 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014639 shadowSize.set(view.getWidth(), view.getHeight());
14640 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070014641 } else {
14642 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
14643 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014644 }
14645
14646 /**
Joe Malin32736f02011-01-19 16:14:20 -080014647 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
14648 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014649 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070014650 *
Joe Malin32736f02011-01-19 16:14:20 -080014651 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070014652 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014653 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070014654 final View view = mView.get();
14655 if (view != null) {
14656 view.draw(canvas);
14657 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014658 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070014659 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014660 }
14661 }
14662
14663 /**
Joe Malin32736f02011-01-19 16:14:20 -080014664 * Starts a drag and drop operation. When your application calls this method, it passes a
14665 * {@link android.view.View.DragShadowBuilder} object to the system. The
14666 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
14667 * to get metrics for the drag shadow, and then calls the object's
14668 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
14669 * <p>
14670 * Once the system has the drag shadow, it begins the drag and drop operation by sending
14671 * drag events to all the View objects in your application that are currently visible. It does
14672 * this either by calling the View object's drag listener (an implementation of
14673 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
14674 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
14675 * Both are passed a {@link android.view.DragEvent} object that has a
14676 * {@link android.view.DragEvent#getAction()} value of
14677 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
14678 * </p>
14679 * <p>
14680 * Your application can invoke startDrag() on any attached View object. The View object does not
14681 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
14682 * be related to the View the user selected for dragging.
14683 * </p>
14684 * @param data A {@link android.content.ClipData} object pointing to the data to be
14685 * transferred by the drag and drop operation.
14686 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
14687 * drag shadow.
14688 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
14689 * drop operation. This Object is put into every DragEvent object sent by the system during the
14690 * current drag.
14691 * <p>
14692 * myLocalState is a lightweight mechanism for the sending information from the dragged View
14693 * to the target Views. For example, it can contain flags that differentiate between a
14694 * a copy operation and a move operation.
14695 * </p>
14696 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
14697 * so the parameter should be set to 0.
14698 * @return {@code true} if the method completes successfully, or
14699 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
14700 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070014701 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014702 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014703 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070014704 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014705 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070014706 }
14707 boolean okay = false;
14708
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014709 Point shadowSize = new Point();
14710 Point shadowTouchPoint = new Point();
14711 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070014712
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014713 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
14714 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
14715 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070014716 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014717
Chris Tatea32dcf72010-10-14 12:13:50 -070014718 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014719 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
14720 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070014721 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014722 Surface surface = new Surface();
14723 try {
14724 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014725 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070014726 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070014727 + " surface=" + surface);
14728 if (token != null) {
14729 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070014730 try {
Chris Tate6b391282010-10-14 15:48:59 -070014731 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014732 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070014733 } finally {
14734 surface.unlockCanvasAndPost(canvas);
14735 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014736
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014737 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080014738
14739 // Cache the local state object for delivery with DragEvents
14740 root.setLocalDragState(myLocalState);
14741
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014742 // repurpose 'shadowSize' for the last touch point
14743 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070014744
Christopher Tatea53146c2010-09-07 11:57:52 -070014745 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014746 shadowSize.x, shadowSize.y,
14747 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070014748 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070014749
14750 // Off and running! Release our local surface instance; the drag
14751 // shadow surface is now managed by the system process.
14752 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070014753 }
14754 } catch (Exception e) {
14755 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
14756 surface.destroy();
14757 }
14758
14759 return okay;
14760 }
14761
Christopher Tatea53146c2010-09-07 11:57:52 -070014762 /**
Joe Malin32736f02011-01-19 16:14:20 -080014763 * Handles drag events sent by the system following a call to
14764 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
14765 *<p>
14766 * When the system calls this method, it passes a
14767 * {@link android.view.DragEvent} object. A call to
14768 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
14769 * in DragEvent. The method uses these to determine what is happening in the drag and drop
14770 * operation.
14771 * @param event The {@link android.view.DragEvent} sent by the system.
14772 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
14773 * in DragEvent, indicating the type of drag event represented by this object.
14774 * @return {@code true} if the method was successful, otherwise {@code false}.
14775 * <p>
14776 * The method should return {@code true} in response to an action type of
14777 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
14778 * operation.
14779 * </p>
14780 * <p>
14781 * The method should also return {@code true} in response to an action type of
14782 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
14783 * {@code false} if it didn't.
14784 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014785 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070014786 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070014787 return false;
14788 }
14789
14790 /**
Joe Malin32736f02011-01-19 16:14:20 -080014791 * Detects if this View is enabled and has a drag event listener.
14792 * If both are true, then it calls the drag event listener with the
14793 * {@link android.view.DragEvent} it received. If the drag event listener returns
14794 * {@code true}, then dispatchDragEvent() returns {@code true}.
14795 * <p>
14796 * For all other cases, the method calls the
14797 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
14798 * method and returns its result.
14799 * </p>
14800 * <p>
14801 * This ensures that a drag event is always consumed, even if the View does not have a drag
14802 * event listener. However, if the View has a listener and the listener returns true, then
14803 * onDragEvent() is not called.
14804 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014805 */
14806 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080014807 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014808 ListenerInfo li = mListenerInfo;
14809 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
14810 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070014811 return true;
14812 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014813 return onDragEvent(event);
14814 }
14815
Christopher Tate3d4bf172011-03-28 16:16:46 -070014816 boolean canAcceptDrag() {
14817 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
14818 }
14819
Christopher Tatea53146c2010-09-07 11:57:52 -070014820 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070014821 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
14822 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070014823 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070014824 */
14825 public void onCloseSystemDialogs(String reason) {
14826 }
Joe Malin32736f02011-01-19 16:14:20 -080014827
Dianne Hackbornffa42482009-09-23 22:20:11 -070014828 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014829 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014830 * update a Region being computed for
14831 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014832 * that any non-transparent parts of the Drawable are removed from the
14833 * given transparent region.
14834 *
14835 * @param dr The Drawable whose transparency is to be applied to the region.
14836 * @param region A Region holding the current transparency information,
14837 * where any parts of the region that are set are considered to be
14838 * transparent. On return, this region will be modified to have the
14839 * transparency information reduced by the corresponding parts of the
14840 * Drawable that are not transparent.
14841 * {@hide}
14842 */
14843 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14844 if (DBG) {
14845 Log.i("View", "Getting transparent region for: " + this);
14846 }
14847 final Region r = dr.getTransparentRegion();
14848 final Rect db = dr.getBounds();
14849 final AttachInfo attachInfo = mAttachInfo;
14850 if (r != null && attachInfo != null) {
14851 final int w = getRight()-getLeft();
14852 final int h = getBottom()-getTop();
14853 if (db.left > 0) {
14854 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14855 r.op(0, 0, db.left, h, Region.Op.UNION);
14856 }
14857 if (db.right < w) {
14858 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14859 r.op(db.right, 0, w, h, Region.Op.UNION);
14860 }
14861 if (db.top > 0) {
14862 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14863 r.op(0, 0, w, db.top, Region.Op.UNION);
14864 }
14865 if (db.bottom < h) {
14866 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14867 r.op(0, db.bottom, w, h, Region.Op.UNION);
14868 }
14869 final int[] location = attachInfo.mTransparentLocation;
14870 getLocationInWindow(location);
14871 r.translate(location[0], location[1]);
14872 region.op(r, Region.Op.INTERSECT);
14873 } else {
14874 region.op(db, Region.Op.DIFFERENCE);
14875 }
14876 }
14877
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014878 private void checkForLongClick(int delayOffset) {
14879 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14880 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014881
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014882 if (mPendingCheckForLongPress == null) {
14883 mPendingCheckForLongPress = new CheckForLongPress();
14884 }
14885 mPendingCheckForLongPress.rememberWindowAttachCount();
14886 postDelayed(mPendingCheckForLongPress,
14887 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014889 }
14890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014891 /**
14892 * Inflate a view from an XML resource. This convenience method wraps the {@link
14893 * LayoutInflater} class, which provides a full range of options for view inflation.
14894 *
14895 * @param context The Context object for your activity or application.
14896 * @param resource The resource ID to inflate
14897 * @param root A view group that will be the parent. Used to properly inflate the
14898 * layout_* parameters.
14899 * @see LayoutInflater
14900 */
14901 public static View inflate(Context context, int resource, ViewGroup root) {
14902 LayoutInflater factory = LayoutInflater.from(context);
14903 return factory.inflate(resource, root);
14904 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014906 /**
Adam Powell637d3372010-08-25 14:37:03 -070014907 * Scroll the view with standard behavior for scrolling beyond the normal
14908 * content boundaries. Views that call this method should override
14909 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14910 * results of an over-scroll operation.
14911 *
14912 * Views can use this method to handle any touch or fling-based scrolling.
14913 *
14914 * @param deltaX Change in X in pixels
14915 * @param deltaY Change in Y in pixels
14916 * @param scrollX Current X scroll value in pixels before applying deltaX
14917 * @param scrollY Current Y scroll value in pixels before applying deltaY
14918 * @param scrollRangeX Maximum content scroll range along the X axis
14919 * @param scrollRangeY Maximum content scroll range along the Y axis
14920 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14921 * along the X axis.
14922 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14923 * along the Y axis.
14924 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14925 * @return true if scrolling was clamped to an over-scroll boundary along either
14926 * axis, false otherwise.
14927 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014928 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014929 protected boolean overScrollBy(int deltaX, int deltaY,
14930 int scrollX, int scrollY,
14931 int scrollRangeX, int scrollRangeY,
14932 int maxOverScrollX, int maxOverScrollY,
14933 boolean isTouchEvent) {
14934 final int overScrollMode = mOverScrollMode;
14935 final boolean canScrollHorizontal =
14936 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14937 final boolean canScrollVertical =
14938 computeVerticalScrollRange() > computeVerticalScrollExtent();
14939 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14940 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14941 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14942 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14943
14944 int newScrollX = scrollX + deltaX;
14945 if (!overScrollHorizontal) {
14946 maxOverScrollX = 0;
14947 }
14948
14949 int newScrollY = scrollY + deltaY;
14950 if (!overScrollVertical) {
14951 maxOverScrollY = 0;
14952 }
14953
14954 // Clamp values if at the limits and record
14955 final int left = -maxOverScrollX;
14956 final int right = maxOverScrollX + scrollRangeX;
14957 final int top = -maxOverScrollY;
14958 final int bottom = maxOverScrollY + scrollRangeY;
14959
14960 boolean clampedX = false;
14961 if (newScrollX > right) {
14962 newScrollX = right;
14963 clampedX = true;
14964 } else if (newScrollX < left) {
14965 newScrollX = left;
14966 clampedX = true;
14967 }
14968
14969 boolean clampedY = false;
14970 if (newScrollY > bottom) {
14971 newScrollY = bottom;
14972 clampedY = true;
14973 } else if (newScrollY < top) {
14974 newScrollY = top;
14975 clampedY = true;
14976 }
14977
14978 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14979
14980 return clampedX || clampedY;
14981 }
14982
14983 /**
14984 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14985 * respond to the results of an over-scroll operation.
14986 *
14987 * @param scrollX New X scroll value in pixels
14988 * @param scrollY New Y scroll value in pixels
14989 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14990 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14991 */
14992 protected void onOverScrolled(int scrollX, int scrollY,
14993 boolean clampedX, boolean clampedY) {
14994 // Intentionally empty.
14995 }
14996
14997 /**
14998 * Returns the over-scroll mode for this view. The result will be
14999 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15000 * (allow over-scrolling only if the view content is larger than the container),
15001 * or {@link #OVER_SCROLL_NEVER}.
15002 *
15003 * @return This view's over-scroll mode.
15004 */
15005 public int getOverScrollMode() {
15006 return mOverScrollMode;
15007 }
15008
15009 /**
15010 * Set the over-scroll mode for this view. Valid over-scroll modes are
15011 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15012 * (allow over-scrolling only if the view content is larger than the container),
15013 * or {@link #OVER_SCROLL_NEVER}.
15014 *
15015 * Setting the over-scroll mode of a view will have an effect only if the
15016 * view is capable of scrolling.
15017 *
15018 * @param overScrollMode The new over-scroll mode for this view.
15019 */
15020 public void setOverScrollMode(int overScrollMode) {
15021 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15022 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15023 overScrollMode != OVER_SCROLL_NEVER) {
15024 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15025 }
15026 mOverScrollMode = overScrollMode;
15027 }
15028
15029 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015030 * Gets a scale factor that determines the distance the view should scroll
15031 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15032 * @return The vertical scroll scale factor.
15033 * @hide
15034 */
15035 protected float getVerticalScrollFactor() {
15036 if (mVerticalScrollFactor == 0) {
15037 TypedValue outValue = new TypedValue();
15038 if (!mContext.getTheme().resolveAttribute(
15039 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15040 throw new IllegalStateException(
15041 "Expected theme to define listPreferredItemHeight.");
15042 }
15043 mVerticalScrollFactor = outValue.getDimension(
15044 mContext.getResources().getDisplayMetrics());
15045 }
15046 return mVerticalScrollFactor;
15047 }
15048
15049 /**
15050 * Gets a scale factor that determines the distance the view should scroll
15051 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15052 * @return The horizontal scroll scale factor.
15053 * @hide
15054 */
15055 protected float getHorizontalScrollFactor() {
15056 // TODO: Should use something else.
15057 return getVerticalScrollFactor();
15058 }
15059
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015060 /**
15061 * Return the value specifying the text direction or policy that was set with
15062 * {@link #setTextDirection(int)}.
15063 *
15064 * @return the defined text direction. It can be one of:
15065 *
15066 * {@link #TEXT_DIRECTION_INHERIT},
15067 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15068 * {@link #TEXT_DIRECTION_ANY_RTL},
15069 * {@link #TEXT_DIRECTION_LTR},
15070 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015071 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015072 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015073 @ViewDebug.ExportedProperty(category = "text", mapping = {
15074 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15075 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15076 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15077 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15078 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15079 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15080 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015081 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015082 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015083 }
15084
15085 /**
15086 * Set the text direction.
15087 *
15088 * @param textDirection the direction to set. Should be one of:
15089 *
15090 * {@link #TEXT_DIRECTION_INHERIT},
15091 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15092 * {@link #TEXT_DIRECTION_ANY_RTL},
15093 * {@link #TEXT_DIRECTION_LTR},
15094 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015095 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015096 */
15097 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015098 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015099 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015100 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015101 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015102 // Set the new text direction
15103 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015104 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015105 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015106 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015107 }
15108 }
15109
15110 /**
15111 * Return the resolved text direction.
15112 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015113 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15114 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15115 * up the parent chain of the view. if there is no parent, then it will return the default
15116 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15117 *
15118 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015119 *
Doug Feltcb3791202011-07-07 11:57:48 -070015120 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15121 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015122 * {@link #TEXT_DIRECTION_LTR},
15123 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015124 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015125 */
15126 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015127 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015128 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015129 resolveTextDirection();
15130 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015131 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015132 }
15133
15134 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015135 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15136 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015137 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015138 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015139 // Reset any previous text direction resolution
15140 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15141
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015142 if (hasRtlSupport()) {
15143 // Set resolved text direction flag depending on text direction flag
15144 final int textDirection = getTextDirection();
15145 switch(textDirection) {
15146 case TEXT_DIRECTION_INHERIT:
15147 if (canResolveTextDirection()) {
15148 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015149
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015150 // Set current resolved direction to the same value as the parent's one
15151 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15152 switch (parentResolvedDirection) {
15153 case TEXT_DIRECTION_FIRST_STRONG:
15154 case TEXT_DIRECTION_ANY_RTL:
15155 case TEXT_DIRECTION_LTR:
15156 case TEXT_DIRECTION_RTL:
15157 case TEXT_DIRECTION_LOCALE:
15158 mPrivateFlags2 |=
15159 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15160 break;
15161 default:
15162 // Default resolved direction is "first strong" heuristic
15163 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15164 }
15165 } else {
15166 // We cannot do the resolution if there is no parent, so use the default one
15167 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015168 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015169 break;
15170 case TEXT_DIRECTION_FIRST_STRONG:
15171 case TEXT_DIRECTION_ANY_RTL:
15172 case TEXT_DIRECTION_LTR:
15173 case TEXT_DIRECTION_RTL:
15174 case TEXT_DIRECTION_LOCALE:
15175 // Resolved direction is the same as text direction
15176 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15177 break;
15178 default:
15179 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015180 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015181 }
15182 } else {
15183 // Default resolved direction is "first strong" heuristic
15184 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015185 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015186
15187 // Set to resolved
15188 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015189 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015190 }
15191
15192 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015193 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015194 * resolution should override this method.
15195 *
15196 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015197 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015198 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015199 }
15200
15201 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015202 * Check if text direction resolution can be done.
15203 *
15204 * @return true if text direction resolution can be done otherwise return false.
15205 */
15206 public boolean canResolveTextDirection() {
15207 switch (getTextDirection()) {
15208 case TEXT_DIRECTION_INHERIT:
15209 return (mParent != null) && (mParent instanceof ViewGroup);
15210 default:
15211 return true;
15212 }
15213 }
15214
15215 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015216 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015217 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015218 * reset is done.
15219 */
15220 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015221 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015222 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015223 }
15224
15225 /**
15226 * Called when text direction is reset. Subclasses that care about text direction reset should
15227 * override this method and do a reset of the text direction of their children. The default
15228 * implementation does nothing.
15229 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015230 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015231 }
15232
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015233 /**
15234 * Return the value specifying the text alignment or policy that was set with
15235 * {@link #setTextAlignment(int)}.
15236 *
15237 * @return the defined text alignment. It can be one of:
15238 *
15239 * {@link #TEXT_ALIGNMENT_INHERIT},
15240 * {@link #TEXT_ALIGNMENT_GRAVITY},
15241 * {@link #TEXT_ALIGNMENT_CENTER},
15242 * {@link #TEXT_ALIGNMENT_TEXT_START},
15243 * {@link #TEXT_ALIGNMENT_TEXT_END},
15244 * {@link #TEXT_ALIGNMENT_VIEW_START},
15245 * {@link #TEXT_ALIGNMENT_VIEW_END}
15246 */
15247 @ViewDebug.ExportedProperty(category = "text", mapping = {
15248 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15249 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15250 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15251 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15252 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15253 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15254 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15255 })
15256 public int getTextAlignment() {
15257 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15258 }
15259
15260 /**
15261 * Set the text alignment.
15262 *
15263 * @param textAlignment The text alignment to set. Should be one of
15264 *
15265 * {@link #TEXT_ALIGNMENT_INHERIT},
15266 * {@link #TEXT_ALIGNMENT_GRAVITY},
15267 * {@link #TEXT_ALIGNMENT_CENTER},
15268 * {@link #TEXT_ALIGNMENT_TEXT_START},
15269 * {@link #TEXT_ALIGNMENT_TEXT_END},
15270 * {@link #TEXT_ALIGNMENT_VIEW_START},
15271 * {@link #TEXT_ALIGNMENT_VIEW_END}
15272 *
15273 * @attr ref android.R.styleable#View_textAlignment
15274 */
15275 public void setTextAlignment(int textAlignment) {
15276 if (textAlignment != getTextAlignment()) {
15277 // Reset the current and resolved text alignment
15278 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15279 resetResolvedTextAlignment();
15280 // Set the new text alignment
15281 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15282 // Refresh
15283 requestLayout();
15284 invalidate(true);
15285 }
15286 }
15287
15288 /**
15289 * Return the resolved text alignment.
15290 *
15291 * The resolved text alignment. This needs resolution if the value is
15292 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15293 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15294 *
15295 * @return the resolved text alignment. Returns one of:
15296 *
15297 * {@link #TEXT_ALIGNMENT_GRAVITY},
15298 * {@link #TEXT_ALIGNMENT_CENTER},
15299 * {@link #TEXT_ALIGNMENT_TEXT_START},
15300 * {@link #TEXT_ALIGNMENT_TEXT_END},
15301 * {@link #TEXT_ALIGNMENT_VIEW_START},
15302 * {@link #TEXT_ALIGNMENT_VIEW_END}
15303 */
15304 @ViewDebug.ExportedProperty(category = "text", mapping = {
15305 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15306 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15307 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15308 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15309 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15310 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15311 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15312 })
15313 public int getResolvedTextAlignment() {
15314 // If text alignment is not resolved, then resolve it
15315 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15316 resolveTextAlignment();
15317 }
15318 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15319 }
15320
15321 /**
15322 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15323 * resolution is done.
15324 */
15325 public void resolveTextAlignment() {
15326 // Reset any previous text alignment resolution
15327 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15328
15329 if (hasRtlSupport()) {
15330 // Set resolved text alignment flag depending on text alignment flag
15331 final int textAlignment = getTextAlignment();
15332 switch (textAlignment) {
15333 case TEXT_ALIGNMENT_INHERIT:
15334 // Check if we can resolve the text alignment
15335 if (canResolveLayoutDirection() && mParent instanceof View) {
15336 View view = (View) mParent;
15337
15338 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15339 switch (parentResolvedTextAlignment) {
15340 case TEXT_ALIGNMENT_GRAVITY:
15341 case TEXT_ALIGNMENT_TEXT_START:
15342 case TEXT_ALIGNMENT_TEXT_END:
15343 case TEXT_ALIGNMENT_CENTER:
15344 case TEXT_ALIGNMENT_VIEW_START:
15345 case TEXT_ALIGNMENT_VIEW_END:
15346 // Resolved text alignment is the same as the parent resolved
15347 // text alignment
15348 mPrivateFlags2 |=
15349 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15350 break;
15351 default:
15352 // Use default resolved text alignment
15353 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15354 }
15355 }
15356 else {
15357 // We cannot do the resolution if there is no parent so use the default
15358 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15359 }
15360 break;
15361 case TEXT_ALIGNMENT_GRAVITY:
15362 case TEXT_ALIGNMENT_TEXT_START:
15363 case TEXT_ALIGNMENT_TEXT_END:
15364 case TEXT_ALIGNMENT_CENTER:
15365 case TEXT_ALIGNMENT_VIEW_START:
15366 case TEXT_ALIGNMENT_VIEW_END:
15367 // Resolved text alignment is the same as text alignment
15368 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15369 break;
15370 default:
15371 // Use default resolved text alignment
15372 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15373 }
15374 } else {
15375 // Use default resolved text alignment
15376 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15377 }
15378
15379 // Set the resolved
15380 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
15381 onResolvedTextAlignmentChanged();
15382 }
15383
15384 /**
15385 * Check if text alignment resolution can be done.
15386 *
15387 * @return true if text alignment resolution can be done otherwise return false.
15388 */
15389 public boolean canResolveTextAlignment() {
15390 switch (getTextAlignment()) {
15391 case TEXT_DIRECTION_INHERIT:
15392 return (mParent != null);
15393 default:
15394 return true;
15395 }
15396 }
15397
15398 /**
15399 * Called when text alignment has been resolved. Subclasses that care about text alignment
15400 * resolution should override this method.
15401 *
15402 * The default implementation does nothing.
15403 */
15404 public void onResolvedTextAlignmentChanged() {
15405 }
15406
15407 /**
15408 * Reset resolved text alignment. Text alignment can be resolved with a call to
15409 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
15410 * reset is done.
15411 */
15412 public void resetResolvedTextAlignment() {
15413 // Reset any previous text alignment resolution
15414 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15415 onResolvedTextAlignmentReset();
15416 }
15417
15418 /**
15419 * Called when text alignment is reset. Subclasses that care about text alignment reset should
15420 * override this method and do a reset of the text alignment of their children. The default
15421 * implementation does nothing.
15422 */
15423 public void onResolvedTextAlignmentReset() {
15424 }
15425
Chet Haaseb39f0512011-05-24 14:36:40 -070015426 //
15427 // Properties
15428 //
15429 /**
15430 * A Property wrapper around the <code>alpha</code> functionality handled by the
15431 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
15432 */
Chet Haased47f1532011-12-16 11:18:52 -080015433 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015434 @Override
15435 public void setValue(View object, float value) {
15436 object.setAlpha(value);
15437 }
15438
15439 @Override
15440 public Float get(View object) {
15441 return object.getAlpha();
15442 }
15443 };
15444
15445 /**
15446 * A Property wrapper around the <code>translationX</code> functionality handled by the
15447 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
15448 */
Chet Haased47f1532011-12-16 11:18:52 -080015449 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015450 @Override
15451 public void setValue(View object, float value) {
15452 object.setTranslationX(value);
15453 }
15454
15455 @Override
15456 public Float get(View object) {
15457 return object.getTranslationX();
15458 }
15459 };
15460
15461 /**
15462 * A Property wrapper around the <code>translationY</code> functionality handled by the
15463 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
15464 */
Chet Haased47f1532011-12-16 11:18:52 -080015465 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015466 @Override
15467 public void setValue(View object, float value) {
15468 object.setTranslationY(value);
15469 }
15470
15471 @Override
15472 public Float get(View object) {
15473 return object.getTranslationY();
15474 }
15475 };
15476
15477 /**
15478 * A Property wrapper around the <code>x</code> functionality handled by the
15479 * {@link View#setX(float)} and {@link View#getX()} methods.
15480 */
Chet Haased47f1532011-12-16 11:18:52 -080015481 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015482 @Override
15483 public void setValue(View object, float value) {
15484 object.setX(value);
15485 }
15486
15487 @Override
15488 public Float get(View object) {
15489 return object.getX();
15490 }
15491 };
15492
15493 /**
15494 * A Property wrapper around the <code>y</code> functionality handled by the
15495 * {@link View#setY(float)} and {@link View#getY()} methods.
15496 */
Chet Haased47f1532011-12-16 11:18:52 -080015497 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015498 @Override
15499 public void setValue(View object, float value) {
15500 object.setY(value);
15501 }
15502
15503 @Override
15504 public Float get(View object) {
15505 return object.getY();
15506 }
15507 };
15508
15509 /**
15510 * A Property wrapper around the <code>rotation</code> functionality handled by the
15511 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
15512 */
Chet Haased47f1532011-12-16 11:18:52 -080015513 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015514 @Override
15515 public void setValue(View object, float value) {
15516 object.setRotation(value);
15517 }
15518
15519 @Override
15520 public Float get(View object) {
15521 return object.getRotation();
15522 }
15523 };
15524
15525 /**
15526 * A Property wrapper around the <code>rotationX</code> functionality handled by the
15527 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
15528 */
Chet Haased47f1532011-12-16 11:18:52 -080015529 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015530 @Override
15531 public void setValue(View object, float value) {
15532 object.setRotationX(value);
15533 }
15534
15535 @Override
15536 public Float get(View object) {
15537 return object.getRotationX();
15538 }
15539 };
15540
15541 /**
15542 * A Property wrapper around the <code>rotationY</code> functionality handled by the
15543 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
15544 */
Chet Haased47f1532011-12-16 11:18:52 -080015545 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015546 @Override
15547 public void setValue(View object, float value) {
15548 object.setRotationY(value);
15549 }
15550
15551 @Override
15552 public Float get(View object) {
15553 return object.getRotationY();
15554 }
15555 };
15556
15557 /**
15558 * A Property wrapper around the <code>scaleX</code> functionality handled by the
15559 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
15560 */
Chet Haased47f1532011-12-16 11:18:52 -080015561 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015562 @Override
15563 public void setValue(View object, float value) {
15564 object.setScaleX(value);
15565 }
15566
15567 @Override
15568 public Float get(View object) {
15569 return object.getScaleX();
15570 }
15571 };
15572
15573 /**
15574 * A Property wrapper around the <code>scaleY</code> functionality handled by the
15575 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
15576 */
Chet Haased47f1532011-12-16 11:18:52 -080015577 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015578 @Override
15579 public void setValue(View object, float value) {
15580 object.setScaleY(value);
15581 }
15582
15583 @Override
15584 public Float get(View object) {
15585 return object.getScaleY();
15586 }
15587 };
15588
Jeff Brown33bbfd22011-02-24 20:55:35 -080015589 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015590 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
15591 * Each MeasureSpec represents a requirement for either the width or the height.
15592 * A MeasureSpec is comprised of a size and a mode. There are three possible
15593 * modes:
15594 * <dl>
15595 * <dt>UNSPECIFIED</dt>
15596 * <dd>
15597 * The parent has not imposed any constraint on the child. It can be whatever size
15598 * it wants.
15599 * </dd>
15600 *
15601 * <dt>EXACTLY</dt>
15602 * <dd>
15603 * The parent has determined an exact size for the child. The child is going to be
15604 * given those bounds regardless of how big it wants to be.
15605 * </dd>
15606 *
15607 * <dt>AT_MOST</dt>
15608 * <dd>
15609 * The child can be as large as it wants up to the specified size.
15610 * </dd>
15611 * </dl>
15612 *
15613 * MeasureSpecs are implemented as ints to reduce object allocation. This class
15614 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
15615 */
15616 public static class MeasureSpec {
15617 private static final int MODE_SHIFT = 30;
15618 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
15619
15620 /**
15621 * Measure specification mode: The parent has not imposed any constraint
15622 * on the child. It can be whatever size it wants.
15623 */
15624 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
15625
15626 /**
15627 * Measure specification mode: The parent has determined an exact size
15628 * for the child. The child is going to be given those bounds regardless
15629 * of how big it wants to be.
15630 */
15631 public static final int EXACTLY = 1 << MODE_SHIFT;
15632
15633 /**
15634 * Measure specification mode: The child can be as large as it wants up
15635 * to the specified size.
15636 */
15637 public static final int AT_MOST = 2 << MODE_SHIFT;
15638
15639 /**
15640 * Creates a measure specification based on the supplied size and mode.
15641 *
15642 * The mode must always be one of the following:
15643 * <ul>
15644 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
15645 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
15646 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
15647 * </ul>
15648 *
15649 * @param size the size of the measure specification
15650 * @param mode the mode of the measure specification
15651 * @return the measure specification based on size and mode
15652 */
15653 public static int makeMeasureSpec(int size, int mode) {
15654 return size + mode;
15655 }
15656
15657 /**
15658 * Extracts the mode from the supplied measure specification.
15659 *
15660 * @param measureSpec the measure specification to extract the mode from
15661 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
15662 * {@link android.view.View.MeasureSpec#AT_MOST} or
15663 * {@link android.view.View.MeasureSpec#EXACTLY}
15664 */
15665 public static int getMode(int measureSpec) {
15666 return (measureSpec & MODE_MASK);
15667 }
15668
15669 /**
15670 * Extracts the size from the supplied measure specification.
15671 *
15672 * @param measureSpec the measure specification to extract the size from
15673 * @return the size in pixels defined in the supplied measure specification
15674 */
15675 public static int getSize(int measureSpec) {
15676 return (measureSpec & ~MODE_MASK);
15677 }
15678
15679 /**
15680 * Returns a String representation of the specified measure
15681 * specification.
15682 *
15683 * @param measureSpec the measure specification to convert to a String
15684 * @return a String with the following format: "MeasureSpec: MODE SIZE"
15685 */
15686 public static String toString(int measureSpec) {
15687 int mode = getMode(measureSpec);
15688 int size = getSize(measureSpec);
15689
15690 StringBuilder sb = new StringBuilder("MeasureSpec: ");
15691
15692 if (mode == UNSPECIFIED)
15693 sb.append("UNSPECIFIED ");
15694 else if (mode == EXACTLY)
15695 sb.append("EXACTLY ");
15696 else if (mode == AT_MOST)
15697 sb.append("AT_MOST ");
15698 else
15699 sb.append(mode).append(" ");
15700
15701 sb.append(size);
15702 return sb.toString();
15703 }
15704 }
15705
15706 class CheckForLongPress implements Runnable {
15707
15708 private int mOriginalWindowAttachCount;
15709
15710 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070015711 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015712 && mOriginalWindowAttachCount == mWindowAttachCount) {
15713 if (performLongClick()) {
15714 mHasPerformedLongPress = true;
15715 }
15716 }
15717 }
15718
15719 public void rememberWindowAttachCount() {
15720 mOriginalWindowAttachCount = mWindowAttachCount;
15721 }
15722 }
Joe Malin32736f02011-01-19 16:14:20 -080015723
Adam Powelle14579b2009-12-16 18:39:52 -080015724 private final class CheckForTap implements Runnable {
15725 public void run() {
15726 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080015727 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015728 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080015729 }
15730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015731
Adam Powella35d7682010-03-12 14:48:13 -080015732 private final class PerformClick implements Runnable {
15733 public void run() {
15734 performClick();
15735 }
15736 }
15737
Dianne Hackborn63042d62011-01-26 18:56:29 -080015738 /** @hide */
15739 public void hackTurnOffWindowResizeAnim(boolean off) {
15740 mAttachInfo.mTurnOffWindowResizeAnim = off;
15741 }
Joe Malin32736f02011-01-19 16:14:20 -080015742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015743 /**
Chet Haasea00f3862011-02-22 06:34:40 -080015744 * This method returns a ViewPropertyAnimator object, which can be used to animate
15745 * specific properties on this View.
15746 *
15747 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
15748 */
15749 public ViewPropertyAnimator animate() {
15750 if (mAnimator == null) {
15751 mAnimator = new ViewPropertyAnimator(this);
15752 }
15753 return mAnimator;
15754 }
15755
15756 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015757 * Interface definition for a callback to be invoked when a key event is
15758 * dispatched to this view. The callback will be invoked before the key
15759 * event is given to the view.
15760 */
15761 public interface OnKeyListener {
15762 /**
15763 * Called when a key is dispatched to a view. This allows listeners to
15764 * get a chance to respond before the target view.
15765 *
15766 * @param v The view the key has been dispatched to.
15767 * @param keyCode The code for the physical key that was pressed
15768 * @param event The KeyEvent object containing full information about
15769 * the event.
15770 * @return True if the listener has consumed the event, false otherwise.
15771 */
15772 boolean onKey(View v, int keyCode, KeyEvent event);
15773 }
15774
15775 /**
15776 * Interface definition for a callback to be invoked when a touch event is
15777 * dispatched to this view. The callback will be invoked before the touch
15778 * event is given to the view.
15779 */
15780 public interface OnTouchListener {
15781 /**
15782 * Called when a touch event is dispatched to a view. This allows listeners to
15783 * get a chance to respond before the target view.
15784 *
15785 * @param v The view the touch event has been dispatched to.
15786 * @param event The MotionEvent object containing full information about
15787 * the event.
15788 * @return True if the listener has consumed the event, false otherwise.
15789 */
15790 boolean onTouch(View v, MotionEvent event);
15791 }
15792
15793 /**
Jeff Brown10b62902011-06-20 16:40:37 -070015794 * Interface definition for a callback to be invoked when a hover event is
15795 * dispatched to this view. The callback will be invoked before the hover
15796 * event is given to the view.
15797 */
15798 public interface OnHoverListener {
15799 /**
15800 * Called when a hover event is dispatched to a view. This allows listeners to
15801 * get a chance to respond before the target view.
15802 *
15803 * @param v The view the hover event has been dispatched to.
15804 * @param event The MotionEvent object containing full information about
15805 * the event.
15806 * @return True if the listener has consumed the event, false otherwise.
15807 */
15808 boolean onHover(View v, MotionEvent event);
15809 }
15810
15811 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015812 * Interface definition for a callback to be invoked when a generic motion event is
15813 * dispatched to this view. The callback will be invoked before the generic motion
15814 * event is given to the view.
15815 */
15816 public interface OnGenericMotionListener {
15817 /**
15818 * Called when a generic motion event is dispatched to a view. This allows listeners to
15819 * get a chance to respond before the target view.
15820 *
15821 * @param v The view the generic motion event has been dispatched to.
15822 * @param event The MotionEvent object containing full information about
15823 * the event.
15824 * @return True if the listener has consumed the event, false otherwise.
15825 */
15826 boolean onGenericMotion(View v, MotionEvent event);
15827 }
15828
15829 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015830 * Interface definition for a callback to be invoked when a view has been clicked and held.
15831 */
15832 public interface OnLongClickListener {
15833 /**
15834 * Called when a view has been clicked and held.
15835 *
15836 * @param v The view that was clicked and held.
15837 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080015838 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015839 */
15840 boolean onLongClick(View v);
15841 }
15842
15843 /**
Chris Tate32affef2010-10-18 15:29:21 -070015844 * Interface definition for a callback to be invoked when a drag is being dispatched
15845 * to this view. The callback will be invoked before the hosting view's own
15846 * onDrag(event) method. If the listener wants to fall back to the hosting view's
15847 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070015848 *
15849 * <div class="special reference">
15850 * <h3>Developer Guides</h3>
15851 * <p>For a guide to implementing drag and drop features, read the
15852 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15853 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070015854 */
15855 public interface OnDragListener {
15856 /**
15857 * Called when a drag event is dispatched to a view. This allows listeners
15858 * to get a chance to override base View behavior.
15859 *
Joe Malin32736f02011-01-19 16:14:20 -080015860 * @param v The View that received the drag event.
15861 * @param event The {@link android.view.DragEvent} object for the drag event.
15862 * @return {@code true} if the drag event was handled successfully, or {@code false}
15863 * if the drag event was not handled. Note that {@code false} will trigger the View
15864 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070015865 */
15866 boolean onDrag(View v, DragEvent event);
15867 }
15868
15869 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015870 * Interface definition for a callback to be invoked when the focus state of
15871 * a view changed.
15872 */
15873 public interface OnFocusChangeListener {
15874 /**
15875 * Called when the focus state of a view has changed.
15876 *
15877 * @param v The view whose state has changed.
15878 * @param hasFocus The new focus state of v.
15879 */
15880 void onFocusChange(View v, boolean hasFocus);
15881 }
15882
15883 /**
15884 * Interface definition for a callback to be invoked when a view is clicked.
15885 */
15886 public interface OnClickListener {
15887 /**
15888 * Called when a view has been clicked.
15889 *
15890 * @param v The view that was clicked.
15891 */
15892 void onClick(View v);
15893 }
15894
15895 /**
15896 * Interface definition for a callback to be invoked when the context menu
15897 * for this view is being built.
15898 */
15899 public interface OnCreateContextMenuListener {
15900 /**
15901 * Called when the context menu for this view is being built. It is not
15902 * safe to hold onto the menu after this method returns.
15903 *
15904 * @param menu The context menu that is being built
15905 * @param v The view for which the context menu is being built
15906 * @param menuInfo Extra information about the item for which the
15907 * context menu should be shown. This information will vary
15908 * depending on the class of v.
15909 */
15910 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
15911 }
15912
Joe Onorato664644d2011-01-23 17:53:23 -080015913 /**
15914 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015915 * visibility. This reports <strong>global</strong> changes to the system UI
15916 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080015917 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015918 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080015919 */
15920 public interface OnSystemUiVisibilityChangeListener {
15921 /**
15922 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070015923 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080015924 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040015925 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015926 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
15927 * <strong>global</strong> state of the UI visibility flags, not what your
15928 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080015929 */
15930 public void onSystemUiVisibilityChange(int visibility);
15931 }
15932
Adam Powell4afd62b2011-02-18 15:02:18 -080015933 /**
15934 * Interface definition for a callback to be invoked when this view is attached
15935 * or detached from its window.
15936 */
15937 public interface OnAttachStateChangeListener {
15938 /**
15939 * Called when the view is attached to a window.
15940 * @param v The view that was attached
15941 */
15942 public void onViewAttachedToWindow(View v);
15943 /**
15944 * Called when the view is detached from a window.
15945 * @param v The view that was detached
15946 */
15947 public void onViewDetachedFromWindow(View v);
15948 }
15949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015950 private final class UnsetPressedState implements Runnable {
15951 public void run() {
15952 setPressed(false);
15953 }
15954 }
15955
15956 /**
15957 * Base class for derived classes that want to save and restore their own
15958 * state in {@link android.view.View#onSaveInstanceState()}.
15959 */
15960 public static class BaseSavedState extends AbsSavedState {
15961 /**
15962 * Constructor used when reading from a parcel. Reads the state of the superclass.
15963 *
15964 * @param source
15965 */
15966 public BaseSavedState(Parcel source) {
15967 super(source);
15968 }
15969
15970 /**
15971 * Constructor called by derived classes when creating their SavedState objects
15972 *
15973 * @param superState The state of the superclass of this view
15974 */
15975 public BaseSavedState(Parcelable superState) {
15976 super(superState);
15977 }
15978
15979 public static final Parcelable.Creator<BaseSavedState> CREATOR =
15980 new Parcelable.Creator<BaseSavedState>() {
15981 public BaseSavedState createFromParcel(Parcel in) {
15982 return new BaseSavedState(in);
15983 }
15984
15985 public BaseSavedState[] newArray(int size) {
15986 return new BaseSavedState[size];
15987 }
15988 };
15989 }
15990
15991 /**
15992 * A set of information given to a view when it is attached to its parent
15993 * window.
15994 */
15995 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015996 interface Callbacks {
15997 void playSoundEffect(int effectId);
15998 boolean performHapticFeedback(int effectId, boolean always);
15999 }
16000
16001 /**
16002 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16003 * to a Handler. This class contains the target (View) to invalidate and
16004 * the coordinates of the dirty rectangle.
16005 *
16006 * For performance purposes, this class also implements a pool of up to
16007 * POOL_LIMIT objects that get reused. This reduces memory allocations
16008 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016009 */
Romain Guyd928d682009-03-31 17:52:16 -070016010 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016011 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016012 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16013 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016014 public InvalidateInfo newInstance() {
16015 return new InvalidateInfo();
16016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016017
Romain Guyd928d682009-03-31 17:52:16 -070016018 public void onAcquired(InvalidateInfo element) {
16019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016020
Romain Guyd928d682009-03-31 17:52:16 -070016021 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016022 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016023 }
16024 }, POOL_LIMIT)
16025 );
16026
16027 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016028 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016029
16030 View target;
16031
16032 int left;
16033 int top;
16034 int right;
16035 int bottom;
16036
Romain Guyd928d682009-03-31 17:52:16 -070016037 public void setNextPoolable(InvalidateInfo element) {
16038 mNext = element;
16039 }
16040
16041 public InvalidateInfo getNextPoolable() {
16042 return mNext;
16043 }
16044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016045 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016046 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016047 }
16048
16049 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016050 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016051 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016052
16053 public boolean isPooled() {
16054 return mIsPooled;
16055 }
16056
16057 public void setPooled(boolean isPooled) {
16058 mIsPooled = isPooled;
16059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016060 }
16061
16062 final IWindowSession mSession;
16063
16064 final IWindow mWindow;
16065
16066 final IBinder mWindowToken;
16067
16068 final Callbacks mRootCallbacks;
16069
Romain Guy59a12ca2011-06-09 17:48:21 -070016070 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016072 /**
16073 * The top view of the hierarchy.
16074 */
16075 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016077 IBinder mPanelParentWindowToken;
16078 Surface mSurface;
16079
Romain Guyb051e892010-09-28 19:09:36 -070016080 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016081 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016082 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016083
Romain Guy7e4e5612012-03-05 14:37:29 -080016084 boolean mScreenOn;
16085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016086 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016087 * Scale factor used by the compatibility mode
16088 */
16089 float mApplicationScale;
16090
16091 /**
16092 * Indicates whether the application is in compatibility mode
16093 */
16094 boolean mScalingRequired;
16095
16096 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016097 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016098 */
16099 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016100
Dianne Hackborn63042d62011-01-26 18:56:29 -080016101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016102 * Left position of this view's window
16103 */
16104 int mWindowLeft;
16105
16106 /**
16107 * Top position of this view's window
16108 */
16109 int mWindowTop;
16110
16111 /**
Adam Powell26153a32010-11-08 15:22:27 -080016112 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016113 */
Adam Powell26153a32010-11-08 15:22:27 -080016114 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016115
16116 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016117 * For windows that are full-screen but using insets to layout inside
16118 * of the screen decorations, these are the current insets for the
16119 * content of the window.
16120 */
16121 final Rect mContentInsets = new Rect();
16122
16123 /**
16124 * For windows that are full-screen but using insets to layout inside
16125 * of the screen decorations, these are the current insets for the
16126 * actual visible parts of the window.
16127 */
16128 final Rect mVisibleInsets = new Rect();
16129
16130 /**
16131 * The internal insets given by this window. This value is
16132 * supplied by the client (through
16133 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16134 * be given to the window manager when changed to be used in laying
16135 * out windows behind it.
16136 */
16137 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16138 = new ViewTreeObserver.InternalInsetsInfo();
16139
16140 /**
16141 * All views in the window's hierarchy that serve as scroll containers,
16142 * used to determine if the window can be resized or must be panned
16143 * to adjust for a soft input area.
16144 */
16145 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16146
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016147 final KeyEvent.DispatcherState mKeyDispatchState
16148 = new KeyEvent.DispatcherState();
16149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016150 /**
16151 * Indicates whether the view's window currently has the focus.
16152 */
16153 boolean mHasWindowFocus;
16154
16155 /**
16156 * The current visibility of the window.
16157 */
16158 int mWindowVisibility;
16159
16160 /**
16161 * Indicates the time at which drawing started to occur.
16162 */
16163 long mDrawingTime;
16164
16165 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016166 * Indicates whether or not ignoring the DIRTY_MASK flags.
16167 */
16168 boolean mIgnoreDirtyState;
16169
16170 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016171 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16172 * to avoid clearing that flag prematurely.
16173 */
16174 boolean mSetIgnoreDirtyState = false;
16175
16176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016177 * Indicates whether the view's window is currently in touch mode.
16178 */
16179 boolean mInTouchMode;
16180
16181 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016182 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016183 * the next time it performs a traversal
16184 */
16185 boolean mRecomputeGlobalAttributes;
16186
16187 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016188 * Always report new attributes at next traversal.
16189 */
16190 boolean mForceReportNewAttributes;
16191
16192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016193 * Set during a traveral if any views want to keep the screen on.
16194 */
16195 boolean mKeepScreenOn;
16196
16197 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016198 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16199 */
16200 int mSystemUiVisibility;
16201
16202 /**
16203 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16204 * attached.
16205 */
16206 boolean mHasSystemUiListeners;
16207
16208 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016209 * Set if the visibility of any views has changed.
16210 */
16211 boolean mViewVisibilityChanged;
16212
16213 /**
16214 * Set to true if a view has been scrolled.
16215 */
16216 boolean mViewScrollChanged;
16217
16218 /**
16219 * Global to the view hierarchy used as a temporary for dealing with
16220 * x/y points in the transparent region computations.
16221 */
16222 final int[] mTransparentLocation = new int[2];
16223
16224 /**
16225 * Global to the view hierarchy used as a temporary for dealing with
16226 * x/y points in the ViewGroup.invalidateChild implementation.
16227 */
16228 final int[] mInvalidateChildLocation = new int[2];
16229
Chet Haasec3aa3612010-06-17 08:50:37 -070016230
16231 /**
16232 * Global to the view hierarchy used as a temporary for dealing with
16233 * x/y location when view is transformed.
16234 */
16235 final float[] mTmpTransformLocation = new float[2];
16236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016237 /**
16238 * The view tree observer used to dispatch global events like
16239 * layout, pre-draw, touch mode change, etc.
16240 */
16241 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16242
16243 /**
16244 * A Canvas used by the view hierarchy to perform bitmap caching.
16245 */
16246 Canvas mCanvas;
16247
16248 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016249 * The view root impl.
16250 */
16251 final ViewRootImpl mViewRootImpl;
16252
16253 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016254 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016255 * handler can be used to pump events in the UI events queue.
16256 */
16257 final Handler mHandler;
16258
16259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016260 * Temporary for use in computing invalidate rectangles while
16261 * calling up the hierarchy.
16262 */
16263 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016264
16265 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016266 * Temporary for use in computing hit areas with transformed views
16267 */
16268 final RectF mTmpTransformRect = new RectF();
16269
16270 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016271 * Temporary list for use in collecting focusable descendents of a view.
16272 */
16273 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
16274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016275 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016276 * The id of the window for accessibility purposes.
16277 */
16278 int mAccessibilityWindowId = View.NO_ID;
16279
16280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016281 * Creates a new set of attachment information with the specified
16282 * events handler and thread.
16283 *
16284 * @param handler the events handler the view must use
16285 */
16286 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016287 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016288 mSession = session;
16289 mWindow = window;
16290 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016291 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016292 mHandler = handler;
16293 mRootCallbacks = effectPlayer;
16294 }
16295 }
16296
16297 /**
16298 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16299 * is supported. This avoids keeping too many unused fields in most
16300 * instances of View.</p>
16301 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016302 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016303
Mike Cleronf116bf82009-09-27 19:14:12 -070016304 /**
16305 * Scrollbars are not visible
16306 */
16307 public static final int OFF = 0;
16308
16309 /**
16310 * Scrollbars are visible
16311 */
16312 public static final int ON = 1;
16313
16314 /**
16315 * Scrollbars are fading away
16316 */
16317 public static final int FADING = 2;
16318
16319 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016321 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016322 public int scrollBarDefaultDelayBeforeFade;
16323 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016324
16325 public int scrollBarSize;
16326 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016327 public float[] interpolatorValues;
16328 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016329
16330 public final Paint paint;
16331 public final Matrix matrix;
16332 public Shader shader;
16333
Mike Cleronf116bf82009-09-27 19:14:12 -070016334 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16335
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016336 private static final float[] OPAQUE = { 255 };
16337 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016338
Mike Cleronf116bf82009-09-27 19:14:12 -070016339 /**
16340 * When fading should start. This time moves into the future every time
16341 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16342 */
16343 public long fadeStartTime;
16344
16345
16346 /**
16347 * The current state of the scrollbars: ON, OFF, or FADING
16348 */
16349 public int state = OFF;
16350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016351 private int mLastColor;
16352
Mike Cleronf116bf82009-09-27 19:14:12 -070016353 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016354 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16355 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016356 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16357 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016358
16359 paint = new Paint();
16360 matrix = new Matrix();
16361 // use use a height of 1, and then wack the matrix each time we
16362 // actually use it.
16363 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016365 paint.setShader(shader);
16366 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070016367 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016368 }
Romain Guy8506ab42009-06-11 17:35:47 -070016369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016370 public void setFadeColor(int color) {
16371 if (color != 0 && color != mLastColor) {
16372 mLastColor = color;
16373 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070016374
Romain Guye55e1a72009-08-27 10:42:26 -070016375 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
16376 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016378 paint.setShader(shader);
16379 // Restore the default transfer mode (src_over)
16380 paint.setXfermode(null);
16381 }
16382 }
Joe Malin32736f02011-01-19 16:14:20 -080016383
Mike Cleronf116bf82009-09-27 19:14:12 -070016384 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070016385 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070016386 if (now >= fadeStartTime) {
16387
16388 // the animation fades the scrollbars out by changing
16389 // the opacity (alpha) from fully opaque to fully
16390 // transparent
16391 int nextFrame = (int) now;
16392 int framesCount = 0;
16393
16394 Interpolator interpolator = scrollBarInterpolator;
16395
16396 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016397 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070016398
16399 // End transparent
16400 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016401 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070016402
16403 state = FADING;
16404
16405 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080016406 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070016407 }
16408 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070016409 }
Mike Cleronf116bf82009-09-27 19:14:12 -070016410
Svetoslav Ganova0156172011-06-26 17:55:44 -070016411 /**
16412 * Resuable callback for sending
16413 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
16414 */
16415 private class SendViewScrolledAccessibilityEvent implements Runnable {
16416 public volatile boolean mIsPending;
16417
16418 public void run() {
16419 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
16420 mIsPending = false;
16421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016422 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016423
16424 /**
16425 * <p>
16426 * This class represents a delegate that can be registered in a {@link View}
16427 * to enhance accessibility support via composition rather via inheritance.
16428 * It is specifically targeted to widget developers that extend basic View
16429 * classes i.e. classes in package android.view, that would like their
16430 * applications to be backwards compatible.
16431 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080016432 * <div class="special reference">
16433 * <h3>Developer Guides</h3>
16434 * <p>For more information about making applications accessible, read the
16435 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
16436 * developer guide.</p>
16437 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016438 * <p>
16439 * A scenario in which a developer would like to use an accessibility delegate
16440 * is overriding a method introduced in a later API version then the minimal API
16441 * version supported by the application. For example, the method
16442 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
16443 * in API version 4 when the accessibility APIs were first introduced. If a
16444 * developer would like his application to run on API version 4 devices (assuming
16445 * all other APIs used by the application are version 4 or lower) and take advantage
16446 * of this method, instead of overriding the method which would break the application's
16447 * backwards compatibility, he can override the corresponding method in this
16448 * delegate and register the delegate in the target View if the API version of
16449 * the system is high enough i.e. the API version is same or higher to the API
16450 * version that introduced
16451 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
16452 * </p>
16453 * <p>
16454 * Here is an example implementation:
16455 * </p>
16456 * <code><pre><p>
16457 * if (Build.VERSION.SDK_INT >= 14) {
16458 * // If the API version is equal of higher than the version in
16459 * // which onInitializeAccessibilityNodeInfo was introduced we
16460 * // register a delegate with a customized implementation.
16461 * View view = findViewById(R.id.view_id);
16462 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
16463 * public void onInitializeAccessibilityNodeInfo(View host,
16464 * AccessibilityNodeInfo info) {
16465 * // Let the default implementation populate the info.
16466 * super.onInitializeAccessibilityNodeInfo(host, info);
16467 * // Set some other information.
16468 * info.setEnabled(host.isEnabled());
16469 * }
16470 * });
16471 * }
16472 * </code></pre></p>
16473 * <p>
16474 * This delegate contains methods that correspond to the accessibility methods
16475 * in View. If a delegate has been specified the implementation in View hands
16476 * off handling to the corresponding method in this delegate. The default
16477 * implementation the delegate methods behaves exactly as the corresponding
16478 * method in View for the case of no accessibility delegate been set. Hence,
16479 * to customize the behavior of a View method, clients can override only the
16480 * corresponding delegate method without altering the behavior of the rest
16481 * accessibility related methods of the host view.
16482 * </p>
16483 */
16484 public static class AccessibilityDelegate {
16485
16486 /**
16487 * Sends an accessibility event of the given type. If accessibility is not
16488 * enabled this method has no effect.
16489 * <p>
16490 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
16491 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
16492 * been set.
16493 * </p>
16494 *
16495 * @param host The View hosting the delegate.
16496 * @param eventType The type of the event to send.
16497 *
16498 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
16499 */
16500 public void sendAccessibilityEvent(View host, int eventType) {
16501 host.sendAccessibilityEventInternal(eventType);
16502 }
16503
16504 /**
16505 * Sends an accessibility event. This method behaves exactly as
16506 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
16507 * empty {@link AccessibilityEvent} and does not perform a check whether
16508 * accessibility is enabled.
16509 * <p>
16510 * The default implementation behaves as
16511 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16512 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
16513 * the case of no accessibility delegate been set.
16514 * </p>
16515 *
16516 * @param host The View hosting the delegate.
16517 * @param event The event to send.
16518 *
16519 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16520 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16521 */
16522 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
16523 host.sendAccessibilityEventUncheckedInternal(event);
16524 }
16525
16526 /**
16527 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
16528 * to its children for adding their text content to the event.
16529 * <p>
16530 * The default implementation behaves as
16531 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16532 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
16533 * the case of no accessibility delegate been set.
16534 * </p>
16535 *
16536 * @param host The View hosting the delegate.
16537 * @param event The event.
16538 * @return True if the event population was completed.
16539 *
16540 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16541 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16542 */
16543 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16544 return host.dispatchPopulateAccessibilityEventInternal(event);
16545 }
16546
16547 /**
16548 * Gives a chance to the host View to populate the accessibility event with its
16549 * text content.
16550 * <p>
16551 * The default implementation behaves as
16552 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
16553 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
16554 * the case of no accessibility delegate been set.
16555 * </p>
16556 *
16557 * @param host The View hosting the delegate.
16558 * @param event The accessibility event which to populate.
16559 *
16560 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
16561 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
16562 */
16563 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16564 host.onPopulateAccessibilityEventInternal(event);
16565 }
16566
16567 /**
16568 * Initializes an {@link AccessibilityEvent} with information about the
16569 * the host View which is the event source.
16570 * <p>
16571 * The default implementation behaves as
16572 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
16573 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
16574 * the case of no accessibility delegate been set.
16575 * </p>
16576 *
16577 * @param host The View hosting the delegate.
16578 * @param event The event to initialize.
16579 *
16580 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
16581 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
16582 */
16583 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
16584 host.onInitializeAccessibilityEventInternal(event);
16585 }
16586
16587 /**
16588 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
16589 * <p>
16590 * The default implementation behaves as
16591 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16592 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
16593 * the case of no accessibility delegate been set.
16594 * </p>
16595 *
16596 * @param host The View hosting the delegate.
16597 * @param info The instance to initialize.
16598 *
16599 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16600 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16601 */
16602 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
16603 host.onInitializeAccessibilityNodeInfoInternal(info);
16604 }
16605
16606 /**
16607 * Called when a child of the host View has requested sending an
16608 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
16609 * to augment the event.
16610 * <p>
16611 * The default implementation behaves as
16612 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16613 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
16614 * the case of no accessibility delegate been set.
16615 * </p>
16616 *
16617 * @param host The View hosting the delegate.
16618 * @param child The child which requests sending the event.
16619 * @param event The event to be sent.
16620 * @return True if the event should be sent
16621 *
16622 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16623 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16624 */
16625 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
16626 AccessibilityEvent event) {
16627 return host.onRequestSendAccessibilityEventInternal(child, event);
16628 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070016629
16630 /**
16631 * Gets the provider for managing a virtual view hierarchy rooted at this View
16632 * and reported to {@link android.accessibilityservice.AccessibilityService}s
16633 * that explore the window content.
16634 * <p>
16635 * The default implementation behaves as
16636 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
16637 * the case of no accessibility delegate been set.
16638 * </p>
16639 *
16640 * @return The provider.
16641 *
16642 * @see AccessibilityNodeProvider
16643 */
16644 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
16645 return null;
16646 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016648}