blob: 393fa65464927ece6962c12419d04c54e3f1a8b9 [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;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
42import android.os.Handler;
43import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Parcel;
45import android.os.Parcelable;
46import android.os.RemoteException;
47import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070048import android.os.SystemProperties;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070049import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070065import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070066import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070068import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080069import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.inputmethod.InputConnection;
72import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.widget.ScrollBarDrawable;
74
Romain Guy1ef3fdb2011-09-09 15:30:30 -070075import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070076import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070077
Doug Feltcb3791202011-07-07 11:57:48 -070078import com.android.internal.R;
79import com.android.internal.util.Predicate;
80import com.android.internal.view.menu.MenuBuilder;
81
Christopher Tatea0374192010-10-05 13:06:41 -070082import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070083import java.lang.reflect.InvocationTargetException;
84import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.util.ArrayList;
86import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070087import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080088import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90/**
91 * <p>
92 * This class represents the basic building block for user interface components. A View
93 * occupies a rectangular area on the screen and is responsible for drawing and
94 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070095 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
97 * are invisible containers that hold other Views (or other ViewGroups) and define
98 * their layout properties.
99 * </p>
100 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700101 * <div class="special reference">
102 * <h3>Developer Guides</h3>
103 * <p>For information about using this class to develop your application's user interface,
104 * 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 -0800105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700132 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700176 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700182 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700195 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700202 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700207 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700212 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700217 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700224 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700230 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700249 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700342 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
343 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
345 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 * </p>
347 *
348 * <p>
349 * Even though a view can define a padding, it does not provide any support for
350 * margins. However, view groups provide such a support. Refer to
351 * {@link android.view.ViewGroup} and
352 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
353 * </p>
354 *
355 * <a name="Layout"></a>
356 * <h3>Layout</h3>
357 * <p>
358 * Layout is a two pass process: a measure pass and a layout pass. The measuring
359 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
360 * of the view tree. Each view pushes dimension specifications down the tree
361 * during the recursion. At the end of the measure pass, every view has stored
362 * its measurements. The second pass happens in
363 * {@link #layout(int,int,int,int)} and is also top-down. During
364 * this pass each parent is responsible for positioning all of its children
365 * using the sizes computed in the measure pass.
366 * </p>
367 *
368 * <p>
369 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
370 * {@link #getMeasuredHeight()} values must be set, along with those for all of
371 * that view's descendants. A view's measured width and measured height values
372 * must respect the constraints imposed by the view's parents. This guarantees
373 * that at the end of the measure pass, all parents accept all of their
374 * children's measurements. A parent view may call measure() more than once on
375 * its children. For example, the parent may measure each child once with
376 * unspecified dimensions to find out how big they want to be, then call
377 * measure() on them again with actual numbers if the sum of all the children's
378 * unconstrained sizes is too big or too small.
379 * </p>
380 *
381 * <p>
382 * The measure pass uses two classes to communicate dimensions. The
383 * {@link MeasureSpec} class is used by views to tell their parents how they
384 * want to be measured and positioned. The base LayoutParams class just
385 * describes how big the view wants to be for both width and height. For each
386 * dimension, it can specify one of:
387 * <ul>
388 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800389 * <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 -0800390 * (minus padding)
391 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
392 * enclose its content (plus padding).
393 * </ul>
394 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
395 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
396 * an X and Y value.
397 * </p>
398 *
399 * <p>
400 * MeasureSpecs are used to push requirements down the tree from parent to
401 * child. A MeasureSpec can be in one of three modes:
402 * <ul>
403 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
404 * of a child view. For example, a LinearLayout may call measure() on its child
405 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
406 * tall the child view wants to be given a width of 240 pixels.
407 * <li>EXACTLY: This is used by the parent to impose an exact size on the
408 * child. The child must use this size, and guarantee that all of its
409 * descendants will fit within this size.
410 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
411 * child. The child must gurantee that it and all of its descendants will fit
412 * within this size.
413 * </ul>
414 * </p>
415 *
416 * <p>
417 * To intiate a layout, call {@link #requestLayout}. This method is typically
418 * called by a view on itself when it believes that is can no longer fit within
419 * its current bounds.
420 * </p>
421 *
422 * <a name="Drawing"></a>
423 * <h3>Drawing</h3>
424 * <p>
425 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700426 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 * this means that parents will draw before (i.e., behind) their children, with
428 * siblings drawn in the order they appear in the tree.
429 * If you set a background drawable for a View, then the View will draw it for you
430 * before calling back to its <code>onDraw()</code> method.
431 * </p>
432 *
433 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700434 * 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 -0800435 * </p>
436 *
437 * <p>
438 * To force a view to draw, call {@link #invalidate()}.
439 * </p>
440 *
441 * <a name="EventHandlingThreading"></a>
442 * <h3>Event Handling and Threading</h3>
443 * <p>
444 * The basic cycle of a view is as follows:
445 * <ol>
446 * <li>An event comes in and is dispatched to the appropriate view. The view
447 * handles the event and notifies any listeners.</li>
448 * <li>If in the course of processing the event, the view's bounds may need
449 * to be changed, the view will call {@link #requestLayout()}.</li>
450 * <li>Similarly, if in the course of processing the event the view's appearance
451 * may need to be changed, the view will call {@link #invalidate()}.</li>
452 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
453 * the framework will take care of measuring, laying out, and drawing the tree
454 * as appropriate.</li>
455 * </ol>
456 * </p>
457 *
458 * <p><em>Note: The entire view tree is single threaded. You must always be on
459 * the UI thread when calling any method on any view.</em>
460 * If you are doing work on other threads and want to update the state of a view
461 * from that thread, you should use a {@link Handler}.
462 * </p>
463 *
464 * <a name="FocusHandling"></a>
465 * <h3>Focus Handling</h3>
466 * <p>
467 * The framework will handle routine focus movement in response to user input.
468 * This includes changing the focus as views are removed or hidden, or as new
469 * views become available. Views indicate their willingness to take focus
470 * through the {@link #isFocusable} method. To change whether a view can take
471 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
472 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
473 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
474 * </p>
475 * <p>
476 * Focus movement is based on an algorithm which finds the nearest neighbor in a
477 * given direction. In rare cases, the default algorithm may not match the
478 * intended behavior of the developer. In these situations, you can provide
479 * explicit overrides by using these XML attributes in the layout file:
480 * <pre>
481 * nextFocusDown
482 * nextFocusLeft
483 * nextFocusRight
484 * nextFocusUp
485 * </pre>
486 * </p>
487 *
488 *
489 * <p>
490 * To get a particular view to take focus, call {@link #requestFocus()}.
491 * </p>
492 *
493 * <a name="TouchMode"></a>
494 * <h3>Touch Mode</h3>
495 * <p>
496 * When a user is navigating a user interface via directional keys such as a D-pad, it is
497 * necessary to give focus to actionable items such as buttons so the user can see
498 * what will take input. If the device has touch capabilities, however, and the user
499 * begins interacting with the interface by touching it, it is no longer necessary to
500 * always highlight, or give focus to, a particular view. This motivates a mode
501 * for interaction named 'touch mode'.
502 * </p>
503 * <p>
504 * For a touch capable device, once the user touches the screen, the device
505 * will enter touch mode. From this point onward, only views for which
506 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
507 * Other views that are touchable, like buttons, will not take focus when touched; they will
508 * only fire the on click listeners.
509 * </p>
510 * <p>
511 * Any time a user hits a directional key, such as a D-pad direction, the view device will
512 * exit touch mode, and find a view to take focus, so that the user may resume interacting
513 * with the user interface without touching the screen again.
514 * </p>
515 * <p>
516 * The touch mode state is maintained across {@link android.app.Activity}s. Call
517 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
518 * </p>
519 *
520 * <a name="Scrolling"></a>
521 * <h3>Scrolling</h3>
522 * <p>
523 * The framework provides basic support for views that wish to internally
524 * scroll their content. This includes keeping track of the X and Y scroll
525 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800526 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700527 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 * </p>
529 *
530 * <a name="Tags"></a>
531 * <h3>Tags</h3>
532 * <p>
533 * Unlike IDs, tags are not used to identify views. Tags are essentially an
534 * extra piece of information that can be associated with a view. They are most
535 * often used as a convenience to store data related to views in the views
536 * themselves rather than by putting them in a separate structure.
537 * </p>
538 *
539 * <a name="Animation"></a>
540 * <h3>Animation</h3>
541 * <p>
542 * You can attach an {@link Animation} object to a view using
543 * {@link #setAnimation(Animation)} or
544 * {@link #startAnimation(Animation)}. The animation can alter the scale,
545 * rotation, translation and alpha of a view over time. If the animation is
546 * attached to a view that has children, the animation will affect the entire
547 * subtree rooted by that node. When an animation is started, the framework will
548 * take care of redrawing the appropriate views until the animation completes.
549 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800550 * <p>
551 * Starting with Android 3.0, the preferred way of animating views is to use the
552 * {@link android.animation} package APIs.
553 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 *
Jeff Brown85a31762010-09-01 17:01:00 -0700555 * <a name="Security"></a>
556 * <h3>Security</h3>
557 * <p>
558 * Sometimes it is essential that an application be able to verify that an action
559 * is being performed with the full knowledge and consent of the user, such as
560 * granting a permission request, making a purchase or clicking on an advertisement.
561 * Unfortunately, a malicious application could try to spoof the user into
562 * performing these actions, unaware, by concealing the intended purpose of the view.
563 * As a remedy, the framework offers a touch filtering mechanism that can be used to
564 * improve the security of views that provide access to sensitive functionality.
565 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700566 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800567 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700568 * will discard touches that are received whenever the view's window is obscured by
569 * another visible window. As a result, the view will not receive touches whenever a
570 * toast, dialog or other window appears above the view's window.
571 * </p><p>
572 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700573 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
574 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700575 * </p>
576 *
Romain Guy171c5922011-01-06 10:04:23 -0800577 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700578 * @attr ref android.R.styleable#View_background
579 * @attr ref android.R.styleable#View_clickable
580 * @attr ref android.R.styleable#View_contentDescription
581 * @attr ref android.R.styleable#View_drawingCacheQuality
582 * @attr ref android.R.styleable#View_duplicateParentState
583 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700584 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700585 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700586 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700587 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700589 * @attr ref android.R.styleable#View_isScrollContainer
590 * @attr ref android.R.styleable#View_focusable
591 * @attr ref android.R.styleable#View_focusableInTouchMode
592 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
593 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800594 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700595 * @attr ref android.R.styleable#View_longClickable
596 * @attr ref android.R.styleable#View_minHeight
597 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 * @attr ref android.R.styleable#View_nextFocusDown
599 * @attr ref android.R.styleable#View_nextFocusLeft
600 * @attr ref android.R.styleable#View_nextFocusRight
601 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700602 * @attr ref android.R.styleable#View_onClick
603 * @attr ref android.R.styleable#View_padding
604 * @attr ref android.R.styleable#View_paddingBottom
605 * @attr ref android.R.styleable#View_paddingLeft
606 * @attr ref android.R.styleable#View_paddingRight
607 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800608 * @attr ref android.R.styleable#View_paddingStart
609 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700610 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800611 * @attr ref android.R.styleable#View_rotation
612 * @attr ref android.R.styleable#View_rotationX
613 * @attr ref android.R.styleable#View_rotationY
614 * @attr ref android.R.styleable#View_scaleX
615 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * @attr ref android.R.styleable#View_scrollX
617 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700618 * @attr ref android.R.styleable#View_scrollbarSize
619 * @attr ref android.R.styleable#View_scrollbarStyle
620 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700621 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
622 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
624 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarThumbVertical
626 * @attr ref android.R.styleable#View_scrollbarTrackVertical
627 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
628 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700629 * @attr ref android.R.styleable#View_soundEffectsEnabled
630 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700631 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800632 * @attr ref android.R.styleable#View_transformPivotX
633 * @attr ref android.R.styleable#View_transformPivotY
634 * @attr ref android.R.styleable#View_translationX
635 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700636 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 *
638 * @see android.view.ViewGroup
639 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700640public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
641 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 private static final boolean DBG = false;
643
644 /**
645 * The logging tag used by this class with android.util.Log.
646 */
647 protected static final String VIEW_LOG_TAG = "View";
648
649 /**
650 * Used to mark a View that has no ID.
651 */
652 public static final int NO_ID = -1;
653
654 /**
655 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
656 * calling setFlags.
657 */
658 private static final int NOT_FOCUSABLE = 0x00000000;
659
660 /**
661 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
662 * setFlags.
663 */
664 private static final int FOCUSABLE = 0x00000001;
665
666 /**
667 * Mask for use with setFlags indicating bits used for focus.
668 */
669 private static final int FOCUSABLE_MASK = 0x00000001;
670
671 /**
672 * This view will adjust its padding to fit sytem windows (e.g. status bar)
673 */
674 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
675
676 /**
Scott Main812634c22011-07-27 13:22:35 -0700677 * This view is visible.
678 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
679 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 */
681 public static final int VISIBLE = 0x00000000;
682
683 /**
684 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700685 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
686 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 */
688 public static final int INVISIBLE = 0x00000004;
689
690 /**
691 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700692 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
693 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 */
695 public static final int GONE = 0x00000008;
696
697 /**
698 * Mask for use with setFlags indicating bits used for visibility.
699 * {@hide}
700 */
701 static final int VISIBILITY_MASK = 0x0000000C;
702
703 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
704
705 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700706 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 * Use with ENABLED_MASK when calling setFlags.
708 * {@hide}
709 */
710 static final int ENABLED = 0x00000000;
711
712 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700713 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 * Use with ENABLED_MASK when calling setFlags.
715 * {@hide}
716 */
717 static final int DISABLED = 0x00000020;
718
719 /**
720 * Mask for use with setFlags indicating bits used for indicating whether
721 * this view is enabled
722 * {@hide}
723 */
724 static final int ENABLED_MASK = 0x00000020;
725
726 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700727 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
728 * called and further optimizations will be performed. It is okay to have
729 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 * {@hide}
731 */
732 static final int WILL_NOT_DRAW = 0x00000080;
733
734 /**
735 * Mask for use with setFlags indicating bits used for indicating whether
736 * this view is will draw
737 * {@hide}
738 */
739 static final int DRAW_MASK = 0x00000080;
740
741 /**
742 * <p>This view doesn't show scrollbars.</p>
743 * {@hide}
744 */
745 static final int SCROLLBARS_NONE = 0x00000000;
746
747 /**
748 * <p>This view shows horizontal scrollbars.</p>
749 * {@hide}
750 */
751 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
752
753 /**
754 * <p>This view shows vertical scrollbars.</p>
755 * {@hide}
756 */
757 static final int SCROLLBARS_VERTICAL = 0x00000200;
758
759 /**
760 * <p>Mask for use with setFlags indicating bits used for indicating which
761 * scrollbars are enabled.</p>
762 * {@hide}
763 */
764 static final int SCROLLBARS_MASK = 0x00000300;
765
Jeff Brown85a31762010-09-01 17:01:00 -0700766 /**
767 * Indicates that the view should filter touches when its window is obscured.
768 * Refer to the class comments for more information about this security feature.
769 * {@hide}
770 */
771 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
772
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700773 /**
774 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
775 * that they are optional and should be skipped if the window has
776 * requested system UI flags that ignore those insets for layout.
777 */
778 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779
780 /**
781 * <p>This view doesn't show fading edges.</p>
782 * {@hide}
783 */
784 static final int FADING_EDGE_NONE = 0x00000000;
785
786 /**
787 * <p>This view shows horizontal fading edges.</p>
788 * {@hide}
789 */
790 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
791
792 /**
793 * <p>This view shows vertical fading edges.</p>
794 * {@hide}
795 */
796 static final int FADING_EDGE_VERTICAL = 0x00002000;
797
798 /**
799 * <p>Mask for use with setFlags indicating bits used for indicating which
800 * fading edges are enabled.</p>
801 * {@hide}
802 */
803 static final int FADING_EDGE_MASK = 0x00003000;
804
805 /**
806 * <p>Indicates this view can be clicked. When clickable, a View reacts
807 * to clicks by notifying the OnClickListener.<p>
808 * {@hide}
809 */
810 static final int CLICKABLE = 0x00004000;
811
812 /**
813 * <p>Indicates this view is caching its drawing into a bitmap.</p>
814 * {@hide}
815 */
816 static final int DRAWING_CACHE_ENABLED = 0x00008000;
817
818 /**
819 * <p>Indicates that no icicle should be saved for this view.<p>
820 * {@hide}
821 */
822 static final int SAVE_DISABLED = 0x000010000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
826 * property.</p>
827 * {@hide}
828 */
829 static final int SAVE_DISABLED_MASK = 0x000010000;
830
831 /**
832 * <p>Indicates that no drawing cache should ever be created for this view.<p>
833 * {@hide}
834 */
835 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
836
837 /**
838 * <p>Indicates this view can take / keep focus when int touch mode.</p>
839 * {@hide}
840 */
841 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
842
843 /**
844 * <p>Enables low quality mode for the drawing cache.</p>
845 */
846 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
847
848 /**
849 * <p>Enables high quality mode for the drawing cache.</p>
850 */
851 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
852
853 /**
854 * <p>Enables automatic quality mode for the drawing cache.</p>
855 */
856 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
857
858 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
859 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
860 };
861
862 /**
863 * <p>Mask for use with setFlags indicating bits used for the cache
864 * quality property.</p>
865 * {@hide}
866 */
867 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
868
869 /**
870 * <p>
871 * Indicates this view can be long clicked. When long clickable, a View
872 * reacts to long clicks by notifying the OnLongClickListener or showing a
873 * context menu.
874 * </p>
875 * {@hide}
876 */
877 static final int LONG_CLICKABLE = 0x00200000;
878
879 /**
880 * <p>Indicates that this view gets its drawable states from its direct parent
881 * and ignores its original internal states.</p>
882 *
883 * @hide
884 */
885 static final int DUPLICATE_PARENT_STATE = 0x00400000;
886
887 /**
888 * The scrollbar style to display the scrollbars inside the content area,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency on the view's content.
891 */
892 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
893
894 /**
895 * The scrollbar style to display the scrollbars inside the padded area,
896 * increasing the padding of the view. The scrollbars will not overlap the
897 * content area of the view.
898 */
899 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
900
901 /**
902 * The scrollbar style to display the scrollbars at the edge of the view,
903 * without increasing the padding. The scrollbars will be overlaid with
904 * translucency.
905 */
906 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
907
908 /**
909 * The scrollbar style to display the scrollbars at the edge of the view,
910 * increasing the padding of the view. The scrollbars will only overlap the
911 * background, if any.
912 */
913 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
914
915 /**
916 * Mask to check if the scrollbar style is overlay or inset.
917 * {@hide}
918 */
919 static final int SCROLLBARS_INSET_MASK = 0x01000000;
920
921 /**
922 * Mask to check if the scrollbar style is inside or outside.
923 * {@hide}
924 */
925 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
926
927 /**
928 * Mask for scrollbar style.
929 * {@hide}
930 */
931 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
932
933 /**
934 * View flag indicating that the screen should remain on while the
935 * window containing this view is visible to the user. This effectively
936 * takes care of automatically setting the WindowManager's
937 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
938 */
939 public static final int KEEP_SCREEN_ON = 0x04000000;
940
941 /**
942 * View flag indicating whether this view should have sound effects enabled
943 * for events such as clicking and touching.
944 */
945 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
946
947 /**
948 * View flag indicating whether this view should have haptic feedback
949 * enabled for events such as long presses.
950 */
951 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
952
953 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700954 * <p>Indicates that the view hierarchy should stop saving state when
955 * it reaches this view. If state saving is initiated immediately at
956 * the view, it will be allowed.
957 * {@hide}
958 */
959 static final int PARENT_SAVE_DISABLED = 0x20000000;
960
961 /**
962 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
963 * {@hide}
964 */
965 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
966
967 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700968 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
969 * should add all focusable Views regardless if they are focusable in touch mode.
970 */
971 public static final int FOCUSABLES_ALL = 0x00000000;
972
973 /**
974 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
975 * should add only Views focusable in touch mode.
976 */
977 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
978
979 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -0700980 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
981 * should add only accessibility focusable Views.
982 *
983 * @hide
984 */
985 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
986
987 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700988 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 * item.
990 */
991 public static final int FOCUS_BACKWARD = 0x00000001;
992
993 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700994 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 * item.
996 */
997 public static final int FOCUS_FORWARD = 0x00000002;
998
999 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001000 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 */
1002 public static final int FOCUS_LEFT = 0x00000011;
1003
1004 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001005 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 */
1007 public static final int FOCUS_UP = 0x00000021;
1008
1009 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001010 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 */
1012 public static final int FOCUS_RIGHT = 0x00000042;
1013
1014 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001015 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 */
1017 public static final int FOCUS_DOWN = 0x00000082;
1018
Svetoslav Ganov42138042012-03-20 11:51:39 -07001019 // Accessibility focus directions.
1020
1021 /**
1022 * The accessibility focus which is the current user position when
1023 * interacting with the accessibility framework.
1024 */
1025 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1026
1027 /**
1028 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1029 */
1030 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1031
1032 /**
1033 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1034 */
1035 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1036
1037 /**
1038 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1039 */
1040 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1041
1042 /**
1043 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1044 */
1045 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1046
1047 /**
1048 * Use with {@link #focusSearch(int)}. Move acessibility focus to the next view.
1049 */
1050 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1051
1052 /**
1053 * Use with {@link #focusSearch(int)}. Move acessibility focus to the previous view.
1054 */
1055 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1056
1057 /**
1058 * Use with {@link #focusSearch(int)}. Move acessibility focus in a view.
1059 */
1060 public static final int ACCESSIBILITY_FOCUS_IN = 0x00000004 | FOCUS_ACCESSIBILITY;
1061
1062 /**
1063 * Use with {@link #focusSearch(int)}. Move acessibility focus out of a view.
1064 */
1065 public static final int ACCESSIBILITY_FOCUS_OUT = 0x00000008 | FOCUS_ACCESSIBILITY;
1066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001068 * Bits of {@link #getMeasuredWidthAndState()} and
1069 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1070 */
1071 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1072
1073 /**
1074 * Bits of {@link #getMeasuredWidthAndState()} and
1075 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1076 */
1077 public static final int MEASURED_STATE_MASK = 0xff000000;
1078
1079 /**
1080 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1081 * for functions that combine both width and height into a single int,
1082 * such as {@link #getMeasuredState()} and the childState argument of
1083 * {@link #resolveSizeAndState(int, int, int)}.
1084 */
1085 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1086
1087 /**
1088 * Bit of {@link #getMeasuredWidthAndState()} and
1089 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1090 * is smaller that the space the view would like to have.
1091 */
1092 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1093
1094 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 * Base View state sets
1096 */
1097 // Singles
1098 /**
1099 * Indicates the view has no states set. States are used with
1100 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1101 * view depending on its state.
1102 *
1103 * @see android.graphics.drawable.Drawable
1104 * @see #getDrawableState()
1105 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001106 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 /**
1108 * Indicates the view is enabled. States are used with
1109 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1110 * view depending on its state.
1111 *
1112 * @see android.graphics.drawable.Drawable
1113 * @see #getDrawableState()
1114 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001115 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 /**
1117 * Indicates the view is focused. States are used with
1118 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1119 * view depending on its state.
1120 *
1121 * @see android.graphics.drawable.Drawable
1122 * @see #getDrawableState()
1123 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001124 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 /**
1126 * Indicates the view is selected. States are used with
1127 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1128 * view depending on its state.
1129 *
1130 * @see android.graphics.drawable.Drawable
1131 * @see #getDrawableState()
1132 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001133 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 /**
1135 * Indicates the view is pressed. States are used with
1136 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1137 * view depending on its state.
1138 *
1139 * @see android.graphics.drawable.Drawable
1140 * @see #getDrawableState()
1141 * @hide
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view's window has focus. States are used with
1146 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1147 * view depending on its state.
1148 *
1149 * @see android.graphics.drawable.Drawable
1150 * @see #getDrawableState()
1151 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001152 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 // Doubles
1154 /**
1155 * Indicates the view is enabled and has the focus.
1156 *
1157 * @see #ENABLED_STATE_SET
1158 * @see #FOCUSED_STATE_SET
1159 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001160 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 /**
1162 * Indicates the view is enabled and selected.
1163 *
1164 * @see #ENABLED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001167 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 /**
1169 * Indicates the view is enabled and that its window has focus.
1170 *
1171 * @see #ENABLED_STATE_SET
1172 * @see #WINDOW_FOCUSED_STATE_SET
1173 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001174 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 /**
1176 * Indicates the view is focused and selected.
1177 *
1178 * @see #FOCUSED_STATE_SET
1179 * @see #SELECTED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 /**
1183 * Indicates the view has the focus and that its window has the focus.
1184 *
1185 * @see #FOCUSED_STATE_SET
1186 * @see #WINDOW_FOCUSED_STATE_SET
1187 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001188 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 /**
1190 * Indicates the view is selected and that its window has the focus.
1191 *
1192 * @see #SELECTED_STATE_SET
1193 * @see #WINDOW_FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 // Triples
1197 /**
1198 * Indicates the view is enabled, focused and selected.
1199 *
1200 * @see #ENABLED_STATE_SET
1201 * @see #FOCUSED_STATE_SET
1202 * @see #SELECTED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is enabled, focused and its window has the focus.
1207 *
1208 * @see #ENABLED_STATE_SET
1209 * @see #FOCUSED_STATE_SET
1210 * @see #WINDOW_FOCUSED_STATE_SET
1211 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001212 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 /**
1214 * Indicates the view is enabled, selected and its window has the focus.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #SELECTED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is focused, selected and its window has the focus.
1223 *
1224 * @see #FOCUSED_STATE_SET
1225 * @see #SELECTED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, focused, selected and its window
1231 * has the focus.
1232 *
1233 * @see #ENABLED_STATE_SET
1234 * @see #FOCUSED_STATE_SET
1235 * @see #SELECTED_STATE_SET
1236 * @see #WINDOW_FOCUSED_STATE_SET
1237 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001238 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 /**
1240 * Indicates the view is pressed and its window has the focus.
1241 *
1242 * @see #PRESSED_STATE_SET
1243 * @see #WINDOW_FOCUSED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed and selected.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #SELECTED_STATE_SET
1251 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001252 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * Indicates the view is pressed, selected and its window has the focus.
1255 *
1256 * @see #PRESSED_STATE_SET
1257 * @see #SELECTED_STATE_SET
1258 * @see #WINDOW_FOCUSED_STATE_SET
1259 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001260 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 /**
1262 * Indicates the view is pressed and focused.
1263 *
1264 * @see #PRESSED_STATE_SET
1265 * @see #FOCUSED_STATE_SET
1266 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001267 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * Indicates the view is pressed, focused and its window has the focus.
1270 *
1271 * @see #PRESSED_STATE_SET
1272 * @see #FOCUSED_STATE_SET
1273 * @see #WINDOW_FOCUSED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed, focused and selected.
1278 *
1279 * @see #PRESSED_STATE_SET
1280 * @see #SELECTED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused, selected and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #WINDOW_FOCUSED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed and enabled.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, enabled and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #ENABLED_STATE_SET
1305 * @see #WINDOW_FOCUSED_STATE_SET
1306 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 /**
1309 * Indicates the view is pressed, enabled and selected.
1310 *
1311 * @see #PRESSED_STATE_SET
1312 * @see #ENABLED_STATE_SET
1313 * @see #SELECTED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled, selected and its window has the
1318 * focus.
1319 *
1320 * @see #PRESSED_STATE_SET
1321 * @see #ENABLED_STATE_SET
1322 * @see #SELECTED_STATE_SET
1323 * @see #WINDOW_FOCUSED_STATE_SET
1324 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 /**
1327 * Indicates the view is pressed, enabled and focused.
1328 *
1329 * @see #PRESSED_STATE_SET
1330 * @see #ENABLED_STATE_SET
1331 * @see #FOCUSED_STATE_SET
1332 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 /**
1335 * Indicates the view is pressed, enabled, focused and its window has the
1336 * focus.
1337 *
1338 * @see #PRESSED_STATE_SET
1339 * @see #ENABLED_STATE_SET
1340 * @see #FOCUSED_STATE_SET
1341 * @see #WINDOW_FOCUSED_STATE_SET
1342 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 /**
1345 * Indicates the view is pressed, enabled, focused and selected.
1346 *
1347 * @see #PRESSED_STATE_SET
1348 * @see #ENABLED_STATE_SET
1349 * @see #SELECTED_STATE_SET
1350 * @see #FOCUSED_STATE_SET
1351 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001352 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 /**
1354 * Indicates the view is pressed, enabled, focused, selected and its window
1355 * has the focus.
1356 *
1357 * @see #PRESSED_STATE_SET
1358 * @see #ENABLED_STATE_SET
1359 * @see #SELECTED_STATE_SET
1360 * @see #FOCUSED_STATE_SET
1361 * @see #WINDOW_FOCUSED_STATE_SET
1362 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001363 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364
1365 /**
1366 * The order here is very important to {@link #getDrawableState()}
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 private static final int[][] VIEW_STATE_SETS;
1369
Romain Guyb051e892010-09-28 19:09:36 -07001370 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1371 static final int VIEW_STATE_SELECTED = 1 << 1;
1372 static final int VIEW_STATE_FOCUSED = 1 << 2;
1373 static final int VIEW_STATE_ENABLED = 1 << 3;
1374 static final int VIEW_STATE_PRESSED = 1 << 4;
1375 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001376 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001377 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001378 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1379 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380
1381 static final int[] VIEW_STATE_IDS = new int[] {
1382 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1383 R.attr.state_selected, VIEW_STATE_SELECTED,
1384 R.attr.state_focused, VIEW_STATE_FOCUSED,
1385 R.attr.state_enabled, VIEW_STATE_ENABLED,
1386 R.attr.state_pressed, VIEW_STATE_PRESSED,
1387 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001388 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001389 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001390 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001391 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 };
1393
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001395 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1396 throw new IllegalStateException(
1397 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1398 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001399 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001400 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001401 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001402 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001403 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001404 orderedIds[i * 2] = viewState;
1405 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001406 }
1407 }
1408 }
Romain Guyb051e892010-09-28 19:09:36 -07001409 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1410 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1411 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001412 int numBits = Integer.bitCount(i);
1413 int[] set = new int[numBits];
1414 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001415 for (int j = 0; j < orderedIds.length; j += 2) {
1416 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 set[pos++] = orderedIds[j];
1418 }
1419 }
1420 VIEW_STATE_SETS[i] = set;
1421 }
1422
1423 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1424 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1425 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1426 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1428 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1429 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1431 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1433 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1435 | VIEW_STATE_FOCUSED];
1436 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1437 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1439 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1441 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1443 | VIEW_STATE_ENABLED];
1444 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1446 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1448 | VIEW_STATE_ENABLED];
1449 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1451 | VIEW_STATE_ENABLED];
1452 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1454 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1455
1456 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1457 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1459 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1461 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1463 | VIEW_STATE_PRESSED];
1464 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1466 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1468 | VIEW_STATE_PRESSED];
1469 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1471 | VIEW_STATE_PRESSED];
1472 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1474 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1482 | VIEW_STATE_PRESSED];
1483 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1484 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1485 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1486 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1487 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1488 | VIEW_STATE_PRESSED];
1489 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1490 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1491 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1492 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1493 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1494 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1495 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1496 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1497 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 }
1500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001502 * Accessibility event types that are dispatched for text population.
1503 */
1504 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1505 AccessibilityEvent.TYPE_VIEW_CLICKED
1506 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1507 | AccessibilityEvent.TYPE_VIEW_SELECTED
1508 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1509 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1510 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001511 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001512 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001513 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
1514 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001515
1516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 * Temporary Rect currently for use in setBackground(). This will probably
1518 * be extended in the future to hold our own class with more than just
1519 * a Rect. :)
1520 */
1521 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001522
1523 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001524 * Temporary flag, used to enable processing of View properties in the native DisplayList
1525 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1526 * apps.
1527 * @hide
1528 */
Chet Haase8d56b0e2012-04-02 16:34:48 -07001529 public static final boolean USE_DISPLAY_LIST_PROPERTIES = true;
Chet Haasea1cff502012-02-21 13:43:44 -08001530
1531 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001532 * Map used to store views' tags.
1533 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001534 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001537 * The next available accessiiblity id.
1538 */
1539 private static int sNextAccessibilityViewId;
1540
1541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 * The animation currently associated with this view.
1543 * @hide
1544 */
1545 protected Animation mCurrentAnimation = null;
1546
1547 /**
1548 * Width as measured during measure pass.
1549 * {@hide}
1550 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001551 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001552 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553
1554 /**
1555 * Height as measured during measure pass.
1556 * {@hide}
1557 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001558 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001559 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560
1561 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001562 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1563 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1564 * its display list. This flag, used only when hw accelerated, allows us to clear the
1565 * flag while retaining this information until it's needed (at getDisplayList() time and
1566 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1567 *
1568 * {@hide}
1569 */
1570 boolean mRecreateDisplayList = false;
1571
1572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 * The view's identifier.
1574 * {@hide}
1575 *
1576 * @see #setId(int)
1577 * @see #getId()
1578 */
1579 @ViewDebug.ExportedProperty(resolveId = true)
1580 int mID = NO_ID;
1581
1582 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001583 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001584 */
1585 int mAccessibilityViewId = NO_ID;
1586
1587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 * The view's tag.
1589 * {@hide}
1590 *
1591 * @see #setTag(Object)
1592 * @see #getTag()
1593 */
1594 protected Object mTag;
1595
1596 // for mPrivateFlags:
1597 /** {@hide} */
1598 static final int WANTS_FOCUS = 0x00000001;
1599 /** {@hide} */
1600 static final int FOCUSED = 0x00000002;
1601 /** {@hide} */
1602 static final int SELECTED = 0x00000004;
1603 /** {@hide} */
1604 static final int IS_ROOT_NAMESPACE = 0x00000008;
1605 /** {@hide} */
1606 static final int HAS_BOUNDS = 0x00000010;
1607 /** {@hide} */
1608 static final int DRAWN = 0x00000020;
1609 /**
1610 * When this flag is set, this view is running an animation on behalf of its
1611 * children and should therefore not cancel invalidate requests, even if they
1612 * lie outside of this view's bounds.
1613 *
1614 * {@hide}
1615 */
1616 static final int DRAW_ANIMATION = 0x00000040;
1617 /** {@hide} */
1618 static final int SKIP_DRAW = 0x00000080;
1619 /** {@hide} */
1620 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1621 /** {@hide} */
1622 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1623 /** {@hide} */
1624 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1625 /** {@hide} */
1626 static final int MEASURED_DIMENSION_SET = 0x00000800;
1627 /** {@hide} */
1628 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001629 /** {@hide} */
1630 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
1632 private static final int PRESSED = 0x00004000;
1633
1634 /** {@hide} */
1635 static final int DRAWING_CACHE_VALID = 0x00008000;
1636 /**
1637 * Flag used to indicate that this view should be drawn once more (and only once
1638 * more) after its animation has completed.
1639 * {@hide}
1640 */
1641 static final int ANIMATION_STARTED = 0x00010000;
1642
1643 private static final int SAVE_STATE_CALLED = 0x00020000;
1644
1645 /**
1646 * Indicates that the View returned true when onSetAlpha() was called and that
1647 * the alpha must be restored.
1648 * {@hide}
1649 */
1650 static final int ALPHA_SET = 0x00040000;
1651
1652 /**
1653 * Set by {@link #setScrollContainer(boolean)}.
1654 */
1655 static final int SCROLL_CONTAINER = 0x00080000;
1656
1657 /**
1658 * Set by {@link #setScrollContainer(boolean)}.
1659 */
1660 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1661
1662 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001663 * View flag indicating whether this view was invalidated (fully or partially.)
1664 *
1665 * @hide
1666 */
1667 static final int DIRTY = 0x00200000;
1668
1669 /**
1670 * View flag indicating whether this view was invalidated by an opaque
1671 * invalidate request.
1672 *
1673 * @hide
1674 */
1675 static final int DIRTY_OPAQUE = 0x00400000;
1676
1677 /**
1678 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1679 *
1680 * @hide
1681 */
1682 static final int DIRTY_MASK = 0x00600000;
1683
1684 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001685 * Indicates whether the background is opaque.
1686 *
1687 * @hide
1688 */
1689 static final int OPAQUE_BACKGROUND = 0x00800000;
1690
1691 /**
1692 * Indicates whether the scrollbars are opaque.
1693 *
1694 * @hide
1695 */
1696 static final int OPAQUE_SCROLLBARS = 0x01000000;
1697
1698 /**
1699 * Indicates whether the view is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001704
Adam Powelle14579b2009-12-16 18:39:52 -08001705 /**
1706 * Indicates a prepressed state;
1707 * the short time between ACTION_DOWN and recognizing
1708 * a 'real' press. Prepressed is used to recognize quick taps
1709 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001710 *
Adam Powelle14579b2009-12-16 18:39:52 -08001711 * @hide
1712 */
1713 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001714
Adam Powellc9fbaab2010-02-16 17:16:19 -08001715 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001716 * Indicates whether the view is temporarily detached.
1717 *
1718 * @hide
1719 */
1720 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001721
Adam Powell8568c3a2010-04-19 14:26:11 -07001722 /**
1723 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001725 * @hide
1726 */
1727 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001728
1729 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001730 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1731 * @hide
1732 */
1733 private static final int HOVERED = 0x10000000;
1734
1735 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001736 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1737 * for transform operations
1738 *
1739 * @hide
1740 */
Adam Powellf37df072010-09-17 16:22:49 -07001741 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001742
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001743 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001744 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001745
Chet Haasefd2b0022010-08-06 13:08:56 -07001746 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001747 * Indicates that this view was specifically invalidated, not just dirtied because some
1748 * child view was invalidated. The flag is used to determine when we need to recreate
1749 * a view's display list (as opposed to just returning a reference to its existing
1750 * display list).
1751 *
1752 * @hide
1753 */
1754 static final int INVALIDATED = 0x80000000;
1755
Christopher Tate3d4bf172011-03-28 16:16:46 -07001756 /* Masks for mPrivateFlags2 */
1757
1758 /**
1759 * Indicates that this view has reported that it can accept the current drag's content.
1760 * Cleared when the drag operation concludes.
1761 * @hide
1762 */
1763 static final int DRAG_CAN_ACCEPT = 0x00000001;
1764
1765 /**
1766 * Indicates that this view is currently directly under the drag location in a
1767 * drag-and-drop operation involving content that it can accept. Cleared when
1768 * the drag exits the view, or when the drag operation concludes.
1769 * @hide
1770 */
1771 static final int DRAG_HOVERED = 0x00000002;
1772
Cibu Johny86666632010-02-22 13:01:02 -08001773 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001774 * Horizontal layout direction of this view is from Left to Right.
1775 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001776 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001777 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001778
1779 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001780 * Horizontal layout direction of this view is from Right to Left.
1781 * Use with {@link #setLayoutDirection}.
1782 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001783 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001784
1785 /**
1786 * Horizontal layout direction of this view is inherited from its parent.
1787 * Use with {@link #setLayoutDirection}.
1788 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001789 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001790
1791 /**
1792 * Horizontal layout direction of this view is from deduced from the default language
1793 * script for the locale. Use with {@link #setLayoutDirection}.
1794 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001795 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001796
1797 /**
1798 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001799 * @hide
1800 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001801 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1802
1803 /**
1804 * Mask for use with private flags indicating bits used for horizontal layout direction.
1805 * @hide
1806 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001807 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001808
1809 /**
1810 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1811 * right-to-left direction.
1812 * @hide
1813 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001814 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001815
1816 /**
1817 * Indicates whether the view horizontal layout direction has been resolved.
1818 * @hide
1819 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001820 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001821
1822 /**
1823 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1824 * @hide
1825 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001826 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001827
1828 /*
1829 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1830 * flag value.
1831 * @hide
1832 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001833 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1834 LAYOUT_DIRECTION_LTR,
1835 LAYOUT_DIRECTION_RTL,
1836 LAYOUT_DIRECTION_INHERIT,
1837 LAYOUT_DIRECTION_LOCALE
1838 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Default horizontal layout direction.
1842 * @hide
1843 */
1844 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001845
Adam Powell539ee872012-02-03 19:00:49 -08001846 /**
1847 * Indicates that the view is tracking some sort of transient state
1848 * that the app should not need to be aware of, but that the framework
1849 * should take special care to preserve.
1850 *
1851 * @hide
1852 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001853 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001854
1855
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001856 /**
1857 * Text direction is inherited thru {@link ViewGroup}
1858 */
1859 public static final int TEXT_DIRECTION_INHERIT = 0;
1860
1861 /**
1862 * Text direction is using "first strong algorithm". The first strong directional character
1863 * determines the paragraph direction. If there is no strong directional character, the
1864 * paragraph direction is the view's resolved layout direction.
1865 */
1866 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1867
1868 /**
1869 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1870 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1871 * If there are neither, the paragraph direction is the view's resolved layout direction.
1872 */
1873 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1874
1875 /**
1876 * Text direction is forced to LTR.
1877 */
1878 public static final int TEXT_DIRECTION_LTR = 3;
1879
1880 /**
1881 * Text direction is forced to RTL.
1882 */
1883 public static final int TEXT_DIRECTION_RTL = 4;
1884
1885 /**
1886 * Text direction is coming from the system Locale.
1887 */
1888 public static final int TEXT_DIRECTION_LOCALE = 5;
1889
1890 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001891 * Default text direction is inherited
1892 */
1893 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1894
1895 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001896 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1897 * @hide
1898 */
1899 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1900
1901 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001902 * Mask for use with private flags indicating bits used for text direction.
1903 * @hide
1904 */
1905 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1906
1907 /**
1908 * Array of text direction flags for mapping attribute "textDirection" to correct
1909 * flag value.
1910 * @hide
1911 */
1912 private static final int[] TEXT_DIRECTION_FLAGS = {
1913 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1914 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1915 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1916 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1917 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1918 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1919 };
1920
1921 /**
1922 * Indicates whether the view text direction has been resolved.
1923 * @hide
1924 */
1925 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1926
1927 /**
1928 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1929 * @hide
1930 */
1931 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1932
1933 /**
1934 * Mask for use with private flags indicating bits used for resolved text direction.
1935 * @hide
1936 */
1937 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1938
1939 /**
1940 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1941 * @hide
1942 */
1943 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1944 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1945
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001946 /*
1947 * Default text alignment. The text alignment of this View is inherited from its parent.
1948 * Use with {@link #setTextAlignment(int)}
1949 */
1950 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1951
1952 /**
1953 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1954 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1955 *
1956 * Use with {@link #setTextAlignment(int)}
1957 */
1958 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1959
1960 /**
1961 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1962 *
1963 * Use with {@link #setTextAlignment(int)}
1964 */
1965 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1966
1967 /**
1968 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1969 *
1970 * Use with {@link #setTextAlignment(int)}
1971 */
1972 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1973
1974 /**
1975 * Center the paragraph, e.g. ALIGN_CENTER.
1976 *
1977 * Use with {@link #setTextAlignment(int)}
1978 */
1979 public static final int TEXT_ALIGNMENT_CENTER = 4;
1980
1981 /**
1982 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1983 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1984 *
1985 * Use with {@link #setTextAlignment(int)}
1986 */
1987 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1988
1989 /**
1990 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1991 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1992 *
1993 * Use with {@link #setTextAlignment(int)}
1994 */
1995 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1996
1997 /**
1998 * Default text alignment is inherited
1999 */
2000 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2001
2002 /**
2003 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2004 * @hide
2005 */
2006 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2007
2008 /**
2009 * Mask for use with private flags indicating bits used for text alignment.
2010 * @hide
2011 */
2012 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2013
2014 /**
2015 * Array of text direction flags for mapping attribute "textAlignment" to correct
2016 * flag value.
2017 * @hide
2018 */
2019 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2020 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2021 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2022 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2023 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2024 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2025 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2026 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2027 };
2028
2029 /**
2030 * Indicates whether the view text alignment has been resolved.
2031 * @hide
2032 */
2033 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2034
2035 /**
2036 * Bit shift to get the resolved text alignment.
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2046
2047 /**
2048 * Indicates whether if the view text alignment has been resolved to gravity
2049 */
2050 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2051 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2052
Svetoslav Ganov42138042012-03-20 11:51:39 -07002053 // Accessiblity constants for mPrivateFlags2
2054
2055 /**
2056 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2057 */
2058 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2059
2060 /**
2061 * Automatically determine whether a view is important for accessibility.
2062 */
2063 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2064
2065 /**
2066 * The view is important for accessibility.
2067 */
2068 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2069
2070 /**
2071 * The view is not important for accessibility.
2072 */
2073 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2074
2075 /**
2076 * The default whether the view is important for accessiblity.
2077 */
2078 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2079
2080 /**
2081 * Mask for obtainig the bits which specify how to determine
2082 * whether a view is important for accessibility.
2083 */
2084 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2085 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2086 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2087
2088 /**
2089 * Flag indicating whether a view has accessibility focus.
2090 */
2091 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2092
2093 /**
2094 * Flag indicating whether a view state for accessibility has changed.
2095 */
2096 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002097
Christopher Tate3d4bf172011-03-28 16:16:46 -07002098 /* End of masks for mPrivateFlags2 */
2099
2100 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2101
Chet Haasedaf98e92011-01-10 14:10:36 -08002102 /**
Adam Powell637d3372010-08-25 14:37:03 -07002103 * Always allow a user to over-scroll this view, provided it is a
2104 * view that can scroll.
2105 *
2106 * @see #getOverScrollMode()
2107 * @see #setOverScrollMode(int)
2108 */
2109 public static final int OVER_SCROLL_ALWAYS = 0;
2110
2111 /**
2112 * Allow a user to over-scroll this view only if the content is large
2113 * enough to meaningfully scroll, provided it is a view that can scroll.
2114 *
2115 * @see #getOverScrollMode()
2116 * @see #setOverScrollMode(int)
2117 */
2118 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2119
2120 /**
2121 * Never allow a user to over-scroll this view.
2122 *
2123 * @see #getOverScrollMode()
2124 * @see #setOverScrollMode(int)
2125 */
2126 public static final int OVER_SCROLL_NEVER = 2;
2127
2128 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002129 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2130 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002131 *
Joe Malin32736f02011-01-19 16:14:20 -08002132 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002133 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002134 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002135
2136 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002137 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2138 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002139 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002140 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002141 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002142 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002143 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002144 *
Joe Malin32736f02011-01-19 16:14:20 -08002145 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002146 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002147 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2148
2149 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002150 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2151 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002152 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002153 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002154 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2155 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2156 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002157 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002158 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2159 * window flags) for displaying content using every last pixel on the display.
2160 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002161 * <p>There is a limitation: because navigation controls are so important, the least user
2162 * interaction will cause them to reappear immediately. When this happens, both
2163 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2164 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002165 *
2166 * @see #setSystemUiVisibility(int)
2167 */
2168 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2169
2170 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002171 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2172 * into the normal fullscreen mode so that its content can take over the screen
2173 * while still allowing the user to interact with the application.
2174 *
2175 * <p>This has the same visual effect as
2176 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2177 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2178 * meaning that non-critical screen decorations (such as the status bar) will be
2179 * hidden while the user is in the View's window, focusing the experience on
2180 * that content. Unlike the window flag, if you are using ActionBar in
2181 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2182 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2183 * hide the action bar.
2184 *
2185 * <p>This approach to going fullscreen is best used over the window flag when
2186 * it is a transient state -- that is, the application does this at certain
2187 * points in its user interaction where it wants to allow the user to focus
2188 * on content, but not as a continuous state. For situations where the application
2189 * would like to simply stay full screen the entire time (such as a game that
2190 * wants to take over the screen), the
2191 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2192 * is usually a better approach. The state set here will be removed by the system
2193 * in various situations (such as the user moving to another application) like
2194 * the other system UI states.
2195 *
2196 * <p>When using this flag, the application should provide some easy facility
2197 * for the user to go out of it. A common example would be in an e-book
2198 * reader, where tapping on the screen brings back whatever screen and UI
2199 * decorations that had been hidden while the user was immersed in reading
2200 * the book.
2201 *
2202 * @see #setSystemUiVisibility(int)
2203 */
2204 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2205
2206 /**
2207 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2208 * flags, we would like a stable view of the content insets given to
2209 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2210 * will always represent the worst case that the application can expect
2211 * as a continue state. In practice this means with any of system bar,
2212 * nav bar, and status bar shown, but not the space that would be needed
2213 * for an input method.
2214 *
2215 * <p>If you are using ActionBar in
2216 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2217 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2218 * insets it adds to those given to the application.
2219 */
2220 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2221
2222 /**
2223 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2224 * to be layed out as if it has requested
2225 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2226 * allows it to avoid artifacts when switching in and out of that mode, at
2227 * the expense that some of its user interface may be covered by screen
2228 * decorations when they are shown. You can perform layout of your inner
2229 * UI elements to account for the navagation system UI through the
2230 * {@link #fitSystemWindows(Rect)} method.
2231 */
2232 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2233
2234 /**
2235 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2236 * to be layed out as if it has requested
2237 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2238 * allows it to avoid artifacts when switching in and out of that mode, at
2239 * the expense that some of its user interface may be covered by screen
2240 * decorations when they are shown. You can perform layout of your inner
2241 * UI elements to account for non-fullscreen system UI through the
2242 * {@link #fitSystemWindows(Rect)} method.
2243 */
2244 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2245
2246 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002247 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2248 */
2249 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2250
2251 /**
2252 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2253 */
2254 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002255
2256 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002257 * @hide
2258 *
2259 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2260 * out of the public fields to keep the undefined bits out of the developer's way.
2261 *
2262 * Flag to make the status bar not expandable. Unless you also
2263 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2264 */
2265 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2266
2267 /**
2268 * @hide
2269 *
2270 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2271 * out of the public fields to keep the undefined bits out of the developer's way.
2272 *
2273 * Flag to hide notification icons and scrolling ticker text.
2274 */
2275 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2276
2277 /**
2278 * @hide
2279 *
2280 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2281 * out of the public fields to keep the undefined bits out of the developer's way.
2282 *
2283 * Flag to disable incoming notification alerts. This will not block
2284 * icons, but it will block sound, vibrating and other visual or aural notifications.
2285 */
2286 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2287
2288 /**
2289 * @hide
2290 *
2291 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2292 * out of the public fields to keep the undefined bits out of the developer's way.
2293 *
2294 * Flag to hide only the scrolling ticker. Note that
2295 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2296 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2297 */
2298 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2299
2300 /**
2301 * @hide
2302 *
2303 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2304 * out of the public fields to keep the undefined bits out of the developer's way.
2305 *
2306 * Flag to hide the center system info area.
2307 */
2308 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2309
2310 /**
2311 * @hide
2312 *
2313 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2314 * out of the public fields to keep the undefined bits out of the developer's way.
2315 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002316 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002317 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2318 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002319 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002320
2321 /**
2322 * @hide
2323 *
2324 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2325 * out of the public fields to keep the undefined bits out of the developer's way.
2326 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002327 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002328 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2329 */
2330 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2331
2332 /**
2333 * @hide
2334 *
2335 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2336 * out of the public fields to keep the undefined bits out of the developer's way.
2337 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002338 * Flag to hide only the clock. You might use this if your activity has
2339 * its own clock making the status bar's clock redundant.
2340 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002341 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2342
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002343 /**
2344 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002345 *
2346 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2347 * out of the public fields to keep the undefined bits out of the developer's way.
2348 *
2349 * Flag to hide only the recent apps button. Don't use this
2350 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2351 */
2352 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2353
2354 /**
2355 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002356 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002357 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002358
2359 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002360 * These are the system UI flags that can be cleared by events outside
2361 * of an application. Currently this is just the ability to tap on the
2362 * screen while hiding the navigation bar to have it return.
2363 * @hide
2364 */
2365 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002366 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2367 | SYSTEM_UI_FLAG_FULLSCREEN;
2368
2369 /**
2370 * Flags that can impact the layout in relation to system UI.
2371 */
2372 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2373 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2374 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002375
2376 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002377 * Find views that render the specified text.
2378 *
2379 * @see #findViewsWithText(ArrayList, CharSequence, int)
2380 */
2381 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2382
2383 /**
2384 * Find find views that contain the specified content description.
2385 *
2386 * @see #findViewsWithText(ArrayList, CharSequence, int)
2387 */
2388 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2389
2390 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002391 * Find views that contain {@link AccessibilityNodeProvider}. Such
2392 * a View is a root of virtual view hierarchy and may contain the searched
2393 * text. If this flag is set Views with providers are automatically
2394 * added and it is a responsibility of the client to call the APIs of
2395 * the provider to determine whether the virtual tree rooted at this View
2396 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2397 * represeting the virtual views with this text.
2398 *
2399 * @see #findViewsWithText(ArrayList, CharSequence, int)
2400 *
2401 * @hide
2402 */
2403 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2404
2405 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002406 * Indicates that the screen has changed state and is now off.
2407 *
2408 * @see #onScreenStateChanged(int)
2409 */
2410 public static final int SCREEN_STATE_OFF = 0x0;
2411
2412 /**
2413 * Indicates that the screen has changed state and is now on.
2414 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002415 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002416 */
2417 public static final int SCREEN_STATE_ON = 0x1;
2418
2419 /**
Adam Powell637d3372010-08-25 14:37:03 -07002420 * Controls the over-scroll mode for this view.
2421 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2422 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2423 * and {@link #OVER_SCROLL_NEVER}.
2424 */
2425 private int mOverScrollMode;
2426
2427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 * The parent this view is attached to.
2429 * {@hide}
2430 *
2431 * @see #getParent()
2432 */
2433 protected ViewParent mParent;
2434
2435 /**
2436 * {@hide}
2437 */
2438 AttachInfo mAttachInfo;
2439
2440 /**
2441 * {@hide}
2442 */
Romain Guy809a7f62009-05-14 15:44:42 -07002443 @ViewDebug.ExportedProperty(flagMapping = {
2444 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2445 name = "FORCE_LAYOUT"),
2446 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2447 name = "LAYOUT_REQUIRED"),
2448 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002449 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002450 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2451 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2452 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2453 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2454 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002456 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457
2458 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002459 * This view's request for the visibility of the status bar.
2460 * @hide
2461 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002462 @ViewDebug.ExportedProperty(flagMapping = {
2463 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2464 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2465 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2466 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2467 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2468 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2469 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2470 equals = SYSTEM_UI_FLAG_VISIBLE,
2471 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2472 })
Joe Onorato664644d2011-01-23 17:53:23 -08002473 int mSystemUiVisibility;
2474
2475 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002476 * Reference count for transient state.
2477 * @see #setHasTransientState(boolean)
2478 */
2479 int mTransientStateCount = 0;
2480
2481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 * Count of how many windows this view has been attached to.
2483 */
2484 int mWindowAttachCount;
2485
2486 /**
2487 * The layout parameters associated with this view and used by the parent
2488 * {@link android.view.ViewGroup} to determine how this view should be
2489 * laid out.
2490 * {@hide}
2491 */
2492 protected ViewGroup.LayoutParams mLayoutParams;
2493
2494 /**
2495 * The view flags hold various views states.
2496 * {@hide}
2497 */
2498 @ViewDebug.ExportedProperty
2499 int mViewFlags;
2500
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002501 static class TransformationInfo {
2502 /**
2503 * The transform matrix for the View. This transform is calculated internally
2504 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2505 * is used by default. Do *not* use this variable directly; instead call
2506 * getMatrix(), which will automatically recalculate the matrix if necessary
2507 * to get the correct matrix based on the latest rotation and scale properties.
2508 */
2509 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002510
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002511 /**
2512 * The transform matrix for the View. This transform is calculated internally
2513 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2514 * is used by default. Do *not* use this variable directly; instead call
2515 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2516 * to get the correct matrix based on the latest rotation and scale properties.
2517 */
2518 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002519
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002520 /**
2521 * An internal variable that tracks whether we need to recalculate the
2522 * transform matrix, based on whether the rotation or scaleX/Y properties
2523 * have changed since the matrix was last calculated.
2524 */
2525 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002526
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002527 /**
2528 * An internal variable that tracks whether we need to recalculate the
2529 * transform matrix, based on whether the rotation or scaleX/Y properties
2530 * have changed since the matrix was last calculated.
2531 */
2532 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002533
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002534 /**
2535 * A variable that tracks whether we need to recalculate the
2536 * transform matrix, based on whether the rotation or scaleX/Y properties
2537 * have changed since the matrix was last calculated. This variable
2538 * is only valid after a call to updateMatrix() or to a function that
2539 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2540 */
2541 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002542
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002543 /**
2544 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2545 */
2546 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002547
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002548 /**
2549 * This matrix is used when computing the matrix for 3D rotations.
2550 */
2551 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002553 /**
2554 * These prev values are used to recalculate a centered pivot point when necessary. The
2555 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2556 * set), so thes values are only used then as well.
2557 */
2558 private int mPrevWidth = -1;
2559 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002560
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002561 /**
2562 * The degrees rotation around the vertical axis through the pivot point.
2563 */
2564 @ViewDebug.ExportedProperty
2565 float mRotationY = 0f;
2566
2567 /**
2568 * The degrees rotation around the horizontal axis through the pivot point.
2569 */
2570 @ViewDebug.ExportedProperty
2571 float mRotationX = 0f;
2572
2573 /**
2574 * The degrees rotation around the pivot point.
2575 */
2576 @ViewDebug.ExportedProperty
2577 float mRotation = 0f;
2578
2579 /**
2580 * The amount of translation of the object away from its left property (post-layout).
2581 */
2582 @ViewDebug.ExportedProperty
2583 float mTranslationX = 0f;
2584
2585 /**
2586 * The amount of translation of the object away from its top property (post-layout).
2587 */
2588 @ViewDebug.ExportedProperty
2589 float mTranslationY = 0f;
2590
2591 /**
2592 * The amount of scale in the x direction around the pivot point. A
2593 * value of 1 means no scaling is applied.
2594 */
2595 @ViewDebug.ExportedProperty
2596 float mScaleX = 1f;
2597
2598 /**
2599 * The amount of scale in the y direction around the pivot point. A
2600 * value of 1 means no scaling is applied.
2601 */
2602 @ViewDebug.ExportedProperty
2603 float mScaleY = 1f;
2604
2605 /**
Chet Haasea33de552012-02-03 16:28:24 -08002606 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002607 */
2608 @ViewDebug.ExportedProperty
2609 float mPivotX = 0f;
2610
2611 /**
Chet Haasea33de552012-02-03 16:28:24 -08002612 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002613 */
2614 @ViewDebug.ExportedProperty
2615 float mPivotY = 0f;
2616
2617 /**
2618 * The opacity of the View. This is a value from 0 to 1, where 0 means
2619 * completely transparent and 1 means completely opaque.
2620 */
2621 @ViewDebug.ExportedProperty
2622 float mAlpha = 1f;
2623 }
2624
2625 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002626
Joe Malin32736f02011-01-19 16:14:20 -08002627 private boolean mLastIsOpaque;
2628
Chet Haasefd2b0022010-08-06 13:08:56 -07002629 /**
2630 * Convenience value to check for float values that are close enough to zero to be considered
2631 * zero.
2632 */
Romain Guy2542d192010-08-18 11:47:12 -07002633 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002634
2635 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 * The distance in pixels from the left edge of this view's parent
2637 * to the left edge of this view.
2638 * {@hide}
2639 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002640 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 protected int mLeft;
2642 /**
2643 * The distance in pixels from the left edge of this view's parent
2644 * to the right edge of this view.
2645 * {@hide}
2646 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002647 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 protected int mRight;
2649 /**
2650 * The distance in pixels from the top edge of this view's parent
2651 * to the top edge of this view.
2652 * {@hide}
2653 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002654 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 protected int mTop;
2656 /**
2657 * The distance in pixels from the top edge of this view's parent
2658 * to the bottom edge of this view.
2659 * {@hide}
2660 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002661 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 protected int mBottom;
2663
2664 /**
2665 * The offset, in pixels, by which the content of this view is scrolled
2666 * horizontally.
2667 * {@hide}
2668 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002669 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 protected int mScrollX;
2671 /**
2672 * The offset, in pixels, by which the content of this view is scrolled
2673 * vertically.
2674 * {@hide}
2675 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002676 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 protected int mScrollY;
2678
2679 /**
2680 * The left padding in pixels, that is the distance in pixels between the
2681 * left edge of this view and the left edge of its content.
2682 * {@hide}
2683 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002684 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 protected int mPaddingLeft;
2686 /**
2687 * The right padding in pixels, that is the distance in pixels between the
2688 * right edge of this view and the right edge of its content.
2689 * {@hide}
2690 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002691 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 protected int mPaddingRight;
2693 /**
2694 * The top padding in pixels, that is the distance in pixels between the
2695 * top edge of this view and the top edge of its content.
2696 * {@hide}
2697 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002698 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 protected int mPaddingTop;
2700 /**
2701 * The bottom padding in pixels, that is the distance in pixels between the
2702 * bottom edge of this view and the bottom edge of its content.
2703 * {@hide}
2704 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002705 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 protected int mPaddingBottom;
2707
2708 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002709 * The layout insets in pixels, that is the distance in pixels between the
2710 * visible edges of this view its bounds.
2711 */
2712 private Insets mLayoutInsets;
2713
2714 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002715 * Briefly describes the view and is primarily used for accessibility support.
2716 */
2717 private CharSequence mContentDescription;
2718
2719 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002721 *
2722 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002724 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002725 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726
2727 /**
2728 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002729 *
2730 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002732 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002733 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002735 /**
Adam Powell20232d02010-12-08 21:08:53 -08002736 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002737 *
2738 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002739 */
2740 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002741 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002742
2743 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002744 * Cache if the user padding is relative.
2745 *
2746 */
2747 @ViewDebug.ExportedProperty(category = "padding")
2748 boolean mUserPaddingRelative;
2749
2750 /**
2751 * Cache the paddingStart set by the user to append to the scrollbar's size.
2752 *
2753 */
2754 @ViewDebug.ExportedProperty(category = "padding")
2755 int mUserPaddingStart;
2756
2757 /**
2758 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2759 *
2760 */
2761 @ViewDebug.ExportedProperty(category = "padding")
2762 int mUserPaddingEnd;
2763
2764 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002765 * @hide
2766 */
2767 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2768 /**
2769 * @hide
2770 */
2771 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772
Philip Milne6c8ea062012-04-03 17:38:43 -07002773 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774
2775 private int mBackgroundResource;
2776 private boolean mBackgroundSizeChanged;
2777
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002778 static class ListenerInfo {
2779 /**
2780 * Listener used to dispatch focus change events.
2781 * This field should be made private, so it is hidden from the SDK.
2782 * {@hide}
2783 */
2784 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002786 /**
2787 * Listeners for layout change events.
2788 */
2789 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002790
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002791 /**
2792 * Listeners for attach events.
2793 */
2794 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002795
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002796 /**
2797 * Listener used to dispatch click events.
2798 * This field should be made private, so it is hidden from the SDK.
2799 * {@hide}
2800 */
2801 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002803 /**
2804 * Listener used to dispatch long click events.
2805 * This field should be made private, so it is hidden from the SDK.
2806 * {@hide}
2807 */
2808 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002810 /**
2811 * Listener used to build the context menu.
2812 * This field should be made private, so it is hidden from the SDK.
2813 * {@hide}
2814 */
2815 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002817 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002819 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002821 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002822
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002823 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002824
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002825 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002826
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002827 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2828 }
2829
2830 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 /**
2833 * The application environment this view lives in.
2834 * This field should be made private, so it is hidden from the SDK.
2835 * {@hide}
2836 */
2837 protected Context mContext;
2838
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002839 private final Resources mResources;
2840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 private ScrollabilityCache mScrollCache;
2842
2843 private int[] mDrawableState = null;
2844
Romain Guy0211a0a2011-02-14 16:34:59 -08002845 /**
2846 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002847 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002848 * @hide
2849 */
2850 public boolean mCachingFailed;
2851
Romain Guy02890fd2010-08-06 17:58:44 -07002852 private Bitmap mDrawingCache;
2853 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002854 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002855 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
2857 /**
2858 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2859 * the user may specify which view to go to next.
2860 */
2861 private int mNextFocusLeftId = View.NO_ID;
2862
2863 /**
2864 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2865 * the user may specify which view to go to next.
2866 */
2867 private int mNextFocusRightId = View.NO_ID;
2868
2869 /**
2870 * When this view has focus and the next focus is {@link #FOCUS_UP},
2871 * the user may specify which view to go to next.
2872 */
2873 private int mNextFocusUpId = View.NO_ID;
2874
2875 /**
2876 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2877 * the user may specify which view to go to next.
2878 */
2879 private int mNextFocusDownId = View.NO_ID;
2880
Jeff Brown4e6319b2010-12-13 10:36:51 -08002881 /**
2882 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2883 * the user may specify which view to go to next.
2884 */
2885 int mNextFocusForwardId = View.NO_ID;
2886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002888 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002889 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002890 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 private UnsetPressedState mUnsetPressedState;
2893
2894 /**
2895 * Whether the long press's action has been invoked. The tap's action is invoked on the
2896 * up event while a long press is invoked as soon as the long press duration is reached, so
2897 * a long press could be performed before the tap is checked, in which case the tap's action
2898 * should not be invoked.
2899 */
2900 private boolean mHasPerformedLongPress;
2901
2902 /**
2903 * The minimum height of the view. We'll try our best to have the height
2904 * of this view to at least this amount.
2905 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002906 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 private int mMinHeight;
2908
2909 /**
2910 * The minimum width of the view. We'll try our best to have the width
2911 * of this view to at least this amount.
2912 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002913 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 private int mMinWidth;
2915
2916 /**
2917 * The delegate to handle touch events that are physically in this view
2918 * but should be handled by another view.
2919 */
2920 private TouchDelegate mTouchDelegate = null;
2921
2922 /**
2923 * Solid color to use as a background when creating the drawing cache. Enables
2924 * the cache to use 16 bit bitmaps instead of 32 bit.
2925 */
2926 private int mDrawingCacheBackgroundColor = 0;
2927
2928 /**
2929 * Special tree observer used when mAttachInfo is null.
2930 */
2931 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002932
Adam Powelle14579b2009-12-16 18:39:52 -08002933 /**
2934 * Cache the touch slop from the context that created the view.
2935 */
2936 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002939 * Object that handles automatic animation of view properties.
2940 */
2941 private ViewPropertyAnimator mAnimator = null;
2942
2943 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002944 * Flag indicating that a drag can cross window boundaries. When
2945 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2946 * with this flag set, all visible applications will be able to participate
2947 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002948 *
2949 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002950 */
2951 public static final int DRAG_FLAG_GLOBAL = 1;
2952
2953 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002954 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2955 */
2956 private float mVerticalScrollFactor;
2957
2958 /**
Adam Powell20232d02010-12-08 21:08:53 -08002959 * Position of the vertical scroll bar.
2960 */
2961 private int mVerticalScrollbarPosition;
2962
2963 /**
2964 * Position the scroll bar at the default position as determined by the system.
2965 */
2966 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2967
2968 /**
2969 * Position the scroll bar along the left edge.
2970 */
2971 public static final int SCROLLBAR_POSITION_LEFT = 1;
2972
2973 /**
2974 * Position the scroll bar along the right edge.
2975 */
2976 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2977
2978 /**
Romain Guy171c5922011-01-06 10:04:23 -08002979 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002980 *
2981 * @see #getLayerType()
2982 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002983 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002984 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002985 */
2986 public static final int LAYER_TYPE_NONE = 0;
2987
2988 /**
2989 * <p>Indicates that the view has a software layer. A software layer is backed
2990 * by a bitmap and causes the view to be rendered using Android's software
2991 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002992 *
Romain Guy171c5922011-01-06 10:04:23 -08002993 * <p>Software layers have various usages:</p>
2994 * <p>When the application is not using hardware acceleration, a software layer
2995 * is useful to apply a specific color filter and/or blending mode and/or
2996 * translucency to a view and all its children.</p>
2997 * <p>When the application is using hardware acceleration, a software layer
2998 * is useful to render drawing primitives not supported by the hardware
2999 * accelerated pipeline. It can also be used to cache a complex view tree
3000 * into a texture and reduce the complexity of drawing operations. For instance,
3001 * when animating a complex view tree with a translation, a software layer can
3002 * be used to render the view tree only once.</p>
3003 * <p>Software layers should be avoided when the affected view tree updates
3004 * often. Every update will require to re-render the software layer, which can
3005 * potentially be slow (particularly when hardware acceleration is turned on
3006 * since the layer will have to be uploaded into a hardware texture after every
3007 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003008 *
3009 * @see #getLayerType()
3010 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003011 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003012 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003013 */
3014 public static final int LAYER_TYPE_SOFTWARE = 1;
3015
3016 /**
3017 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3018 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3019 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3020 * rendering pipeline, but only if hardware acceleration is turned on for the
3021 * view hierarchy. When hardware acceleration is turned off, hardware layers
3022 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003023 *
Romain Guy171c5922011-01-06 10:04:23 -08003024 * <p>A hardware layer is useful to apply a specific color filter and/or
3025 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003026 * <p>A hardware layer can be used to cache a complex view tree into a
3027 * texture and reduce the complexity of drawing operations. For instance,
3028 * when animating a complex view tree with a translation, a hardware layer can
3029 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003030 * <p>A hardware layer can also be used to increase the rendering quality when
3031 * rotation transformations are applied on a view. It can also be used to
3032 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003033 *
3034 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003035 * @see #setLayerType(int, android.graphics.Paint)
3036 * @see #LAYER_TYPE_NONE
3037 * @see #LAYER_TYPE_SOFTWARE
3038 */
3039 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003040
Romain Guy3aaff3a2011-01-12 14:18:47 -08003041 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3042 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3043 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3044 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3045 })
Romain Guy171c5922011-01-06 10:04:23 -08003046 int mLayerType = LAYER_TYPE_NONE;
3047 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003048 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003049
3050 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003051 * Set to true when the view is sending hover accessibility events because it
3052 * is the innermost hovered view.
3053 */
3054 private boolean mSendingHoverAccessibilityEvents;
3055
3056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 * Simple constructor to use when creating a view from code.
3058 *
3059 * @param context The Context the view is running in, through which it can
3060 * access the current theme, resources, etc.
3061 */
3062 public View(Context context) {
3063 mContext = context;
3064 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003065 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003066 // Set layout and text direction defaults
3067 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003068 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003069 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3070 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003071 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003072 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003073 mUserPaddingStart = -1;
3074 mUserPaddingEnd = -1;
3075 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 }
3077
3078 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003079 * Delegate for injecting accessiblity functionality.
3080 */
3081 AccessibilityDelegate mAccessibilityDelegate;
3082
3083 /**
3084 * Consistency verifier for debugging purposes.
3085 * @hide
3086 */
3087 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3088 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3089 new InputEventConsistencyVerifier(this, 0) : null;
3090
3091 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 * Constructor that is called when inflating a view from XML. This is called
3093 * when a view is being constructed from an XML file, supplying attributes
3094 * that were specified in the XML file. This version uses a default style of
3095 * 0, so the only attribute values applied are those in the Context's Theme
3096 * and the given AttributeSet.
3097 *
3098 * <p>
3099 * The method onFinishInflate() will be called after all children have been
3100 * added.
3101 *
3102 * @param context The Context the view is running in, through which it can
3103 * access the current theme, resources, etc.
3104 * @param attrs The attributes of the XML tag that is inflating the view.
3105 * @see #View(Context, AttributeSet, int)
3106 */
3107 public View(Context context, AttributeSet attrs) {
3108 this(context, attrs, 0);
3109 }
3110
3111 /**
3112 * Perform inflation from XML and apply a class-specific base style. This
3113 * constructor of View allows subclasses to use their own base style when
3114 * they are inflating. For example, a Button class's constructor would call
3115 * this version of the super class constructor and supply
3116 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3117 * the theme's button style to modify all of the base view attributes (in
3118 * particular its background) as well as the Button class's attributes.
3119 *
3120 * @param context The Context the view is running in, through which it can
3121 * access the current theme, resources, etc.
3122 * @param attrs The attributes of the XML tag that is inflating the view.
3123 * @param defStyle The default style to apply to this view. If 0, no style
3124 * will be applied (beyond what is included in the theme). This may
3125 * either be an attribute resource, whose value will be retrieved
3126 * from the current theme, or an explicit style resource.
3127 * @see #View(Context, AttributeSet)
3128 */
3129 public View(Context context, AttributeSet attrs, int defStyle) {
3130 this(context);
3131
3132 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3133 defStyle, 0);
3134
3135 Drawable background = null;
3136
3137 int leftPadding = -1;
3138 int topPadding = -1;
3139 int rightPadding = -1;
3140 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003141 int startPadding = -1;
3142 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143
3144 int padding = -1;
3145
3146 int viewFlagValues = 0;
3147 int viewFlagMasks = 0;
3148
3149 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 int x = 0;
3152 int y = 0;
3153
Chet Haase73066682010-11-29 15:55:32 -08003154 float tx = 0;
3155 float ty = 0;
3156 float rotation = 0;
3157 float rotationX = 0;
3158 float rotationY = 0;
3159 float sx = 1f;
3160 float sy = 1f;
3161 boolean transformSet = false;
3162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3164
Adam Powell637d3372010-08-25 14:37:03 -07003165 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 final int N = a.getIndexCount();
3167 for (int i = 0; i < N; i++) {
3168 int attr = a.getIndex(i);
3169 switch (attr) {
3170 case com.android.internal.R.styleable.View_background:
3171 background = a.getDrawable(attr);
3172 break;
3173 case com.android.internal.R.styleable.View_padding:
3174 padding = a.getDimensionPixelSize(attr, -1);
3175 break;
3176 case com.android.internal.R.styleable.View_paddingLeft:
3177 leftPadding = a.getDimensionPixelSize(attr, -1);
3178 break;
3179 case com.android.internal.R.styleable.View_paddingTop:
3180 topPadding = a.getDimensionPixelSize(attr, -1);
3181 break;
3182 case com.android.internal.R.styleable.View_paddingRight:
3183 rightPadding = a.getDimensionPixelSize(attr, -1);
3184 break;
3185 case com.android.internal.R.styleable.View_paddingBottom:
3186 bottomPadding = a.getDimensionPixelSize(attr, -1);
3187 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003188 case com.android.internal.R.styleable.View_paddingStart:
3189 startPadding = a.getDimensionPixelSize(attr, -1);
3190 break;
3191 case com.android.internal.R.styleable.View_paddingEnd:
3192 endPadding = a.getDimensionPixelSize(attr, -1);
3193 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 case com.android.internal.R.styleable.View_scrollX:
3195 x = a.getDimensionPixelOffset(attr, 0);
3196 break;
3197 case com.android.internal.R.styleable.View_scrollY:
3198 y = a.getDimensionPixelOffset(attr, 0);
3199 break;
Chet Haase73066682010-11-29 15:55:32 -08003200 case com.android.internal.R.styleable.View_alpha:
3201 setAlpha(a.getFloat(attr, 1f));
3202 break;
3203 case com.android.internal.R.styleable.View_transformPivotX:
3204 setPivotX(a.getDimensionPixelOffset(attr, 0));
3205 break;
3206 case com.android.internal.R.styleable.View_transformPivotY:
3207 setPivotY(a.getDimensionPixelOffset(attr, 0));
3208 break;
3209 case com.android.internal.R.styleable.View_translationX:
3210 tx = a.getDimensionPixelOffset(attr, 0);
3211 transformSet = true;
3212 break;
3213 case com.android.internal.R.styleable.View_translationY:
3214 ty = a.getDimensionPixelOffset(attr, 0);
3215 transformSet = true;
3216 break;
3217 case com.android.internal.R.styleable.View_rotation:
3218 rotation = a.getFloat(attr, 0);
3219 transformSet = true;
3220 break;
3221 case com.android.internal.R.styleable.View_rotationX:
3222 rotationX = a.getFloat(attr, 0);
3223 transformSet = true;
3224 break;
3225 case com.android.internal.R.styleable.View_rotationY:
3226 rotationY = a.getFloat(attr, 0);
3227 transformSet = true;
3228 break;
3229 case com.android.internal.R.styleable.View_scaleX:
3230 sx = a.getFloat(attr, 1f);
3231 transformSet = true;
3232 break;
3233 case com.android.internal.R.styleable.View_scaleY:
3234 sy = a.getFloat(attr, 1f);
3235 transformSet = true;
3236 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 case com.android.internal.R.styleable.View_id:
3238 mID = a.getResourceId(attr, NO_ID);
3239 break;
3240 case com.android.internal.R.styleable.View_tag:
3241 mTag = a.getText(attr);
3242 break;
3243 case com.android.internal.R.styleable.View_fitsSystemWindows:
3244 if (a.getBoolean(attr, false)) {
3245 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3246 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3247 }
3248 break;
3249 case com.android.internal.R.styleable.View_focusable:
3250 if (a.getBoolean(attr, false)) {
3251 viewFlagValues |= FOCUSABLE;
3252 viewFlagMasks |= FOCUSABLE_MASK;
3253 }
3254 break;
3255 case com.android.internal.R.styleable.View_focusableInTouchMode:
3256 if (a.getBoolean(attr, false)) {
3257 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3258 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3259 }
3260 break;
3261 case com.android.internal.R.styleable.View_clickable:
3262 if (a.getBoolean(attr, false)) {
3263 viewFlagValues |= CLICKABLE;
3264 viewFlagMasks |= CLICKABLE;
3265 }
3266 break;
3267 case com.android.internal.R.styleable.View_longClickable:
3268 if (a.getBoolean(attr, false)) {
3269 viewFlagValues |= LONG_CLICKABLE;
3270 viewFlagMasks |= LONG_CLICKABLE;
3271 }
3272 break;
3273 case com.android.internal.R.styleable.View_saveEnabled:
3274 if (!a.getBoolean(attr, true)) {
3275 viewFlagValues |= SAVE_DISABLED;
3276 viewFlagMasks |= SAVE_DISABLED_MASK;
3277 }
3278 break;
3279 case com.android.internal.R.styleable.View_duplicateParentState:
3280 if (a.getBoolean(attr, false)) {
3281 viewFlagValues |= DUPLICATE_PARENT_STATE;
3282 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3283 }
3284 break;
3285 case com.android.internal.R.styleable.View_visibility:
3286 final int visibility = a.getInt(attr, 0);
3287 if (visibility != 0) {
3288 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3289 viewFlagMasks |= VISIBILITY_MASK;
3290 }
3291 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003292 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003293 // Clear any layout direction flags (included resolved bits) already set
3294 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3295 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003296 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003297 final int value = (layoutDirection != -1) ?
3298 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3299 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003300 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 case com.android.internal.R.styleable.View_drawingCacheQuality:
3302 final int cacheQuality = a.getInt(attr, 0);
3303 if (cacheQuality != 0) {
3304 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3305 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3306 }
3307 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003308 case com.android.internal.R.styleable.View_contentDescription:
3309 mContentDescription = a.getString(attr);
3310 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3312 if (!a.getBoolean(attr, true)) {
3313 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3314 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3315 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003316 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3318 if (!a.getBoolean(attr, true)) {
3319 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3320 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3321 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003322 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 case R.styleable.View_scrollbars:
3324 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3325 if (scrollbars != SCROLLBARS_NONE) {
3326 viewFlagValues |= scrollbars;
3327 viewFlagMasks |= SCROLLBARS_MASK;
3328 initializeScrollbars(a);
3329 }
3330 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003331 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003333 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3334 // Ignore the attribute starting with ICS
3335 break;
3336 }
3337 // With builds < ICS, fall through and apply fading edges
3338 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3340 if (fadingEdge != FADING_EDGE_NONE) {
3341 viewFlagValues |= fadingEdge;
3342 viewFlagMasks |= FADING_EDGE_MASK;
3343 initializeFadingEdge(a);
3344 }
3345 break;
3346 case R.styleable.View_scrollbarStyle:
3347 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3348 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3349 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3350 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3351 }
3352 break;
3353 case R.styleable.View_isScrollContainer:
3354 setScrollContainer = true;
3355 if (a.getBoolean(attr, false)) {
3356 setScrollContainer(true);
3357 }
3358 break;
3359 case com.android.internal.R.styleable.View_keepScreenOn:
3360 if (a.getBoolean(attr, false)) {
3361 viewFlagValues |= KEEP_SCREEN_ON;
3362 viewFlagMasks |= KEEP_SCREEN_ON;
3363 }
3364 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003365 case R.styleable.View_filterTouchesWhenObscured:
3366 if (a.getBoolean(attr, false)) {
3367 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3368 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3369 }
3370 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 case R.styleable.View_nextFocusLeft:
3372 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3373 break;
3374 case R.styleable.View_nextFocusRight:
3375 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3376 break;
3377 case R.styleable.View_nextFocusUp:
3378 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3379 break;
3380 case R.styleable.View_nextFocusDown:
3381 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3382 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003383 case R.styleable.View_nextFocusForward:
3384 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3385 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 case R.styleable.View_minWidth:
3387 mMinWidth = a.getDimensionPixelSize(attr, 0);
3388 break;
3389 case R.styleable.View_minHeight:
3390 mMinHeight = a.getDimensionPixelSize(attr, 0);
3391 break;
Romain Guy9a817362009-05-01 10:57:14 -07003392 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003393 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003394 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003395 + "be used within a restricted context");
3396 }
3397
Romain Guy9a817362009-05-01 10:57:14 -07003398 final String handlerName = a.getString(attr);
3399 if (handlerName != null) {
3400 setOnClickListener(new OnClickListener() {
3401 private Method mHandler;
3402
3403 public void onClick(View v) {
3404 if (mHandler == null) {
3405 try {
3406 mHandler = getContext().getClass().getMethod(handlerName,
3407 View.class);
3408 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003409 int id = getId();
3410 String idText = id == NO_ID ? "" : " with id '"
3411 + getContext().getResources().getResourceEntryName(
3412 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003413 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003414 handlerName + "(View) in the activity "
3415 + getContext().getClass() + " for onClick handler"
3416 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003417 }
3418 }
3419
3420 try {
3421 mHandler.invoke(getContext(), View.this);
3422 } catch (IllegalAccessException e) {
3423 throw new IllegalStateException("Could not execute non "
3424 + "public method of the activity", e);
3425 } catch (InvocationTargetException e) {
3426 throw new IllegalStateException("Could not execute "
3427 + "method of the activity", e);
3428 }
3429 }
3430 });
3431 }
3432 break;
Adam Powell637d3372010-08-25 14:37:03 -07003433 case R.styleable.View_overScrollMode:
3434 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3435 break;
Adam Powell20232d02010-12-08 21:08:53 -08003436 case R.styleable.View_verticalScrollbarPosition:
3437 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3438 break;
Romain Guy171c5922011-01-06 10:04:23 -08003439 case R.styleable.View_layerType:
3440 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3441 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003442 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003443 // Clear any text direction flag already set
3444 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3445 // Set the text direction flags depending on the value of the attribute
3446 final int textDirection = a.getInt(attr, -1);
3447 if (textDirection != -1) {
3448 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3449 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003450 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003451 case R.styleable.View_textAlignment:
3452 // Clear any text alignment flag already set
3453 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3454 // Set the text alignment flag depending on the value of the attribute
3455 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3456 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3457 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003458 case R.styleable.View_importantForAccessibility:
3459 setImportantForAccessibility(a.getInt(attr,
3460 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 }
3462 }
3463
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003464 a.recycle();
3465
Adam Powell637d3372010-08-25 14:37:03 -07003466 setOverScrollMode(overScrollMode);
3467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003469 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 }
3471
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003472 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3473 // layout direction). Those cached values will be used later during padding resolution.
3474 mUserPaddingStart = startPadding;
3475 mUserPaddingEnd = endPadding;
3476
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003477 updateUserPaddingRelative();
3478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 if (padding >= 0) {
3480 leftPadding = padding;
3481 topPadding = padding;
3482 rightPadding = padding;
3483 bottomPadding = padding;
3484 }
3485
3486 // If the user specified the padding (either with android:padding or
3487 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3488 // use the default padding or the padding from the background drawable
3489 // (stored at this point in mPadding*)
3490 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3491 topPadding >= 0 ? topPadding : mPaddingTop,
3492 rightPadding >= 0 ? rightPadding : mPaddingRight,
3493 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3494
3495 if (viewFlagMasks != 0) {
3496 setFlags(viewFlagValues, viewFlagMasks);
3497 }
3498
3499 // Needs to be called after mViewFlags is set
3500 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3501 recomputePadding();
3502 }
3503
3504 if (x != 0 || y != 0) {
3505 scrollTo(x, y);
3506 }
3507
Chet Haase73066682010-11-29 15:55:32 -08003508 if (transformSet) {
3509 setTranslationX(tx);
3510 setTranslationY(ty);
3511 setRotation(rotation);
3512 setRotationX(rotationX);
3513 setRotationY(rotationY);
3514 setScaleX(sx);
3515 setScaleY(sy);
3516 }
3517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3519 setScrollContainer(true);
3520 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003521
3522 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 }
3524
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003525 private void updateUserPaddingRelative() {
3526 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3527 }
3528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 /**
3530 * Non-public constructor for use in testing
3531 */
3532 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003533 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 }
3535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 /**
3537 * <p>
3538 * Initializes the fading edges from a given set of styled attributes. This
3539 * method should be called by subclasses that need fading edges and when an
3540 * instance of these subclasses is created programmatically rather than
3541 * being inflated from XML. This method is automatically called when the XML
3542 * is inflated.
3543 * </p>
3544 *
3545 * @param a the styled attributes set to initialize the fading edges from
3546 */
3547 protected void initializeFadingEdge(TypedArray a) {
3548 initScrollCache();
3549
3550 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3551 R.styleable.View_fadingEdgeLength,
3552 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3553 }
3554
3555 /**
3556 * Returns the size of the vertical faded edges used to indicate that more
3557 * content in this view is visible.
3558 *
3559 * @return The size in pixels of the vertical faded edge or 0 if vertical
3560 * faded edges are not enabled for this view.
3561 * @attr ref android.R.styleable#View_fadingEdgeLength
3562 */
3563 public int getVerticalFadingEdgeLength() {
3564 if (isVerticalFadingEdgeEnabled()) {
3565 ScrollabilityCache cache = mScrollCache;
3566 if (cache != null) {
3567 return cache.fadingEdgeLength;
3568 }
3569 }
3570 return 0;
3571 }
3572
3573 /**
3574 * Set the size of the faded edge used to indicate that more content in this
3575 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003576 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3577 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3578 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 *
3580 * @param length The size in pixels of the faded edge used to indicate that more
3581 * content in this view is visible.
3582 */
3583 public void setFadingEdgeLength(int length) {
3584 initScrollCache();
3585 mScrollCache.fadingEdgeLength = length;
3586 }
3587
3588 /**
3589 * Returns the size of the horizontal faded edges used to indicate that more
3590 * content in this view is visible.
3591 *
3592 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3593 * faded edges are not enabled for this view.
3594 * @attr ref android.R.styleable#View_fadingEdgeLength
3595 */
3596 public int getHorizontalFadingEdgeLength() {
3597 if (isHorizontalFadingEdgeEnabled()) {
3598 ScrollabilityCache cache = mScrollCache;
3599 if (cache != null) {
3600 return cache.fadingEdgeLength;
3601 }
3602 }
3603 return 0;
3604 }
3605
3606 /**
3607 * Returns the width of the vertical scrollbar.
3608 *
3609 * @return The width in pixels of the vertical scrollbar or 0 if there
3610 * is no vertical scrollbar.
3611 */
3612 public int getVerticalScrollbarWidth() {
3613 ScrollabilityCache cache = mScrollCache;
3614 if (cache != null) {
3615 ScrollBarDrawable scrollBar = cache.scrollBar;
3616 if (scrollBar != null) {
3617 int size = scrollBar.getSize(true);
3618 if (size <= 0) {
3619 size = cache.scrollBarSize;
3620 }
3621 return size;
3622 }
3623 return 0;
3624 }
3625 return 0;
3626 }
3627
3628 /**
3629 * Returns the height of the horizontal scrollbar.
3630 *
3631 * @return The height in pixels of the horizontal scrollbar or 0 if
3632 * there is no horizontal scrollbar.
3633 */
3634 protected int getHorizontalScrollbarHeight() {
3635 ScrollabilityCache cache = mScrollCache;
3636 if (cache != null) {
3637 ScrollBarDrawable scrollBar = cache.scrollBar;
3638 if (scrollBar != null) {
3639 int size = scrollBar.getSize(false);
3640 if (size <= 0) {
3641 size = cache.scrollBarSize;
3642 }
3643 return size;
3644 }
3645 return 0;
3646 }
3647 return 0;
3648 }
3649
3650 /**
3651 * <p>
3652 * Initializes the scrollbars from a given set of styled attributes. This
3653 * method should be called by subclasses that need scrollbars and when an
3654 * instance of these subclasses is created programmatically rather than
3655 * being inflated from XML. This method is automatically called when the XML
3656 * is inflated.
3657 * </p>
3658 *
3659 * @param a the styled attributes set to initialize the scrollbars from
3660 */
3661 protected void initializeScrollbars(TypedArray a) {
3662 initScrollCache();
3663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003665
Mike Cleronf116bf82009-09-27 19:14:12 -07003666 if (scrollabilityCache.scrollBar == null) {
3667 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3668 }
Joe Malin32736f02011-01-19 16:14:20 -08003669
Romain Guy8bda2482010-03-02 11:42:11 -08003670 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671
Mike Cleronf116bf82009-09-27 19:14:12 -07003672 if (!fadeScrollbars) {
3673 scrollabilityCache.state = ScrollabilityCache.ON;
3674 }
3675 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003676
3677
Mike Cleronf116bf82009-09-27 19:14:12 -07003678 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3679 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3680 .getScrollBarFadeDuration());
3681 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3682 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3683 ViewConfiguration.getScrollDefaultDelay());
3684
Joe Malin32736f02011-01-19 16:14:20 -08003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3687 com.android.internal.R.styleable.View_scrollbarSize,
3688 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3689
3690 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3691 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3692
3693 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3694 if (thumb != null) {
3695 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3696 }
3697
3698 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3699 false);
3700 if (alwaysDraw) {
3701 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3702 }
3703
3704 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3705 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3706
3707 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3708 if (thumb != null) {
3709 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3710 }
3711
3712 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3713 false);
3714 if (alwaysDraw) {
3715 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3716 }
3717
3718 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003719 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 }
3721
3722 /**
3723 * <p>
3724 * Initalizes the scrollability cache if necessary.
3725 * </p>
3726 */
3727 private void initScrollCache() {
3728 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003729 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 }
3731 }
3732
Philip Milne6c8ea062012-04-03 17:38:43 -07003733 private ScrollabilityCache getScrollCache() {
3734 initScrollCache();
3735 return mScrollCache;
3736 }
3737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 /**
Adam Powell20232d02010-12-08 21:08:53 -08003739 * Set the position of the vertical scroll bar. Should be one of
3740 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3741 * {@link #SCROLLBAR_POSITION_RIGHT}.
3742 *
3743 * @param position Where the vertical scroll bar should be positioned.
3744 */
3745 public void setVerticalScrollbarPosition(int position) {
3746 if (mVerticalScrollbarPosition != position) {
3747 mVerticalScrollbarPosition = position;
3748 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003749 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003750 }
3751 }
3752
3753 /**
3754 * @return The position where the vertical scroll bar will show, if applicable.
3755 * @see #setVerticalScrollbarPosition(int)
3756 */
3757 public int getVerticalScrollbarPosition() {
3758 return mVerticalScrollbarPosition;
3759 }
3760
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003761 ListenerInfo getListenerInfo() {
3762 if (mListenerInfo != null) {
3763 return mListenerInfo;
3764 }
3765 mListenerInfo = new ListenerInfo();
3766 return mListenerInfo;
3767 }
3768
Adam Powell20232d02010-12-08 21:08:53 -08003769 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 * Register a callback to be invoked when focus of this view changed.
3771 *
3772 * @param l The callback that will run.
3773 */
3774 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003775 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 }
3777
3778 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003779 * Add a listener that will be called when the bounds of the view change due to
3780 * layout processing.
3781 *
3782 * @param listener The listener that will be called when layout bounds change.
3783 */
3784 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003785 ListenerInfo li = getListenerInfo();
3786 if (li.mOnLayoutChangeListeners == null) {
3787 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003788 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003789 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3790 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003791 }
Chet Haase21cd1382010-09-01 17:42:29 -07003792 }
3793
3794 /**
3795 * Remove a listener for layout changes.
3796 *
3797 * @param listener The listener for layout bounds change.
3798 */
3799 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003800 ListenerInfo li = mListenerInfo;
3801 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003802 return;
3803 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003804 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003805 }
3806
3807 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003808 * Add a listener for attach state changes.
3809 *
3810 * This listener will be called whenever this view is attached or detached
3811 * from a window. Remove the listener using
3812 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3813 *
3814 * @param listener Listener to attach
3815 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3816 */
3817 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003818 ListenerInfo li = getListenerInfo();
3819 if (li.mOnAttachStateChangeListeners == null) {
3820 li.mOnAttachStateChangeListeners
3821 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003822 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003823 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003824 }
3825
3826 /**
3827 * Remove a listener for attach state changes. The listener will receive no further
3828 * notification of window attach/detach events.
3829 *
3830 * @param listener Listener to remove
3831 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3832 */
3833 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003834 ListenerInfo li = mListenerInfo;
3835 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003836 return;
3837 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003838 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003839 }
3840
3841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 * Returns the focus-change callback registered for this view.
3843 *
3844 * @return The callback, or null if one is not registered.
3845 */
3846 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003847 ListenerInfo li = mListenerInfo;
3848 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 }
3850
3851 /**
3852 * Register a callback to be invoked when this view is clicked. If this view is not
3853 * clickable, it becomes clickable.
3854 *
3855 * @param l The callback that will run
3856 *
3857 * @see #setClickable(boolean)
3858 */
3859 public void setOnClickListener(OnClickListener l) {
3860 if (!isClickable()) {
3861 setClickable(true);
3862 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003863 getListenerInfo().mOnClickListener = l;
3864 }
3865
3866 /**
3867 * Return whether this view has an attached OnClickListener. Returns
3868 * true if there is a listener, false if there is none.
3869 */
3870 public boolean hasOnClickListeners() {
3871 ListenerInfo li = mListenerInfo;
3872 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 }
3874
3875 /**
3876 * Register a callback to be invoked when this view is clicked and held. If this view is not
3877 * long clickable, it becomes long clickable.
3878 *
3879 * @param l The callback that will run
3880 *
3881 * @see #setLongClickable(boolean)
3882 */
3883 public void setOnLongClickListener(OnLongClickListener l) {
3884 if (!isLongClickable()) {
3885 setLongClickable(true);
3886 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003887 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 }
3889
3890 /**
3891 * Register a callback to be invoked when the context menu for this view is
3892 * being built. If this view is not long clickable, it becomes long clickable.
3893 *
3894 * @param l The callback that will run
3895 *
3896 */
3897 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3898 if (!isLongClickable()) {
3899 setLongClickable(true);
3900 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003901 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 }
3903
3904 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003905 * Call this view's OnClickListener, if it is defined. Performs all normal
3906 * actions associated with clicking: reporting accessibility event, playing
3907 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 *
3909 * @return True there was an assigned OnClickListener that was called, false
3910 * otherwise is returned.
3911 */
3912 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003913 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3914
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003915 ListenerInfo li = mListenerInfo;
3916 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003918 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 return true;
3920 }
3921
3922 return false;
3923 }
3924
3925 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003926 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3927 * this only calls the listener, and does not do any associated clicking
3928 * actions like reporting an accessibility event.
3929 *
3930 * @return True there was an assigned OnClickListener that was called, false
3931 * otherwise is returned.
3932 */
3933 public boolean callOnClick() {
3934 ListenerInfo li = mListenerInfo;
3935 if (li != null && li.mOnClickListener != null) {
3936 li.mOnClickListener.onClick(this);
3937 return true;
3938 }
3939 return false;
3940 }
3941
3942 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003943 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3944 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003946 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 */
3948 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003949 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003952 ListenerInfo li = mListenerInfo;
3953 if (li != null && li.mOnLongClickListener != null) {
3954 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 }
3956 if (!handled) {
3957 handled = showContextMenu();
3958 }
3959 if (handled) {
3960 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3961 }
3962 return handled;
3963 }
3964
3965 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003966 * Performs button-related actions during a touch down event.
3967 *
3968 * @param event The event.
3969 * @return True if the down was consumed.
3970 *
3971 * @hide
3972 */
3973 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3974 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3975 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3976 return true;
3977 }
3978 }
3979 return false;
3980 }
3981
3982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 * Bring up the context menu for this view.
3984 *
3985 * @return Whether a context menu was displayed.
3986 */
3987 public boolean showContextMenu() {
3988 return getParent().showContextMenuForChild(this);
3989 }
3990
3991 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003992 * Bring up the context menu for this view, referring to the item under the specified point.
3993 *
3994 * @param x The referenced x coordinate.
3995 * @param y The referenced y coordinate.
3996 * @param metaState The keyboard modifiers that were pressed.
3997 * @return Whether a context menu was displayed.
3998 *
3999 * @hide
4000 */
4001 public boolean showContextMenu(float x, float y, int metaState) {
4002 return showContextMenu();
4003 }
4004
4005 /**
Adam Powell6e346362010-07-23 10:18:23 -07004006 * Start an action mode.
4007 *
4008 * @param callback Callback that will control the lifecycle of the action mode
4009 * @return The new action mode if it is started, null otherwise
4010 *
4011 * @see ActionMode
4012 */
4013 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004014 ViewParent parent = getParent();
4015 if (parent == null) return null;
4016 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004017 }
4018
4019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 * Register a callback to be invoked when a key is pressed in this view.
4021 * @param l the key listener to attach to this view
4022 */
4023 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004024 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 }
4026
4027 /**
4028 * Register a callback to be invoked when a touch event is sent to this view.
4029 * @param l the touch listener to attach to this view
4030 */
4031 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004032 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 }
4034
4035 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004036 * Register a callback to be invoked when a generic motion event is sent to this view.
4037 * @param l the generic motion listener to attach to this view
4038 */
4039 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004040 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004041 }
4042
4043 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004044 * Register a callback to be invoked when a hover event is sent to this view.
4045 * @param l the hover listener to attach to this view
4046 */
4047 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004048 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004049 }
4050
4051 /**
Joe Malin32736f02011-01-19 16:14:20 -08004052 * Register a drag event listener callback object for this View. The parameter is
4053 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4054 * View, the system calls the
4055 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4056 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004057 */
4058 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004059 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004060 }
4061
4062 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004063 * Give this view focus. This will cause
4064 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 *
4066 * Note: this does not check whether this {@link View} should get focus, it just
4067 * gives it focus no matter what. It should only be called internally by framework
4068 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4069 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004070 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4071 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 * focus moved when requestFocus() is called. It may not always
4073 * apply, in which case use the default View.FOCUS_DOWN.
4074 * @param previouslyFocusedRect The rectangle of the view that had focus
4075 * prior in this View's coordinate system.
4076 */
4077 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4078 if (DBG) {
4079 System.out.println(this + " requestFocus()");
4080 }
4081
4082 if ((mPrivateFlags & FOCUSED) == 0) {
4083 mPrivateFlags |= FOCUSED;
4084
4085 if (mParent != null) {
4086 mParent.requestChildFocus(this, this);
4087 }
4088
4089 onFocusChanged(true, direction, previouslyFocusedRect);
4090 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004091
4092 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4093 notifyAccessibilityStateChanged();
4094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 }
4096 }
4097
4098 /**
4099 * Request that a rectangle of this view be visible on the screen,
4100 * scrolling if necessary just enough.
4101 *
4102 * <p>A View should call this if it maintains some notion of which part
4103 * of its content is interesting. For example, a text editing view
4104 * should call this when its cursor moves.
4105 *
4106 * @param rectangle The rectangle.
4107 * @return Whether any parent scrolled.
4108 */
4109 public boolean requestRectangleOnScreen(Rect rectangle) {
4110 return requestRectangleOnScreen(rectangle, false);
4111 }
4112
4113 /**
4114 * Request that a rectangle of this view be visible on the screen,
4115 * scrolling if necessary just enough.
4116 *
4117 * <p>A View should call this if it maintains some notion of which part
4118 * of its content is interesting. For example, a text editing view
4119 * should call this when its cursor moves.
4120 *
4121 * <p>When <code>immediate</code> is set to true, scrolling will not be
4122 * animated.
4123 *
4124 * @param rectangle The rectangle.
4125 * @param immediate True to forbid animated scrolling, false otherwise
4126 * @return Whether any parent scrolled.
4127 */
4128 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4129 View child = this;
4130 ViewParent parent = mParent;
4131 boolean scrolled = false;
4132 while (parent != null) {
4133 scrolled |= parent.requestChildRectangleOnScreen(child,
4134 rectangle, immediate);
4135
4136 // offset rect so next call has the rectangle in the
4137 // coordinate system of its direct child.
4138 rectangle.offset(child.getLeft(), child.getTop());
4139 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4140
4141 if (!(parent instanceof View)) {
4142 break;
4143 }
Romain Guy8506ab42009-06-11 17:35:47 -07004144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 child = (View) parent;
4146 parent = child.getParent();
4147 }
4148 return scrolled;
4149 }
4150
4151 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004152 * Called when this view wants to give up focus. If focus is cleared
4153 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4154 * <p>
4155 * <strong>Note:</strong> When a View clears focus the framework is trying
4156 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004157 * View is the first from the top that can take focus, then all callbacks
4158 * related to clearing focus will be invoked after wich the framework will
4159 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004160 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 */
4162 public void clearFocus() {
4163 if (DBG) {
4164 System.out.println(this + " clearFocus()");
4165 }
4166
4167 if ((mPrivateFlags & FOCUSED) != 0) {
4168 mPrivateFlags &= ~FOCUSED;
4169
4170 if (mParent != null) {
4171 mParent.clearChildFocus(this);
4172 }
4173
4174 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004177
4178 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004179
4180 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4181 notifyAccessibilityStateChanged();
4182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 }
4184 }
4185
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004186 void ensureInputFocusOnFirstFocusable() {
4187 View root = getRootView();
4188 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004189 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 }
4191 }
4192
4193 /**
4194 * Called internally by the view system when a new view is getting focus.
4195 * This is what clears the old focus.
4196 */
4197 void unFocus() {
4198 if (DBG) {
4199 System.out.println(this + " unFocus()");
4200 }
4201
4202 if ((mPrivateFlags & FOCUSED) != 0) {
4203 mPrivateFlags &= ~FOCUSED;
4204
4205 onFocusChanged(false, 0, null);
4206 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004207
4208 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4209 notifyAccessibilityStateChanged();
4210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 }
4212 }
4213
4214 /**
4215 * Returns true if this view has focus iteself, or is the ancestor of the
4216 * view that has focus.
4217 *
4218 * @return True if this view has or contains focus, false otherwise.
4219 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004220 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 public boolean hasFocus() {
4222 return (mPrivateFlags & FOCUSED) != 0;
4223 }
4224
4225 /**
4226 * Returns true if this view is focusable or if it contains a reachable View
4227 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4228 * is a View whose parents do not block descendants focus.
4229 *
4230 * Only {@link #VISIBLE} views are considered focusable.
4231 *
4232 * @return True if the view is focusable or if the view contains a focusable
4233 * View, false otherwise.
4234 *
4235 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4236 */
4237 public boolean hasFocusable() {
4238 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4239 }
4240
4241 /**
4242 * Called by the view system when the focus state of this view changes.
4243 * When the focus change event is caused by directional navigation, direction
4244 * and previouslyFocusedRect provide insight into where the focus is coming from.
4245 * When overriding, be sure to call up through to the super class so that
4246 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 * @param gainFocus True if the View has focus; false otherwise.
4249 * @param direction The direction focus has moved when requestFocus()
4250 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004251 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4252 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4253 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4255 * system, of the previously focused view. If applicable, this will be
4256 * passed in as finer grained information about where the focus is coming
4257 * from (in addition to direction). Will be <code>null</code> otherwise.
4258 */
4259 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004260 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004261 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4262 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4263 requestAccessibilityFocus();
4264 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004265 }
4266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 InputMethodManager imm = InputMethodManager.peekInstance();
4268 if (!gainFocus) {
4269 if (isPressed()) {
4270 setPressed(false);
4271 }
4272 if (imm != null && mAttachInfo != null
4273 && mAttachInfo.mHasWindowFocus) {
4274 imm.focusOut(this);
4275 }
Romain Guya2431d02009-04-30 16:30:00 -07004276 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 } else if (imm != null && mAttachInfo != null
4278 && mAttachInfo.mHasWindowFocus) {
4279 imm.focusIn(this);
4280 }
Romain Guy8506ab42009-06-11 17:35:47 -07004281
Romain Guy0fd89bf2011-01-26 15:41:30 -08004282 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004283 ListenerInfo li = mListenerInfo;
4284 if (li != null && li.mOnFocusChangeListener != null) {
4285 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 }
Joe Malin32736f02011-01-19 16:14:20 -08004287
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004288 if (mAttachInfo != null) {
4289 mAttachInfo.mKeyDispatchState.reset(this);
4290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 }
4292
4293 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004294 * Sends an accessibility event of the given type. If accessiiblity is
4295 * not enabled this method has no effect. The default implementation calls
4296 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4297 * to populate information about the event source (this View), then calls
4298 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4299 * populate the text content of the event source including its descendants,
4300 * and last calls
4301 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4302 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004303 * <p>
4304 * If an {@link AccessibilityDelegate} has been specified via calling
4305 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4306 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4307 * responsible for handling this call.
4308 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004309 *
Scott Mainb303d832011-10-12 16:45:18 -07004310 * @param eventType The type of the event to send, as defined by several types from
4311 * {@link android.view.accessibility.AccessibilityEvent}, such as
4312 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4313 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004314 *
4315 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4316 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4317 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004318 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004319 */
4320 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004321 if (mAccessibilityDelegate != null) {
4322 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4323 } else {
4324 sendAccessibilityEventInternal(eventType);
4325 }
4326 }
4327
4328 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004329 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4330 * {@link AccessibilityEvent} to make an announcement which is related to some
4331 * sort of a context change for which none of the events representing UI transitions
4332 * is a good fit. For example, announcing a new page in a book. If accessibility
4333 * is not enabled this method does nothing.
4334 *
4335 * @param text The announcement text.
4336 */
4337 public void announceForAccessibility(CharSequence text) {
4338 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4339 AccessibilityEvent event = AccessibilityEvent.obtain(
4340 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4341 event.getText().add(text);
4342 sendAccessibilityEventUnchecked(event);
4343 }
4344 }
4345
4346 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004347 * @see #sendAccessibilityEvent(int)
4348 *
4349 * Note: Called from the default {@link AccessibilityDelegate}.
4350 */
4351 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004352 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4353 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4354 }
4355 }
4356
4357 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004358 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4359 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004360 * perform a check whether accessibility is enabled.
4361 * <p>
4362 * If an {@link AccessibilityDelegate} has been specified via calling
4363 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4364 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4365 * is responsible for handling this call.
4366 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004367 *
4368 * @param event The event to send.
4369 *
4370 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004371 */
4372 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004373 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004374 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004375 } else {
4376 sendAccessibilityEventUncheckedInternal(event);
4377 }
4378 }
4379
4380 /**
4381 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4382 *
4383 * Note: Called from the default {@link AccessibilityDelegate}.
4384 */
4385 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004386 if (!isShown()) {
4387 return;
4388 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004389 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004390 // Only a subset of accessibility events populates text content.
4391 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4392 dispatchPopulateAccessibilityEvent(event);
4393 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004394 // Intercept accessibility focus events fired by virtual nodes to keep
4395 // track of accessibility focus position in such nodes.
4396 final int eventType = event.getEventType();
4397 switch (eventType) {
4398 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4399 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4400 event.getSourceNodeId());
4401 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4402 ViewRootImpl viewRootImpl = getViewRootImpl();
4403 if (viewRootImpl != null) {
4404 viewRootImpl.setAccessibilityFocusedHost(this);
4405 }
4406 }
4407 } break;
4408 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4409 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4410 event.getSourceNodeId());
4411 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4412 ViewRootImpl viewRootImpl = getViewRootImpl();
4413 if (viewRootImpl != null) {
4414 viewRootImpl.setAccessibilityFocusedHost(null);
4415 }
4416 }
4417 } break;
4418 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004419 // In the beginning we called #isShown(), so we know that getParent() is not null.
4420 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004421 }
4422
4423 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004424 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4425 * to its children for adding their text content to the event. Note that the
4426 * event text is populated in a separate dispatch path since we add to the
4427 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004428 * A typical implementation will call
4429 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4430 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4431 * on each child. Override this method if custom population of the event text
4432 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004433 * <p>
4434 * If an {@link AccessibilityDelegate} has been specified via calling
4435 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4436 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4437 * is responsible for handling this call.
4438 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004439 * <p>
4440 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4441 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4442 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004443 *
4444 * @param event The event.
4445 *
4446 * @return True if the event population was completed.
4447 */
4448 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004449 if (mAccessibilityDelegate != null) {
4450 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4451 } else {
4452 return dispatchPopulateAccessibilityEventInternal(event);
4453 }
4454 }
4455
4456 /**
4457 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4458 *
4459 * Note: Called from the default {@link AccessibilityDelegate}.
4460 */
4461 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004462 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004463 return false;
4464 }
4465
4466 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004467 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004468 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004469 * text content. While this method is free to modify event
4470 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004471 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4472 * <p>
4473 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004474 * to the text added by the super implementation:
4475 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004476 * super.onPopulateAccessibilityEvent(event);
4477 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4478 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4479 * mCurrentDate.getTimeInMillis(), flags);
4480 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004481 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004482 * <p>
4483 * If an {@link AccessibilityDelegate} has been specified via calling
4484 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4485 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4486 * is responsible for handling this call.
4487 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004488 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4489 * information to the event, in case the default implementation has basic information to add.
4490 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004491 *
4492 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004493 *
4494 * @see #sendAccessibilityEvent(int)
4495 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004496 */
4497 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004498 if (mAccessibilityDelegate != null) {
4499 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4500 } else {
4501 onPopulateAccessibilityEventInternal(event);
4502 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004503 }
4504
4505 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004506 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4507 *
4508 * Note: Called from the default {@link AccessibilityDelegate}.
4509 */
4510 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4511
4512 }
4513
4514 /**
4515 * Initializes an {@link AccessibilityEvent} with information about
4516 * this View which is the event source. In other words, the source of
4517 * an accessibility event is the view whose state change triggered firing
4518 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004519 * <p>
4520 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004521 * to properties set by the super implementation:
4522 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4523 * super.onInitializeAccessibilityEvent(event);
4524 * event.setPassword(true);
4525 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004526 * <p>
4527 * If an {@link AccessibilityDelegate} has been specified via calling
4528 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4529 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4530 * is responsible for handling this call.
4531 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004532 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4533 * information to the event, in case the default implementation has basic information to add.
4534 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004535 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004536 *
4537 * @see #sendAccessibilityEvent(int)
4538 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4539 */
4540 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004541 if (mAccessibilityDelegate != null) {
4542 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4543 } else {
4544 onInitializeAccessibilityEventInternal(event);
4545 }
4546 }
4547
4548 /**
4549 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4550 *
4551 * Note: Called from the default {@link AccessibilityDelegate}.
4552 */
4553 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004554 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004555 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004556 event.setPackageName(getContext().getPackageName());
4557 event.setEnabled(isEnabled());
4558 event.setContentDescription(mContentDescription);
4559
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004560 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004561 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004562 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4563 FOCUSABLES_ALL);
4564 event.setItemCount(focusablesTempList.size());
4565 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4566 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004567 }
4568 }
4569
4570 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004571 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4572 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4573 * This method is responsible for obtaining an accessibility node info from a
4574 * pool of reusable instances and calling
4575 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4576 * initialize the former.
4577 * <p>
4578 * Note: The client is responsible for recycling the obtained instance by calling
4579 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4580 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004581 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004582 * @return A populated {@link AccessibilityNodeInfo}.
4583 *
4584 * @see AccessibilityNodeInfo
4585 */
4586 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004587 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4588 if (provider != null) {
4589 return provider.createAccessibilityNodeInfo(View.NO_ID);
4590 } else {
4591 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4592 onInitializeAccessibilityNodeInfo(info);
4593 return info;
4594 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004595 }
4596
4597 /**
4598 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4599 * The base implementation sets:
4600 * <ul>
4601 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004602 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4603 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004604 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4605 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4606 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4607 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4608 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4609 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4610 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4611 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4612 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4613 * </ul>
4614 * <p>
4615 * Subclasses should override this method, call the super implementation,
4616 * and set additional attributes.
4617 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004618 * <p>
4619 * If an {@link AccessibilityDelegate} has been specified via calling
4620 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4621 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4622 * is responsible for handling this call.
4623 * </p>
4624 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004625 * @param info The instance to initialize.
4626 */
4627 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004628 if (mAccessibilityDelegate != null) {
4629 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4630 } else {
4631 onInitializeAccessibilityNodeInfoInternal(info);
4632 }
4633 }
4634
4635 /**
4636 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4637 *
4638 * Note: Called from the default {@link AccessibilityDelegate}.
4639 */
4640 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004641 Rect bounds = mAttachInfo.mTmpInvalRect;
4642 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004643 info.setBoundsInParent(bounds);
4644
Svetoslav Ganovaa6f3de2012-04-09 17:39:00 -07004645 getGlobalVisibleRect(bounds);
4646 bounds.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004647 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004648
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004649 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004650 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004651 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004652 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004653 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004654 }
4655
4656 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004657 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004658 info.setContentDescription(getContentDescription());
4659
4660 info.setEnabled(isEnabled());
4661 info.setClickable(isClickable());
4662 info.setFocusable(isFocusable());
4663 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004664 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004665 info.setSelected(isSelected());
4666 info.setLongClickable(isLongClickable());
4667
4668 // TODO: These make sense only if we are in an AdapterView but all
4669 // views can be selected. Maybe from accessiiblity perspective
4670 // we should report as selectable view in an AdapterView.
4671 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4672 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4673
4674 if (isFocusable()) {
4675 if (isFocused()) {
4676 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4677 } else {
4678 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4679 }
4680 }
4681 }
4682
4683 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004684 * Sets a delegate for implementing accessibility support via compositon as
4685 * opposed to inheritance. The delegate's primary use is for implementing
4686 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4687 *
4688 * @param delegate The delegate instance.
4689 *
4690 * @see AccessibilityDelegate
4691 */
4692 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4693 mAccessibilityDelegate = delegate;
4694 }
4695
4696 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004697 * Gets the provider for managing a virtual view hierarchy rooted at this View
4698 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4699 * that explore the window content.
4700 * <p>
4701 * If this method returns an instance, this instance is responsible for managing
4702 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4703 * View including the one representing the View itself. Similarly the returned
4704 * instance is responsible for performing accessibility actions on any virtual
4705 * view or the root view itself.
4706 * </p>
4707 * <p>
4708 * If an {@link AccessibilityDelegate} has been specified via calling
4709 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4710 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4711 * is responsible for handling this call.
4712 * </p>
4713 *
4714 * @return The provider.
4715 *
4716 * @see AccessibilityNodeProvider
4717 */
4718 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4719 if (mAccessibilityDelegate != null) {
4720 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4721 } else {
4722 return null;
4723 }
4724 }
4725
4726 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004727 * Gets the unique identifier of this view on the screen for accessibility purposes.
4728 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4729 *
4730 * @return The view accessibility id.
4731 *
4732 * @hide
4733 */
4734 public int getAccessibilityViewId() {
4735 if (mAccessibilityViewId == NO_ID) {
4736 mAccessibilityViewId = sNextAccessibilityViewId++;
4737 }
4738 return mAccessibilityViewId;
4739 }
4740
4741 /**
4742 * Gets the unique identifier of the window in which this View reseides.
4743 *
4744 * @return The window accessibility id.
4745 *
4746 * @hide
4747 */
4748 public int getAccessibilityWindowId() {
4749 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4750 }
4751
4752 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004753 * Gets the {@link View} description. It briefly describes the view and is
4754 * primarily used for accessibility support. Set this property to enable
4755 * better accessibility support for your application. This is especially
4756 * true for views that do not have textual representation (For example,
4757 * ImageButton).
4758 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004759 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004760 *
4761 * @attr ref android.R.styleable#View_contentDescription
4762 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004763 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004764 public CharSequence getContentDescription() {
4765 return mContentDescription;
4766 }
4767
4768 /**
4769 * Sets the {@link View} description. It briefly describes the view and is
4770 * primarily used for accessibility support. Set this property to enable
4771 * better accessibility support for your application. This is especially
4772 * true for views that do not have textual representation (For example,
4773 * ImageButton).
4774 *
4775 * @param contentDescription The content description.
4776 *
4777 * @attr ref android.R.styleable#View_contentDescription
4778 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004779 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004780 public void setContentDescription(CharSequence contentDescription) {
4781 mContentDescription = contentDescription;
4782 }
4783
4784 /**
Romain Guya2431d02009-04-30 16:30:00 -07004785 * Invoked whenever this view loses focus, either by losing window focus or by losing
4786 * focus within its window. This method can be used to clear any state tied to the
4787 * focus. For instance, if a button is held pressed with the trackball and the window
4788 * loses focus, this method can be used to cancel the press.
4789 *
4790 * Subclasses of View overriding this method should always call super.onFocusLost().
4791 *
4792 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004793 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004794 *
4795 * @hide pending API council approval
4796 */
4797 protected void onFocusLost() {
4798 resetPressedState();
4799 }
4800
4801 private void resetPressedState() {
4802 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4803 return;
4804 }
4805
4806 if (isPressed()) {
4807 setPressed(false);
4808
4809 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004810 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004811 }
4812 }
4813 }
4814
4815 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004816 * Returns true if this view has focus
4817 *
4818 * @return True if this view has focus, false otherwise.
4819 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004820 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 public boolean isFocused() {
4822 return (mPrivateFlags & FOCUSED) != 0;
4823 }
4824
4825 /**
4826 * Find the view in the hierarchy rooted at this view that currently has
4827 * focus.
4828 *
4829 * @return The view that currently has focus, or null if no focused view can
4830 * be found.
4831 */
4832 public View findFocus() {
4833 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4834 }
4835
4836 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004837 * Indicates whether this view is one of the set of scrollable containers in
4838 * its window.
4839 *
4840 * @return whether this view is one of the set of scrollable containers in
4841 * its window
4842 *
4843 * @attr ref android.R.styleable#View_isScrollContainer
4844 */
4845 public boolean isScrollContainer() {
4846 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4847 }
4848
4849 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850 * Change whether this view is one of the set of scrollable containers in
4851 * its window. This will be used to determine whether the window can
4852 * resize or must pan when a soft input area is open -- scrollable
4853 * containers allow the window to use resize mode since the container
4854 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004855 *
4856 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 */
4858 public void setScrollContainer(boolean isScrollContainer) {
4859 if (isScrollContainer) {
4860 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4861 mAttachInfo.mScrollContainers.add(this);
4862 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4863 }
4864 mPrivateFlags |= SCROLL_CONTAINER;
4865 } else {
4866 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4867 mAttachInfo.mScrollContainers.remove(this);
4868 }
4869 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4870 }
4871 }
4872
4873 /**
4874 * Returns the quality of the drawing cache.
4875 *
4876 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4877 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4878 *
4879 * @see #setDrawingCacheQuality(int)
4880 * @see #setDrawingCacheEnabled(boolean)
4881 * @see #isDrawingCacheEnabled()
4882 *
4883 * @attr ref android.R.styleable#View_drawingCacheQuality
4884 */
4885 public int getDrawingCacheQuality() {
4886 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4887 }
4888
4889 /**
4890 * Set the drawing cache quality of this view. This value is used only when the
4891 * drawing cache is enabled
4892 *
4893 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4894 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4895 *
4896 * @see #getDrawingCacheQuality()
4897 * @see #setDrawingCacheEnabled(boolean)
4898 * @see #isDrawingCacheEnabled()
4899 *
4900 * @attr ref android.R.styleable#View_drawingCacheQuality
4901 */
4902 public void setDrawingCacheQuality(int quality) {
4903 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4904 }
4905
4906 /**
4907 * Returns whether the screen should remain on, corresponding to the current
4908 * value of {@link #KEEP_SCREEN_ON}.
4909 *
4910 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4911 *
4912 * @see #setKeepScreenOn(boolean)
4913 *
4914 * @attr ref android.R.styleable#View_keepScreenOn
4915 */
4916 public boolean getKeepScreenOn() {
4917 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4918 }
4919
4920 /**
4921 * Controls whether the screen should remain on, modifying the
4922 * value of {@link #KEEP_SCREEN_ON}.
4923 *
4924 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4925 *
4926 * @see #getKeepScreenOn()
4927 *
4928 * @attr ref android.R.styleable#View_keepScreenOn
4929 */
4930 public void setKeepScreenOn(boolean keepScreenOn) {
4931 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4932 }
4933
4934 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004935 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4936 * @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 -08004937 *
4938 * @attr ref android.R.styleable#View_nextFocusLeft
4939 */
4940 public int getNextFocusLeftId() {
4941 return mNextFocusLeftId;
4942 }
4943
4944 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004945 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4946 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4947 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 *
4949 * @attr ref android.R.styleable#View_nextFocusLeft
4950 */
4951 public void setNextFocusLeftId(int nextFocusLeftId) {
4952 mNextFocusLeftId = nextFocusLeftId;
4953 }
4954
4955 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004956 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4957 * @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 -08004958 *
4959 * @attr ref android.R.styleable#View_nextFocusRight
4960 */
4961 public int getNextFocusRightId() {
4962 return mNextFocusRightId;
4963 }
4964
4965 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004966 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4967 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4968 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 *
4970 * @attr ref android.R.styleable#View_nextFocusRight
4971 */
4972 public void setNextFocusRightId(int nextFocusRightId) {
4973 mNextFocusRightId = nextFocusRightId;
4974 }
4975
4976 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004977 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4978 * @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 -08004979 *
4980 * @attr ref android.R.styleable#View_nextFocusUp
4981 */
4982 public int getNextFocusUpId() {
4983 return mNextFocusUpId;
4984 }
4985
4986 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004987 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4988 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4989 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990 *
4991 * @attr ref android.R.styleable#View_nextFocusUp
4992 */
4993 public void setNextFocusUpId(int nextFocusUpId) {
4994 mNextFocusUpId = nextFocusUpId;
4995 }
4996
4997 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004998 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4999 * @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 -08005000 *
5001 * @attr ref android.R.styleable#View_nextFocusDown
5002 */
5003 public int getNextFocusDownId() {
5004 return mNextFocusDownId;
5005 }
5006
5007 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005008 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5009 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5010 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 *
5012 * @attr ref android.R.styleable#View_nextFocusDown
5013 */
5014 public void setNextFocusDownId(int nextFocusDownId) {
5015 mNextFocusDownId = nextFocusDownId;
5016 }
5017
5018 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005019 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5020 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5021 *
5022 * @attr ref android.R.styleable#View_nextFocusForward
5023 */
5024 public int getNextFocusForwardId() {
5025 return mNextFocusForwardId;
5026 }
5027
5028 /**
5029 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5030 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5031 * decide automatically.
5032 *
5033 * @attr ref android.R.styleable#View_nextFocusForward
5034 */
5035 public void setNextFocusForwardId(int nextFocusForwardId) {
5036 mNextFocusForwardId = nextFocusForwardId;
5037 }
5038
5039 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 * Returns the visibility of this view and all of its ancestors
5041 *
5042 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5043 */
5044 public boolean isShown() {
5045 View current = this;
5046 //noinspection ConstantConditions
5047 do {
5048 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5049 return false;
5050 }
5051 ViewParent parent = current.mParent;
5052 if (parent == null) {
5053 return false; // We are not attached to the view root
5054 }
5055 if (!(parent instanceof View)) {
5056 return true;
5057 }
5058 current = (View) parent;
5059 } while (current != null);
5060
5061 return false;
5062 }
5063
5064 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005065 * Called by the view hierarchy when the content insets for a window have
5066 * changed, to allow it to adjust its content to fit within those windows.
5067 * The content insets tell you the space that the status bar, input method,
5068 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005070 * <p>You do not normally need to deal with this function, since the default
5071 * window decoration given to applications takes care of applying it to the
5072 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5073 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5074 * and your content can be placed under those system elements. You can then
5075 * use this method within your view hierarchy if you have parts of your UI
5076 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005077 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005078 * <p>The default implementation of this method simply applies the content
5079 * inset's to the view's padding. This can be enabled through
5080 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5081 * the method and handle the insets however you would like. Note that the
5082 * insets provided by the framework are always relative to the far edges
5083 * of the window, not accounting for the location of the called view within
5084 * that window. (In fact when this method is called you do not yet know
5085 * where the layout will place the view, as it is done before layout happens.)
5086 *
5087 * <p>Note: unlike many View methods, there is no dispatch phase to this
5088 * call. If you are overriding it in a ViewGroup and want to allow the
5089 * call to continue to your children, you must be sure to call the super
5090 * implementation.
5091 *
5092 * @param insets Current content insets of the window. Prior to
5093 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5094 * the insets or else you and Android will be unhappy.
5095 *
5096 * @return Return true if this view applied the insets and it should not
5097 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 */
5099 protected boolean fitSystemWindows(Rect insets) {
5100 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005101 mUserPaddingStart = -1;
5102 mUserPaddingEnd = -1;
5103 mUserPaddingRelative = false;
5104 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5105 || mAttachInfo == null
5106 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5107 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5108 return true;
5109 } else {
5110 internalSetPadding(0, 0, 0, 0);
5111 return false;
5112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 }
5114 return false;
5115 }
5116
5117 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005118 * Set whether or not this view should account for system screen decorations
5119 * such as the status bar and inset its content. This allows this view to be
5120 * positioned in absolute screen coordinates and remain visible to the user.
5121 *
5122 * <p>This should only be used by top-level window decor views.
5123 *
5124 * @param fitSystemWindows true to inset content for system screen decorations, false for
5125 * default behavior.
5126 *
5127 * @attr ref android.R.styleable#View_fitsSystemWindows
5128 */
5129 public void setFitsSystemWindows(boolean fitSystemWindows) {
5130 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5131 }
5132
5133 /**
5134 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5135 * will account for system screen decorations such as the status bar and inset its
5136 * content. This allows the view to be positioned in absolute screen coordinates
5137 * and remain visible to the user.
5138 *
5139 * @return true if this view will adjust its content bounds for system screen decorations.
5140 *
5141 * @attr ref android.R.styleable#View_fitsSystemWindows
5142 */
5143 public boolean fitsSystemWindows() {
5144 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5145 }
5146
5147 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005148 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5149 */
5150 public void requestFitSystemWindows() {
5151 if (mParent != null) {
5152 mParent.requestFitSystemWindows();
5153 }
5154 }
5155
5156 /**
5157 * For use by PhoneWindow to make its own system window fitting optional.
5158 * @hide
5159 */
5160 public void makeOptionalFitsSystemWindows() {
5161 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5162 }
5163
5164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 * Returns the visibility status for this view.
5166 *
5167 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5168 * @attr ref android.R.styleable#View_visibility
5169 */
5170 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005171 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5172 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5173 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 })
5175 public int getVisibility() {
5176 return mViewFlags & VISIBILITY_MASK;
5177 }
5178
5179 /**
5180 * Set the enabled state of this view.
5181 *
5182 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5183 * @attr ref android.R.styleable#View_visibility
5184 */
5185 @RemotableViewMethod
5186 public void setVisibility(int visibility) {
5187 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005188 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005189 }
5190
5191 /**
5192 * Returns the enabled status for this view. The interpretation of the
5193 * enabled state varies by subclass.
5194 *
5195 * @return True if this view is enabled, false otherwise.
5196 */
5197 @ViewDebug.ExportedProperty
5198 public boolean isEnabled() {
5199 return (mViewFlags & ENABLED_MASK) == ENABLED;
5200 }
5201
5202 /**
5203 * Set the enabled state of this view. The interpretation of the enabled
5204 * state varies by subclass.
5205 *
5206 * @param enabled True if this view is enabled, false otherwise.
5207 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005208 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005210 if (enabled == isEnabled()) return;
5211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5213
5214 /*
5215 * The View most likely has to change its appearance, so refresh
5216 * the drawable state.
5217 */
5218 refreshDrawableState();
5219
5220 // Invalidate too, since the default behavior for views is to be
5221 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005222 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 }
5224
5225 /**
5226 * Set whether this view can receive the focus.
5227 *
5228 * Setting this to false will also ensure that this view is not focusable
5229 * in touch mode.
5230 *
5231 * @param focusable If true, this view can receive the focus.
5232 *
5233 * @see #setFocusableInTouchMode(boolean)
5234 * @attr ref android.R.styleable#View_focusable
5235 */
5236 public void setFocusable(boolean focusable) {
5237 if (!focusable) {
5238 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5239 }
5240 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5241 }
5242
5243 /**
5244 * Set whether this view can receive focus while in touch mode.
5245 *
5246 * Setting this to true will also ensure that this view is focusable.
5247 *
5248 * @param focusableInTouchMode If true, this view can receive the focus while
5249 * in touch mode.
5250 *
5251 * @see #setFocusable(boolean)
5252 * @attr ref android.R.styleable#View_focusableInTouchMode
5253 */
5254 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5255 // Focusable in touch mode should always be set before the focusable flag
5256 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5257 // which, in touch mode, will not successfully request focus on this view
5258 // because the focusable in touch mode flag is not set
5259 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5260 if (focusableInTouchMode) {
5261 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5262 }
5263 }
5264
5265 /**
5266 * Set whether this view should have sound effects enabled for events such as
5267 * clicking and touching.
5268 *
5269 * <p>You may wish to disable sound effects for a view if you already play sounds,
5270 * for instance, a dial key that plays dtmf tones.
5271 *
5272 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5273 * @see #isSoundEffectsEnabled()
5274 * @see #playSoundEffect(int)
5275 * @attr ref android.R.styleable#View_soundEffectsEnabled
5276 */
5277 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5278 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5279 }
5280
5281 /**
5282 * @return whether this view should have sound effects enabled for events such as
5283 * clicking and touching.
5284 *
5285 * @see #setSoundEffectsEnabled(boolean)
5286 * @see #playSoundEffect(int)
5287 * @attr ref android.R.styleable#View_soundEffectsEnabled
5288 */
5289 @ViewDebug.ExportedProperty
5290 public boolean isSoundEffectsEnabled() {
5291 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5292 }
5293
5294 /**
5295 * Set whether this view should have haptic feedback for events such as
5296 * long presses.
5297 *
5298 * <p>You may wish to disable haptic feedback if your view already controls
5299 * its own haptic feedback.
5300 *
5301 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5302 * @see #isHapticFeedbackEnabled()
5303 * @see #performHapticFeedback(int)
5304 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5305 */
5306 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5307 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5308 }
5309
5310 /**
5311 * @return whether this view should have haptic feedback enabled for events
5312 * long presses.
5313 *
5314 * @see #setHapticFeedbackEnabled(boolean)
5315 * @see #performHapticFeedback(int)
5316 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5317 */
5318 @ViewDebug.ExportedProperty
5319 public boolean isHapticFeedbackEnabled() {
5320 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5321 }
5322
5323 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005324 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005325 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005326 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5327 * {@link #LAYOUT_DIRECTION_RTL},
5328 * {@link #LAYOUT_DIRECTION_INHERIT} or
5329 * {@link #LAYOUT_DIRECTION_LOCALE}.
5330 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005331 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005332 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005333 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5334 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5335 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5336 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005337 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005338 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005339 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005340 }
5341
5342 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005343 * Set the layout direction for this view. This will propagate a reset of layout direction
5344 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005345 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005346 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5347 * {@link #LAYOUT_DIRECTION_RTL},
5348 * {@link #LAYOUT_DIRECTION_INHERIT} or
5349 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005350 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005351 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005352 */
5353 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005354 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005355 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005356 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005357 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005358 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005359 // Set the new layout direction (filtered) and ask for a layout pass
5360 mPrivateFlags2 |=
5361 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5362 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005363 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005364 }
5365
5366 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005367 * Returns the resolved layout direction for this view.
5368 *
5369 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005370 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005371 */
5372 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005373 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5374 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005375 })
5376 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005377 // The layout diretion will be resolved only if needed
5378 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5379 resolveLayoutDirection();
5380 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005381 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005382 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5383 }
5384
5385 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005386 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5387 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005388 *
5389 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005390 */
5391 @ViewDebug.ExportedProperty(category = "layout")
5392 public boolean isLayoutRtl() {
5393 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5394 }
5395
5396 /**
Adam Powell539ee872012-02-03 19:00:49 -08005397 * Indicates whether the view is currently tracking transient state that the
5398 * app should not need to concern itself with saving and restoring, but that
5399 * the framework should take special note to preserve when possible.
5400 *
5401 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005402 */
5403 @ViewDebug.ExportedProperty(category = "layout")
5404 public boolean hasTransientState() {
5405 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5406 }
5407
5408 /**
5409 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005410 * framework should attempt to preserve when possible. This flag is reference counted,
5411 * so every call to setHasTransientState(true) should be paired with a later call
5412 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005413 *
5414 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005415 */
5416 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005417 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5418 mTransientStateCount - 1;
5419 if (mTransientStateCount < 0) {
5420 mTransientStateCount = 0;
5421 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5422 "unmatched pair of setHasTransientState calls");
5423 }
5424 if ((hasTransientState && mTransientStateCount == 1) ||
5425 (hasTransientState && mTransientStateCount == 0)) {
5426 // update flag if we've just incremented up from 0 or decremented down to 0
5427 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5428 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5429 if (mParent != null) {
5430 try {
5431 mParent.childHasTransientStateChanged(this, hasTransientState);
5432 } catch (AbstractMethodError e) {
5433 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5434 " does not fully implement ViewParent", e);
5435 }
Adam Powell539ee872012-02-03 19:00:49 -08005436 }
5437 }
5438 }
5439
5440 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 * If this view doesn't do any drawing on its own, set this flag to
5442 * allow further optimizations. By default, this flag is not set on
5443 * View, but could be set on some View subclasses such as ViewGroup.
5444 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005445 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5446 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 *
5448 * @param willNotDraw whether or not this View draw on its own
5449 */
5450 public void setWillNotDraw(boolean willNotDraw) {
5451 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5452 }
5453
5454 /**
5455 * Returns whether or not this View draws on its own.
5456 *
5457 * @return true if this view has nothing to draw, false otherwise
5458 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005459 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 public boolean willNotDraw() {
5461 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5462 }
5463
5464 /**
5465 * When a View's drawing cache is enabled, drawing is redirected to an
5466 * offscreen bitmap. Some views, like an ImageView, must be able to
5467 * bypass this mechanism if they already draw a single bitmap, to avoid
5468 * unnecessary usage of the memory.
5469 *
5470 * @param willNotCacheDrawing true if this view does not cache its
5471 * drawing, false otherwise
5472 */
5473 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5474 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5475 }
5476
5477 /**
5478 * Returns whether or not this View can cache its drawing or not.
5479 *
5480 * @return true if this view does not cache its drawing, false otherwise
5481 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005482 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 public boolean willNotCacheDrawing() {
5484 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5485 }
5486
5487 /**
5488 * Indicates whether this view reacts to click events or not.
5489 *
5490 * @return true if the view is clickable, false otherwise
5491 *
5492 * @see #setClickable(boolean)
5493 * @attr ref android.R.styleable#View_clickable
5494 */
5495 @ViewDebug.ExportedProperty
5496 public boolean isClickable() {
5497 return (mViewFlags & CLICKABLE) == CLICKABLE;
5498 }
5499
5500 /**
5501 * Enables or disables click events for this view. When a view
5502 * is clickable it will change its state to "pressed" on every click.
5503 * Subclasses should set the view clickable to visually react to
5504 * user's clicks.
5505 *
5506 * @param clickable true to make the view clickable, false otherwise
5507 *
5508 * @see #isClickable()
5509 * @attr ref android.R.styleable#View_clickable
5510 */
5511 public void setClickable(boolean clickable) {
5512 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5513 }
5514
5515 /**
5516 * Indicates whether this view reacts to long click events or not.
5517 *
5518 * @return true if the view is long clickable, false otherwise
5519 *
5520 * @see #setLongClickable(boolean)
5521 * @attr ref android.R.styleable#View_longClickable
5522 */
5523 public boolean isLongClickable() {
5524 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5525 }
5526
5527 /**
5528 * Enables or disables long click events for this view. When a view is long
5529 * clickable it reacts to the user holding down the button for a longer
5530 * duration than a tap. This event can either launch the listener or a
5531 * context menu.
5532 *
5533 * @param longClickable true to make the view long clickable, false otherwise
5534 * @see #isLongClickable()
5535 * @attr ref android.R.styleable#View_longClickable
5536 */
5537 public void setLongClickable(boolean longClickable) {
5538 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5539 }
5540
5541 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005542 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 *
5544 * @see #isClickable()
5545 * @see #setClickable(boolean)
5546 *
5547 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5548 * the View's internal state from a previously set "pressed" state.
5549 */
5550 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005551 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 if (pressed) {
5554 mPrivateFlags |= PRESSED;
5555 } else {
5556 mPrivateFlags &= ~PRESSED;
5557 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005558
5559 if (needsRefresh) {
5560 refreshDrawableState();
5561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 dispatchSetPressed(pressed);
5563 }
5564
5565 /**
5566 * Dispatch setPressed to all of this View's children.
5567 *
5568 * @see #setPressed(boolean)
5569 *
5570 * @param pressed The new pressed state
5571 */
5572 protected void dispatchSetPressed(boolean pressed) {
5573 }
5574
5575 /**
5576 * Indicates whether the view is currently in pressed state. Unless
5577 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5578 * the pressed state.
5579 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005580 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 * @see #isClickable()
5582 * @see #setClickable(boolean)
5583 *
5584 * @return true if the view is currently pressed, false otherwise
5585 */
5586 public boolean isPressed() {
5587 return (mPrivateFlags & PRESSED) == PRESSED;
5588 }
5589
5590 /**
5591 * Indicates whether this view will save its state (that is,
5592 * whether its {@link #onSaveInstanceState} method will be called).
5593 *
5594 * @return Returns true if the view state saving is enabled, else false.
5595 *
5596 * @see #setSaveEnabled(boolean)
5597 * @attr ref android.R.styleable#View_saveEnabled
5598 */
5599 public boolean isSaveEnabled() {
5600 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5601 }
5602
5603 /**
5604 * Controls whether the saving of this view's state is
5605 * enabled (that is, whether its {@link #onSaveInstanceState} method
5606 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005607 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 * for its state to be saved. This flag can only disable the
5609 * saving of this view; any child views may still have their state saved.
5610 *
5611 * @param enabled Set to false to <em>disable</em> state saving, or true
5612 * (the default) to allow it.
5613 *
5614 * @see #isSaveEnabled()
5615 * @see #setId(int)
5616 * @see #onSaveInstanceState()
5617 * @attr ref android.R.styleable#View_saveEnabled
5618 */
5619 public void setSaveEnabled(boolean enabled) {
5620 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5621 }
5622
Jeff Brown85a31762010-09-01 17:01:00 -07005623 /**
5624 * Gets whether the framework should discard touches when the view's
5625 * window is obscured by another visible window.
5626 * Refer to the {@link View} security documentation for more details.
5627 *
5628 * @return True if touch filtering is enabled.
5629 *
5630 * @see #setFilterTouchesWhenObscured(boolean)
5631 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5632 */
5633 @ViewDebug.ExportedProperty
5634 public boolean getFilterTouchesWhenObscured() {
5635 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5636 }
5637
5638 /**
5639 * Sets whether the framework should discard touches when the view's
5640 * window is obscured by another visible window.
5641 * Refer to the {@link View} security documentation for more details.
5642 *
5643 * @param enabled True if touch filtering should be enabled.
5644 *
5645 * @see #getFilterTouchesWhenObscured
5646 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5647 */
5648 public void setFilterTouchesWhenObscured(boolean enabled) {
5649 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5650 FILTER_TOUCHES_WHEN_OBSCURED);
5651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652
5653 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005654 * Indicates whether the entire hierarchy under this view will save its
5655 * state when a state saving traversal occurs from its parent. The default
5656 * is true; if false, these views will not be saved unless
5657 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5658 *
5659 * @return Returns true if the view state saving from parent is enabled, else false.
5660 *
5661 * @see #setSaveFromParentEnabled(boolean)
5662 */
5663 public boolean isSaveFromParentEnabled() {
5664 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5665 }
5666
5667 /**
5668 * Controls whether the entire hierarchy under this view will save its
5669 * state when a state saving traversal occurs from its parent. The default
5670 * is true; if false, these views will not be saved unless
5671 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5672 *
5673 * @param enabled Set to false to <em>disable</em> state saving, or true
5674 * (the default) to allow it.
5675 *
5676 * @see #isSaveFromParentEnabled()
5677 * @see #setId(int)
5678 * @see #onSaveInstanceState()
5679 */
5680 public void setSaveFromParentEnabled(boolean enabled) {
5681 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5682 }
5683
5684
5685 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 * Returns whether this View is able to take focus.
5687 *
5688 * @return True if this view can take focus, or false otherwise.
5689 * @attr ref android.R.styleable#View_focusable
5690 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005691 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005692 public final boolean isFocusable() {
5693 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5694 }
5695
5696 /**
5697 * When a view is focusable, it may not want to take focus when in touch mode.
5698 * For example, a button would like focus when the user is navigating via a D-pad
5699 * so that the user can click on it, but once the user starts touching the screen,
5700 * the button shouldn't take focus
5701 * @return Whether the view is focusable in touch mode.
5702 * @attr ref android.R.styleable#View_focusableInTouchMode
5703 */
5704 @ViewDebug.ExportedProperty
5705 public final boolean isFocusableInTouchMode() {
5706 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5707 }
5708
5709 /**
5710 * Find the nearest view in the specified direction that can take focus.
5711 * This does not actually give focus to that view.
5712 *
5713 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5714 *
5715 * @return The nearest focusable in the specified direction, or null if none
5716 * can be found.
5717 */
5718 public View focusSearch(int direction) {
5719 if (mParent != null) {
5720 return mParent.focusSearch(this, direction);
5721 } else {
5722 return null;
5723 }
5724 }
5725
5726 /**
5727 * This method is the last chance for the focused view and its ancestors to
5728 * respond to an arrow key. This is called when the focused view did not
5729 * consume the key internally, nor could the view system find a new view in
5730 * the requested direction to give focus to.
5731 *
5732 * @param focused The currently focused view.
5733 * @param direction The direction focus wants to move. One of FOCUS_UP,
5734 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5735 * @return True if the this view consumed this unhandled move.
5736 */
5737 public boolean dispatchUnhandledMove(View focused, int direction) {
5738 return false;
5739 }
5740
5741 /**
5742 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005743 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005745 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5746 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 * @return The user specified next view, or null if there is none.
5748 */
5749 View findUserSetNextFocus(View root, int direction) {
5750 switch (direction) {
5751 case FOCUS_LEFT:
5752 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005753 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 case FOCUS_RIGHT:
5755 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005756 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 case FOCUS_UP:
5758 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005759 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 case FOCUS_DOWN:
5761 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005762 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005763 case FOCUS_FORWARD:
5764 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005765 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005766 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005767 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005768 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005769 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005770 @Override
5771 public boolean apply(View t) {
5772 return t.mNextFocusForwardId == id;
5773 }
5774 });
5775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 }
5777 return null;
5778 }
5779
Jeff Brown4dfbec22011-08-15 14:55:37 -07005780 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5781 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5782 @Override
5783 public boolean apply(View t) {
5784 return t.mID == childViewId;
5785 }
5786 });
5787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 if (result == null) {
5789 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5790 + "by user for id " + childViewId);
5791 }
5792 return result;
5793 }
5794
5795 /**
5796 * Find and return all focusable views that are descendants of this view,
5797 * possibly including this view if it is focusable itself.
5798 *
5799 * @param direction The direction of the focus
5800 * @return A list of focusable views
5801 */
5802 public ArrayList<View> getFocusables(int direction) {
5803 ArrayList<View> result = new ArrayList<View>(24);
5804 addFocusables(result, direction);
5805 return result;
5806 }
5807
5808 /**
5809 * Add any focusable views that are descendants of this view (possibly
5810 * including this view if it is focusable itself) to views. If we are in touch mode,
5811 * only add views that are also focusable in touch mode.
5812 *
5813 * @param views Focusable views found so far
5814 * @param direction The direction of the focus
5815 */
5816 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005817 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005819
svetoslavganov75986cf2009-05-14 22:28:01 -07005820 /**
5821 * Adds any focusable views that are descendants of this view (possibly
5822 * including this view if it is focusable itself) to views. This method
5823 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005824 * only views focusable in touch mode if we are in touch mode or
5825 * only views that can take accessibility focus if accessibility is enabeld
5826 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005827 *
5828 * @param views Focusable views found so far or null if all we are interested is
5829 * the number of focusables.
5830 * @param direction The direction of the focus.
5831 * @param focusableMode The type of focusables to be added.
5832 *
5833 * @see #FOCUSABLES_ALL
5834 * @see #FOCUSABLES_TOUCH_MODE
5835 */
5836 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005837 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005838 return;
5839 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005840 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5841 if (AccessibilityManager.getInstance(mContext).isEnabled()
5842 && includeForAccessibility()) {
5843 views.add(this);
5844 return;
5845 }
5846 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005847 if (!isFocusable()) {
5848 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005849 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005850 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5851 && isInTouchMode() && !isFocusableInTouchMode()) {
5852 return;
5853 }
5854 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 }
5856
5857 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005858 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005859 * The search is performed by either the text that the View renders or the content
5860 * description that describes the view for accessibility purposes and the view does
5861 * not render or both. Clients can specify how the search is to be performed via
5862 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5863 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005864 *
5865 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005866 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005867 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005868 * @see #FIND_VIEWS_WITH_TEXT
5869 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5870 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005871 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005872 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005873 if (getAccessibilityNodeProvider() != null) {
5874 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5875 outViews.add(this);
5876 }
5877 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5878 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005879 String searchedLowerCase = searched.toString().toLowerCase();
5880 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5881 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5882 outViews.add(this);
5883 }
5884 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005885 }
5886
5887 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 * Find and return all touchable views that are descendants of this view,
5889 * possibly including this view if it is touchable itself.
5890 *
5891 * @return A list of touchable views
5892 */
5893 public ArrayList<View> getTouchables() {
5894 ArrayList<View> result = new ArrayList<View>();
5895 addTouchables(result);
5896 return result;
5897 }
5898
5899 /**
5900 * Add any touchable views that are descendants of this view (possibly
5901 * including this view if it is touchable itself) to views.
5902 *
5903 * @param views Touchable views found so far
5904 */
5905 public void addTouchables(ArrayList<View> views) {
5906 final int viewFlags = mViewFlags;
5907
5908 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5909 && (viewFlags & ENABLED_MASK) == ENABLED) {
5910 views.add(this);
5911 }
5912 }
5913
5914 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07005915 * Returns whether this View is accessibility focused.
5916 *
5917 * @return True if this View is accessibility focused.
5918 */
5919 boolean isAccessibilityFocused() {
5920 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
5921 }
5922
5923 /**
5924 * Call this to try to give accessibility focus to this view.
5925 *
5926 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
5927 * returns false or the view is no visible or the view already has accessibility
5928 * focus.
5929 *
5930 * See also {@link #focusSearch(int)}, which is what you call to say that you
5931 * have focus, and you want your parent to look for the next one.
5932 *
5933 * @return Whether this view actually took accessibility focus.
5934 *
5935 * @hide
5936 */
5937 public boolean requestAccessibilityFocus() {
5938 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
5939 return false;
5940 }
5941 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5942 return false;
5943 }
5944 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
5945 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
5946 ViewRootImpl viewRootImpl = getViewRootImpl();
5947 if (viewRootImpl != null) {
5948 viewRootImpl.setAccessibilityFocusedHost(this);
5949 }
5950 invalidate();
5951 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
5952 notifyAccessibilityStateChanged();
5953 // Try to give input focus to this view - not a descendant.
5954 requestFocusNoSearch(View.FOCUS_DOWN, null);
5955 return true;
5956 }
5957 return false;
5958 }
5959
5960 /**
5961 * Call this to try to clear accessibility focus of this view.
5962 *
5963 * See also {@link #focusSearch(int)}, which is what you call to say that you
5964 * have focus, and you want your parent to look for the next one.
5965 *
5966 * @hide
5967 */
5968 public void clearAccessibilityFocus() {
5969 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
5970 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
5971 ViewRootImpl viewRootImpl = getViewRootImpl();
5972 if (viewRootImpl != null) {
5973 viewRootImpl.setAccessibilityFocusedHost(null);
5974 }
5975 invalidate();
5976 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
5977 notifyAccessibilityStateChanged();
5978 // Try to move accessibility focus to the input focus.
5979 View rootView = getRootView();
5980 if (rootView != null) {
5981 View inputFocus = rootView.findFocus();
5982 if (inputFocus != null) {
5983 inputFocus.requestAccessibilityFocus();
5984 }
5985 }
5986 }
5987 }
5988
5989 /**
5990 * Find the best view to take accessibility focus from a hover.
5991 * This function finds the deepest actionable view and if that
5992 * fails ask the parent to take accessibility focus from hover.
5993 *
5994 * @param x The X hovered location in this view coorditantes.
5995 * @param y The Y hovered location in this view coorditantes.
5996 * @return Whether the request was handled.
5997 *
5998 * @hide
5999 */
6000 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6001 if (onRequestAccessibilityFocusFromHover(x, y)) {
6002 return true;
6003 }
6004 ViewParent parent = mParent;
6005 if (parent instanceof View) {
6006 View parentView = (View) parent;
6007
6008 float[] position = mAttachInfo.mTmpTransformLocation;
6009 position[0] = x;
6010 position[1] = y;
6011
6012 // Compensate for the transformation of the current matrix.
6013 if (!hasIdentityMatrix()) {
6014 getMatrix().mapPoints(position);
6015 }
6016
6017 // Compensate for the parent scroll and the offset
6018 // of this view stop from the parent top.
6019 position[0] += mLeft - parentView.mScrollX;
6020 position[1] += mTop - parentView.mScrollY;
6021
6022 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6023 }
6024 return false;
6025 }
6026
6027 /**
6028 * Requests to give this View focus from hover.
6029 *
6030 * @param x The X hovered location in this view coorditantes.
6031 * @param y The Y hovered location in this view coorditantes.
6032 * @return Whether the request was handled.
6033 *
6034 * @hide
6035 */
6036 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6037 if (includeForAccessibility()
6038 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6039 return requestAccessibilityFocus();
6040 }
6041 return false;
6042 }
6043
6044 /**
6045 * Clears accessibility focus without calling any callback methods
6046 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6047 * is used for clearing accessibility focus when giving this focus to
6048 * another view.
6049 */
6050 void clearAccessibilityFocusNoCallbacks() {
6051 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6052 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6053 invalidate();
6054 }
6055 }
6056
6057 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006058 * Call this to try to give focus to a specific view or to one of its
6059 * descendants.
6060 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006061 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6062 * false), or if it is focusable and it is not focusable in touch mode
6063 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006064 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006065 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 * have focus, and you want your parent to look for the next one.
6067 *
6068 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6069 * {@link #FOCUS_DOWN} and <code>null</code>.
6070 *
6071 * @return Whether this view or one of its descendants actually took focus.
6072 */
6073 public final boolean requestFocus() {
6074 return requestFocus(View.FOCUS_DOWN);
6075 }
6076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 /**
6078 * Call this to try to give focus to a specific view or to one of its
6079 * descendants and give it a hint about what direction focus is heading.
6080 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006081 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6082 * false), or if it is focusable and it is not focusable in touch mode
6083 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006085 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 * have focus, and you want your parent to look for the next one.
6087 *
6088 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6089 * <code>null</code> set for the previously focused rectangle.
6090 *
6091 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6092 * @return Whether this view or one of its descendants actually took focus.
6093 */
6094 public final boolean requestFocus(int direction) {
6095 return requestFocus(direction, null);
6096 }
6097
6098 /**
6099 * Call this to try to give focus to a specific view or to one of its descendants
6100 * and give it hints about the direction and a specific rectangle that the focus
6101 * is coming from. The rectangle can help give larger views a finer grained hint
6102 * about where focus is coming from, and therefore, where to show selection, or
6103 * forward focus change internally.
6104 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006105 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6106 * false), or if it is focusable and it is not focusable in touch mode
6107 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 *
6109 * A View will not take focus if it is not visible.
6110 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006111 * A View will not take focus if one of its parents has
6112 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6113 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006115 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 * have focus, and you want your parent to look for the next one.
6117 *
6118 * You may wish to override this method if your custom {@link View} has an internal
6119 * {@link View} that it wishes to forward the request to.
6120 *
6121 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6122 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6123 * to give a finer grained hint about where focus is coming from. May be null
6124 * if there is no hint.
6125 * @return Whether this view or one of its descendants actually took focus.
6126 */
6127 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006128 return requestFocusNoSearch(direction, previouslyFocusedRect);
6129 }
6130
6131 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 // need to be focusable
6133 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6134 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6135 return false;
6136 }
6137
6138 // need to be focusable in touch mode if in touch mode
6139 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006140 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6141 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006142 }
6143
6144 // need to not have any parents blocking us
6145 if (hasAncestorThatBlocksDescendantFocus()) {
6146 return false;
6147 }
6148
6149 handleFocusGainInternal(direction, previouslyFocusedRect);
6150 return true;
6151 }
6152
6153 /**
6154 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6155 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6156 * touch mode to request focus when they are touched.
6157 *
6158 * @return Whether this view or one of its descendants actually took focus.
6159 *
6160 * @see #isInTouchMode()
6161 *
6162 */
6163 public final boolean requestFocusFromTouch() {
6164 // Leave touch mode if we need to
6165 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006166 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006167 if (viewRoot != null) {
6168 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 }
6170 }
6171 return requestFocus(View.FOCUS_DOWN);
6172 }
6173
6174 /**
6175 * @return Whether any ancestor of this view blocks descendant focus.
6176 */
6177 private boolean hasAncestorThatBlocksDescendantFocus() {
6178 ViewParent ancestor = mParent;
6179 while (ancestor instanceof ViewGroup) {
6180 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6181 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6182 return true;
6183 } else {
6184 ancestor = vgAncestor.getParent();
6185 }
6186 }
6187 return false;
6188 }
6189
6190 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006191 * Gets the mode for determining whether this View is important for accessibility
6192 * which is if it fires accessibility events and if it is reported to
6193 * accessibility services that query the screen.
6194 *
6195 * @return The mode for determining whether a View is important for accessibility.
6196 *
6197 * @attr ref android.R.styleable#View_importantForAccessibility
6198 *
6199 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6200 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6201 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6202 */
6203 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6204 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6205 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6206 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6207 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6208 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6209 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6210 })
6211 public int getImportantForAccessibility() {
6212 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6213 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6214 }
6215
6216 /**
6217 * Sets how to determine whether this view is important for accessibility
6218 * which is if it fires accessibility events and if it is reported to
6219 * accessibility services that query the screen.
6220 *
6221 * @param mode How to determine whether this view is important for accessibility.
6222 *
6223 * @attr ref android.R.styleable#View_importantForAccessibility
6224 *
6225 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6226 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6227 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6228 */
6229 public void setImportantForAccessibility(int mode) {
6230 if (mode != getImportantForAccessibility()) {
6231 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6232 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6233 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6234 notifyAccessibilityStateChanged();
6235 }
6236 }
6237
6238 /**
6239 * Gets whether this view should be exposed for accessibility.
6240 *
6241 * @return Whether the view is exposed for accessibility.
6242 *
6243 * @hide
6244 */
6245 public boolean isImportantForAccessibility() {
6246 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6247 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6248 switch (mode) {
6249 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6250 return true;
6251 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6252 return false;
6253 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6254 return isActionableForAccessibility() || hasListenersForAccessibility();
6255 default:
6256 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6257 + mode);
6258 }
6259 }
6260
6261 /**
6262 * Gets the parent for accessibility purposes. Note that the parent for
6263 * accessibility is not necessary the immediate parent. It is the first
6264 * predecessor that is important for accessibility.
6265 *
6266 * @return The parent for accessibility purposes.
6267 */
6268 public ViewParent getParentForAccessibility() {
6269 if (mParent instanceof View) {
6270 View parentView = (View) mParent;
6271 if (parentView.includeForAccessibility()) {
6272 return mParent;
6273 } else {
6274 return mParent.getParentForAccessibility();
6275 }
6276 }
6277 return null;
6278 }
6279
6280 /**
6281 * Adds the children of a given View for accessibility. Since some Views are
6282 * not important for accessibility the children for accessibility are not
6283 * necessarily direct children of the riew, rather they are the first level of
6284 * descendants important for accessibility.
6285 *
6286 * @param children The list of children for accessibility.
6287 */
6288 public void addChildrenForAccessibility(ArrayList<View> children) {
6289 if (includeForAccessibility()) {
6290 children.add(this);
6291 }
6292 }
6293
6294 /**
6295 * Whether to regard this view for accessibility. A view is regarded for
6296 * accessibility if it is important for accessibility or the querying
6297 * accessibility service has explicitly requested that view not
6298 * important for accessibility are regarded.
6299 *
6300 * @return Whether to regard the view for accessibility.
6301 */
6302 boolean includeForAccessibility() {
6303 if (mAttachInfo != null) {
6304 if (!mAttachInfo.mIncludeNotImportantViews) {
6305 return isImportantForAccessibility();
6306 } else {
6307 return true;
6308 }
6309 }
6310 return false;
6311 }
6312
6313 /**
6314 * Returns whether the View is considered actionable from
6315 * accessibility perspective. Such view are important for
6316 * accessiiblity.
6317 *
6318 * @return True if the view is actionable for accessibility.
6319 */
6320 private boolean isActionableForAccessibility() {
6321 return (isClickable() || isLongClickable() || isFocusable());
6322 }
6323
6324 /**
6325 * Returns whether the View has registered callbacks wich makes it
6326 * important for accessiiblity.
6327 *
6328 * @return True if the view is actionable for accessibility.
6329 */
6330 private boolean hasListenersForAccessibility() {
6331 ListenerInfo info = getListenerInfo();
6332 return mTouchDelegate != null || info.mOnKeyListener != null
6333 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6334 || info.mOnHoverListener != null || info.mOnDragListener != null;
6335 }
6336
6337 /**
6338 * Notifies accessibility services that some view's important for
6339 * accessibility state has changed. Note that such notifications
6340 * are made at most once every
6341 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6342 * to avoid unnecessary load to the system. Also once a view has
6343 * made a notifucation this method is a NOP until the notification has
6344 * been sent to clients.
6345 *
6346 * @hide
6347 *
6348 * TODO: Makse sure this method is called for any view state change
6349 * that is interesting for accessilility purposes.
6350 */
6351 public void notifyAccessibilityStateChanged() {
6352 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6353 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6354 if (mParent != null) {
6355 mParent.childAccessibilityStateChanged(this);
6356 }
6357 }
6358 }
6359
6360 /**
6361 * Reset the state indicating the this view has requested clients
6362 * interested in its accessiblity state to be notified.
6363 *
6364 * @hide
6365 */
6366 public void resetAccessibilityStateChanged() {
6367 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6368 }
6369
6370 /**
6371 * Performs the specified accessibility action on the view. For
6372 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6373 *
6374 * @param action The action to perform.
6375 * @return Whether the action was performed.
6376 */
6377 public boolean performAccessibilityAction(int action) {
6378 switch (action) {
6379 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006380 if (isClickable()) {
6381 performClick();
6382 }
6383 } break;
6384 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6385 if (isLongClickable()) {
6386 performLongClick();
6387 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006388 } break;
6389 case AccessibilityNodeInfo.ACTION_FOCUS: {
6390 if (!hasFocus()) {
6391 // Get out of touch mode since accessibility
6392 // wants to move focus around.
6393 getViewRootImpl().ensureTouchMode(false);
6394 return requestFocus();
6395 }
6396 } break;
6397 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6398 if (hasFocus()) {
6399 clearFocus();
6400 return !isFocused();
6401 }
6402 } break;
6403 case AccessibilityNodeInfo.ACTION_SELECT: {
6404 if (!isSelected()) {
6405 setSelected(true);
6406 return isSelected();
6407 }
6408 } break;
6409 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6410 if (isSelected()) {
6411 setSelected(false);
6412 return !isSelected();
6413 }
6414 } break;
6415 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6416 if (!isAccessibilityFocused()) {
6417 return requestAccessibilityFocus();
6418 }
6419 } break;
6420 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6421 if (isAccessibilityFocused()) {
6422 clearAccessibilityFocus();
6423 return true;
6424 }
6425 } break;
6426 }
6427 return false;
6428 }
6429
6430 /**
Romain Guya440b002010-02-24 15:57:54 -08006431 * @hide
6432 */
6433 public void dispatchStartTemporaryDetach() {
6434 onStartTemporaryDetach();
6435 }
6436
6437 /**
6438 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6440 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006441 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006442 */
6443 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006444 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006445 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006446 }
6447
6448 /**
6449 * @hide
6450 */
6451 public void dispatchFinishTemporaryDetach() {
6452 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006453 }
Romain Guy8506ab42009-06-11 17:35:47 -07006454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 /**
6456 * Called after {@link #onStartTemporaryDetach} when the container is done
6457 * changing the view.
6458 */
6459 public void onFinishTemporaryDetach() {
6460 }
Romain Guy8506ab42009-06-11 17:35:47 -07006461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006463 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6464 * for this view's window. Returns null if the view is not currently attached
6465 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006466 * just use the standard high-level event callbacks like
6467 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006468 */
6469 public KeyEvent.DispatcherState getKeyDispatcherState() {
6470 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6471 }
Joe Malin32736f02011-01-19 16:14:20 -08006472
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 * Dispatch a key event before it is processed by any input method
6475 * associated with the view hierarchy. This can be used to intercept
6476 * key events in special situations before the IME consumes them; a
6477 * typical example would be handling the BACK key to update the application's
6478 * UI instead of allowing the IME to see it and close itself.
6479 *
6480 * @param event The key event to be dispatched.
6481 * @return True if the event was handled, false otherwise.
6482 */
6483 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6484 return onKeyPreIme(event.getKeyCode(), event);
6485 }
6486
6487 /**
6488 * Dispatch a key event to the next view on the focus path. This path runs
6489 * from the top of the view tree down to the currently focused view. If this
6490 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6491 * the next node down the focus path. This method also fires any key
6492 * listeners.
6493 *
6494 * @param event The key event to be dispatched.
6495 * @return True if the event was handled, false otherwise.
6496 */
6497 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006498 if (mInputEventConsistencyVerifier != null) {
6499 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501
Jeff Brown21bc5c92011-02-28 18:27:14 -08006502 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006503 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006504 ListenerInfo li = mListenerInfo;
6505 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6506 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 return true;
6508 }
6509
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006510 if (event.dispatch(this, mAttachInfo != null
6511 ? mAttachInfo.mKeyDispatchState : null, this)) {
6512 return true;
6513 }
6514
6515 if (mInputEventConsistencyVerifier != null) {
6516 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6517 }
6518 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006519 }
6520
6521 /**
6522 * Dispatches a key shortcut event.
6523 *
6524 * @param event The key event to be dispatched.
6525 * @return True if the event was handled by the view, false otherwise.
6526 */
6527 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6528 return onKeyShortcut(event.getKeyCode(), event);
6529 }
6530
6531 /**
6532 * Pass the touch screen motion event down to the target view, or this
6533 * view if it is the target.
6534 *
6535 * @param event The motion event to be dispatched.
6536 * @return True if the event was handled by the view, false otherwise.
6537 */
6538 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006539 if (mInputEventConsistencyVerifier != null) {
6540 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6541 }
6542
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006543 if (onFilterTouchEventForSecurity(event)) {
6544 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006545 ListenerInfo li = mListenerInfo;
6546 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6547 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006548 return true;
6549 }
6550
6551 if (onTouchEvent(event)) {
6552 return true;
6553 }
Jeff Brown85a31762010-09-01 17:01:00 -07006554 }
6555
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006556 if (mInputEventConsistencyVerifier != null) {
6557 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006559 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 }
6561
6562 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006563 * Filter the touch event to apply security policies.
6564 *
6565 * @param event The motion event to be filtered.
6566 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006567 *
Jeff Brown85a31762010-09-01 17:01:00 -07006568 * @see #getFilterTouchesWhenObscured
6569 */
6570 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006571 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006572 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6573 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6574 // Window is obscured, drop this touch.
6575 return false;
6576 }
6577 return true;
6578 }
6579
6580 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 * Pass a trackball motion event down to the focused view.
6582 *
6583 * @param event The motion event to be dispatched.
6584 * @return True if the event was handled by the view, false otherwise.
6585 */
6586 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006587 if (mInputEventConsistencyVerifier != null) {
6588 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6589 }
6590
Romain Guy02ccac62011-06-24 13:20:23 -07006591 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006592 }
6593
6594 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006595 * Dispatch a generic motion event.
6596 * <p>
6597 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6598 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006599 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006600 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006601 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006602 *
6603 * @param event The motion event to be dispatched.
6604 * @return True if the event was handled by the view, false otherwise.
6605 */
6606 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006607 if (mInputEventConsistencyVerifier != null) {
6608 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6609 }
6610
Jeff Browna032cc02011-03-07 16:56:21 -08006611 final int source = event.getSource();
6612 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6613 final int action = event.getAction();
6614 if (action == MotionEvent.ACTION_HOVER_ENTER
6615 || action == MotionEvent.ACTION_HOVER_MOVE
6616 || action == MotionEvent.ACTION_HOVER_EXIT) {
6617 if (dispatchHoverEvent(event)) {
6618 return true;
6619 }
6620 } else if (dispatchGenericPointerEvent(event)) {
6621 return true;
6622 }
6623 } else if (dispatchGenericFocusedEvent(event)) {
6624 return true;
6625 }
6626
Jeff Brown10b62902011-06-20 16:40:37 -07006627 if (dispatchGenericMotionEventInternal(event)) {
6628 return true;
6629 }
6630
6631 if (mInputEventConsistencyVerifier != null) {
6632 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6633 }
6634 return false;
6635 }
6636
6637 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006638 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006639 ListenerInfo li = mListenerInfo;
6640 if (li != null && li.mOnGenericMotionListener != null
6641 && (mViewFlags & ENABLED_MASK) == ENABLED
6642 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006643 return true;
6644 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006645
6646 if (onGenericMotionEvent(event)) {
6647 return true;
6648 }
6649
6650 if (mInputEventConsistencyVerifier != null) {
6651 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6652 }
6653 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006654 }
6655
6656 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006657 * Dispatch a hover event.
6658 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006659 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006660 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006661 * </p>
6662 *
6663 * @param event The motion event to be dispatched.
6664 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006665 */
6666 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006667 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006668 ListenerInfo li = mListenerInfo;
6669 if (li != null && li.mOnHoverListener != null
6670 && (mViewFlags & ENABLED_MASK) == ENABLED
6671 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006672 return true;
6673 }
6674
Jeff Browna032cc02011-03-07 16:56:21 -08006675 return onHoverEvent(event);
6676 }
6677
6678 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006679 * Returns true if the view has a child to which it has recently sent
6680 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6681 * it does not have a hovered child, then it must be the innermost hovered view.
6682 * @hide
6683 */
6684 protected boolean hasHoveredChild() {
6685 return false;
6686 }
6687
6688 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006689 * Dispatch a generic motion event to the view under the first pointer.
6690 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006691 * Do not call this method directly.
6692 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006693 * </p>
6694 *
6695 * @param event The motion event to be dispatched.
6696 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006697 */
6698 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6699 return false;
6700 }
6701
6702 /**
6703 * Dispatch a generic motion event to the currently focused view.
6704 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006705 * Do not call this method directly.
6706 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006707 * </p>
6708 *
6709 * @param event The motion event to be dispatched.
6710 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006711 */
6712 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6713 return false;
6714 }
6715
6716 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006717 * Dispatch a pointer event.
6718 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006719 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6720 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6721 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006722 * and should not be expected to handle other pointing device features.
6723 * </p>
6724 *
6725 * @param event The motion event to be dispatched.
6726 * @return True if the event was handled by the view, false otherwise.
6727 * @hide
6728 */
6729 public final boolean dispatchPointerEvent(MotionEvent event) {
6730 if (event.isTouchEvent()) {
6731 return dispatchTouchEvent(event);
6732 } else {
6733 return dispatchGenericMotionEvent(event);
6734 }
6735 }
6736
6737 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006738 * Called when the window containing this view gains or loses window focus.
6739 * ViewGroups should override to route to their children.
6740 *
6741 * @param hasFocus True if the window containing this view now has focus,
6742 * false otherwise.
6743 */
6744 public void dispatchWindowFocusChanged(boolean hasFocus) {
6745 onWindowFocusChanged(hasFocus);
6746 }
6747
6748 /**
6749 * Called when the window containing this view gains or loses focus. Note
6750 * that this is separate from view focus: to receive key events, both
6751 * your view and its window must have focus. If a window is displayed
6752 * on top of yours that takes input focus, then your own window will lose
6753 * focus but the view focus will remain unchanged.
6754 *
6755 * @param hasWindowFocus True if the window containing this view now has
6756 * focus, false otherwise.
6757 */
6758 public void onWindowFocusChanged(boolean hasWindowFocus) {
6759 InputMethodManager imm = InputMethodManager.peekInstance();
6760 if (!hasWindowFocus) {
6761 if (isPressed()) {
6762 setPressed(false);
6763 }
6764 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6765 imm.focusOut(this);
6766 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006767 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006768 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006769 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006770 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6771 imm.focusIn(this);
6772 }
6773 refreshDrawableState();
6774 }
6775
6776 /**
6777 * Returns true if this view is in a window that currently has window focus.
6778 * Note that this is not the same as the view itself having focus.
6779 *
6780 * @return True if this view is in a window that currently has window focus.
6781 */
6782 public boolean hasWindowFocus() {
6783 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6784 }
6785
6786 /**
Adam Powell326d8082009-12-09 15:10:07 -08006787 * Dispatch a view visibility change down the view hierarchy.
6788 * ViewGroups should override to route to their children.
6789 * @param changedView The view whose visibility changed. Could be 'this' or
6790 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006791 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6792 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006793 */
6794 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6795 onVisibilityChanged(changedView, visibility);
6796 }
6797
6798 /**
6799 * Called when the visibility of the view or an ancestor of the view is changed.
6800 * @param changedView The view whose visibility changed. Could be 'this' or
6801 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006802 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6803 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006804 */
6805 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006806 if (visibility == VISIBLE) {
6807 if (mAttachInfo != null) {
6808 initialAwakenScrollBars();
6809 } else {
6810 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6811 }
6812 }
Adam Powell326d8082009-12-09 15:10:07 -08006813 }
6814
6815 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006816 * Dispatch a hint about whether this view is displayed. For instance, when
6817 * a View moves out of the screen, it might receives a display hint indicating
6818 * the view is not displayed. Applications should not <em>rely</em> on this hint
6819 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006820 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006821 * @param hint A hint about whether or not this view is displayed:
6822 * {@link #VISIBLE} or {@link #INVISIBLE}.
6823 */
6824 public void dispatchDisplayHint(int hint) {
6825 onDisplayHint(hint);
6826 }
6827
6828 /**
6829 * Gives this view a hint about whether is displayed or not. For instance, when
6830 * a View moves out of the screen, it might receives a display hint indicating
6831 * the view is not displayed. Applications should not <em>rely</em> on this hint
6832 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006833 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006834 * @param hint A hint about whether or not this view is displayed:
6835 * {@link #VISIBLE} or {@link #INVISIBLE}.
6836 */
6837 protected void onDisplayHint(int hint) {
6838 }
6839
6840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006841 * Dispatch a window visibility change down the view hierarchy.
6842 * ViewGroups should override to route to their children.
6843 *
6844 * @param visibility The new visibility of the window.
6845 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006846 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 */
6848 public void dispatchWindowVisibilityChanged(int visibility) {
6849 onWindowVisibilityChanged(visibility);
6850 }
6851
6852 /**
6853 * Called when the window containing has change its visibility
6854 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6855 * that this tells you whether or not your window is being made visible
6856 * to the window manager; this does <em>not</em> tell you whether or not
6857 * your window is obscured by other windows on the screen, even if it
6858 * is itself visible.
6859 *
6860 * @param visibility The new visibility of the window.
6861 */
6862 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006863 if (visibility == VISIBLE) {
6864 initialAwakenScrollBars();
6865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 }
6867
6868 /**
6869 * Returns the current visibility of the window this view is attached to
6870 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6871 *
6872 * @return Returns the current visibility of the view's window.
6873 */
6874 public int getWindowVisibility() {
6875 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6876 }
6877
6878 /**
6879 * Retrieve the overall visible display size in which the window this view is
6880 * attached to has been positioned in. This takes into account screen
6881 * decorations above the window, for both cases where the window itself
6882 * is being position inside of them or the window is being placed under
6883 * then and covered insets are used for the window to position its content
6884 * inside. In effect, this tells you the available area where content can
6885 * be placed and remain visible to users.
6886 *
6887 * <p>This function requires an IPC back to the window manager to retrieve
6888 * the requested information, so should not be used in performance critical
6889 * code like drawing.
6890 *
6891 * @param outRect Filled in with the visible display frame. If the view
6892 * is not attached to a window, this is simply the raw display size.
6893 */
6894 public void getWindowVisibleDisplayFrame(Rect outRect) {
6895 if (mAttachInfo != null) {
6896 try {
6897 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6898 } catch (RemoteException e) {
6899 return;
6900 }
6901 // XXX This is really broken, and probably all needs to be done
6902 // in the window manager, and we need to know more about whether
6903 // we want the area behind or in front of the IME.
6904 final Rect insets = mAttachInfo.mVisibleInsets;
6905 outRect.left += insets.left;
6906 outRect.top += insets.top;
6907 outRect.right -= insets.right;
6908 outRect.bottom -= insets.bottom;
6909 return;
6910 }
6911 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006912 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 }
6914
6915 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006916 * Dispatch a notification about a resource configuration change down
6917 * the view hierarchy.
6918 * ViewGroups should override to route to their children.
6919 *
6920 * @param newConfig The new resource configuration.
6921 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006922 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006923 */
6924 public void dispatchConfigurationChanged(Configuration newConfig) {
6925 onConfigurationChanged(newConfig);
6926 }
6927
6928 /**
6929 * Called when the current configuration of the resources being used
6930 * by the application have changed. You can use this to decide when
6931 * to reload resources that can changed based on orientation and other
6932 * configuration characterstics. You only need to use this if you are
6933 * not relying on the normal {@link android.app.Activity} mechanism of
6934 * recreating the activity instance upon a configuration change.
6935 *
6936 * @param newConfig The new resource configuration.
6937 */
6938 protected void onConfigurationChanged(Configuration newConfig) {
6939 }
6940
6941 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006942 * Private function to aggregate all per-view attributes in to the view
6943 * root.
6944 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006945 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6946 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 }
6948
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006949 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6950 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006951 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006952 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006953 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006954 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006955 ListenerInfo li = mListenerInfo;
6956 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006957 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 }
6960 }
6961
6962 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006963 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006964 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006965 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6966 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 ai.mRecomputeGlobalAttributes = true;
6968 }
6969 }
6970 }
6971
6972 /**
6973 * Returns whether the device is currently in touch mode. Touch mode is entered
6974 * once the user begins interacting with the device by touch, and affects various
6975 * things like whether focus is always visible to the user.
6976 *
6977 * @return Whether the device is in touch mode.
6978 */
6979 @ViewDebug.ExportedProperty
6980 public boolean isInTouchMode() {
6981 if (mAttachInfo != null) {
6982 return mAttachInfo.mInTouchMode;
6983 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006984 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006985 }
6986 }
6987
6988 /**
6989 * Returns the context the view is running in, through which it can
6990 * access the current theme, resources, etc.
6991 *
6992 * @return The view's Context.
6993 */
6994 @ViewDebug.CapturedViewProperty
6995 public final Context getContext() {
6996 return mContext;
6997 }
6998
6999 /**
7000 * Handle a key event before it is processed by any input method
7001 * associated with the view hierarchy. This can be used to intercept
7002 * key events in special situations before the IME consumes them; a
7003 * typical example would be handling the BACK key to update the application's
7004 * UI instead of allowing the IME to see it and close itself.
7005 *
7006 * @param keyCode The value in event.getKeyCode().
7007 * @param event Description of the key event.
7008 * @return If you handled the event, return true. If you want to allow the
7009 * event to be handled by the next receiver, return false.
7010 */
7011 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7012 return false;
7013 }
7014
7015 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007016 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7017 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007018 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7019 * is released, if the view is enabled and clickable.
7020 *
7021 * @param keyCode A key code that represents the button pressed, from
7022 * {@link android.view.KeyEvent}.
7023 * @param event The KeyEvent object that defines the button action.
7024 */
7025 public boolean onKeyDown(int keyCode, KeyEvent event) {
7026 boolean result = false;
7027
7028 switch (keyCode) {
7029 case KeyEvent.KEYCODE_DPAD_CENTER:
7030 case KeyEvent.KEYCODE_ENTER: {
7031 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7032 return true;
7033 }
7034 // Long clickable items don't necessarily have to be clickable
7035 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7036 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7037 (event.getRepeatCount() == 0)) {
7038 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007039 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007040 return true;
7041 }
7042 break;
7043 }
7044 }
7045 return result;
7046 }
7047
7048 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007049 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7050 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7051 * the event).
7052 */
7053 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7054 return false;
7055 }
7056
7057 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007058 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7059 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7061 * {@link KeyEvent#KEYCODE_ENTER} is released.
7062 *
7063 * @param keyCode A key code that represents the button pressed, from
7064 * {@link android.view.KeyEvent}.
7065 * @param event The KeyEvent object that defines the button action.
7066 */
7067 public boolean onKeyUp(int keyCode, KeyEvent event) {
7068 boolean result = false;
7069
7070 switch (keyCode) {
7071 case KeyEvent.KEYCODE_DPAD_CENTER:
7072 case KeyEvent.KEYCODE_ENTER: {
7073 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7074 return true;
7075 }
7076 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7077 setPressed(false);
7078
7079 if (!mHasPerformedLongPress) {
7080 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007081 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082
7083 result = performClick();
7084 }
7085 }
7086 break;
7087 }
7088 }
7089 return result;
7090 }
7091
7092 /**
7093 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7094 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7095 * the event).
7096 *
7097 * @param keyCode A key code that represents the button pressed, from
7098 * {@link android.view.KeyEvent}.
7099 * @param repeatCount The number of times the action was made.
7100 * @param event The KeyEvent object that defines the button action.
7101 */
7102 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7103 return false;
7104 }
7105
7106 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007107 * Called on the focused view when a key shortcut event is not handled.
7108 * Override this method to implement local key shortcuts for the View.
7109 * Key shortcuts can also be implemented by setting the
7110 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 *
7112 * @param keyCode The value in event.getKeyCode().
7113 * @param event Description of the key event.
7114 * @return If you handled the event, return true. If you want to allow the
7115 * event to be handled by the next receiver, return false.
7116 */
7117 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7118 return false;
7119 }
7120
7121 /**
7122 * Check whether the called view is a text editor, in which case it
7123 * would make sense to automatically display a soft input window for
7124 * it. Subclasses should override this if they implement
7125 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007126 * a call on that method would return a non-null InputConnection, and
7127 * they are really a first-class editor that the user would normally
7128 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007129 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007130 * <p>The default implementation always returns false. This does
7131 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7132 * will not be called or the user can not otherwise perform edits on your
7133 * view; it is just a hint to the system that this is not the primary
7134 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007135 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 * @return Returns true if this view is a text editor, else false.
7137 */
7138 public boolean onCheckIsTextEditor() {
7139 return false;
7140 }
Romain Guy8506ab42009-06-11 17:35:47 -07007141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007142 /**
7143 * Create a new InputConnection for an InputMethod to interact
7144 * with the view. The default implementation returns null, since it doesn't
7145 * support input methods. You can override this to implement such support.
7146 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007147 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007148 * <p>When implementing this, you probably also want to implement
7149 * {@link #onCheckIsTextEditor()} to indicate you will return a
7150 * non-null InputConnection.
7151 *
7152 * @param outAttrs Fill in with attribute information about the connection.
7153 */
7154 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7155 return null;
7156 }
7157
7158 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007159 * Called by the {@link android.view.inputmethod.InputMethodManager}
7160 * when a view who is not the current
7161 * input connection target is trying to make a call on the manager. The
7162 * default implementation returns false; you can override this to return
7163 * true for certain views if you are performing InputConnection proxying
7164 * to them.
7165 * @param view The View that is making the InputMethodManager call.
7166 * @return Return true to allow the call, false to reject.
7167 */
7168 public boolean checkInputConnectionProxy(View view) {
7169 return false;
7170 }
Romain Guy8506ab42009-06-11 17:35:47 -07007171
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007173 * Show the context menu for this view. It is not safe to hold on to the
7174 * menu after returning from this method.
7175 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007176 * You should normally not overload this method. Overload
7177 * {@link #onCreateContextMenu(ContextMenu)} or define an
7178 * {@link OnCreateContextMenuListener} to add items to the context menu.
7179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 * @param menu The context menu to populate
7181 */
7182 public void createContextMenu(ContextMenu menu) {
7183 ContextMenuInfo menuInfo = getContextMenuInfo();
7184
7185 // Sets the current menu info so all items added to menu will have
7186 // my extra info set.
7187 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7188
7189 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007190 ListenerInfo li = mListenerInfo;
7191 if (li != null && li.mOnCreateContextMenuListener != null) {
7192 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193 }
7194
7195 // Clear the extra information so subsequent items that aren't mine don't
7196 // have my extra info.
7197 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7198
7199 if (mParent != null) {
7200 mParent.createContextMenu(menu);
7201 }
7202 }
7203
7204 /**
7205 * Views should implement this if they have extra information to associate
7206 * with the context menu. The return result is supplied as a parameter to
7207 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7208 * callback.
7209 *
7210 * @return Extra information about the item for which the context menu
7211 * should be shown. This information will vary across different
7212 * subclasses of View.
7213 */
7214 protected ContextMenuInfo getContextMenuInfo() {
7215 return null;
7216 }
7217
7218 /**
7219 * Views should implement this if the view itself is going to add items to
7220 * the context menu.
7221 *
7222 * @param menu the context menu to populate
7223 */
7224 protected void onCreateContextMenu(ContextMenu menu) {
7225 }
7226
7227 /**
7228 * Implement this method to handle trackball motion events. The
7229 * <em>relative</em> movement of the trackball since the last event
7230 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7231 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7232 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7233 * they will often be fractional values, representing the more fine-grained
7234 * movement information available from a trackball).
7235 *
7236 * @param event The motion event.
7237 * @return True if the event was handled, false otherwise.
7238 */
7239 public boolean onTrackballEvent(MotionEvent event) {
7240 return false;
7241 }
7242
7243 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007244 * Implement this method to handle generic motion events.
7245 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007246 * Generic motion events describe joystick movements, mouse hovers, track pad
7247 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007248 * {@link MotionEvent#getSource() source} of the motion event specifies
7249 * the class of input that was received. Implementations of this method
7250 * must examine the bits in the source before processing the event.
7251 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007252 * </p><p>
7253 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7254 * are delivered to the view under the pointer. All other generic motion events are
7255 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007256 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007257 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007258 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007259 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7260 * // process the joystick movement...
7261 * return true;
7262 * }
7263 * }
7264 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7265 * switch (event.getAction()) {
7266 * case MotionEvent.ACTION_HOVER_MOVE:
7267 * // process the mouse hover movement...
7268 * return true;
7269 * case MotionEvent.ACTION_SCROLL:
7270 * // process the scroll wheel movement...
7271 * return true;
7272 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007273 * }
7274 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007275 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007276 *
7277 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007278 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007279 */
7280 public boolean onGenericMotionEvent(MotionEvent event) {
7281 return false;
7282 }
7283
7284 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007285 * Implement this method to handle hover events.
7286 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007287 * This method is called whenever a pointer is hovering into, over, or out of the
7288 * bounds of a view and the view is not currently being touched.
7289 * Hover events are represented as pointer events with action
7290 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7291 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7292 * </p>
7293 * <ul>
7294 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7295 * when the pointer enters the bounds of the view.</li>
7296 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7297 * when the pointer has already entered the bounds of the view and has moved.</li>
7298 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7299 * when the pointer has exited the bounds of the view or when the pointer is
7300 * about to go down due to a button click, tap, or similar user action that
7301 * causes the view to be touched.</li>
7302 * </ul>
7303 * <p>
7304 * The view should implement this method to return true to indicate that it is
7305 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007306 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007307 * The default implementation calls {@link #setHovered} to update the hovered state
7308 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007309 * is enabled and is clickable. The default implementation also sends hover
7310 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007311 * </p>
7312 *
7313 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007314 * @return True if the view handled the hover event.
7315 *
7316 * @see #isHovered
7317 * @see #setHovered
7318 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007319 */
7320 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007321 // The root view may receive hover (or touch) events that are outside the bounds of
7322 // the window. This code ensures that we only send accessibility events for
7323 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007324 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007325 if (!mSendingHoverAccessibilityEvents) {
7326 if ((action == MotionEvent.ACTION_HOVER_ENTER
7327 || action == MotionEvent.ACTION_HOVER_MOVE)
7328 && !hasHoveredChild()
7329 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007330 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007331 mSendingHoverAccessibilityEvents = true;
7332 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007333 }
7334 } else {
7335 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007336 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007337 && !pointInView(event.getX(), event.getY()))) {
7338 mSendingHoverAccessibilityEvents = false;
7339 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007340 // If the window does not have input focus we take away accessibility
7341 // focus as soon as the user stop hovering over the view.
7342 if (!mAttachInfo.mHasWindowFocus) {
7343 getViewRootImpl().setAccessibilityFocusedHost(null);
7344 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007345 }
Jeff Browna1b24182011-07-28 13:38:24 -07007346 }
7347
Jeff Brown87b7f802011-06-21 18:35:45 -07007348 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007349 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007350 case MotionEvent.ACTION_HOVER_ENTER:
7351 setHovered(true);
7352 break;
7353 case MotionEvent.ACTION_HOVER_EXIT:
7354 setHovered(false);
7355 break;
7356 }
Jeff Browna1b24182011-07-28 13:38:24 -07007357
7358 // Dispatch the event to onGenericMotionEvent before returning true.
7359 // This is to provide compatibility with existing applications that
7360 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7361 // break because of the new default handling for hoverable views
7362 // in onHoverEvent.
7363 // Note that onGenericMotionEvent will be called by default when
7364 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7365 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007366 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007367 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007368
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007369 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007370 }
7371
7372 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007373 * Returns true if the view should handle {@link #onHoverEvent}
7374 * by calling {@link #setHovered} to change its hovered state.
7375 *
7376 * @return True if the view is hoverable.
7377 */
7378 private boolean isHoverable() {
7379 final int viewFlags = mViewFlags;
7380 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7381 return false;
7382 }
7383
7384 return (viewFlags & CLICKABLE) == CLICKABLE
7385 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7386 }
7387
7388 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007389 * Returns true if the view is currently hovered.
7390 *
7391 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007392 *
7393 * @see #setHovered
7394 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007395 */
Jeff Brown10b62902011-06-20 16:40:37 -07007396 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007397 public boolean isHovered() {
7398 return (mPrivateFlags & HOVERED) != 0;
7399 }
7400
7401 /**
7402 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007403 * <p>
7404 * Calling this method also changes the drawable state of the view. This
7405 * enables the view to react to hover by using different drawable resources
7406 * to change its appearance.
7407 * </p><p>
7408 * The {@link #onHoverChanged} method is called when the hovered state changes.
7409 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007410 *
7411 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007412 *
7413 * @see #isHovered
7414 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007415 */
7416 public void setHovered(boolean hovered) {
7417 if (hovered) {
7418 if ((mPrivateFlags & HOVERED) == 0) {
7419 mPrivateFlags |= HOVERED;
7420 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007421 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007422 }
7423 } else {
7424 if ((mPrivateFlags & HOVERED) != 0) {
7425 mPrivateFlags &= ~HOVERED;
7426 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007427 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007428 }
7429 }
7430 }
7431
7432 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007433 * Implement this method to handle hover state changes.
7434 * <p>
7435 * This method is called whenever the hover state changes as a result of a
7436 * call to {@link #setHovered}.
7437 * </p>
7438 *
7439 * @param hovered The current hover state, as returned by {@link #isHovered}.
7440 *
7441 * @see #isHovered
7442 * @see #setHovered
7443 */
7444 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007445 }
7446
7447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 * Implement this method to handle touch screen motion events.
7449 *
7450 * @param event The motion event.
7451 * @return True if the event was handled, false otherwise.
7452 */
7453 public boolean onTouchEvent(MotionEvent event) {
7454 final int viewFlags = mViewFlags;
7455
7456 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007457 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007458 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007460 // A disabled view that is clickable still consumes the touch
7461 // events, it just doesn't respond to them.
7462 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7463 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7464 }
7465
7466 if (mTouchDelegate != null) {
7467 if (mTouchDelegate.onTouchEvent(event)) {
7468 return true;
7469 }
7470 }
7471
7472 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7473 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7474 switch (event.getAction()) {
7475 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007476 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7477 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007478 // take focus if we don't have it already and we should in
7479 // touch mode.
7480 boolean focusTaken = false;
7481 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7482 focusTaken = requestFocus();
7483 }
7484
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007485 if (prepressed) {
7486 // The button is being released before we actually
7487 // showed it as pressed. Make it show the pressed
7488 // state now (before scheduling the click) to ensure
7489 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007490 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007491 }
Joe Malin32736f02011-01-19 16:14:20 -08007492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 if (!mHasPerformedLongPress) {
7494 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007495 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496
7497 // Only perform take click actions if we were in the pressed state
7498 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007499 // Use a Runnable and post this rather than calling
7500 // performClick directly. This lets other visual state
7501 // of the view update before click actions start.
7502 if (mPerformClick == null) {
7503 mPerformClick = new PerformClick();
7504 }
7505 if (!post(mPerformClick)) {
7506 performClick();
7507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007508 }
7509 }
7510
7511 if (mUnsetPressedState == null) {
7512 mUnsetPressedState = new UnsetPressedState();
7513 }
7514
Adam Powelle14579b2009-12-16 18:39:52 -08007515 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007516 postDelayed(mUnsetPressedState,
7517 ViewConfiguration.getPressedStateDuration());
7518 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 // If the post failed, unpress right now
7520 mUnsetPressedState.run();
7521 }
Adam Powelle14579b2009-12-16 18:39:52 -08007522 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 }
7524 break;
7525
7526 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007527 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007528
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007529 if (performButtonActionOnTouchDown(event)) {
7530 break;
7531 }
7532
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007533 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007534 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007535
7536 // For views inside a scrolling container, delay the pressed feedback for
7537 // a short period in case this is a scroll.
7538 if (isInScrollingContainer) {
7539 mPrivateFlags |= PREPRESSED;
7540 if (mPendingCheckForTap == null) {
7541 mPendingCheckForTap = new CheckForTap();
7542 }
7543 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7544 } else {
7545 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007546 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007547 checkForLongClick(0);
7548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007549 break;
7550
7551 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007552 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007553 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007554 break;
7555
7556 case MotionEvent.ACTION_MOVE:
7557 final int x = (int) event.getX();
7558 final int y = (int) event.getY();
7559
7560 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007561 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007563 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007564 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007565 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007566 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567
Adam Powell4d6f0662012-02-21 15:11:11 -08007568 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 }
7571 break;
7572 }
7573 return true;
7574 }
7575
7576 return false;
7577 }
7578
7579 /**
Adam Powell10298662011-08-14 18:26:30 -07007580 * @hide
7581 */
7582 public boolean isInScrollingContainer() {
7583 ViewParent p = getParent();
7584 while (p != null && p instanceof ViewGroup) {
7585 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7586 return true;
7587 }
7588 p = p.getParent();
7589 }
7590 return false;
7591 }
7592
7593 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007594 * Remove the longpress detection timer.
7595 */
7596 private void removeLongPressCallback() {
7597 if (mPendingCheckForLongPress != null) {
7598 removeCallbacks(mPendingCheckForLongPress);
7599 }
7600 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007601
7602 /**
7603 * Remove the pending click action
7604 */
7605 private void removePerformClickCallback() {
7606 if (mPerformClick != null) {
7607 removeCallbacks(mPerformClick);
7608 }
7609 }
7610
Adam Powelle14579b2009-12-16 18:39:52 -08007611 /**
Romain Guya440b002010-02-24 15:57:54 -08007612 * Remove the prepress detection timer.
7613 */
7614 private void removeUnsetPressCallback() {
7615 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7616 setPressed(false);
7617 removeCallbacks(mUnsetPressedState);
7618 }
7619 }
7620
7621 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007622 * Remove the tap detection timer.
7623 */
7624 private void removeTapCallback() {
7625 if (mPendingCheckForTap != null) {
7626 mPrivateFlags &= ~PREPRESSED;
7627 removeCallbacks(mPendingCheckForTap);
7628 }
7629 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007630
7631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 * Cancels a pending long press. Your subclass can use this if you
7633 * want the context menu to come up if the user presses and holds
7634 * at the same place, but you don't want it to come up if they press
7635 * and then move around enough to cause scrolling.
7636 */
7637 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007638 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007639
7640 /*
7641 * The prepressed state handled by the tap callback is a display
7642 * construct, but the tap callback will post a long press callback
7643 * less its own timeout. Remove it here.
7644 */
7645 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007646 }
7647
7648 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007649 * Remove the pending callback for sending a
7650 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7651 */
7652 private void removeSendViewScrolledAccessibilityEventCallback() {
7653 if (mSendViewScrolledAccessibilityEvent != null) {
7654 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7655 }
7656 }
7657
7658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659 * Sets the TouchDelegate for this View.
7660 */
7661 public void setTouchDelegate(TouchDelegate delegate) {
7662 mTouchDelegate = delegate;
7663 }
7664
7665 /**
7666 * Gets the TouchDelegate for this View.
7667 */
7668 public TouchDelegate getTouchDelegate() {
7669 return mTouchDelegate;
7670 }
7671
7672 /**
7673 * Set flags controlling behavior of this view.
7674 *
7675 * @param flags Constant indicating the value which should be set
7676 * @param mask Constant indicating the bit range that should be changed
7677 */
7678 void setFlags(int flags, int mask) {
7679 int old = mViewFlags;
7680 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7681
7682 int changed = mViewFlags ^ old;
7683 if (changed == 0) {
7684 return;
7685 }
7686 int privateFlags = mPrivateFlags;
7687
7688 /* Check if the FOCUSABLE bit has changed */
7689 if (((changed & FOCUSABLE_MASK) != 0) &&
7690 ((privateFlags & HAS_BOUNDS) !=0)) {
7691 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7692 && ((privateFlags & FOCUSED) != 0)) {
7693 /* Give up focus if we are no longer focusable */
7694 clearFocus();
7695 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7696 && ((privateFlags & FOCUSED) == 0)) {
7697 /*
7698 * Tell the view system that we are now available to take focus
7699 * if no one else already has it.
7700 */
7701 if (mParent != null) mParent.focusableViewAvailable(this);
7702 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007703 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7704 notifyAccessibilityStateChanged();
7705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007706 }
7707
7708 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7709 if ((changed & VISIBILITY_MASK) != 0) {
7710 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007711 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007712 * it was not visible. Marking it drawn ensures that the invalidation will
7713 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007714 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007715 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007716 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007717
7718 needGlobalAttributesUpdate(true);
7719
7720 // a view becoming visible is worth notifying the parent
7721 // about in case nothing has focus. even if this specific view
7722 // isn't focusable, it may contain something that is, so let
7723 // the root view try to give this focus if nothing else does.
7724 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7725 mParent.focusableViewAvailable(this);
7726 }
7727 }
7728 }
7729
7730 /* Check if the GONE bit has changed */
7731 if ((changed & GONE) != 0) {
7732 needGlobalAttributesUpdate(false);
7733 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734
Romain Guyecd80ee2009-12-03 17:13:02 -08007735 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7736 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007737 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007738 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007739 if (mParent instanceof View) {
7740 // GONE views noop invalidation, so invalidate the parent
7741 ((View) mParent).invalidate(true);
7742 }
7743 // Mark the view drawn to ensure that it gets invalidated properly the next
7744 // time it is visible and gets invalidated
7745 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 }
7747 if (mAttachInfo != null) {
7748 mAttachInfo.mViewVisibilityChanged = true;
7749 }
7750 }
7751
7752 /* Check if the VISIBLE bit has changed */
7753 if ((changed & INVISIBLE) != 0) {
7754 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007755 /*
7756 * If this view is becoming invisible, set the DRAWN flag so that
7757 * the next invalidate() will not be skipped.
7758 */
7759 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007760
7761 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007762 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 if (getRootView() != this) {
7764 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007765 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 }
7767 }
7768 if (mAttachInfo != null) {
7769 mAttachInfo.mViewVisibilityChanged = true;
7770 }
7771 }
7772
Adam Powell326d8082009-12-09 15:10:07 -08007773 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007774 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007775 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7776 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007777 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007778 } else if (mParent != null) {
7779 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007780 }
Adam Powell326d8082009-12-09 15:10:07 -08007781 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7782 }
7783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007784 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7785 destroyDrawingCache();
7786 }
7787
7788 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7789 destroyDrawingCache();
7790 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007791 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 }
7793
7794 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7795 destroyDrawingCache();
7796 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7797 }
7798
7799 if ((changed & DRAW_MASK) != 0) {
7800 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007801 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 mPrivateFlags &= ~SKIP_DRAW;
7803 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7804 } else {
7805 mPrivateFlags |= SKIP_DRAW;
7806 }
7807 } else {
7808 mPrivateFlags &= ~SKIP_DRAW;
7809 }
7810 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007811 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007812 }
7813
7814 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007815 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007816 mParent.recomputeViewAttributes(this);
7817 }
7818 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007819
7820 if (AccessibilityManager.getInstance(mContext).isEnabled()
7821 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7822 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7823 notifyAccessibilityStateChanged();
7824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007825 }
7826
7827 /**
7828 * Change the view's z order in the tree, so it's on top of other sibling
7829 * views
7830 */
7831 public void bringToFront() {
7832 if (mParent != null) {
7833 mParent.bringChildToFront(this);
7834 }
7835 }
7836
7837 /**
7838 * This is called in response to an internal scroll in this view (i.e., the
7839 * view scrolled its own contents). This is typically as a result of
7840 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7841 * called.
7842 *
7843 * @param l Current horizontal scroll origin.
7844 * @param t Current vertical scroll origin.
7845 * @param oldl Previous horizontal scroll origin.
7846 * @param oldt Previous vertical scroll origin.
7847 */
7848 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007849 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7850 postSendViewScrolledAccessibilityEventCallback();
7851 }
7852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007853 mBackgroundSizeChanged = true;
7854
7855 final AttachInfo ai = mAttachInfo;
7856 if (ai != null) {
7857 ai.mViewScrollChanged = true;
7858 }
7859 }
7860
7861 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007862 * Interface definition for a callback to be invoked when the layout bounds of a view
7863 * changes due to layout processing.
7864 */
7865 public interface OnLayoutChangeListener {
7866 /**
7867 * Called when the focus state of a view has changed.
7868 *
7869 * @param v The view whose state has changed.
7870 * @param left The new value of the view's left property.
7871 * @param top The new value of the view's top property.
7872 * @param right The new value of the view's right property.
7873 * @param bottom The new value of the view's bottom property.
7874 * @param oldLeft The previous value of the view's left property.
7875 * @param oldTop The previous value of the view's top property.
7876 * @param oldRight The previous value of the view's right property.
7877 * @param oldBottom The previous value of the view's bottom property.
7878 */
7879 void onLayoutChange(View v, int left, int top, int right, int bottom,
7880 int oldLeft, int oldTop, int oldRight, int oldBottom);
7881 }
7882
7883 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 * This is called during layout when the size of this view has changed. If
7885 * you were just added to the view hierarchy, you're called with the old
7886 * values of 0.
7887 *
7888 * @param w Current width of this view.
7889 * @param h Current height of this view.
7890 * @param oldw Old width of this view.
7891 * @param oldh Old height of this view.
7892 */
7893 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7894 }
7895
7896 /**
7897 * Called by draw to draw the child views. This may be overridden
7898 * by derived classes to gain control just before its children are drawn
7899 * (but after its own view has been drawn).
7900 * @param canvas the canvas on which to draw the view
7901 */
7902 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904 }
7905
7906 /**
7907 * Gets the parent of this view. Note that the parent is a
7908 * ViewParent and not necessarily a View.
7909 *
7910 * @return Parent of this view.
7911 */
7912 public final ViewParent getParent() {
7913 return mParent;
7914 }
7915
7916 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007917 * Set the horizontal scrolled position of your view. This will cause a call to
7918 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7919 * invalidated.
7920 * @param value the x position to scroll to
7921 */
7922 public void setScrollX(int value) {
7923 scrollTo(value, mScrollY);
7924 }
7925
7926 /**
7927 * Set the vertical scrolled position of your view. This will cause a call to
7928 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7929 * invalidated.
7930 * @param value the y position to scroll to
7931 */
7932 public void setScrollY(int value) {
7933 scrollTo(mScrollX, value);
7934 }
7935
7936 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937 * Return the scrolled left position of this view. This is the left edge of
7938 * the displayed part of your view. You do not need to draw any pixels
7939 * farther left, since those are outside of the frame of your view on
7940 * screen.
7941 *
7942 * @return The left edge of the displayed part of your view, in pixels.
7943 */
7944 public final int getScrollX() {
7945 return mScrollX;
7946 }
7947
7948 /**
7949 * Return the scrolled top position of this view. This is the top edge of
7950 * the displayed part of your view. You do not need to draw any pixels above
7951 * it, since those are outside of the frame of your view on screen.
7952 *
7953 * @return The top edge of the displayed part of your view, in pixels.
7954 */
7955 public final int getScrollY() {
7956 return mScrollY;
7957 }
7958
7959 /**
7960 * Return the width of the your view.
7961 *
7962 * @return The width of your view, in pixels.
7963 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007964 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 public final int getWidth() {
7966 return mRight - mLeft;
7967 }
7968
7969 /**
7970 * Return the height of your view.
7971 *
7972 * @return The height of your view, in pixels.
7973 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007974 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007975 public final int getHeight() {
7976 return mBottom - mTop;
7977 }
7978
7979 /**
7980 * Return the visible drawing bounds of your view. Fills in the output
7981 * rectangle with the values from getScrollX(), getScrollY(),
7982 * getWidth(), and getHeight().
7983 *
7984 * @param outRect The (scrolled) drawing bounds of the view.
7985 */
7986 public void getDrawingRect(Rect outRect) {
7987 outRect.left = mScrollX;
7988 outRect.top = mScrollY;
7989 outRect.right = mScrollX + (mRight - mLeft);
7990 outRect.bottom = mScrollY + (mBottom - mTop);
7991 }
7992
7993 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007994 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7995 * raw width component (that is the result is masked by
7996 * {@link #MEASURED_SIZE_MASK}).
7997 *
7998 * @return The raw measured width of this view.
7999 */
8000 public final int getMeasuredWidth() {
8001 return mMeasuredWidth & MEASURED_SIZE_MASK;
8002 }
8003
8004 /**
8005 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008006 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008007 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 * This should be used during measurement and layout calculations only. Use
8009 * {@link #getWidth()} to see how wide a view is after layout.
8010 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008011 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008012 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008013 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008014 return mMeasuredWidth;
8015 }
8016
8017 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008018 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8019 * raw width component (that is the result is masked by
8020 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008022 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 */
8024 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008025 return mMeasuredHeight & MEASURED_SIZE_MASK;
8026 }
8027
8028 /**
8029 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008030 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008031 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8032 * This should be used during measurement and layout calculations only. Use
8033 * {@link #getHeight()} to see how wide a view is after layout.
8034 *
8035 * @return The measured width of this view as a bit mask.
8036 */
8037 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 return mMeasuredHeight;
8039 }
8040
8041 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008042 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8043 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8044 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8045 * and the height component is at the shifted bits
8046 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8047 */
8048 public final int getMeasuredState() {
8049 return (mMeasuredWidth&MEASURED_STATE_MASK)
8050 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8051 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8052 }
8053
8054 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008055 * The transform matrix of this view, which is calculated based on the current
8056 * roation, scale, and pivot properties.
8057 *
8058 * @see #getRotation()
8059 * @see #getScaleX()
8060 * @see #getScaleY()
8061 * @see #getPivotX()
8062 * @see #getPivotY()
8063 * @return The current transform matrix for the view
8064 */
8065 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008066 if (mTransformationInfo != null) {
8067 updateMatrix();
8068 return mTransformationInfo.mMatrix;
8069 }
8070 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008071 }
8072
8073 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008074 * Utility function to determine if the value is far enough away from zero to be
8075 * considered non-zero.
8076 * @param value A floating point value to check for zero-ness
8077 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8078 */
8079 private static boolean nonzero(float value) {
8080 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8081 }
8082
8083 /**
Jeff Brown86671742010-09-30 20:00:15 -07008084 * Returns true if the transform matrix is the identity matrix.
8085 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008086 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008087 * @return True if the transform matrix is the identity matrix, false otherwise.
8088 */
Jeff Brown86671742010-09-30 20:00:15 -07008089 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008090 if (mTransformationInfo != null) {
8091 updateMatrix();
8092 return mTransformationInfo.mMatrixIsIdentity;
8093 }
8094 return true;
8095 }
8096
8097 void ensureTransformationInfo() {
8098 if (mTransformationInfo == null) {
8099 mTransformationInfo = new TransformationInfo();
8100 }
Jeff Brown86671742010-09-30 20:00:15 -07008101 }
8102
8103 /**
8104 * Recomputes the transform matrix if necessary.
8105 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008106 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008107 final TransformationInfo info = mTransformationInfo;
8108 if (info == null) {
8109 return;
8110 }
8111 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008112 // transform-related properties have changed since the last time someone
8113 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008114
8115 // Figure out if we need to update the pivot point
8116 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008117 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8118 info.mPrevWidth = mRight - mLeft;
8119 info.mPrevHeight = mBottom - mTop;
8120 info.mPivotX = info.mPrevWidth / 2f;
8121 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008122 }
8123 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008124 info.mMatrix.reset();
8125 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8126 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8127 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8128 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008129 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008130 if (info.mCamera == null) {
8131 info.mCamera = new Camera();
8132 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008133 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008134 info.mCamera.save();
8135 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8136 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8137 info.mCamera.getMatrix(info.matrix3D);
8138 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8139 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8140 info.mPivotY + info.mTranslationY);
8141 info.mMatrix.postConcat(info.matrix3D);
8142 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008143 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008144 info.mMatrixDirty = false;
8145 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8146 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008147 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008148 }
8149
8150 /**
8151 * Utility method to retrieve the inverse of the current mMatrix property.
8152 * We cache the matrix to avoid recalculating it when transform properties
8153 * have not changed.
8154 *
8155 * @return The inverse of the current matrix of this view.
8156 */
Jeff Brown86671742010-09-30 20:00:15 -07008157 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008158 final TransformationInfo info = mTransformationInfo;
8159 if (info != null) {
8160 updateMatrix();
8161 if (info.mInverseMatrixDirty) {
8162 if (info.mInverseMatrix == null) {
8163 info.mInverseMatrix = new Matrix();
8164 }
8165 info.mMatrix.invert(info.mInverseMatrix);
8166 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008167 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008168 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008169 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008170 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008171 }
8172
8173 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008174 * Gets the distance along the Z axis from the camera to this view.
8175 *
8176 * @see #setCameraDistance(float)
8177 *
8178 * @return The distance along the Z axis.
8179 */
8180 public float getCameraDistance() {
8181 ensureTransformationInfo();
8182 final float dpi = mResources.getDisplayMetrics().densityDpi;
8183 final TransformationInfo info = mTransformationInfo;
8184 if (info.mCamera == null) {
8185 info.mCamera = new Camera();
8186 info.matrix3D = new Matrix();
8187 }
8188 return -(info.mCamera.getLocationZ() * dpi);
8189 }
8190
8191 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008192 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8193 * views are drawn) from the camera to this view. The camera's distance
8194 * affects 3D transformations, for instance rotations around the X and Y
8195 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008196 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008197 * use a camera distance that's greater than the height (X axis rotation) or
8198 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008199 *
Romain Guya5364ee2011-02-24 14:46:04 -08008200 * <p>The distance of the camera from the view plane can have an affect on the
8201 * perspective distortion of the view when it is rotated around the x or y axis.
8202 * For example, a large distance will result in a large viewing angle, and there
8203 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008204 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008205 * also result in some drawing artifacts if the rotated view ends up partially
8206 * behind the camera (which is why the recommendation is to use a distance at
8207 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008208 *
Romain Guya5364ee2011-02-24 14:46:04 -08008209 * <p>The distance is expressed in "depth pixels." The default distance depends
8210 * on the screen density. For instance, on a medium density display, the
8211 * default distance is 1280. On a high density display, the default distance
8212 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008213 *
Romain Guya5364ee2011-02-24 14:46:04 -08008214 * <p>If you want to specify a distance that leads to visually consistent
8215 * results across various densities, use the following formula:</p>
8216 * <pre>
8217 * float scale = context.getResources().getDisplayMetrics().density;
8218 * view.setCameraDistance(distance * scale);
8219 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008220 *
Romain Guya5364ee2011-02-24 14:46:04 -08008221 * <p>The density scale factor of a high density display is 1.5,
8222 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008223 *
Romain Guya5364ee2011-02-24 14:46:04 -08008224 * @param distance The distance in "depth pixels", if negative the opposite
8225 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008226 *
8227 * @see #setRotationX(float)
8228 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008229 */
8230 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008231 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008232
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008233 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008234 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008235 final TransformationInfo info = mTransformationInfo;
8236 if (info.mCamera == null) {
8237 info.mCamera = new Camera();
8238 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008239 }
8240
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008241 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8242 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008243
Chet Haase9d1992d2012-03-13 11:03:25 -07008244 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008245 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008246 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008247 }
Romain Guya5364ee2011-02-24 14:46:04 -08008248 }
8249
8250 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008251 * The degrees that the view is rotated around the pivot point.
8252 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008253 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008254 * @see #getPivotX()
8255 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008256 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008257 * @return The degrees of rotation.
8258 */
Chet Haasea5531132012-02-02 13:41:44 -08008259 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008260 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008261 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008262 }
8263
8264 /**
Chet Haase897247b2010-09-09 14:54:47 -07008265 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8266 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008267 *
8268 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008269 *
8270 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008271 * @see #getPivotX()
8272 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008273 * @see #setRotationX(float)
8274 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008275 *
8276 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008277 */
8278 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008279 ensureTransformationInfo();
8280 final TransformationInfo info = mTransformationInfo;
8281 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008282 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008283 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008284 info.mRotation = rotation;
8285 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008286 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008287 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8288 mDisplayList.setRotation(rotation);
8289 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008290 }
8291 }
8292
8293 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008294 * The degrees that the view is rotated around the vertical axis through the pivot point.
8295 *
8296 * @see #getPivotX()
8297 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008298 * @see #setRotationY(float)
8299 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008300 * @return The degrees of Y rotation.
8301 */
Chet Haasea5531132012-02-02 13:41:44 -08008302 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008303 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008304 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008305 }
8306
8307 /**
Chet Haase897247b2010-09-09 14:54:47 -07008308 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8309 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8310 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008311 *
Romain Guya5364ee2011-02-24 14:46:04 -08008312 * When rotating large views, it is recommended to adjust the camera distance
8313 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008314 *
8315 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008316 *
8317 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008318 * @see #getPivotX()
8319 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008320 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008321 * @see #setRotationX(float)
8322 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008323 *
8324 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008325 */
8326 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008327 ensureTransformationInfo();
8328 final TransformationInfo info = mTransformationInfo;
8329 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008330 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008331 info.mRotationY = rotationY;
8332 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008333 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008334 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8335 mDisplayList.setRotationY(rotationY);
8336 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008337 }
8338 }
8339
8340 /**
8341 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8342 *
8343 * @see #getPivotX()
8344 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008345 * @see #setRotationX(float)
8346 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008347 * @return The degrees of X rotation.
8348 */
Chet Haasea5531132012-02-02 13:41:44 -08008349 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008350 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008351 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008352 }
8353
8354 /**
Chet Haase897247b2010-09-09 14:54:47 -07008355 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8356 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8357 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008358 *
Romain Guya5364ee2011-02-24 14:46:04 -08008359 * When rotating large views, it is recommended to adjust the camera distance
8360 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008361 *
8362 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008363 *
8364 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008365 * @see #getPivotX()
8366 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008367 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008368 * @see #setRotationY(float)
8369 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008370 *
8371 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008372 */
8373 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008374 ensureTransformationInfo();
8375 final TransformationInfo info = mTransformationInfo;
8376 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008377 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008378 info.mRotationX = rotationX;
8379 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008380 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008381 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8382 mDisplayList.setRotationX(rotationX);
8383 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008384 }
8385 }
8386
8387 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008388 * The amount that the view is scaled in x around the pivot point, as a proportion of
8389 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8390 *
Joe Onorato93162322010-09-16 15:42:01 -04008391 * <p>By default, this is 1.0f.
8392 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008393 * @see #getPivotX()
8394 * @see #getPivotY()
8395 * @return The scaling factor.
8396 */
Chet Haasea5531132012-02-02 13:41:44 -08008397 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008398 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008399 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008400 }
8401
8402 /**
8403 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8404 * the view's unscaled width. A value of 1 means that no scaling is applied.
8405 *
8406 * @param scaleX The scaling factor.
8407 * @see #getPivotX()
8408 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008409 *
8410 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008411 */
8412 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008413 ensureTransformationInfo();
8414 final TransformationInfo info = mTransformationInfo;
8415 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008416 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008417 info.mScaleX = scaleX;
8418 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008419 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008420 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8421 mDisplayList.setScaleX(scaleX);
8422 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008423 }
8424 }
8425
8426 /**
8427 * The amount that the view is scaled in y around the pivot point, as a proportion of
8428 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8429 *
Joe Onorato93162322010-09-16 15:42:01 -04008430 * <p>By default, this is 1.0f.
8431 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008432 * @see #getPivotX()
8433 * @see #getPivotY()
8434 * @return The scaling factor.
8435 */
Chet Haasea5531132012-02-02 13:41:44 -08008436 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008437 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008438 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008439 }
8440
8441 /**
8442 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8443 * the view's unscaled width. A value of 1 means that no scaling is applied.
8444 *
8445 * @param scaleY The scaling factor.
8446 * @see #getPivotX()
8447 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008448 *
8449 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008450 */
8451 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008452 ensureTransformationInfo();
8453 final TransformationInfo info = mTransformationInfo;
8454 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008455 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008456 info.mScaleY = scaleY;
8457 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008458 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008459 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8460 mDisplayList.setScaleY(scaleY);
8461 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008462 }
8463 }
8464
8465 /**
8466 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8467 * and {@link #setScaleX(float) scaled}.
8468 *
8469 * @see #getRotation()
8470 * @see #getScaleX()
8471 * @see #getScaleY()
8472 * @see #getPivotY()
8473 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008474 *
8475 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008476 */
Chet Haasea5531132012-02-02 13:41:44 -08008477 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008478 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008479 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008480 }
8481
8482 /**
8483 * Sets the x location of the point around which the view is
8484 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008485 * By default, the pivot point is centered on the object.
8486 * Setting this property disables this behavior and causes the view to use only the
8487 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008488 *
8489 * @param pivotX The x location of the pivot point.
8490 * @see #getRotation()
8491 * @see #getScaleX()
8492 * @see #getScaleY()
8493 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008494 *
8495 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008496 */
8497 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008498 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008499 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008500 final TransformationInfo info = mTransformationInfo;
8501 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008502 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008503 info.mPivotX = pivotX;
8504 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008505 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008506 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8507 mDisplayList.setPivotX(pivotX);
8508 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008509 }
8510 }
8511
8512 /**
8513 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8514 * and {@link #setScaleY(float) scaled}.
8515 *
8516 * @see #getRotation()
8517 * @see #getScaleX()
8518 * @see #getScaleY()
8519 * @see #getPivotY()
8520 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008521 *
8522 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008523 */
Chet Haasea5531132012-02-02 13:41:44 -08008524 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008525 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008526 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008527 }
8528
8529 /**
8530 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008531 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8532 * Setting this property disables this behavior and causes the view to use only the
8533 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008534 *
8535 * @param pivotY The y location of the pivot point.
8536 * @see #getRotation()
8537 * @see #getScaleX()
8538 * @see #getScaleY()
8539 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008540 *
8541 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008542 */
8543 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008544 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008545 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008546 final TransformationInfo info = mTransformationInfo;
8547 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008548 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008549 info.mPivotY = pivotY;
8550 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008551 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008552 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8553 mDisplayList.setPivotY(pivotY);
8554 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008555 }
8556 }
8557
8558 /**
8559 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8560 * completely transparent and 1 means the view is completely opaque.
8561 *
Joe Onorato93162322010-09-16 15:42:01 -04008562 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008563 * @return The opacity of the view.
8564 */
Chet Haasea5531132012-02-02 13:41:44 -08008565 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008566 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008567 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008568 }
8569
8570 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008571 * Returns whether this View has content which overlaps. This function, intended to be
8572 * overridden by specific View types, is an optimization when alpha is set on a view. If
8573 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8574 * and then composited it into place, which can be expensive. If the view has no overlapping
8575 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8576 * An example of overlapping rendering is a TextView with a background image, such as a
8577 * Button. An example of non-overlapping rendering is a TextView with no background, or
8578 * an ImageView with only the foreground image. The default implementation returns true;
8579 * subclasses should override if they have cases which can be optimized.
8580 *
8581 * @return true if the content in this view might overlap, false otherwise.
8582 */
8583 public boolean hasOverlappingRendering() {
8584 return true;
8585 }
8586
8587 /**
Romain Guy171c5922011-01-06 10:04:23 -08008588 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8589 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008590 *
Romain Guy171c5922011-01-06 10:04:23 -08008591 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8592 * responsible for applying the opacity itself. Otherwise, calling this method is
8593 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008594 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008595 *
Chet Haasea5531132012-02-02 13:41:44 -08008596 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8597 * performance implications. It is generally best to use the alpha property sparingly and
8598 * transiently, as in the case of fading animations.</p>
8599 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008600 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008601 *
Joe Malin32736f02011-01-19 16:14:20 -08008602 * @see #setLayerType(int, android.graphics.Paint)
8603 *
Chet Haase73066682010-11-29 15:55:32 -08008604 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008605 */
8606 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008607 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008608 if (mTransformationInfo.mAlpha != alpha) {
8609 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008610 if (onSetAlpha((int) (alpha * 255))) {
8611 mPrivateFlags |= ALPHA_SET;
8612 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008613 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008614 invalidate(true);
8615 } else {
8616 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008617 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008618 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8619 mDisplayList.setAlpha(alpha);
8620 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008621 }
Chet Haaseed032702010-10-01 14:05:54 -07008622 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008623 }
8624
8625 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008626 * Faster version of setAlpha() which performs the same steps except there are
8627 * no calls to invalidate(). The caller of this function should perform proper invalidation
8628 * on the parent and this object. The return value indicates whether the subclass handles
8629 * alpha (the return value for onSetAlpha()).
8630 *
8631 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008632 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8633 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008634 */
8635 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008636 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008637 if (mTransformationInfo.mAlpha != alpha) {
8638 mTransformationInfo.mAlpha = alpha;
8639 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8640 if (subclassHandlesAlpha) {
8641 mPrivateFlags |= ALPHA_SET;
8642 return true;
8643 } else {
8644 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08008645 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8646 mDisplayList.setAlpha(alpha);
8647 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008648 }
Chet Haasea00f3862011-02-22 06:34:40 -08008649 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008650 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008651 }
8652
8653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008654 * Top position of this view relative to its parent.
8655 *
8656 * @return The top of this view, in pixels.
8657 */
8658 @ViewDebug.CapturedViewProperty
8659 public final int getTop() {
8660 return mTop;
8661 }
8662
8663 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008664 * Sets the top position of this view relative to its parent. This method is meant to be called
8665 * by the layout system and should not generally be called otherwise, because the property
8666 * may be changed at any time by the layout.
8667 *
8668 * @param top The top of this view, in pixels.
8669 */
8670 public final void setTop(int top) {
8671 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008672 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008673 final boolean matrixIsIdentity = mTransformationInfo == null
8674 || mTransformationInfo.mMatrixIsIdentity;
8675 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008676 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008677 int minTop;
8678 int yLoc;
8679 if (top < mTop) {
8680 minTop = top;
8681 yLoc = top - mTop;
8682 } else {
8683 minTop = mTop;
8684 yLoc = 0;
8685 }
Chet Haasee9140a72011-02-16 16:23:29 -08008686 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008687 }
8688 } else {
8689 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008690 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008691 }
8692
Chet Haaseed032702010-10-01 14:05:54 -07008693 int width = mRight - mLeft;
8694 int oldHeight = mBottom - mTop;
8695
Chet Haase21cd1382010-09-01 17:42:29 -07008696 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008697 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8698 mDisplayList.setTop(mTop);
8699 }
Chet Haase21cd1382010-09-01 17:42:29 -07008700
Chet Haaseed032702010-10-01 14:05:54 -07008701 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8702
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008703 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008704 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8705 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008706 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008707 }
Chet Haase21cd1382010-09-01 17:42:29 -07008708 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008709 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008710 }
Chet Haase55dbb652010-12-21 20:15:08 -08008711 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008712 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008713 }
8714 }
8715
8716 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 * Bottom position of this view relative to its parent.
8718 *
8719 * @return The bottom of this view, in pixels.
8720 */
8721 @ViewDebug.CapturedViewProperty
8722 public final int getBottom() {
8723 return mBottom;
8724 }
8725
8726 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008727 * True if this view has changed since the last time being drawn.
8728 *
8729 * @return The dirty state of this view.
8730 */
8731 public boolean isDirty() {
8732 return (mPrivateFlags & DIRTY_MASK) != 0;
8733 }
8734
8735 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008736 * Sets the bottom position of this view relative to its parent. This method is meant to be
8737 * called by the layout system and should not generally be called otherwise, because the
8738 * property may be changed at any time by the layout.
8739 *
8740 * @param bottom The bottom of this view, in pixels.
8741 */
8742 public final void setBottom(int bottom) {
8743 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008744 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008745 final boolean matrixIsIdentity = mTransformationInfo == null
8746 || mTransformationInfo.mMatrixIsIdentity;
8747 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008748 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008749 int maxBottom;
8750 if (bottom < mBottom) {
8751 maxBottom = mBottom;
8752 } else {
8753 maxBottom = bottom;
8754 }
Chet Haasee9140a72011-02-16 16:23:29 -08008755 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008756 }
8757 } else {
8758 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008759 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008760 }
8761
Chet Haaseed032702010-10-01 14:05:54 -07008762 int width = mRight - mLeft;
8763 int oldHeight = mBottom - mTop;
8764
Chet Haase21cd1382010-09-01 17:42:29 -07008765 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008766 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8767 mDisplayList.setBottom(mBottom);
8768 }
Chet Haase21cd1382010-09-01 17:42:29 -07008769
Chet Haaseed032702010-10-01 14:05:54 -07008770 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8771
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008772 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008773 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8774 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008775 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008776 }
Chet Haase21cd1382010-09-01 17:42:29 -07008777 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008778 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008779 }
Chet Haase55dbb652010-12-21 20:15:08 -08008780 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008781 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008782 }
8783 }
8784
8785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008786 * Left position of this view relative to its parent.
8787 *
8788 * @return The left edge of this view, in pixels.
8789 */
8790 @ViewDebug.CapturedViewProperty
8791 public final int getLeft() {
8792 return mLeft;
8793 }
8794
8795 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008796 * Sets the left position of this view relative to its parent. This method is meant to be called
8797 * by the layout system and should not generally be called otherwise, because the property
8798 * may be changed at any time by the layout.
8799 *
8800 * @param left The bottom of this view, in pixels.
8801 */
8802 public final void setLeft(int left) {
8803 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008804 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008805 final boolean matrixIsIdentity = mTransformationInfo == null
8806 || mTransformationInfo.mMatrixIsIdentity;
8807 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008808 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008809 int minLeft;
8810 int xLoc;
8811 if (left < mLeft) {
8812 minLeft = left;
8813 xLoc = left - mLeft;
8814 } else {
8815 minLeft = mLeft;
8816 xLoc = 0;
8817 }
Chet Haasee9140a72011-02-16 16:23:29 -08008818 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008819 }
8820 } else {
8821 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008822 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008823 }
8824
Chet Haaseed032702010-10-01 14:05:54 -07008825 int oldWidth = mRight - mLeft;
8826 int height = mBottom - mTop;
8827
Chet Haase21cd1382010-09-01 17:42:29 -07008828 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008829 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8830 mDisplayList.setLeft(left);
8831 }
Chet Haase21cd1382010-09-01 17:42:29 -07008832
Chet Haaseed032702010-10-01 14:05:54 -07008833 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8834
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008835 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008836 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8837 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008838 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008839 }
Chet Haase21cd1382010-09-01 17:42:29 -07008840 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008841 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008842 }
Chet Haase55dbb652010-12-21 20:15:08 -08008843 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008844 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008845 if (USE_DISPLAY_LIST_PROPERTIES) {
8846
8847 }
Chet Haase21cd1382010-09-01 17:42:29 -07008848 }
8849 }
8850
8851 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008852 * Right position of this view relative to its parent.
8853 *
8854 * @return The right edge of this view, in pixels.
8855 */
8856 @ViewDebug.CapturedViewProperty
8857 public final int getRight() {
8858 return mRight;
8859 }
8860
8861 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008862 * Sets the right position of this view relative to its parent. This method is meant to be called
8863 * by the layout system and should not generally be called otherwise, because the property
8864 * may be changed at any time by the layout.
8865 *
8866 * @param right The bottom of this view, in pixels.
8867 */
8868 public final void setRight(int right) {
8869 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008870 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008871 final boolean matrixIsIdentity = mTransformationInfo == null
8872 || mTransformationInfo.mMatrixIsIdentity;
8873 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008874 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008875 int maxRight;
8876 if (right < mRight) {
8877 maxRight = mRight;
8878 } else {
8879 maxRight = right;
8880 }
Chet Haasee9140a72011-02-16 16:23:29 -08008881 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008882 }
8883 } else {
8884 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008885 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008886 }
8887
Chet Haaseed032702010-10-01 14:05:54 -07008888 int oldWidth = mRight - mLeft;
8889 int height = mBottom - mTop;
8890
Chet Haase21cd1382010-09-01 17:42:29 -07008891 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008892 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8893 mDisplayList.setRight(mRight);
8894 }
Chet Haase21cd1382010-09-01 17:42:29 -07008895
Chet Haaseed032702010-10-01 14:05:54 -07008896 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8897
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008898 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008899 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8900 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008901 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008902 }
Chet Haase21cd1382010-09-01 17:42:29 -07008903 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008904 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008905 }
Chet Haase55dbb652010-12-21 20:15:08 -08008906 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008907 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008908 }
8909 }
8910
8911 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008912 * The visual x position of this view, in pixels. This is equivalent to the
8913 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008914 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008915 *
Chet Haasedf030d22010-07-30 17:22:38 -07008916 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008917 */
Chet Haasea5531132012-02-02 13:41:44 -08008918 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008919 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008920 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008921 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008922
Chet Haasedf030d22010-07-30 17:22:38 -07008923 /**
8924 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8925 * {@link #setTranslationX(float) translationX} property to be the difference between
8926 * the x value passed in and the current {@link #getLeft() left} property.
8927 *
8928 * @param x The visual x position of this view, in pixels.
8929 */
8930 public void setX(float x) {
8931 setTranslationX(x - mLeft);
8932 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008933
Chet Haasedf030d22010-07-30 17:22:38 -07008934 /**
8935 * The visual y position of this view, in pixels. This is equivalent to the
8936 * {@link #setTranslationY(float) translationY} property plus the current
8937 * {@link #getTop() top} property.
8938 *
8939 * @return The visual y position of this view, in pixels.
8940 */
Chet Haasea5531132012-02-02 13:41:44 -08008941 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008942 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008943 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008944 }
8945
8946 /**
8947 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8948 * {@link #setTranslationY(float) translationY} property to be the difference between
8949 * the y value passed in and the current {@link #getTop() top} property.
8950 *
8951 * @param y The visual y position of this view, in pixels.
8952 */
8953 public void setY(float y) {
8954 setTranslationY(y - mTop);
8955 }
8956
8957
8958 /**
8959 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8960 * This position is post-layout, in addition to wherever the object's
8961 * layout placed it.
8962 *
8963 * @return The horizontal position of this view relative to its left position, in pixels.
8964 */
Chet Haasea5531132012-02-02 13:41:44 -08008965 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008966 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008967 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008968 }
8969
8970 /**
8971 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8972 * This effectively positions the object post-layout, in addition to wherever the object's
8973 * layout placed it.
8974 *
8975 * @param translationX The horizontal position of this view relative to its left position,
8976 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008977 *
8978 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008979 */
8980 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008981 ensureTransformationInfo();
8982 final TransformationInfo info = mTransformationInfo;
8983 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008984 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008985 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008986 info.mTranslationX = translationX;
8987 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008988 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008989 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8990 mDisplayList.setTranslationX(translationX);
8991 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008992 }
8993 }
8994
8995 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008996 * The horizontal location of this view relative to its {@link #getTop() top} position.
8997 * This position is post-layout, in addition to wherever the object's
8998 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008999 *
Chet Haasedf030d22010-07-30 17:22:38 -07009000 * @return The vertical position of this view relative to its top position,
9001 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009002 */
Chet Haasea5531132012-02-02 13:41:44 -08009003 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009004 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009005 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009006 }
9007
9008 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009009 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9010 * This effectively positions the object post-layout, in addition to wherever the object's
9011 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009012 *
Chet Haasedf030d22010-07-30 17:22:38 -07009013 * @param translationY The vertical position of this view relative to its top position,
9014 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009015 *
9016 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009017 */
Chet Haasedf030d22010-07-30 17:22:38 -07009018 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009019 ensureTransformationInfo();
9020 final TransformationInfo info = mTransformationInfo;
9021 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009022 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009023 info.mTranslationY = translationY;
9024 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009025 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08009026 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9027 mDisplayList.setTranslationY(translationY);
9028 }
Chet Haasedf030d22010-07-30 17:22:38 -07009029 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009030 }
9031
9032 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009033 * Hit rectangle in parent's coordinates
9034 *
9035 * @param outRect The hit rectangle of the view.
9036 */
9037 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009038 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009039 final TransformationInfo info = mTransformationInfo;
9040 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009041 outRect.set(mLeft, mTop, mRight, mBottom);
9042 } else {
9043 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009044 tmpRect.set(-info.mPivotX, -info.mPivotY,
9045 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9046 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009047 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9048 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009049 }
9050 }
9051
9052 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009053 * Determines whether the given point, in local coordinates is inside the view.
9054 */
9055 /*package*/ final boolean pointInView(float localX, float localY) {
9056 return localX >= 0 && localX < (mRight - mLeft)
9057 && localY >= 0 && localY < (mBottom - mTop);
9058 }
9059
9060 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009061 * Utility method to determine whether the given point, in local coordinates,
9062 * is inside the view, where the area of the view is expanded by the slop factor.
9063 * This method is called while processing touch-move events to determine if the event
9064 * is still within the view.
9065 */
9066 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009067 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009068 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009069 }
9070
9071 /**
9072 * When a view has focus and the user navigates away from it, the next view is searched for
9073 * starting from the rectangle filled in by this method.
9074 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009075 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9076 * of the view. However, if your view maintains some idea of internal selection,
9077 * such as a cursor, or a selected row or column, you should override this method and
9078 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009079 *
9080 * @param r The rectangle to fill in, in this view's coordinates.
9081 */
9082 public void getFocusedRect(Rect r) {
9083 getDrawingRect(r);
9084 }
9085
9086 /**
9087 * If some part of this view is not clipped by any of its parents, then
9088 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009089 * coordinates (without taking possible View rotations into account), offset
9090 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9091 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 *
9093 * @param r If true is returned, r holds the global coordinates of the
9094 * visible portion of this view.
9095 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9096 * between this view and its root. globalOffet may be null.
9097 * @return true if r is non-empty (i.e. part of the view is visible at the
9098 * root level.
9099 */
9100 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9101 int width = mRight - mLeft;
9102 int height = mBottom - mTop;
9103 if (width > 0 && height > 0) {
9104 r.set(0, 0, width, height);
9105 if (globalOffset != null) {
9106 globalOffset.set(-mScrollX, -mScrollY);
9107 }
9108 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9109 }
9110 return false;
9111 }
9112
9113 public final boolean getGlobalVisibleRect(Rect r) {
9114 return getGlobalVisibleRect(r, null);
9115 }
9116
9117 public final boolean getLocalVisibleRect(Rect r) {
9118 Point offset = new Point();
9119 if (getGlobalVisibleRect(r, offset)) {
9120 r.offset(-offset.x, -offset.y); // make r local
9121 return true;
9122 }
9123 return false;
9124 }
9125
9126 /**
9127 * Offset this view's vertical location by the specified number of pixels.
9128 *
9129 * @param offset the number of pixels to offset the view by
9130 */
9131 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009132 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009133 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009134 final boolean matrixIsIdentity = mTransformationInfo == null
9135 || mTransformationInfo.mMatrixIsIdentity;
9136 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009137 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9138 invalidateViewProperty(false, false);
9139 } else {
9140 final ViewParent p = mParent;
9141 if (p != null && mAttachInfo != null) {
9142 final Rect r = mAttachInfo.mTmpInvalRect;
9143 int minTop;
9144 int maxBottom;
9145 int yLoc;
9146 if (offset < 0) {
9147 minTop = mTop + offset;
9148 maxBottom = mBottom;
9149 yLoc = offset;
9150 } else {
9151 minTop = mTop;
9152 maxBottom = mBottom + offset;
9153 yLoc = 0;
9154 }
9155 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9156 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009157 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009158 }
9159 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009160 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009161 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009162
Chet Haasec3aa3612010-06-17 08:50:37 -07009163 mTop += offset;
9164 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009165 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9166 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009167 invalidateViewProperty(false, false);
9168 } else {
9169 if (!matrixIsIdentity) {
9170 invalidateViewProperty(false, true);
9171 }
9172 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009173 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009175 }
9176
9177 /**
9178 * Offset this view's horizontal location by the specified amount of pixels.
9179 *
9180 * @param offset the numer of pixels to offset the view by
9181 */
9182 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009183 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009184 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009185 final boolean matrixIsIdentity = mTransformationInfo == null
9186 || mTransformationInfo.mMatrixIsIdentity;
9187 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009188 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9189 invalidateViewProperty(false, false);
9190 } else {
9191 final ViewParent p = mParent;
9192 if (p != null && mAttachInfo != null) {
9193 final Rect r = mAttachInfo.mTmpInvalRect;
9194 int minLeft;
9195 int maxRight;
9196 if (offset < 0) {
9197 minLeft = mLeft + offset;
9198 maxRight = mRight;
9199 } else {
9200 minLeft = mLeft;
9201 maxRight = mRight + offset;
9202 }
9203 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9204 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009205 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009206 }
9207 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009208 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009209 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009210
Chet Haasec3aa3612010-06-17 08:50:37 -07009211 mLeft += offset;
9212 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009213 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9214 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009215 invalidateViewProperty(false, false);
9216 } else {
9217 if (!matrixIsIdentity) {
9218 invalidateViewProperty(false, true);
9219 }
9220 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009221 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009223 }
9224
9225 /**
9226 * Get the LayoutParams associated with this view. All views should have
9227 * layout parameters. These supply parameters to the <i>parent</i> of this
9228 * view specifying how it should be arranged. There are many subclasses of
9229 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9230 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009231 *
9232 * This method may return null if this View is not attached to a parent
9233 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9234 * was not invoked successfully. When a View is attached to a parent
9235 * ViewGroup, this method must not return null.
9236 *
9237 * @return The LayoutParams associated with this view, or null if no
9238 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009240 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009241 public ViewGroup.LayoutParams getLayoutParams() {
9242 return mLayoutParams;
9243 }
9244
9245 /**
9246 * Set the layout parameters associated with this view. These supply
9247 * parameters to the <i>parent</i> of this view specifying how it should be
9248 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9249 * correspond to the different subclasses of ViewGroup that are responsible
9250 * for arranging their children.
9251 *
Romain Guy01c174b2011-02-22 11:51:06 -08009252 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009253 */
9254 public void setLayoutParams(ViewGroup.LayoutParams params) {
9255 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009256 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009257 }
9258 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009259 if (mParent instanceof ViewGroup) {
9260 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262 requestLayout();
9263 }
9264
9265 /**
9266 * Set the scrolled position of your view. This will cause a call to
9267 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9268 * invalidated.
9269 * @param x the x position to scroll to
9270 * @param y the y position to scroll to
9271 */
9272 public void scrollTo(int x, int y) {
9273 if (mScrollX != x || mScrollY != y) {
9274 int oldX = mScrollX;
9275 int oldY = mScrollY;
9276 mScrollX = x;
9277 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009278 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009279 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009280 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009281 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009283 }
9284 }
9285
9286 /**
9287 * Move the scrolled position of your view. This will cause a call to
9288 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9289 * invalidated.
9290 * @param x the amount of pixels to scroll by horizontally
9291 * @param y the amount of pixels to scroll by vertically
9292 */
9293 public void scrollBy(int x, int y) {
9294 scrollTo(mScrollX + x, mScrollY + y);
9295 }
9296
9297 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009298 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9299 * animation to fade the scrollbars out after a default delay. If a subclass
9300 * provides animated scrolling, the start delay should equal the duration
9301 * of the scrolling animation.</p>
9302 *
9303 * <p>The animation starts only if at least one of the scrollbars is
9304 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9305 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9306 * this method returns true, and false otherwise. If the animation is
9307 * started, this method calls {@link #invalidate()}; in that case the
9308 * caller should not call {@link #invalidate()}.</p>
9309 *
9310 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009311 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009312 *
9313 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9314 * and {@link #scrollTo(int, int)}.</p>
9315 *
9316 * @return true if the animation is played, false otherwise
9317 *
9318 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009319 * @see #scrollBy(int, int)
9320 * @see #scrollTo(int, int)
9321 * @see #isHorizontalScrollBarEnabled()
9322 * @see #isVerticalScrollBarEnabled()
9323 * @see #setHorizontalScrollBarEnabled(boolean)
9324 * @see #setVerticalScrollBarEnabled(boolean)
9325 */
9326 protected boolean awakenScrollBars() {
9327 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009328 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009329 }
9330
9331 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009332 * Trigger the scrollbars to draw.
9333 * This method differs from awakenScrollBars() only in its default duration.
9334 * initialAwakenScrollBars() will show the scroll bars for longer than
9335 * usual to give the user more of a chance to notice them.
9336 *
9337 * @return true if the animation is played, false otherwise.
9338 */
9339 private boolean initialAwakenScrollBars() {
9340 return mScrollCache != null &&
9341 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9342 }
9343
9344 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009345 * <p>
9346 * Trigger the scrollbars to draw. When invoked this method starts an
9347 * animation to fade the scrollbars out after a fixed delay. If a subclass
9348 * provides animated scrolling, the start delay should equal the duration of
9349 * the scrolling animation.
9350 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009351 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009352 * <p>
9353 * The animation starts only if at least one of the scrollbars is enabled,
9354 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9355 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9356 * this method returns true, and false otherwise. If the animation is
9357 * started, this method calls {@link #invalidate()}; in that case the caller
9358 * should not call {@link #invalidate()}.
9359 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009360 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009361 * <p>
9362 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009363 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009364 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009365 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009366 * @param startDelay the delay, in milliseconds, after which the animation
9367 * should start; when the delay is 0, the animation starts
9368 * immediately
9369 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009370 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009371 * @see #scrollBy(int, int)
9372 * @see #scrollTo(int, int)
9373 * @see #isHorizontalScrollBarEnabled()
9374 * @see #isVerticalScrollBarEnabled()
9375 * @see #setHorizontalScrollBarEnabled(boolean)
9376 * @see #setVerticalScrollBarEnabled(boolean)
9377 */
9378 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009379 return awakenScrollBars(startDelay, true);
9380 }
Joe Malin32736f02011-01-19 16:14:20 -08009381
Mike Cleron290947b2009-09-29 18:34:32 -07009382 /**
9383 * <p>
9384 * Trigger the scrollbars to draw. When invoked this method starts an
9385 * animation to fade the scrollbars out after a fixed delay. If a subclass
9386 * provides animated scrolling, the start delay should equal the duration of
9387 * the scrolling animation.
9388 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009389 *
Mike Cleron290947b2009-09-29 18:34:32 -07009390 * <p>
9391 * The animation starts only if at least one of the scrollbars is enabled,
9392 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9393 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9394 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009395 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009396 * is set to true; in that case the caller
9397 * should not call {@link #invalidate()}.
9398 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009399 *
Mike Cleron290947b2009-09-29 18:34:32 -07009400 * <p>
9401 * This method should be invoked everytime a subclass directly updates the
9402 * scroll parameters.
9403 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009404 *
Mike Cleron290947b2009-09-29 18:34:32 -07009405 * @param startDelay the delay, in milliseconds, after which the animation
9406 * should start; when the delay is 0, the animation starts
9407 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009408 *
Mike Cleron290947b2009-09-29 18:34:32 -07009409 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009410 *
Mike Cleron290947b2009-09-29 18:34:32 -07009411 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009412 *
Mike Cleron290947b2009-09-29 18:34:32 -07009413 * @see #scrollBy(int, int)
9414 * @see #scrollTo(int, int)
9415 * @see #isHorizontalScrollBarEnabled()
9416 * @see #isVerticalScrollBarEnabled()
9417 * @see #setHorizontalScrollBarEnabled(boolean)
9418 * @see #setVerticalScrollBarEnabled(boolean)
9419 */
9420 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009421 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009422
Mike Cleronf116bf82009-09-27 19:14:12 -07009423 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9424 return false;
9425 }
9426
9427 if (scrollCache.scrollBar == null) {
9428 scrollCache.scrollBar = new ScrollBarDrawable();
9429 }
9430
9431 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9432
Mike Cleron290947b2009-09-29 18:34:32 -07009433 if (invalidate) {
9434 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009435 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009436 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009437
9438 if (scrollCache.state == ScrollabilityCache.OFF) {
9439 // FIXME: this is copied from WindowManagerService.
9440 // We should get this value from the system when it
9441 // is possible to do so.
9442 final int KEY_REPEAT_FIRST_DELAY = 750;
9443 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9444 }
9445
9446 // Tell mScrollCache when we should start fading. This may
9447 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009448 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009449 scrollCache.fadeStartTime = fadeStartTime;
9450 scrollCache.state = ScrollabilityCache.ON;
9451
9452 // Schedule our fader to run, unscheduling any old ones first
9453 if (mAttachInfo != null) {
9454 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9455 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9456 }
9457
9458 return true;
9459 }
9460
9461 return false;
9462 }
9463
9464 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009465 * Do not invalidate views which are not visible and which are not running an animation. They
9466 * will not get drawn and they should not set dirty flags as if they will be drawn
9467 */
9468 private boolean skipInvalidate() {
9469 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9470 (!(mParent instanceof ViewGroup) ||
9471 !((ViewGroup) mParent).isViewTransitioning(this));
9472 }
9473 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009474 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009475 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9476 * in the future. This must be called from a UI thread. To call from a non-UI
9477 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009478 *
9479 * WARNING: This method is destructive to dirty.
9480 * @param dirty the rectangle representing the bounds of the dirty region
9481 */
9482 public void invalidate(Rect dirty) {
9483 if (ViewDebug.TRACE_HIERARCHY) {
9484 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9485 }
9486
Chet Haaseaceafe62011-08-26 15:44:33 -07009487 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009488 return;
9489 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009490 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009491 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9492 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009494 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009495 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009496 final ViewParent p = mParent;
9497 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009498 //noinspection PointlessBooleanExpression,ConstantConditions
9499 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9500 if (p != null && ai != null && ai.mHardwareAccelerated) {
9501 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009502 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009503 p.invalidateChild(this, null);
9504 return;
9505 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009507 if (p != null && ai != null) {
9508 final int scrollX = mScrollX;
9509 final int scrollY = mScrollY;
9510 final Rect r = ai.mTmpInvalRect;
9511 r.set(dirty.left - scrollX, dirty.top - scrollY,
9512 dirty.right - scrollX, dirty.bottom - scrollY);
9513 mParent.invalidateChild(this, r);
9514 }
9515 }
9516 }
9517
9518 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009519 * 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 -08009520 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009521 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9522 * will be called at some point in the future. This must be called from
9523 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524 * @param l the left position of the dirty region
9525 * @param t the top position of the dirty region
9526 * @param r the right position of the dirty region
9527 * @param b the bottom position of the dirty region
9528 */
9529 public void invalidate(int l, int t, int r, int b) {
9530 if (ViewDebug.TRACE_HIERARCHY) {
9531 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9532 }
9533
Chet Haaseaceafe62011-08-26 15:44:33 -07009534 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009535 return;
9536 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009537 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009538 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9539 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009540 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009541 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009542 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009543 final ViewParent p = mParent;
9544 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009545 //noinspection PointlessBooleanExpression,ConstantConditions
9546 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9547 if (p != null && ai != null && ai.mHardwareAccelerated) {
9548 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009549 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009550 p.invalidateChild(this, null);
9551 return;
9552 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009554 if (p != null && ai != null && l < r && t < b) {
9555 final int scrollX = mScrollX;
9556 final int scrollY = mScrollY;
9557 final Rect tmpr = ai.mTmpInvalRect;
9558 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9559 p.invalidateChild(this, tmpr);
9560 }
9561 }
9562 }
9563
9564 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009565 * Invalidate the whole view. If the view is visible,
9566 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9567 * the future. This must be called from a UI thread. To call from a non-UI thread,
9568 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009569 */
9570 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009571 invalidate(true);
9572 }
Joe Malin32736f02011-01-19 16:14:20 -08009573
Chet Haaseed032702010-10-01 14:05:54 -07009574 /**
9575 * This is where the invalidate() work actually happens. A full invalidate()
9576 * causes the drawing cache to be invalidated, but this function can be called with
9577 * invalidateCache set to false to skip that invalidation step for cases that do not
9578 * need it (for example, a component that remains at the same dimensions with the same
9579 * content).
9580 *
9581 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9582 * well. This is usually true for a full invalidate, but may be set to false if the
9583 * View's contents or dimensions have not changed.
9584 */
Romain Guy849d0a32011-02-01 17:20:48 -08009585 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586 if (ViewDebug.TRACE_HIERARCHY) {
9587 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9588 }
9589
Chet Haaseaceafe62011-08-26 15:44:33 -07009590 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009591 return;
9592 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009593 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009594 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009595 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9596 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009597 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009598 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009599 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009600 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009601 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009603 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009604 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009605 //noinspection PointlessBooleanExpression,ConstantConditions
9606 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9607 if (p != null && ai != null && ai.mHardwareAccelerated) {
9608 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009609 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009610 p.invalidateChild(this, null);
9611 return;
9612 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009613 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615 if (p != null && ai != null) {
9616 final Rect r = ai.mTmpInvalRect;
9617 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9618 // Don't call invalidate -- we don't want to internally scroll
9619 // our own bounds
9620 p.invalidateChild(this, r);
9621 }
9622 }
9623 }
9624
9625 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009626 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9627 * set any flags or handle all of the cases handled by the default invalidation methods.
9628 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9629 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9630 * walk up the hierarchy, transforming the dirty rect as necessary.
9631 *
9632 * The method also handles normal invalidation logic if display list properties are not
9633 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9634 * backup approach, to handle these cases used in the various property-setting methods.
9635 *
9636 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9637 * are not being used in this view
9638 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9639 * list properties are not being used in this view
9640 */
9641 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
9642 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
9643 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
9644 if (invalidateParent) {
9645 invalidateParentCaches();
9646 }
9647 if (forceRedraw) {
9648 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9649 }
9650 invalidate(false);
9651 } else {
9652 final AttachInfo ai = mAttachInfo;
9653 final ViewParent p = mParent;
9654 if (p != null && ai != null) {
9655 final Rect r = ai.mTmpInvalRect;
9656 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9657 if (mParent instanceof ViewGroup) {
9658 ((ViewGroup) mParent).invalidateChildFast(this, r);
9659 } else {
9660 mParent.invalidateChild(this, r);
9661 }
9662 }
9663 }
9664 }
9665
9666 /**
9667 * Utility method to transform a given Rect by the current matrix of this view.
9668 */
9669 void transformRect(final Rect rect) {
9670 if (!getMatrix().isIdentity()) {
9671 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9672 boundingRect.set(rect);
9673 getMatrix().mapRect(boundingRect);
9674 rect.set((int) (boundingRect.left - 0.5f),
9675 (int) (boundingRect.top - 0.5f),
9676 (int) (boundingRect.right + 0.5f),
9677 (int) (boundingRect.bottom + 0.5f));
9678 }
9679 }
9680
9681 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009682 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009683 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9684 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009685 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9686 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009687 *
9688 * @hide
9689 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009690 protected void invalidateParentCaches() {
9691 if (mParent instanceof View) {
9692 ((View) mParent).mPrivateFlags |= INVALIDATED;
9693 }
9694 }
Joe Malin32736f02011-01-19 16:14:20 -08009695
Romain Guy0fd89bf2011-01-26 15:41:30 -08009696 /**
9697 * Used to indicate that the parent of this view should be invalidated. This functionality
9698 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9699 * which is necessary when various parent-managed properties of the view change, such as
9700 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9701 * an invalidation event to the parent.
9702 *
9703 * @hide
9704 */
9705 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009706 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009707 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009708 }
9709 }
9710
9711 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009712 * Indicates whether this View is opaque. An opaque View guarantees that it will
9713 * draw all the pixels overlapping its bounds using a fully opaque color.
9714 *
9715 * Subclasses of View should override this method whenever possible to indicate
9716 * whether an instance is opaque. Opaque Views are treated in a special way by
9717 * the View hierarchy, possibly allowing it to perform optimizations during
9718 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009719 *
Romain Guy24443ea2009-05-11 11:56:30 -07009720 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009721 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009722 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009723 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009724 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009725 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9726 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009727 }
9728
Adam Powell20232d02010-12-08 21:08:53 -08009729 /**
9730 * @hide
9731 */
9732 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009733 // Opaque if:
9734 // - Has a background
9735 // - Background is opaque
9736 // - Doesn't have scrollbars or scrollbars are inside overlay
9737
Philip Milne6c8ea062012-04-03 17:38:43 -07009738 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009739 mPrivateFlags |= OPAQUE_BACKGROUND;
9740 } else {
9741 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9742 }
9743
9744 final int flags = mViewFlags;
9745 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9746 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9747 mPrivateFlags |= OPAQUE_SCROLLBARS;
9748 } else {
9749 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9750 }
9751 }
9752
9753 /**
9754 * @hide
9755 */
9756 protected boolean hasOpaqueScrollbars() {
9757 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009758 }
9759
9760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009761 * @return A handler associated with the thread running the View. This
9762 * handler can be used to pump events in the UI events queue.
9763 */
9764 public Handler getHandler() {
9765 if (mAttachInfo != null) {
9766 return mAttachInfo.mHandler;
9767 }
9768 return null;
9769 }
9770
9771 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009772 * Gets the view root associated with the View.
9773 * @return The view root, or null if none.
9774 * @hide
9775 */
9776 public ViewRootImpl getViewRootImpl() {
9777 if (mAttachInfo != null) {
9778 return mAttachInfo.mViewRootImpl;
9779 }
9780 return null;
9781 }
9782
9783 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009784 * <p>Causes the Runnable to be added to the message queue.
9785 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009786 *
Romain Guye63a4f32011-08-11 11:33:31 -07009787 * <p>This method can be invoked from outside of the UI thread
9788 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009789 *
9790 * @param action The Runnable that will be executed.
9791 *
9792 * @return Returns true if the Runnable was successfully placed in to the
9793 * message queue. Returns false on failure, usually because the
9794 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009795 *
9796 * @see #postDelayed
9797 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009798 */
9799 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009800 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009801 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009802 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009804 // Assume that post will succeed later
9805 ViewRootImpl.getRunQueue().post(action);
9806 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009807 }
9808
9809 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009810 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009812 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009813 *
Romain Guye63a4f32011-08-11 11:33:31 -07009814 * <p>This method can be invoked from outside of the UI thread
9815 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 *
9817 * @param action The Runnable that will be executed.
9818 * @param delayMillis The delay (in milliseconds) until the Runnable
9819 * will be executed.
9820 *
9821 * @return true if the Runnable was successfully placed in to the
9822 * message queue. Returns false on failure, usually because the
9823 * looper processing the message queue is exiting. Note that a
9824 * result of true does not mean the Runnable will be processed --
9825 * if the looper is quit before the delivery time of the message
9826 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009827 *
9828 * @see #post
9829 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 */
9831 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009832 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009833 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009834 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009835 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009836 // Assume that post will succeed later
9837 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9838 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 }
9840
9841 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009842 * <p>Causes the Runnable to execute on the next animation time step.
9843 * The runnable will be run on the user interface thread.</p>
9844 *
9845 * <p>This method can be invoked from outside of the UI thread
9846 * only when this View is attached to a window.</p>
9847 *
9848 * @param action The Runnable that will be executed.
9849 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009850 * @see #postOnAnimationDelayed
9851 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009852 */
9853 public void postOnAnimation(Runnable action) {
9854 final AttachInfo attachInfo = mAttachInfo;
9855 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009856 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9857 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009858 } else {
9859 // Assume that post will succeed later
9860 ViewRootImpl.getRunQueue().post(action);
9861 }
9862 }
9863
9864 /**
9865 * <p>Causes the Runnable to execute on the next animation time step,
9866 * after the specified amount of time elapses.
9867 * The runnable will be run on the user interface thread.</p>
9868 *
9869 * <p>This method can be invoked from outside of the UI thread
9870 * only when this View is attached to a window.</p>
9871 *
9872 * @param action The Runnable that will be executed.
9873 * @param delayMillis The delay (in milliseconds) until the Runnable
9874 * will be executed.
9875 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009876 * @see #postOnAnimation
9877 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009878 */
9879 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9880 final AttachInfo attachInfo = mAttachInfo;
9881 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009882 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9883 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009884 } else {
9885 // Assume that post will succeed later
9886 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9887 }
9888 }
9889
9890 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009891 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009892 *
Romain Guye63a4f32011-08-11 11:33:31 -07009893 * <p>This method can be invoked from outside of the UI thread
9894 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009895 *
9896 * @param action The Runnable to remove from the message handling queue
9897 *
9898 * @return true if this view could ask the Handler to remove the Runnable,
9899 * false otherwise. When the returned value is true, the Runnable
9900 * may or may not have been actually removed from the message queue
9901 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009902 *
9903 * @see #post
9904 * @see #postDelayed
9905 * @see #postOnAnimation
9906 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009907 */
9908 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009909 if (action != null) {
9910 final AttachInfo attachInfo = mAttachInfo;
9911 if (attachInfo != null) {
9912 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009913 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9914 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009915 } else {
9916 // Assume that post will succeed later
9917 ViewRootImpl.getRunQueue().removeCallbacks(action);
9918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009920 return true;
9921 }
9922
9923 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009924 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9925 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 *
Romain Guye63a4f32011-08-11 11:33:31 -07009927 * <p>This method can be invoked from outside of the UI thread
9928 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009929 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009931 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009932 */
9933 public void postInvalidate() {
9934 postInvalidateDelayed(0);
9935 }
9936
9937 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009938 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9939 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009940 *
Romain Guye63a4f32011-08-11 11:33:31 -07009941 * <p>This method can be invoked from outside of the UI thread
9942 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 *
9944 * @param left The left coordinate of the rectangle to invalidate.
9945 * @param top The top coordinate of the rectangle to invalidate.
9946 * @param right The right coordinate of the rectangle to invalidate.
9947 * @param bottom The bottom coordinate of the rectangle to invalidate.
9948 *
9949 * @see #invalidate(int, int, int, int)
9950 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009951 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009952 */
9953 public void postInvalidate(int left, int top, int right, int bottom) {
9954 postInvalidateDelayed(0, left, top, right, bottom);
9955 }
9956
9957 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009958 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9959 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009960 *
Romain Guye63a4f32011-08-11 11:33:31 -07009961 * <p>This method can be invoked from outside of the UI thread
9962 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009963 *
9964 * @param delayMilliseconds the duration in milliseconds to delay the
9965 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009966 *
9967 * @see #invalidate()
9968 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969 */
9970 public void postInvalidateDelayed(long delayMilliseconds) {
9971 // We try only with the AttachInfo because there's no point in invalidating
9972 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009973 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009974 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009975 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009976 }
9977 }
9978
9979 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009980 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9981 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009982 *
Romain Guye63a4f32011-08-11 11:33:31 -07009983 * <p>This method can be invoked from outside of the UI thread
9984 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009985 *
9986 * @param delayMilliseconds the duration in milliseconds to delay the
9987 * invalidation by
9988 * @param left The left coordinate of the rectangle to invalidate.
9989 * @param top The top coordinate of the rectangle to invalidate.
9990 * @param right The right coordinate of the rectangle to invalidate.
9991 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009992 *
9993 * @see #invalidate(int, int, int, int)
9994 * @see #invalidate(Rect)
9995 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009996 */
9997 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9998 int right, int bottom) {
9999
10000 // We try only with the AttachInfo because there's no point in invalidating
10001 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010002 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010003 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010004 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10005 info.target = this;
10006 info.left = left;
10007 info.top = top;
10008 info.right = right;
10009 info.bottom = bottom;
10010
Jeff Browna175a5b2012-02-15 19:18:31 -080010011 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010012 }
10013 }
10014
10015 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010016 * <p>Cause an invalidate to happen on the next animation time step, typically the
10017 * next display frame.</p>
10018 *
10019 * <p>This method can be invoked from outside of the UI thread
10020 * only when this View is attached to a window.</p>
10021 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010022 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010023 */
10024 public void postInvalidateOnAnimation() {
10025 // We try only with the AttachInfo because there's no point in invalidating
10026 // if we are not attached to our window
10027 final AttachInfo attachInfo = mAttachInfo;
10028 if (attachInfo != null) {
10029 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10030 }
10031 }
10032
10033 /**
10034 * <p>Cause an invalidate of the specified area to happen on the next animation
10035 * time step, typically the next display frame.</p>
10036 *
10037 * <p>This method can be invoked from outside of the UI thread
10038 * only when this View is attached to a window.</p>
10039 *
10040 * @param left The left coordinate of the rectangle to invalidate.
10041 * @param top The top coordinate of the rectangle to invalidate.
10042 * @param right The right coordinate of the rectangle to invalidate.
10043 * @param bottom The bottom coordinate of the rectangle to invalidate.
10044 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010045 * @see #invalidate(int, int, int, int)
10046 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010047 */
10048 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10049 // We try only with the AttachInfo because there's no point in invalidating
10050 // if we are not attached to our window
10051 final AttachInfo attachInfo = mAttachInfo;
10052 if (attachInfo != null) {
10053 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10054 info.target = this;
10055 info.left = left;
10056 info.top = top;
10057 info.right = right;
10058 info.bottom = bottom;
10059
10060 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10061 }
10062 }
10063
10064 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010065 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10066 * This event is sent at most once every
10067 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10068 */
10069 private void postSendViewScrolledAccessibilityEventCallback() {
10070 if (mSendViewScrolledAccessibilityEvent == null) {
10071 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10072 }
10073 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10074 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10075 postDelayed(mSendViewScrolledAccessibilityEvent,
10076 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10077 }
10078 }
10079
10080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 * Called by a parent to request that a child update its values for mScrollX
10082 * and mScrollY if necessary. This will typically be done if the child is
10083 * animating a scroll using a {@link android.widget.Scroller Scroller}
10084 * object.
10085 */
10086 public void computeScroll() {
10087 }
10088
10089 /**
10090 * <p>Indicate whether the horizontal edges are faded when the view is
10091 * scrolled horizontally.</p>
10092 *
10093 * @return true if the horizontal edges should are faded on scroll, false
10094 * otherwise
10095 *
10096 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010097 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010098 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010099 */
10100 public boolean isHorizontalFadingEdgeEnabled() {
10101 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10102 }
10103
10104 /**
10105 * <p>Define whether the horizontal edges should be faded when this view
10106 * is scrolled horizontally.</p>
10107 *
10108 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10109 * be faded when the view is scrolled
10110 * horizontally
10111 *
10112 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010113 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010114 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010115 */
10116 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10117 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10118 if (horizontalFadingEdgeEnabled) {
10119 initScrollCache();
10120 }
10121
10122 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10123 }
10124 }
10125
10126 /**
10127 * <p>Indicate whether the vertical edges are faded when the view is
10128 * scrolled horizontally.</p>
10129 *
10130 * @return true if the vertical edges should are faded on scroll, false
10131 * otherwise
10132 *
10133 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010134 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010135 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 */
10137 public boolean isVerticalFadingEdgeEnabled() {
10138 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10139 }
10140
10141 /**
10142 * <p>Define whether the vertical edges should be faded when this view
10143 * is scrolled vertically.</p>
10144 *
10145 * @param verticalFadingEdgeEnabled true if the vertical edges should
10146 * be faded when the view is scrolled
10147 * vertically
10148 *
10149 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010150 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010151 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 */
10153 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10154 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10155 if (verticalFadingEdgeEnabled) {
10156 initScrollCache();
10157 }
10158
10159 mViewFlags ^= FADING_EDGE_VERTICAL;
10160 }
10161 }
10162
10163 /**
10164 * Returns the strength, or intensity, of the top faded edge. The strength is
10165 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10166 * returns 0.0 or 1.0 but no value in between.
10167 *
10168 * Subclasses should override this method to provide a smoother fade transition
10169 * when scrolling occurs.
10170 *
10171 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10172 */
10173 protected float getTopFadingEdgeStrength() {
10174 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10175 }
10176
10177 /**
10178 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10179 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10180 * returns 0.0 or 1.0 but no value in between.
10181 *
10182 * Subclasses should override this method to provide a smoother fade transition
10183 * when scrolling occurs.
10184 *
10185 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10186 */
10187 protected float getBottomFadingEdgeStrength() {
10188 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10189 computeVerticalScrollRange() ? 1.0f : 0.0f;
10190 }
10191
10192 /**
10193 * Returns the strength, or intensity, of the left faded edge. The strength is
10194 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10195 * returns 0.0 or 1.0 but no value in between.
10196 *
10197 * Subclasses should override this method to provide a smoother fade transition
10198 * when scrolling occurs.
10199 *
10200 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10201 */
10202 protected float getLeftFadingEdgeStrength() {
10203 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10204 }
10205
10206 /**
10207 * Returns the strength, or intensity, of the right faded edge. The strength is
10208 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10209 * returns 0.0 or 1.0 but no value in between.
10210 *
10211 * Subclasses should override this method to provide a smoother fade transition
10212 * when scrolling occurs.
10213 *
10214 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10215 */
10216 protected float getRightFadingEdgeStrength() {
10217 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10218 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10219 }
10220
10221 /**
10222 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10223 * scrollbar is not drawn by default.</p>
10224 *
10225 * @return true if the horizontal scrollbar should be painted, false
10226 * otherwise
10227 *
10228 * @see #setHorizontalScrollBarEnabled(boolean)
10229 */
10230 public boolean isHorizontalScrollBarEnabled() {
10231 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10232 }
10233
10234 /**
10235 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10236 * scrollbar is not drawn by default.</p>
10237 *
10238 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10239 * be painted
10240 *
10241 * @see #isHorizontalScrollBarEnabled()
10242 */
10243 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10244 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10245 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010246 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010247 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010248 }
10249 }
10250
10251 /**
10252 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10253 * scrollbar is not drawn by default.</p>
10254 *
10255 * @return true if the vertical scrollbar should be painted, false
10256 * otherwise
10257 *
10258 * @see #setVerticalScrollBarEnabled(boolean)
10259 */
10260 public boolean isVerticalScrollBarEnabled() {
10261 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10262 }
10263
10264 /**
10265 * <p>Define whether the vertical scrollbar should be drawn or not. The
10266 * scrollbar is not drawn by default.</p>
10267 *
10268 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10269 * be painted
10270 *
10271 * @see #isVerticalScrollBarEnabled()
10272 */
10273 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10274 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10275 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010276 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010277 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010278 }
10279 }
10280
Adam Powell20232d02010-12-08 21:08:53 -080010281 /**
10282 * @hide
10283 */
10284 protected void recomputePadding() {
10285 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 }
Joe Malin32736f02011-01-19 16:14:20 -080010287
Mike Cleronfe81d382009-09-28 14:22:16 -070010288 /**
10289 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010290 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010291 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010292 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010293 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010294 */
10295 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10296 initScrollCache();
10297 final ScrollabilityCache scrollabilityCache = mScrollCache;
10298 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010299 if (fadeScrollbars) {
10300 scrollabilityCache.state = ScrollabilityCache.OFF;
10301 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010302 scrollabilityCache.state = ScrollabilityCache.ON;
10303 }
10304 }
Joe Malin32736f02011-01-19 16:14:20 -080010305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010306 /**
Joe Malin32736f02011-01-19 16:14:20 -080010307 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010308 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010309 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010310 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010311 *
10312 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010313 */
10314 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010315 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010316 }
Joe Malin32736f02011-01-19 16:14:20 -080010317
Mike Cleron52f0a642009-09-28 18:21:37 -070010318 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010319 *
10320 * Returns the delay before scrollbars fade.
10321 *
10322 * @return the delay before scrollbars fade
10323 *
10324 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10325 */
10326 public int getScrollBarDefaultDelayBeforeFade() {
10327 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10328 mScrollCache.scrollBarDefaultDelayBeforeFade;
10329 }
10330
10331 /**
10332 * Define the delay before scrollbars fade.
10333 *
10334 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10335 *
10336 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10337 */
10338 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10339 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10340 }
10341
10342 /**
10343 *
10344 * Returns the scrollbar fade duration.
10345 *
10346 * @return the scrollbar fade duration
10347 *
10348 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10349 */
10350 public int getScrollBarFadeDuration() {
10351 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10352 mScrollCache.scrollBarFadeDuration;
10353 }
10354
10355 /**
10356 * Define the scrollbar fade duration.
10357 *
10358 * @param scrollBarFadeDuration - the scrollbar fade duration
10359 *
10360 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10361 */
10362 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10363 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10364 }
10365
10366 /**
10367 *
10368 * Returns the scrollbar size.
10369 *
10370 * @return the scrollbar size
10371 *
10372 * @attr ref android.R.styleable#View_scrollbarSize
10373 */
10374 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010375 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010376 mScrollCache.scrollBarSize;
10377 }
10378
10379 /**
10380 * Define the scrollbar size.
10381 *
10382 * @param scrollBarSize - the scrollbar size
10383 *
10384 * @attr ref android.R.styleable#View_scrollbarSize
10385 */
10386 public void setScrollBarSize(int scrollBarSize) {
10387 getScrollCache().scrollBarSize = scrollBarSize;
10388 }
10389
10390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010391 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10392 * inset. When inset, they add to the padding of the view. And the scrollbars
10393 * can be drawn inside the padding area or on the edge of the view. For example,
10394 * if a view has a background drawable and you want to draw the scrollbars
10395 * inside the padding specified by the drawable, you can use
10396 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10397 * appear at the edge of the view, ignoring the padding, then you can use
10398 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10399 * @param style the style of the scrollbars. Should be one of
10400 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10401 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10402 * @see #SCROLLBARS_INSIDE_OVERLAY
10403 * @see #SCROLLBARS_INSIDE_INSET
10404 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10405 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010406 *
10407 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 */
10409 public void setScrollBarStyle(int style) {
10410 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10411 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010412 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010413 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010414 }
10415 }
10416
10417 /**
10418 * <p>Returns the current scrollbar style.</p>
10419 * @return the current scrollbar style
10420 * @see #SCROLLBARS_INSIDE_OVERLAY
10421 * @see #SCROLLBARS_INSIDE_INSET
10422 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10423 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010424 *
10425 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010427 @ViewDebug.ExportedProperty(mapping = {
10428 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10429 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10430 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10431 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10432 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 public int getScrollBarStyle() {
10434 return mViewFlags & SCROLLBARS_STYLE_MASK;
10435 }
10436
10437 /**
10438 * <p>Compute the horizontal range that the horizontal scrollbar
10439 * represents.</p>
10440 *
10441 * <p>The range is expressed in arbitrary units that must be the same as the
10442 * units used by {@link #computeHorizontalScrollExtent()} and
10443 * {@link #computeHorizontalScrollOffset()}.</p>
10444 *
10445 * <p>The default range is the drawing width of this view.</p>
10446 *
10447 * @return the total horizontal range represented by the horizontal
10448 * scrollbar
10449 *
10450 * @see #computeHorizontalScrollExtent()
10451 * @see #computeHorizontalScrollOffset()
10452 * @see android.widget.ScrollBarDrawable
10453 */
10454 protected int computeHorizontalScrollRange() {
10455 return getWidth();
10456 }
10457
10458 /**
10459 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10460 * within the horizontal range. This value is used to compute the position
10461 * of the thumb within the scrollbar's track.</p>
10462 *
10463 * <p>The range is expressed in arbitrary units that must be the same as the
10464 * units used by {@link #computeHorizontalScrollRange()} and
10465 * {@link #computeHorizontalScrollExtent()}.</p>
10466 *
10467 * <p>The default offset is the scroll offset of this view.</p>
10468 *
10469 * @return the horizontal offset of the scrollbar's thumb
10470 *
10471 * @see #computeHorizontalScrollRange()
10472 * @see #computeHorizontalScrollExtent()
10473 * @see android.widget.ScrollBarDrawable
10474 */
10475 protected int computeHorizontalScrollOffset() {
10476 return mScrollX;
10477 }
10478
10479 /**
10480 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10481 * within the horizontal range. This value is used to compute the length
10482 * of the thumb within the scrollbar's track.</p>
10483 *
10484 * <p>The range is expressed in arbitrary units that must be the same as the
10485 * units used by {@link #computeHorizontalScrollRange()} and
10486 * {@link #computeHorizontalScrollOffset()}.</p>
10487 *
10488 * <p>The default extent is the drawing width of this view.</p>
10489 *
10490 * @return the horizontal extent of the scrollbar's thumb
10491 *
10492 * @see #computeHorizontalScrollRange()
10493 * @see #computeHorizontalScrollOffset()
10494 * @see android.widget.ScrollBarDrawable
10495 */
10496 protected int computeHorizontalScrollExtent() {
10497 return getWidth();
10498 }
10499
10500 /**
10501 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10502 *
10503 * <p>The range is expressed in arbitrary units that must be the same as the
10504 * units used by {@link #computeVerticalScrollExtent()} and
10505 * {@link #computeVerticalScrollOffset()}.</p>
10506 *
10507 * @return the total vertical range represented by the vertical scrollbar
10508 *
10509 * <p>The default range is the drawing height of this view.</p>
10510 *
10511 * @see #computeVerticalScrollExtent()
10512 * @see #computeVerticalScrollOffset()
10513 * @see android.widget.ScrollBarDrawable
10514 */
10515 protected int computeVerticalScrollRange() {
10516 return getHeight();
10517 }
10518
10519 /**
10520 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10521 * within the horizontal range. This value is used to compute the position
10522 * of the thumb within the scrollbar's track.</p>
10523 *
10524 * <p>The range is expressed in arbitrary units that must be the same as the
10525 * units used by {@link #computeVerticalScrollRange()} and
10526 * {@link #computeVerticalScrollExtent()}.</p>
10527 *
10528 * <p>The default offset is the scroll offset of this view.</p>
10529 *
10530 * @return the vertical offset of the scrollbar's thumb
10531 *
10532 * @see #computeVerticalScrollRange()
10533 * @see #computeVerticalScrollExtent()
10534 * @see android.widget.ScrollBarDrawable
10535 */
10536 protected int computeVerticalScrollOffset() {
10537 return mScrollY;
10538 }
10539
10540 /**
10541 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10542 * within the vertical range. This value is used to compute the length
10543 * of the thumb within the scrollbar's track.</p>
10544 *
10545 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010546 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 * {@link #computeVerticalScrollOffset()}.</p>
10548 *
10549 * <p>The default extent is the drawing height of this view.</p>
10550 *
10551 * @return the vertical extent of the scrollbar's thumb
10552 *
10553 * @see #computeVerticalScrollRange()
10554 * @see #computeVerticalScrollOffset()
10555 * @see android.widget.ScrollBarDrawable
10556 */
10557 protected int computeVerticalScrollExtent() {
10558 return getHeight();
10559 }
10560
10561 /**
Adam Powell69159442011-06-13 17:53:06 -070010562 * Check if this view can be scrolled horizontally in a certain direction.
10563 *
10564 * @param direction Negative to check scrolling left, positive to check scrolling right.
10565 * @return true if this view can be scrolled in the specified direction, false otherwise.
10566 */
10567 public boolean canScrollHorizontally(int direction) {
10568 final int offset = computeHorizontalScrollOffset();
10569 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10570 if (range == 0) return false;
10571 if (direction < 0) {
10572 return offset > 0;
10573 } else {
10574 return offset < range - 1;
10575 }
10576 }
10577
10578 /**
10579 * Check if this view can be scrolled vertically in a certain direction.
10580 *
10581 * @param direction Negative to check scrolling up, positive to check scrolling down.
10582 * @return true if this view can be scrolled in the specified direction, false otherwise.
10583 */
10584 public boolean canScrollVertically(int direction) {
10585 final int offset = computeVerticalScrollOffset();
10586 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10587 if (range == 0) return false;
10588 if (direction < 0) {
10589 return offset > 0;
10590 } else {
10591 return offset < range - 1;
10592 }
10593 }
10594
10595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010596 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10597 * scrollbars are painted only if they have been awakened first.</p>
10598 *
10599 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010600 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010601 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010603 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010604 // scrollbars are drawn only when the animation is running
10605 final ScrollabilityCache cache = mScrollCache;
10606 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010607
Mike Cleronf116bf82009-09-27 19:14:12 -070010608 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010609
Mike Cleronf116bf82009-09-27 19:14:12 -070010610 if (state == ScrollabilityCache.OFF) {
10611 return;
10612 }
Joe Malin32736f02011-01-19 16:14:20 -080010613
Mike Cleronf116bf82009-09-27 19:14:12 -070010614 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010615
Mike Cleronf116bf82009-09-27 19:14:12 -070010616 if (state == ScrollabilityCache.FADING) {
10617 // We're fading -- get our fade interpolation
10618 if (cache.interpolatorValues == null) {
10619 cache.interpolatorValues = new float[1];
10620 }
Joe Malin32736f02011-01-19 16:14:20 -080010621
Mike Cleronf116bf82009-09-27 19:14:12 -070010622 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010623
Mike Cleronf116bf82009-09-27 19:14:12 -070010624 // Stops the animation if we're done
10625 if (cache.scrollBarInterpolator.timeToValues(values) ==
10626 Interpolator.Result.FREEZE_END) {
10627 cache.state = ScrollabilityCache.OFF;
10628 } else {
10629 cache.scrollBar.setAlpha(Math.round(values[0]));
10630 }
Joe Malin32736f02011-01-19 16:14:20 -080010631
10632 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010633 // drawing. We only want this when we're fading so that
10634 // we prevent excessive redraws
10635 invalidate = true;
10636 } else {
10637 // We're just on -- but we may have been fading before so
10638 // reset alpha
10639 cache.scrollBar.setAlpha(255);
10640 }
10641
Joe Malin32736f02011-01-19 16:14:20 -080010642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 final int viewFlags = mViewFlags;
10644
10645 final boolean drawHorizontalScrollBar =
10646 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10647 final boolean drawVerticalScrollBar =
10648 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10649 && !isVerticalScrollBarHidden();
10650
10651 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10652 final int width = mRight - mLeft;
10653 final int height = mBottom - mTop;
10654
10655 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010656
Mike Reede8853fc2009-09-04 14:01:48 -040010657 final int scrollX = mScrollX;
10658 final int scrollY = mScrollY;
10659 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10660
Mike Cleronf116bf82009-09-27 19:14:12 -070010661 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010664 int size = scrollBar.getSize(false);
10665 if (size <= 0) {
10666 size = cache.scrollBarSize;
10667 }
10668
Mike Cleronf116bf82009-09-27 19:14:12 -070010669 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010670 computeHorizontalScrollOffset(),
10671 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010672 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010673 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010674 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010675 left = scrollX + (mPaddingLeft & inside);
10676 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10677 bottom = top + size;
10678 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10679 if (invalidate) {
10680 invalidate(left, top, right, bottom);
10681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 }
10683
10684 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010685 int size = scrollBar.getSize(true);
10686 if (size <= 0) {
10687 size = cache.scrollBarSize;
10688 }
10689
Mike Reede8853fc2009-09-04 14:01:48 -040010690 scrollBar.setParameters(computeVerticalScrollRange(),
10691 computeVerticalScrollOffset(),
10692 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010693 switch (mVerticalScrollbarPosition) {
10694 default:
10695 case SCROLLBAR_POSITION_DEFAULT:
10696 case SCROLLBAR_POSITION_RIGHT:
10697 left = scrollX + width - size - (mUserPaddingRight & inside);
10698 break;
10699 case SCROLLBAR_POSITION_LEFT:
10700 left = scrollX + (mUserPaddingLeft & inside);
10701 break;
10702 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010703 top = scrollY + (mPaddingTop & inside);
10704 right = left + size;
10705 bottom = scrollY + height - (mUserPaddingBottom & inside);
10706 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10707 if (invalidate) {
10708 invalidate(left, top, right, bottom);
10709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010710 }
10711 }
10712 }
10713 }
Romain Guy8506ab42009-06-11 17:35:47 -070010714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010716 * 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 -080010717 * FastScroller is visible.
10718 * @return whether to temporarily hide the vertical scrollbar
10719 * @hide
10720 */
10721 protected boolean isVerticalScrollBarHidden() {
10722 return false;
10723 }
10724
10725 /**
10726 * <p>Draw the horizontal scrollbar if
10727 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10728 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729 * @param canvas the canvas on which to draw the scrollbar
10730 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010731 *
10732 * @see #isHorizontalScrollBarEnabled()
10733 * @see #computeHorizontalScrollRange()
10734 * @see #computeHorizontalScrollExtent()
10735 * @see #computeHorizontalScrollOffset()
10736 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010737 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010738 */
Romain Guy8fb95422010-08-17 18:38:51 -070010739 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10740 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010741 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010742 scrollBar.draw(canvas);
10743 }
Mike Reede8853fc2009-09-04 14:01:48 -040010744
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10747 * returns true.</p>
10748 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 * @param canvas the canvas on which to draw the scrollbar
10750 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010751 *
10752 * @see #isVerticalScrollBarEnabled()
10753 * @see #computeVerticalScrollRange()
10754 * @see #computeVerticalScrollExtent()
10755 * @see #computeVerticalScrollOffset()
10756 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010757 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010758 */
Romain Guy8fb95422010-08-17 18:38:51 -070010759 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10760 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010761 scrollBar.setBounds(l, t, r, b);
10762 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 }
10764
10765 /**
10766 * Implement this to do your drawing.
10767 *
10768 * @param canvas the canvas on which the background will be drawn
10769 */
10770 protected void onDraw(Canvas canvas) {
10771 }
10772
10773 /*
10774 * Caller is responsible for calling requestLayout if necessary.
10775 * (This allows addViewInLayout to not request a new layout.)
10776 */
10777 void assignParent(ViewParent parent) {
10778 if (mParent == null) {
10779 mParent = parent;
10780 } else if (parent == null) {
10781 mParent = null;
10782 } else {
10783 throw new RuntimeException("view " + this + " being added, but"
10784 + " it already has a parent");
10785 }
10786 }
10787
10788 /**
10789 * This is called when the view is attached to a window. At this point it
10790 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010791 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10792 * however it may be called any time before the first onDraw -- including
10793 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 *
10795 * @see #onDetachedFromWindow()
10796 */
10797 protected void onAttachedToWindow() {
10798 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10799 mParent.requestTransparentRegion(this);
10800 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010801 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10802 initialAwakenScrollBars();
10803 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10804 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010805 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010806 // Order is important here: LayoutDirection MUST be resolved before Padding
10807 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010808 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010809 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010810 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010811 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010812 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010813 if (isFocused()) {
10814 InputMethodManager imm = InputMethodManager.peekInstance();
10815 imm.focusIn(this);
10816 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010817 }
Cibu Johny86666632010-02-22 13:01:02 -080010818
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010819 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010820 * @see #onScreenStateChanged(int)
10821 */
10822 void dispatchScreenStateChanged(int screenState) {
10823 onScreenStateChanged(screenState);
10824 }
10825
10826 /**
10827 * This method is called whenever the state of the screen this view is
10828 * attached to changes. A state change will usually occurs when the screen
10829 * turns on or off (whether it happens automatically or the user does it
10830 * manually.)
10831 *
10832 * @param screenState The new state of the screen. Can be either
10833 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10834 */
10835 public void onScreenStateChanged(int screenState) {
10836 }
10837
10838 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010839 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10840 */
10841 private boolean hasRtlSupport() {
10842 return mContext.getApplicationInfo().hasRtlSupport();
10843 }
10844
10845 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010846 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10847 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010848 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010849 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010850 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010851 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010852 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010853
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010854 if (hasRtlSupport()) {
10855 // Set resolved depending on layout direction
10856 switch (getLayoutDirection()) {
10857 case LAYOUT_DIRECTION_INHERIT:
10858 // If this is root view, no need to look at parent's layout dir.
10859 if (canResolveLayoutDirection()) {
10860 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010861
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010862 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10863 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10864 }
10865 } else {
10866 // Nothing to do, LTR by default
10867 }
10868 break;
10869 case LAYOUT_DIRECTION_RTL:
10870 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10871 break;
10872 case LAYOUT_DIRECTION_LOCALE:
10873 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010874 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10875 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010876 break;
10877 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010878 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010879 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010880 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010881
10882 // Set to resolved
10883 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010884 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010885 // Resolve padding
10886 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010887 }
10888
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010889 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010890 * Called when layout direction has been resolved.
10891 *
10892 * The default implementation does nothing.
10893 */
10894 public void onResolvedLayoutDirectionChanged() {
10895 }
10896
10897 /**
10898 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010899 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010900 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010901 // If the user specified the absolute padding (either with android:padding or
10902 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10903 // use the default padding or the padding from the background drawable
10904 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010905 int resolvedLayoutDirection = getResolvedLayoutDirection();
10906 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010907 case LAYOUT_DIRECTION_RTL:
10908 // Start user padding override Right user padding. Otherwise, if Right user
10909 // padding is not defined, use the default Right padding. If Right user padding
10910 // is defined, just use it.
10911 if (mUserPaddingStart >= 0) {
10912 mUserPaddingRight = mUserPaddingStart;
10913 } else if (mUserPaddingRight < 0) {
10914 mUserPaddingRight = mPaddingRight;
10915 }
10916 if (mUserPaddingEnd >= 0) {
10917 mUserPaddingLeft = mUserPaddingEnd;
10918 } else if (mUserPaddingLeft < 0) {
10919 mUserPaddingLeft = mPaddingLeft;
10920 }
10921 break;
10922 case LAYOUT_DIRECTION_LTR:
10923 default:
10924 // Start user padding override Left user padding. Otherwise, if Left user
10925 // padding is not defined, use the default left padding. If Left user padding
10926 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010927 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010928 mUserPaddingLeft = mUserPaddingStart;
10929 } else if (mUserPaddingLeft < 0) {
10930 mUserPaddingLeft = mPaddingLeft;
10931 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010932 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010933 mUserPaddingRight = mUserPaddingEnd;
10934 } else if (mUserPaddingRight < 0) {
10935 mUserPaddingRight = mPaddingRight;
10936 }
10937 }
10938
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010939 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10940
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010941 if(isPaddingRelative()) {
10942 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10943 } else {
10944 recomputePadding();
10945 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010946 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010947 }
10948
10949 /**
10950 * Resolve padding depending on the layout direction. Subclasses that care about
10951 * padding resolution should override this method. The default implementation does
10952 * nothing.
10953 *
10954 * @param layoutDirection the direction of the layout
10955 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010956 * @see {@link #LAYOUT_DIRECTION_LTR}
10957 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010958 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010959 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010960 }
10961
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010962 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010963 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010964 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010965 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010966 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010967 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010968 switch (getLayoutDirection()) {
10969 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010970 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010971 default:
10972 return true;
10973 }
10974 }
10975
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010976 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010977 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10978 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010979 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010980 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010981 // Reset the current resolved bits
10982 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010983 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010984 // Reset also the text direction
10985 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010986 }
10987
10988 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010989 * Called during reset of resolved layout direction.
10990 *
10991 * Subclasses need to override this method to clear cached information that depends on the
10992 * resolved layout direction, or to inform child views that inherit their layout direction.
10993 *
10994 * The default implementation does nothing.
10995 */
10996 public void onResolvedLayoutDirectionReset() {
10997 }
10998
10999 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011000 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011001 *
11002 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011003 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011004 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011005 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011006 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 }
11008
11009 /**
11010 * This is called when the view is detached from a window. At this point it
11011 * no longer has a surface for drawing.
11012 *
11013 * @see #onAttachedToWindow()
11014 */
11015 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011016 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011017
Romain Guya440b002010-02-24 15:57:54 -080011018 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011019 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011020 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011021 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011023 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011024
Romain Guya998dff2012-03-23 18:58:36 -070011025 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011026
11027 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011028 if (mDisplayList != null) {
11029 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11030 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011031 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011032 } else {
11033 if (mDisplayList != null) {
11034 // Should never happen
11035 mDisplayList.invalidate();
11036 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011037 }
11038
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011039 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011040
11041 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011042 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011043 resetAccessibilityStateChanged();
11044 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011045 }
11046
11047 /**
11048 * @return The number of times this view has been attached to a window
11049 */
11050 protected int getWindowAttachCount() {
11051 return mWindowAttachCount;
11052 }
11053
11054 /**
11055 * Retrieve a unique token identifying the window this view is attached to.
11056 * @return Return the window's token for use in
11057 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11058 */
11059 public IBinder getWindowToken() {
11060 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11061 }
11062
11063 /**
11064 * Retrieve a unique token identifying the top-level "real" window of
11065 * the window that this view is attached to. That is, this is like
11066 * {@link #getWindowToken}, except if the window this view in is a panel
11067 * window (attached to another containing window), then the token of
11068 * the containing window is returned instead.
11069 *
11070 * @return Returns the associated window token, either
11071 * {@link #getWindowToken()} or the containing window's token.
11072 */
11073 public IBinder getApplicationWindowToken() {
11074 AttachInfo ai = mAttachInfo;
11075 if (ai != null) {
11076 IBinder appWindowToken = ai.mPanelParentWindowToken;
11077 if (appWindowToken == null) {
11078 appWindowToken = ai.mWindowToken;
11079 }
11080 return appWindowToken;
11081 }
11082 return null;
11083 }
11084
11085 /**
11086 * Retrieve private session object this view hierarchy is using to
11087 * communicate with the window manager.
11088 * @return the session object to communicate with the window manager
11089 */
11090 /*package*/ IWindowSession getWindowSession() {
11091 return mAttachInfo != null ? mAttachInfo.mSession : null;
11092 }
11093
11094 /**
11095 * @param info the {@link android.view.View.AttachInfo} to associated with
11096 * this view
11097 */
11098 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11099 //System.out.println("Attached! " + this);
11100 mAttachInfo = info;
11101 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011102 // We will need to evaluate the drawable state at least once.
11103 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011104 if (mFloatingTreeObserver != null) {
11105 info.mTreeObserver.merge(mFloatingTreeObserver);
11106 mFloatingTreeObserver = null;
11107 }
11108 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11109 mAttachInfo.mScrollContainers.add(this);
11110 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11111 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011112 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011113 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011114
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011115 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011116 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011117 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011118 if (listeners != null && listeners.size() > 0) {
11119 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11120 // perform the dispatching. The iterator is a safe guard against listeners that
11121 // could mutate the list by calling the various add/remove methods. This prevents
11122 // the array from being modified while we iterate it.
11123 for (OnAttachStateChangeListener listener : listeners) {
11124 listener.onViewAttachedToWindow(this);
11125 }
11126 }
11127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011128 int vis = info.mWindowVisibility;
11129 if (vis != GONE) {
11130 onWindowVisibilityChanged(vis);
11131 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011132 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11133 // If nobody has evaluated the drawable state yet, then do it now.
11134 refreshDrawableState();
11135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011136 }
11137
11138 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 AttachInfo info = mAttachInfo;
11140 if (info != null) {
11141 int vis = info.mWindowVisibility;
11142 if (vis != GONE) {
11143 onWindowVisibilityChanged(GONE);
11144 }
11145 }
11146
11147 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011148
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011149 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011150 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011151 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011152 if (listeners != null && listeners.size() > 0) {
11153 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11154 // perform the dispatching. The iterator is a safe guard against listeners that
11155 // could mutate the list by calling the various add/remove methods. This prevents
11156 // the array from being modified while we iterate it.
11157 for (OnAttachStateChangeListener listener : listeners) {
11158 listener.onViewDetachedFromWindow(this);
11159 }
11160 }
11161
Romain Guy01d5edc2011-01-28 11:28:53 -080011162 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011163 mAttachInfo.mScrollContainers.remove(this);
11164 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11165 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011167 mAttachInfo = null;
11168 }
11169
11170 /**
11171 * Store this view hierarchy's frozen state into the given container.
11172 *
11173 * @param container The SparseArray in which to save the view's state.
11174 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011175 * @see #restoreHierarchyState(android.util.SparseArray)
11176 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11177 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 */
11179 public void saveHierarchyState(SparseArray<Parcelable> container) {
11180 dispatchSaveInstanceState(container);
11181 }
11182
11183 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011184 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11185 * this view and its children. May be overridden to modify how freezing happens to a
11186 * 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 -080011187 *
11188 * @param container The SparseArray in which to save the view's state.
11189 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011190 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11191 * @see #saveHierarchyState(android.util.SparseArray)
11192 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011193 */
11194 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11195 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11196 mPrivateFlags &= ~SAVE_STATE_CALLED;
11197 Parcelable state = onSaveInstanceState();
11198 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11199 throw new IllegalStateException(
11200 "Derived class did not call super.onSaveInstanceState()");
11201 }
11202 if (state != null) {
11203 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11204 // + ": " + state);
11205 container.put(mID, state);
11206 }
11207 }
11208 }
11209
11210 /**
11211 * Hook allowing a view to generate a representation of its internal state
11212 * that can later be used to create a new instance with that same state.
11213 * This state should only contain information that is not persistent or can
11214 * not be reconstructed later. For example, you will never store your
11215 * current position on screen because that will be computed again when a
11216 * new instance of the view is placed in its view hierarchy.
11217 * <p>
11218 * Some examples of things you may store here: the current cursor position
11219 * in a text view (but usually not the text itself since that is stored in a
11220 * content provider or other persistent storage), the currently selected
11221 * item in a list view.
11222 *
11223 * @return Returns a Parcelable object containing the view's current dynamic
11224 * state, or null if there is nothing interesting to save. The
11225 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011226 * @see #onRestoreInstanceState(android.os.Parcelable)
11227 * @see #saveHierarchyState(android.util.SparseArray)
11228 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011229 * @see #setSaveEnabled(boolean)
11230 */
11231 protected Parcelable onSaveInstanceState() {
11232 mPrivateFlags |= SAVE_STATE_CALLED;
11233 return BaseSavedState.EMPTY_STATE;
11234 }
11235
11236 /**
11237 * Restore this view hierarchy's frozen state from the given container.
11238 *
11239 * @param container The SparseArray which holds previously frozen states.
11240 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011241 * @see #saveHierarchyState(android.util.SparseArray)
11242 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11243 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011244 */
11245 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11246 dispatchRestoreInstanceState(container);
11247 }
11248
11249 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011250 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11251 * state for this view and its children. May be overridden to modify how restoring
11252 * happens to a view's children; for example, some views may want to not store state
11253 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011254 *
11255 * @param container The SparseArray which holds previously saved state.
11256 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011257 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11258 * @see #restoreHierarchyState(android.util.SparseArray)
11259 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260 */
11261 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11262 if (mID != NO_ID) {
11263 Parcelable state = container.get(mID);
11264 if (state != null) {
11265 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11266 // + ": " + state);
11267 mPrivateFlags &= ~SAVE_STATE_CALLED;
11268 onRestoreInstanceState(state);
11269 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11270 throw new IllegalStateException(
11271 "Derived class did not call super.onRestoreInstanceState()");
11272 }
11273 }
11274 }
11275 }
11276
11277 /**
11278 * Hook allowing a view to re-apply a representation of its internal state that had previously
11279 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11280 * null state.
11281 *
11282 * @param state The frozen state that had previously been returned by
11283 * {@link #onSaveInstanceState}.
11284 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011285 * @see #onSaveInstanceState()
11286 * @see #restoreHierarchyState(android.util.SparseArray)
11287 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011288 */
11289 protected void onRestoreInstanceState(Parcelable state) {
11290 mPrivateFlags |= SAVE_STATE_CALLED;
11291 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011292 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11293 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011294 + "when two views of different type have the same id in the same hierarchy. "
11295 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011296 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011297 }
11298 }
11299
11300 /**
11301 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11302 *
11303 * @return the drawing start time in milliseconds
11304 */
11305 public long getDrawingTime() {
11306 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11307 }
11308
11309 /**
11310 * <p>Enables or disables the duplication of the parent's state into this view. When
11311 * duplication is enabled, this view gets its drawable state from its parent rather
11312 * than from its own internal properties.</p>
11313 *
11314 * <p>Note: in the current implementation, setting this property to true after the
11315 * view was added to a ViewGroup might have no effect at all. This property should
11316 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11317 *
11318 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11319 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011320 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011321 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11322 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011323 *
11324 * @param enabled True to enable duplication of the parent's drawable state, false
11325 * to disable it.
11326 *
11327 * @see #getDrawableState()
11328 * @see #isDuplicateParentStateEnabled()
11329 */
11330 public void setDuplicateParentStateEnabled(boolean enabled) {
11331 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11332 }
11333
11334 /**
11335 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11336 *
11337 * @return True if this view's drawable state is duplicated from the parent,
11338 * false otherwise
11339 *
11340 * @see #getDrawableState()
11341 * @see #setDuplicateParentStateEnabled(boolean)
11342 */
11343 public boolean isDuplicateParentStateEnabled() {
11344 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11345 }
11346
11347 /**
Romain Guy171c5922011-01-06 10:04:23 -080011348 * <p>Specifies the type of layer backing this view. The layer can be
11349 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11350 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011351 *
Romain Guy171c5922011-01-06 10:04:23 -080011352 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11353 * instance that controls how the layer is composed on screen. The following
11354 * properties of the paint are taken into account when composing the layer:</p>
11355 * <ul>
11356 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11357 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11358 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11359 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011360 *
Romain Guy171c5922011-01-06 10:04:23 -080011361 * <p>If this view has an alpha value set to < 1.0 by calling
11362 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11363 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11364 * equivalent to setting a hardware layer on this view and providing a paint with
11365 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011366 *
Romain Guy171c5922011-01-06 10:04:23 -080011367 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11368 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11369 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011370 *
Romain Guy171c5922011-01-06 10:04:23 -080011371 * @param layerType The ype of layer to use with this view, must be one of
11372 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11373 * {@link #LAYER_TYPE_HARDWARE}
11374 * @param paint The paint used to compose the layer. This argument is optional
11375 * and can be null. It is ignored when the layer type is
11376 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011377 *
11378 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011379 * @see #LAYER_TYPE_NONE
11380 * @see #LAYER_TYPE_SOFTWARE
11381 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011382 * @see #setAlpha(float)
11383 *
Romain Guy171c5922011-01-06 10:04:23 -080011384 * @attr ref android.R.styleable#View_layerType
11385 */
11386 public void setLayerType(int layerType, Paint paint) {
11387 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011388 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011389 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11390 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011391
Romain Guyd6cd5722011-01-17 14:42:41 -080011392 if (layerType == mLayerType) {
11393 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11394 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011395 invalidateParentCaches();
11396 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011397 }
11398 return;
11399 }
Romain Guy171c5922011-01-06 10:04:23 -080011400
11401 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011402 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011403 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011404 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011405 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011406 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011407 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011408 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011409 default:
11410 break;
Romain Guy171c5922011-01-06 10:04:23 -080011411 }
11412
11413 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011414 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11415 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11416 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011417
Romain Guy0fd89bf2011-01-26 15:41:30 -080011418 invalidateParentCaches();
11419 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011420 }
11421
11422 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011423 * Indicates whether this view has a static layer. A view with layer type
11424 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11425 * dynamic.
11426 */
11427 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011428 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011429 }
11430
11431 /**
Romain Guy171c5922011-01-06 10:04:23 -080011432 * Indicates what type of layer is currently associated with this view. By default
11433 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11434 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11435 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011436 *
Romain Guy171c5922011-01-06 10:04:23 -080011437 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11438 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011439 *
11440 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011441 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011442 * @see #LAYER_TYPE_NONE
11443 * @see #LAYER_TYPE_SOFTWARE
11444 * @see #LAYER_TYPE_HARDWARE
11445 */
11446 public int getLayerType() {
11447 return mLayerType;
11448 }
Joe Malin32736f02011-01-19 16:14:20 -080011449
Romain Guy6c319ca2011-01-11 14:29:25 -080011450 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011451 * Forces this view's layer to be created and this view to be rendered
11452 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11453 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011454 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011455 * This method can for instance be used to render a view into its layer before
11456 * starting an animation. If this view is complex, rendering into the layer
11457 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011458 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011459 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011460 *
11461 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011462 */
11463 public void buildLayer() {
11464 if (mLayerType == LAYER_TYPE_NONE) return;
11465
11466 if (mAttachInfo == null) {
11467 throw new IllegalStateException("This view must be attached to a window first");
11468 }
11469
11470 switch (mLayerType) {
11471 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011472 if (mAttachInfo.mHardwareRenderer != null &&
11473 mAttachInfo.mHardwareRenderer.isEnabled() &&
11474 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011475 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011476 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011477 break;
11478 case LAYER_TYPE_SOFTWARE:
11479 buildDrawingCache(true);
11480 break;
11481 }
11482 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011483
Romain Guy9c4b79a2011-11-10 19:23:58 -080011484 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11485 void flushLayer() {
11486 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11487 mHardwareLayer.flush();
11488 }
11489 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011490
11491 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011492 * <p>Returns a hardware layer that can be used to draw this view again
11493 * without executing its draw method.</p>
11494 *
11495 * @return A HardwareLayer ready to render, or null if an error occurred.
11496 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011497 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011498 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11499 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011500 return null;
11501 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011502
Romain Guy9c4b79a2011-11-10 19:23:58 -080011503 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011504
11505 final int width = mRight - mLeft;
11506 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011507
Romain Guy6c319ca2011-01-11 14:29:25 -080011508 if (width == 0 || height == 0) {
11509 return null;
11510 }
11511
11512 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11513 if (mHardwareLayer == null) {
11514 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11515 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011516 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011517 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11518 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011519 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011520 }
11521
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011522 // The layer is not valid if the underlying GPU resources cannot be allocated
11523 if (!mHardwareLayer.isValid()) {
11524 return null;
11525 }
11526
Chet Haasea1cff502012-02-21 13:43:44 -080011527 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011528 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011529 }
11530
11531 return mHardwareLayer;
11532 }
Romain Guy171c5922011-01-06 10:04:23 -080011533
Romain Guy589b0bb2011-10-10 13:57:47 -070011534 /**
11535 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011536 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011537 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011538 *
11539 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011540 * @see #LAYER_TYPE_HARDWARE
11541 */
Romain Guya998dff2012-03-23 18:58:36 -070011542 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011543 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011544 AttachInfo info = mAttachInfo;
11545 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011546 info.mHardwareRenderer.isEnabled() &&
11547 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011548 mHardwareLayer.destroy();
11549 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011550
Romain Guy9c4b79a2011-11-10 19:23:58 -080011551 invalidate(true);
11552 invalidateParentCaches();
11553 }
Romain Guy65b345f2011-07-27 18:51:50 -070011554 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011555 }
Romain Guy65b345f2011-07-27 18:51:50 -070011556 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011557 }
11558
Romain Guy171c5922011-01-06 10:04:23 -080011559 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011560 * Destroys all hardware rendering resources. This method is invoked
11561 * when the system needs to reclaim resources. Upon execution of this
11562 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011563 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011564 * Note: you <strong>must</strong> call
11565 * <code>super.destroyHardwareResources()</code> when overriding
11566 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011567 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011568 * @hide
11569 */
11570 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011571 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011572 }
11573
11574 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011575 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11576 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11577 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11578 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11579 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11580 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011581 *
Romain Guy171c5922011-01-06 10:04:23 -080011582 * <p>Enabling the drawing cache is similar to
11583 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011584 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11585 * drawing cache has no effect on rendering because the system uses a different mechanism
11586 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11587 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11588 * for information on how to enable software and hardware layers.</p>
11589 *
11590 * <p>This API can be used to manually generate
11591 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11592 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011593 *
11594 * @param enabled true to enable the drawing cache, false otherwise
11595 *
11596 * @see #isDrawingCacheEnabled()
11597 * @see #getDrawingCache()
11598 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011599 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011600 */
11601 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011602 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011603 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11604 }
11605
11606 /**
11607 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11608 *
11609 * @return true if the drawing cache is enabled
11610 *
11611 * @see #setDrawingCacheEnabled(boolean)
11612 * @see #getDrawingCache()
11613 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011614 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011615 public boolean isDrawingCacheEnabled() {
11616 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11617 }
11618
11619 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011620 * Debugging utility which recursively outputs the dirty state of a view and its
11621 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011622 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011623 * @hide
11624 */
Romain Guy676b1732011-02-14 14:45:33 -080011625 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011626 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11627 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11628 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11629 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11630 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11631 if (clear) {
11632 mPrivateFlags &= clearMask;
11633 }
11634 if (this instanceof ViewGroup) {
11635 ViewGroup parent = (ViewGroup) this;
11636 final int count = parent.getChildCount();
11637 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011638 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011639 child.outputDirtyFlags(indent + " ", clear, clearMask);
11640 }
11641 }
11642 }
11643
11644 /**
11645 * This method is used by ViewGroup to cause its children to restore or recreate their
11646 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11647 * to recreate its own display list, which would happen if it went through the normal
11648 * draw/dispatchDraw mechanisms.
11649 *
11650 * @hide
11651 */
11652 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011653
11654 /**
11655 * A view that is not attached or hardware accelerated cannot create a display list.
11656 * This method checks these conditions and returns the appropriate result.
11657 *
11658 * @return true if view has the ability to create a display list, false otherwise.
11659 *
11660 * @hide
11661 */
11662 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011663 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011664 }
Joe Malin32736f02011-01-19 16:14:20 -080011665
Chet Haasedaf98e92011-01-10 14:10:36 -080011666 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011667 * @return The HardwareRenderer associated with that view or null if hardware rendering
11668 * is not supported or this this has not been attached to a window.
11669 *
11670 * @hide
11671 */
11672 public HardwareRenderer getHardwareRenderer() {
11673 if (mAttachInfo != null) {
11674 return mAttachInfo.mHardwareRenderer;
11675 }
11676 return null;
11677 }
11678
11679 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011680 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11681 * Otherwise, the same display list will be returned (after having been rendered into
11682 * along the way, depending on the invalidation state of the view).
11683 *
11684 * @param displayList The previous version of this displayList, could be null.
11685 * @param isLayer Whether the requester of the display list is a layer. If so,
11686 * the view will avoid creating a layer inside the resulting display list.
11687 * @return A new or reused DisplayList object.
11688 */
11689 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11690 if (!canHaveDisplayList()) {
11691 return null;
11692 }
11693
11694 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11695 displayList == null || !displayList.isValid() ||
11696 (!isLayer && mRecreateDisplayList))) {
11697 // Don't need to recreate the display list, just need to tell our
11698 // children to restore/recreate theirs
11699 if (displayList != null && displayList.isValid() &&
11700 !isLayer && !mRecreateDisplayList) {
11701 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11702 mPrivateFlags &= ~DIRTY_MASK;
11703 dispatchGetDisplayList();
11704
11705 return displayList;
11706 }
11707
11708 if (!isLayer) {
11709 // If we got here, we're recreating it. Mark it as such to ensure that
11710 // we copy in child display lists into ours in drawChild()
11711 mRecreateDisplayList = true;
11712 }
11713 if (displayList == null) {
11714 final String name = getClass().getSimpleName();
11715 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11716 // If we're creating a new display list, make sure our parent gets invalidated
11717 // since they will need to recreate their display list to account for this
11718 // new child display list.
11719 invalidateParentCaches();
11720 }
11721
11722 boolean caching = false;
11723 final HardwareCanvas canvas = displayList.start();
11724 int restoreCount = 0;
11725 int width = mRight - mLeft;
11726 int height = mBottom - mTop;
11727
11728 try {
11729 canvas.setViewport(width, height);
11730 // The dirty rect should always be null for a display list
11731 canvas.onPreDraw(null);
11732 int layerType = (
11733 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11734 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011735 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11736 if (layerType == LAYER_TYPE_HARDWARE) {
11737 final HardwareLayer layer = getHardwareLayer();
11738 if (layer != null && layer.isValid()) {
11739 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11740 } else {
11741 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11742 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11743 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11744 }
11745 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011746 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011747 buildDrawingCache(true);
11748 Bitmap cache = getDrawingCache(true);
11749 if (cache != null) {
11750 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11751 caching = true;
11752 }
Chet Haasea1cff502012-02-21 13:43:44 -080011753 }
Chet Haasea1cff502012-02-21 13:43:44 -080011754 } else {
11755
11756 computeScroll();
11757
11758 if (!USE_DISPLAY_LIST_PROPERTIES) {
11759 restoreCount = canvas.save();
11760 }
11761 canvas.translate(-mScrollX, -mScrollY);
11762 if (!isLayer) {
11763 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11764 mPrivateFlags &= ~DIRTY_MASK;
11765 }
11766
11767 // Fast path for layouts with no backgrounds
11768 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11769 dispatchDraw(canvas);
11770 } else {
11771 draw(canvas);
11772 }
11773 }
11774 } finally {
11775 if (USE_DISPLAY_LIST_PROPERTIES) {
11776 canvas.restoreToCount(restoreCount);
11777 }
11778 canvas.onPostDraw();
11779
11780 displayList.end();
11781 if (USE_DISPLAY_LIST_PROPERTIES) {
11782 displayList.setCaching(caching);
11783 }
11784 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11785 displayList.setLeftTopRightBottom(0, 0, width, height);
11786 } else {
11787 setDisplayListProperties(displayList);
11788 }
11789 }
11790 } else if (!isLayer) {
11791 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11792 mPrivateFlags &= ~DIRTY_MASK;
11793 }
11794
11795 return displayList;
11796 }
11797
11798 /**
11799 * Get the DisplayList for the HardwareLayer
11800 *
11801 * @param layer The HardwareLayer whose DisplayList we want
11802 * @return A DisplayList fopr the specified HardwareLayer
11803 */
11804 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11805 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11806 layer.setDisplayList(displayList);
11807 return displayList;
11808 }
11809
11810
11811 /**
Romain Guyb051e892010-09-28 19:09:36 -070011812 * <p>Returns a display list that can be used to draw this view again
11813 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011814 *
Romain Guyb051e892010-09-28 19:09:36 -070011815 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011816 *
11817 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011818 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011819 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011820 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011821 return mDisplayList;
11822 }
11823
11824 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011825 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011826 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011827 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011828 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011829 * @see #getDrawingCache(boolean)
11830 */
11831 public Bitmap getDrawingCache() {
11832 return getDrawingCache(false);
11833 }
11834
11835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011836 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11837 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11838 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11839 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11840 * request the drawing cache by calling this method and draw it on screen if the
11841 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011842 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011843 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11844 * this method will create a bitmap of the same size as this view. Because this bitmap
11845 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11846 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11847 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11848 * size than the view. This implies that your application must be able to handle this
11849 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011850 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011851 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11852 * the current density of the screen when the application is in compatibility
11853 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011854 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011855 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011856 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011857 * @see #setDrawingCacheEnabled(boolean)
11858 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011859 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011860 * @see #destroyDrawingCache()
11861 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011862 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011863 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11864 return null;
11865 }
11866 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011867 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011868 }
Romain Guy02890fd2010-08-06 17:58:44 -070011869 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011870 }
11871
11872 /**
11873 * <p>Frees the resources used by the drawing cache. If you call
11874 * {@link #buildDrawingCache()} manually without calling
11875 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11876 * should cleanup the cache with this method afterwards.</p>
11877 *
11878 * @see #setDrawingCacheEnabled(boolean)
11879 * @see #buildDrawingCache()
11880 * @see #getDrawingCache()
11881 */
11882 public void destroyDrawingCache() {
11883 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011884 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011885 mDrawingCache = null;
11886 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011887 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011888 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011889 mUnscaledDrawingCache = null;
11890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011891 }
11892
11893 /**
11894 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011895 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011896 * view will always be drawn on top of a solid color.
11897 *
11898 * @param color The background color to use for the drawing cache's bitmap
11899 *
11900 * @see #setDrawingCacheEnabled(boolean)
11901 * @see #buildDrawingCache()
11902 * @see #getDrawingCache()
11903 */
11904 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011905 if (color != mDrawingCacheBackgroundColor) {
11906 mDrawingCacheBackgroundColor = color;
11907 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011909 }
11910
11911 /**
11912 * @see #setDrawingCacheBackgroundColor(int)
11913 *
11914 * @return The background color to used for the drawing cache's bitmap
11915 */
11916 public int getDrawingCacheBackgroundColor() {
11917 return mDrawingCacheBackgroundColor;
11918 }
11919
11920 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011921 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011922 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011923 * @see #buildDrawingCache(boolean)
11924 */
11925 public void buildDrawingCache() {
11926 buildDrawingCache(false);
11927 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011928
Romain Guyfbd8f692009-06-26 14:51:58 -070011929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011930 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11931 *
11932 * <p>If you call {@link #buildDrawingCache()} manually without calling
11933 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11934 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011935 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011936 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11937 * this method will create a bitmap of the same size as this view. Because this bitmap
11938 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11939 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11940 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11941 * size than the view. This implies that your application must be able to handle this
11942 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011943 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011944 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11945 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011946 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011947 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011948 *
11949 * @see #getDrawingCache()
11950 * @see #destroyDrawingCache()
11951 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011952 public void buildDrawingCache(boolean autoScale) {
11953 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011954 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011955 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011956
11957 if (ViewDebug.TRACE_HIERARCHY) {
11958 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011960
Romain Guy8506ab42009-06-11 17:35:47 -070011961 int width = mRight - mLeft;
11962 int height = mBottom - mTop;
11963
11964 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011965 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011966
Romain Guye1123222009-06-29 14:24:56 -070011967 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011968 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11969 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011971
11972 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011973 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011974 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011975
11976 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011977 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011978 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011979 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11980 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011981 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011982 return;
11983 }
11984
11985 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011986 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987
11988 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 Bitmap.Config quality;
11990 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011991 // Never pick ARGB_4444 because it looks awful
11992 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011993 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11994 case DRAWING_CACHE_QUALITY_AUTO:
11995 quality = Bitmap.Config.ARGB_8888;
11996 break;
11997 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011998 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999 break;
12000 case DRAWING_CACHE_QUALITY_HIGH:
12001 quality = Bitmap.Config.ARGB_8888;
12002 break;
12003 default:
12004 quality = Bitmap.Config.ARGB_8888;
12005 break;
12006 }
12007 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012008 // Optimization for translucent windows
12009 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012010 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012011 }
12012
12013 // Try to cleanup memory
12014 if (bitmap != null) bitmap.recycle();
12015
12016 try {
12017 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012018 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012019 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012020 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012021 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012022 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012023 }
Adam Powell26153a32010-11-08 15:22:27 -080012024 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012025 } catch (OutOfMemoryError e) {
12026 // If there is not enough memory to create the bitmap cache, just
12027 // ignore the issue as bitmap caches are not required to draw the
12028 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012029 if (autoScale) {
12030 mDrawingCache = null;
12031 } else {
12032 mUnscaledDrawingCache = null;
12033 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012034 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012035 return;
12036 }
12037
12038 clear = drawingCacheBackgroundColor != 0;
12039 }
12040
12041 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 if (attachInfo != null) {
12043 canvas = attachInfo.mCanvas;
12044 if (canvas == null) {
12045 canvas = new Canvas();
12046 }
12047 canvas.setBitmap(bitmap);
12048 // Temporarily clobber the cached Canvas in case one of our children
12049 // is also using a drawing cache. Without this, the children would
12050 // steal the canvas by attaching their own bitmap to it and bad, bad
12051 // thing would happen (invisible views, corrupted drawings, etc.)
12052 attachInfo.mCanvas = null;
12053 } else {
12054 // This case should hopefully never or seldom happen
12055 canvas = new Canvas(bitmap);
12056 }
12057
12058 if (clear) {
12059 bitmap.eraseColor(drawingCacheBackgroundColor);
12060 }
12061
12062 computeScroll();
12063 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012064
Romain Guye1123222009-06-29 14:24:56 -070012065 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012066 final float scale = attachInfo.mApplicationScale;
12067 canvas.scale(scale, scale);
12068 }
Joe Malin32736f02011-01-19 16:14:20 -080012069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012070 canvas.translate(-mScrollX, -mScrollY);
12071
Romain Guy5bcdff42009-05-14 21:27:18 -070012072 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012073 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12074 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012075 mPrivateFlags |= DRAWING_CACHE_VALID;
12076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012077
12078 // Fast path for layouts with no backgrounds
12079 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12080 if (ViewDebug.TRACE_HIERARCHY) {
12081 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12082 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012083 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012084 dispatchDraw(canvas);
12085 } else {
12086 draw(canvas);
12087 }
12088
12089 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012090 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012091
12092 if (attachInfo != null) {
12093 // Restore the cached Canvas for our siblings
12094 attachInfo.mCanvas = canvas;
12095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012096 }
12097 }
12098
12099 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012100 * Create a snapshot of the view into a bitmap. We should probably make
12101 * some form of this public, but should think about the API.
12102 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012103 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012104 int width = mRight - mLeft;
12105 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012106
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012107 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012108 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012109 width = (int) ((width * scale) + 0.5f);
12110 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012111
Romain Guy8c11e312009-09-14 15:15:30 -070012112 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012113 if (bitmap == null) {
12114 throw new OutOfMemoryError();
12115 }
12116
Romain Guyc529d8d2011-09-06 15:01:39 -070012117 Resources resources = getResources();
12118 if (resources != null) {
12119 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12120 }
Joe Malin32736f02011-01-19 16:14:20 -080012121
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012122 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012123 if (attachInfo != null) {
12124 canvas = attachInfo.mCanvas;
12125 if (canvas == null) {
12126 canvas = new Canvas();
12127 }
12128 canvas.setBitmap(bitmap);
12129 // Temporarily clobber the cached Canvas in case one of our children
12130 // is also using a drawing cache. Without this, the children would
12131 // steal the canvas by attaching their own bitmap to it and bad, bad
12132 // things would happen (invisible views, corrupted drawings, etc.)
12133 attachInfo.mCanvas = null;
12134 } else {
12135 // This case should hopefully never or seldom happen
12136 canvas = new Canvas(bitmap);
12137 }
12138
Romain Guy5bcdff42009-05-14 21:27:18 -070012139 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012140 bitmap.eraseColor(backgroundColor);
12141 }
12142
12143 computeScroll();
12144 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012145 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012146 canvas.translate(-mScrollX, -mScrollY);
12147
Romain Guy5bcdff42009-05-14 21:27:18 -070012148 // Temporarily remove the dirty mask
12149 int flags = mPrivateFlags;
12150 mPrivateFlags &= ~DIRTY_MASK;
12151
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012152 // Fast path for layouts with no backgrounds
12153 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12154 dispatchDraw(canvas);
12155 } else {
12156 draw(canvas);
12157 }
12158
Romain Guy5bcdff42009-05-14 21:27:18 -070012159 mPrivateFlags = flags;
12160
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012161 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012162 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012163
12164 if (attachInfo != null) {
12165 // Restore the cached Canvas for our siblings
12166 attachInfo.mCanvas = canvas;
12167 }
Romain Guy8506ab42009-06-11 17:35:47 -070012168
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012169 return bitmap;
12170 }
12171
12172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012173 * Indicates whether this View is currently in edit mode. A View is usually
12174 * in edit mode when displayed within a developer tool. For instance, if
12175 * this View is being drawn by a visual user interface builder, this method
12176 * should return true.
12177 *
12178 * Subclasses should check the return value of this method to provide
12179 * different behaviors if their normal behavior might interfere with the
12180 * host environment. For instance: the class spawns a thread in its
12181 * constructor, the drawing code relies on device-specific features, etc.
12182 *
12183 * This method is usually checked in the drawing code of custom widgets.
12184 *
12185 * @return True if this View is in edit mode, false otherwise.
12186 */
12187 public boolean isInEditMode() {
12188 return false;
12189 }
12190
12191 /**
12192 * If the View draws content inside its padding and enables fading edges,
12193 * it needs to support padding offsets. Padding offsets are added to the
12194 * fading edges to extend the length of the fade so that it covers pixels
12195 * drawn inside the padding.
12196 *
12197 * Subclasses of this class should override this method if they need
12198 * to draw content inside the padding.
12199 *
12200 * @return True if padding offset must be applied, false otherwise.
12201 *
12202 * @see #getLeftPaddingOffset()
12203 * @see #getRightPaddingOffset()
12204 * @see #getTopPaddingOffset()
12205 * @see #getBottomPaddingOffset()
12206 *
12207 * @since CURRENT
12208 */
12209 protected boolean isPaddingOffsetRequired() {
12210 return false;
12211 }
12212
12213 /**
12214 * Amount by which to extend the left fading region. Called only when
12215 * {@link #isPaddingOffsetRequired()} returns true.
12216 *
12217 * @return The left padding offset in pixels.
12218 *
12219 * @see #isPaddingOffsetRequired()
12220 *
12221 * @since CURRENT
12222 */
12223 protected int getLeftPaddingOffset() {
12224 return 0;
12225 }
12226
12227 /**
12228 * Amount by which to extend the right fading region. Called only when
12229 * {@link #isPaddingOffsetRequired()} returns true.
12230 *
12231 * @return The right padding offset in pixels.
12232 *
12233 * @see #isPaddingOffsetRequired()
12234 *
12235 * @since CURRENT
12236 */
12237 protected int getRightPaddingOffset() {
12238 return 0;
12239 }
12240
12241 /**
12242 * Amount by which to extend the top fading region. Called only when
12243 * {@link #isPaddingOffsetRequired()} returns true.
12244 *
12245 * @return The top padding offset in pixels.
12246 *
12247 * @see #isPaddingOffsetRequired()
12248 *
12249 * @since CURRENT
12250 */
12251 protected int getTopPaddingOffset() {
12252 return 0;
12253 }
12254
12255 /**
12256 * Amount by which to extend the bottom fading region. Called only when
12257 * {@link #isPaddingOffsetRequired()} returns true.
12258 *
12259 * @return The bottom padding offset in pixels.
12260 *
12261 * @see #isPaddingOffsetRequired()
12262 *
12263 * @since CURRENT
12264 */
12265 protected int getBottomPaddingOffset() {
12266 return 0;
12267 }
12268
12269 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012270 * @hide
12271 * @param offsetRequired
12272 */
12273 protected int getFadeTop(boolean offsetRequired) {
12274 int top = mPaddingTop;
12275 if (offsetRequired) top += getTopPaddingOffset();
12276 return top;
12277 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012278
Romain Guyf2fc4602011-07-19 15:20:03 -070012279 /**
12280 * @hide
12281 * @param offsetRequired
12282 */
12283 protected int getFadeHeight(boolean offsetRequired) {
12284 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012285 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012286 return mBottom - mTop - mPaddingBottom - padding;
12287 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012288
Romain Guyf2fc4602011-07-19 15:20:03 -070012289 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012290 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012291 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012292 *
Romain Guy2bffd262010-09-12 17:40:02 -070012293 * <p>Even if this method returns true, it does not mean that every call
12294 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12295 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012296 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012297 * window is hardware accelerated,
12298 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12299 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012300 *
Romain Guy2bffd262010-09-12 17:40:02 -070012301 * @return True if the view is attached to a window and the window is
12302 * hardware accelerated; false in any other case.
12303 */
12304 public boolean isHardwareAccelerated() {
12305 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12306 }
Joe Malin32736f02011-01-19 16:14:20 -080012307
Romain Guy2bffd262010-09-12 17:40:02 -070012308 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012309 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12310 * case of an active Animation being run on the view.
12311 */
12312 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12313 Animation a, boolean scalingRequired) {
12314 Transformation invalidationTransform;
12315 final int flags = parent.mGroupFlags;
12316 final boolean initialized = a.isInitialized();
12317 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012318 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012319 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12320 onAnimationStart();
12321 }
12322
12323 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12324 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12325 if (parent.mInvalidationTransformation == null) {
12326 parent.mInvalidationTransformation = new Transformation();
12327 }
12328 invalidationTransform = parent.mInvalidationTransformation;
12329 a.getTransformation(drawingTime, invalidationTransform, 1f);
12330 } else {
12331 invalidationTransform = parent.mChildTransformation;
12332 }
12333 if (more) {
12334 if (!a.willChangeBounds()) {
12335 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12336 parent.FLAG_OPTIMIZE_INVALIDATE) {
12337 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12338 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12339 // The child need to draw an animation, potentially offscreen, so
12340 // make sure we do not cancel invalidate requests
12341 parent.mPrivateFlags |= DRAW_ANIMATION;
12342 parent.invalidate(mLeft, mTop, mRight, mBottom);
12343 }
12344 } else {
12345 if (parent.mInvalidateRegion == null) {
12346 parent.mInvalidateRegion = new RectF();
12347 }
12348 final RectF region = parent.mInvalidateRegion;
12349 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12350 invalidationTransform);
12351
12352 // The child need to draw an animation, potentially offscreen, so
12353 // make sure we do not cancel invalidate requests
12354 parent.mPrivateFlags |= DRAW_ANIMATION;
12355
12356 final int left = mLeft + (int) region.left;
12357 final int top = mTop + (int) region.top;
12358 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12359 top + (int) (region.height() + .5f));
12360 }
12361 }
12362 return more;
12363 }
12364
Chet Haasea1cff502012-02-21 13:43:44 -080012365 void setDisplayListProperties() {
12366 setDisplayListProperties(mDisplayList);
12367 }
12368
12369 /**
12370 * This method is called by getDisplayList() when a display list is created or re-rendered.
12371 * It sets or resets the current value of all properties on that display list (resetting is
12372 * necessary when a display list is being re-created, because we need to make sure that
12373 * previously-set transform values
12374 */
12375 void setDisplayListProperties(DisplayList displayList) {
12376 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
12377 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012378 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012379 if (mParent instanceof ViewGroup) {
12380 displayList.setClipChildren(
12381 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12382 }
Chet Haase9420abd2012-03-29 16:28:32 -070012383 float alpha = 1;
12384 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12385 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12386 ViewGroup parentVG = (ViewGroup) mParent;
12387 final boolean hasTransform =
12388 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12389 if (hasTransform) {
12390 Transformation transform = parentVG.mChildTransformation;
12391 final int transformType = parentVG.mChildTransformation.getTransformationType();
12392 if (transformType != Transformation.TYPE_IDENTITY) {
12393 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12394 alpha = transform.getAlpha();
12395 }
12396 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12397 displayList.setStaticMatrix(transform.getMatrix());
12398 }
12399 }
12400 }
Chet Haasea1cff502012-02-21 13:43:44 -080012401 }
12402 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012403 alpha *= mTransformationInfo.mAlpha;
12404 if (alpha < 1) {
12405 final int multipliedAlpha = (int) (255 * alpha);
12406 if (onSetAlpha(multipliedAlpha)) {
12407 alpha = 1;
12408 }
12409 }
12410 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012411 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12412 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12413 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12414 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012415 if (mTransformationInfo.mCamera == null) {
12416 mTransformationInfo.mCamera = new Camera();
12417 mTransformationInfo.matrix3D = new Matrix();
12418 }
12419 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012420 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12421 displayList.setPivotX(getPivotX());
12422 displayList.setPivotY(getPivotY());
12423 }
Chet Haase9420abd2012-03-29 16:28:32 -070012424 } else if (alpha < 1) {
12425 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012426 }
12427 }
12428 }
12429
Chet Haasebcca79a2012-02-14 08:45:14 -080012430 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012431 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12432 * This draw() method is an implementation detail and is not intended to be overridden or
12433 * to be called from anywhere else other than ViewGroup.drawChild().
12434 */
12435 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080012436 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
12437 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012438 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012439 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012440 final int flags = parent.mGroupFlags;
12441
Chet Haasea1cff502012-02-21 13:43:44 -080012442 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012443 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012444 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012445 }
12446
12447 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012448 boolean concatMatrix = false;
12449
12450 boolean scalingRequired = false;
12451 boolean caching;
12452 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12453
12454 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012455 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12456 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012457 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012458 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012459 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12460 } else {
12461 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12462 }
12463
Chet Haasebcca79a2012-02-14 08:45:14 -080012464 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012465 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012466 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012467 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012468 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012469 } else if (!useDisplayListProperties &&
12470 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012471 final boolean hasTransform =
12472 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012473 if (hasTransform) {
12474 final int transformType = parent.mChildTransformation.getTransformationType();
12475 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12476 parent.mChildTransformation : null;
12477 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12478 }
12479 }
12480
12481 concatMatrix |= !childHasIdentityMatrix;
12482
12483 // Sets the flag as early as possible to allow draw() implementations
12484 // to call invalidate() successfully when doing animations
12485 mPrivateFlags |= DRAWN;
12486
Chet Haasebcca79a2012-02-14 08:45:14 -080012487 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012488 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12489 return more;
12490 }
12491
12492 if (hardwareAccelerated) {
12493 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12494 // retain the flag's value temporarily in the mRecreateDisplayList flag
12495 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12496 mPrivateFlags &= ~INVALIDATED;
12497 }
12498
12499 computeScroll();
12500
12501 final int sx = mScrollX;
12502 final int sy = mScrollY;
12503
12504 DisplayList displayList = null;
12505 Bitmap cache = null;
12506 boolean hasDisplayList = false;
12507 if (caching) {
12508 if (!hardwareAccelerated) {
12509 if (layerType != LAYER_TYPE_NONE) {
12510 layerType = LAYER_TYPE_SOFTWARE;
12511 buildDrawingCache(true);
12512 }
12513 cache = getDrawingCache(true);
12514 } else {
12515 switch (layerType) {
12516 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012517 if (useDisplayListProperties) {
12518 hasDisplayList = canHaveDisplayList();
12519 } else {
12520 buildDrawingCache(true);
12521 cache = getDrawingCache(true);
12522 }
Chet Haase64a48c12012-02-13 16:33:29 -080012523 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012524 case LAYER_TYPE_HARDWARE:
12525 if (useDisplayListProperties) {
12526 hasDisplayList = canHaveDisplayList();
12527 }
12528 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012529 case LAYER_TYPE_NONE:
12530 // Delay getting the display list until animation-driven alpha values are
12531 // set up and possibly passed on to the view
12532 hasDisplayList = canHaveDisplayList();
12533 break;
12534 }
12535 }
12536 }
Chet Haasea1cff502012-02-21 13:43:44 -080012537 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012538 if (useDisplayListProperties) {
12539 displayList = getDisplayList();
12540 if (!displayList.isValid()) {
12541 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12542 // to getDisplayList(), the display list will be marked invalid and we should not
12543 // try to use it again.
12544 displayList = null;
12545 hasDisplayList = false;
12546 useDisplayListProperties = false;
12547 }
12548 }
Chet Haase64a48c12012-02-13 16:33:29 -080012549
12550 final boolean hasNoCache = cache == null || hasDisplayList;
12551 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12552 layerType != LAYER_TYPE_HARDWARE;
12553
Chet Haasea1cff502012-02-21 13:43:44 -080012554 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012555 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012556 restoreTo = canvas.save();
12557 }
Chet Haase64a48c12012-02-13 16:33:29 -080012558 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012559 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012560 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012561 if (!useDisplayListProperties) {
12562 canvas.translate(mLeft, mTop);
12563 }
Chet Haase64a48c12012-02-13 16:33:29 -080012564 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012565 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012566 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012567 restoreTo = canvas.save();
12568 }
Chet Haase64a48c12012-02-13 16:33:29 -080012569 // mAttachInfo cannot be null, otherwise scalingRequired == false
12570 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12571 canvas.scale(scale, scale);
12572 }
12573 }
12574
Chet Haasea1cff502012-02-21 13:43:44 -080012575 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012576 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012577 if (transformToApply != null || !childHasIdentityMatrix) {
12578 int transX = 0;
12579 int transY = 0;
12580
12581 if (offsetForScroll) {
12582 transX = -sx;
12583 transY = -sy;
12584 }
12585
12586 if (transformToApply != null) {
12587 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012588 if (useDisplayListProperties) {
12589 displayList.setAnimationMatrix(transformToApply.getMatrix());
12590 } else {
12591 // Undo the scroll translation, apply the transformation matrix,
12592 // then redo the scroll translate to get the correct result.
12593 canvas.translate(-transX, -transY);
12594 canvas.concat(transformToApply.getMatrix());
12595 canvas.translate(transX, transY);
12596 }
Chet Haasea1cff502012-02-21 13:43:44 -080012597 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012598 }
12599
12600 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012601 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012602 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012603 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012604 }
12605 }
12606
Chet Haasea1cff502012-02-21 13:43:44 -080012607 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012608 canvas.translate(-transX, -transY);
12609 canvas.concat(getMatrix());
12610 canvas.translate(transX, transY);
12611 }
12612 }
12613
Chet Haase9420abd2012-03-29 16:28:32 -070012614 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012615 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012616 if (hasNoCache) {
12617 final int multipliedAlpha = (int) (255 * alpha);
12618 if (!onSetAlpha(multipliedAlpha)) {
12619 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012620 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012621 layerType != LAYER_TYPE_NONE) {
12622 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12623 }
Chet Haase9420abd2012-03-29 16:28:32 -070012624 if (useDisplayListProperties) {
12625 displayList.setAlpha(alpha * getAlpha());
12626 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012627 final int scrollX = hasDisplayList ? 0 : sx;
12628 final int scrollY = hasDisplayList ? 0 : sy;
12629 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12630 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012631 }
12632 } else {
12633 // Alpha is handled by the child directly, clobber the layer's alpha
12634 mPrivateFlags |= ALPHA_SET;
12635 }
12636 }
12637 }
12638 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12639 onSetAlpha(255);
12640 mPrivateFlags &= ~ALPHA_SET;
12641 }
12642
Chet Haasea1cff502012-02-21 13:43:44 -080012643 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12644 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012645 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012646 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012647 } else {
12648 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012649 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012650 } else {
12651 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12652 }
12653 }
12654 }
12655
Chet Haase9420abd2012-03-29 16:28:32 -070012656 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012657 displayList = getDisplayList();
12658 if (!displayList.isValid()) {
12659 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12660 // to getDisplayList(), the display list will be marked invalid and we should not
12661 // try to use it again.
12662 displayList = null;
12663 hasDisplayList = false;
12664 }
12665 }
12666
12667 if (hasNoCache) {
12668 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012669 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012670 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012671 if (layer != null && layer.isValid()) {
12672 mLayerPaint.setAlpha((int) (alpha * 255));
12673 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12674 layerRendered = true;
12675 } else {
12676 final int scrollX = hasDisplayList ? 0 : sx;
12677 final int scrollY = hasDisplayList ? 0 : sy;
12678 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012679 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012680 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12681 }
12682 }
12683
12684 if (!layerRendered) {
12685 if (!hasDisplayList) {
12686 // Fast path for layouts with no backgrounds
12687 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12688 if (ViewDebug.TRACE_HIERARCHY) {
12689 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12690 }
12691 mPrivateFlags &= ~DIRTY_MASK;
12692 dispatchDraw(canvas);
12693 } else {
12694 draw(canvas);
12695 }
12696 } else {
12697 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080012698 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080012699 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012700 }
12701 }
12702 } else if (cache != null) {
12703 mPrivateFlags &= ~DIRTY_MASK;
12704 Paint cachePaint;
12705
12706 if (layerType == LAYER_TYPE_NONE) {
12707 cachePaint = parent.mCachePaint;
12708 if (cachePaint == null) {
12709 cachePaint = new Paint();
12710 cachePaint.setDither(false);
12711 parent.mCachePaint = cachePaint;
12712 }
Chet Haase9420abd2012-03-29 16:28:32 -070012713 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012714 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012715 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12716 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012717 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012718 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012719 }
12720 } else {
12721 cachePaint = mLayerPaint;
12722 cachePaint.setAlpha((int) (alpha * 255));
12723 }
12724 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12725 }
12726
Chet Haasea1cff502012-02-21 13:43:44 -080012727 if (restoreTo >= 0) {
12728 canvas.restoreToCount(restoreTo);
12729 }
Chet Haase64a48c12012-02-13 16:33:29 -080012730
12731 if (a != null && !more) {
12732 if (!hardwareAccelerated && !a.getFillAfter()) {
12733 onSetAlpha(255);
12734 }
12735 parent.finishAnimatingView(this, a);
12736 }
12737
12738 if (more && hardwareAccelerated) {
12739 // invalidation is the trigger to recreate display lists, so if we're using
12740 // display lists to render, force an invalidate to allow the animation to
12741 // continue drawing another frame
12742 parent.invalidate(true);
12743 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12744 // alpha animations should cause the child to recreate its display list
12745 invalidate(true);
12746 }
12747 }
12748
12749 mRecreateDisplayList = false;
12750
12751 return more;
12752 }
12753
12754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012755 * Manually render this view (and all of its children) to the given Canvas.
12756 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012757 * called. When implementing a view, implement
12758 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12759 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012760 *
12761 * @param canvas The Canvas to which the View is rendered.
12762 */
12763 public void draw(Canvas canvas) {
12764 if (ViewDebug.TRACE_HIERARCHY) {
12765 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12766 }
12767
Romain Guy5bcdff42009-05-14 21:27:18 -070012768 final int privateFlags = mPrivateFlags;
12769 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12770 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12771 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012773 /*
12774 * Draw traversal performs several drawing steps which must be executed
12775 * in the appropriate order:
12776 *
12777 * 1. Draw the background
12778 * 2. If necessary, save the canvas' layers to prepare for fading
12779 * 3. Draw view's content
12780 * 4. Draw children
12781 * 5. If necessary, draw the fading edges and restore layers
12782 * 6. Draw decorations (scrollbars for instance)
12783 */
12784
12785 // Step 1, draw the background, if needed
12786 int saveCount;
12787
Romain Guy24443ea2009-05-11 11:56:30 -070012788 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012789 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012790 if (background != null) {
12791 final int scrollX = mScrollX;
12792 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012793
Romain Guy24443ea2009-05-11 11:56:30 -070012794 if (mBackgroundSizeChanged) {
12795 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12796 mBackgroundSizeChanged = false;
12797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012798
Romain Guy24443ea2009-05-11 11:56:30 -070012799 if ((scrollX | scrollY) == 0) {
12800 background.draw(canvas);
12801 } else {
12802 canvas.translate(scrollX, scrollY);
12803 background.draw(canvas);
12804 canvas.translate(-scrollX, -scrollY);
12805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012806 }
12807 }
12808
12809 // skip step 2 & 5 if possible (common case)
12810 final int viewFlags = mViewFlags;
12811 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12812 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12813 if (!verticalEdges && !horizontalEdges) {
12814 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012815 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012816
12817 // Step 4, draw the children
12818 dispatchDraw(canvas);
12819
12820 // Step 6, draw decorations (scrollbars)
12821 onDrawScrollBars(canvas);
12822
12823 // we're done...
12824 return;
12825 }
12826
12827 /*
12828 * Here we do the full fledged routine...
12829 * (this is an uncommon case where speed matters less,
12830 * this is why we repeat some of the tests that have been
12831 * done above)
12832 */
12833
12834 boolean drawTop = false;
12835 boolean drawBottom = false;
12836 boolean drawLeft = false;
12837 boolean drawRight = false;
12838
12839 float topFadeStrength = 0.0f;
12840 float bottomFadeStrength = 0.0f;
12841 float leftFadeStrength = 0.0f;
12842 float rightFadeStrength = 0.0f;
12843
12844 // Step 2, save the canvas' layers
12845 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012846
12847 final boolean offsetRequired = isPaddingOffsetRequired();
12848 if (offsetRequired) {
12849 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012850 }
12851
12852 int left = mScrollX + paddingLeft;
12853 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012854 int top = mScrollY + getFadeTop(offsetRequired);
12855 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012856
12857 if (offsetRequired) {
12858 right += getRightPaddingOffset();
12859 bottom += getBottomPaddingOffset();
12860 }
12861
12862 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012863 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012864 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012865
12866 // clip the fade length if top and bottom fades overlap
12867 // overlapping fades produce odd-looking artifacts
12868 if (verticalEdges && (top + length > bottom - length)) {
12869 length = (bottom - top) / 2;
12870 }
12871
12872 // also clip horizontal fades if necessary
12873 if (horizontalEdges && (left + length > right - length)) {
12874 length = (right - left) / 2;
12875 }
12876
12877 if (verticalEdges) {
12878 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012879 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012880 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012881 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012882 }
12883
12884 if (horizontalEdges) {
12885 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012886 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012887 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012888 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012889 }
12890
12891 saveCount = canvas.getSaveCount();
12892
12893 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012894 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012895 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12896
12897 if (drawTop) {
12898 canvas.saveLayer(left, top, right, top + length, null, flags);
12899 }
12900
12901 if (drawBottom) {
12902 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12903 }
12904
12905 if (drawLeft) {
12906 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12907 }
12908
12909 if (drawRight) {
12910 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12911 }
12912 } else {
12913 scrollabilityCache.setFadeColor(solidColor);
12914 }
12915
12916 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012917 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012918
12919 // Step 4, draw the children
12920 dispatchDraw(canvas);
12921
12922 // Step 5, draw the fade effect and restore layers
12923 final Paint p = scrollabilityCache.paint;
12924 final Matrix matrix = scrollabilityCache.matrix;
12925 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012926
12927 if (drawTop) {
12928 matrix.setScale(1, fadeHeight * topFadeStrength);
12929 matrix.postTranslate(left, top);
12930 fade.setLocalMatrix(matrix);
12931 canvas.drawRect(left, top, right, top + length, p);
12932 }
12933
12934 if (drawBottom) {
12935 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12936 matrix.postRotate(180);
12937 matrix.postTranslate(left, bottom);
12938 fade.setLocalMatrix(matrix);
12939 canvas.drawRect(left, bottom - length, right, bottom, p);
12940 }
12941
12942 if (drawLeft) {
12943 matrix.setScale(1, fadeHeight * leftFadeStrength);
12944 matrix.postRotate(-90);
12945 matrix.postTranslate(left, top);
12946 fade.setLocalMatrix(matrix);
12947 canvas.drawRect(left, top, left + length, bottom, p);
12948 }
12949
12950 if (drawRight) {
12951 matrix.setScale(1, fadeHeight * rightFadeStrength);
12952 matrix.postRotate(90);
12953 matrix.postTranslate(right, top);
12954 fade.setLocalMatrix(matrix);
12955 canvas.drawRect(right - length, top, right, bottom, p);
12956 }
12957
12958 canvas.restoreToCount(saveCount);
12959
12960 // Step 6, draw decorations (scrollbars)
12961 onDrawScrollBars(canvas);
12962 }
12963
12964 /**
12965 * Override this if your view is known to always be drawn on top of a solid color background,
12966 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12967 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12968 * should be set to 0xFF.
12969 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012970 * @see #setVerticalFadingEdgeEnabled(boolean)
12971 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012972 *
12973 * @return The known solid color background for this view, or 0 if the color may vary
12974 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012975 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012976 public int getSolidColor() {
12977 return 0;
12978 }
12979
12980 /**
12981 * Build a human readable string representation of the specified view flags.
12982 *
12983 * @param flags the view flags to convert to a string
12984 * @return a String representing the supplied flags
12985 */
12986 private static String printFlags(int flags) {
12987 String output = "";
12988 int numFlags = 0;
12989 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12990 output += "TAKES_FOCUS";
12991 numFlags++;
12992 }
12993
12994 switch (flags & VISIBILITY_MASK) {
12995 case INVISIBLE:
12996 if (numFlags > 0) {
12997 output += " ";
12998 }
12999 output += "INVISIBLE";
13000 // USELESS HERE numFlags++;
13001 break;
13002 case GONE:
13003 if (numFlags > 0) {
13004 output += " ";
13005 }
13006 output += "GONE";
13007 // USELESS HERE numFlags++;
13008 break;
13009 default:
13010 break;
13011 }
13012 return output;
13013 }
13014
13015 /**
13016 * Build a human readable string representation of the specified private
13017 * view flags.
13018 *
13019 * @param privateFlags the private view flags to convert to a string
13020 * @return a String representing the supplied flags
13021 */
13022 private static String printPrivateFlags(int privateFlags) {
13023 String output = "";
13024 int numFlags = 0;
13025
13026 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13027 output += "WANTS_FOCUS";
13028 numFlags++;
13029 }
13030
13031 if ((privateFlags & FOCUSED) == FOCUSED) {
13032 if (numFlags > 0) {
13033 output += " ";
13034 }
13035 output += "FOCUSED";
13036 numFlags++;
13037 }
13038
13039 if ((privateFlags & SELECTED) == SELECTED) {
13040 if (numFlags > 0) {
13041 output += " ";
13042 }
13043 output += "SELECTED";
13044 numFlags++;
13045 }
13046
13047 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13048 if (numFlags > 0) {
13049 output += " ";
13050 }
13051 output += "IS_ROOT_NAMESPACE";
13052 numFlags++;
13053 }
13054
13055 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13056 if (numFlags > 0) {
13057 output += " ";
13058 }
13059 output += "HAS_BOUNDS";
13060 numFlags++;
13061 }
13062
13063 if ((privateFlags & DRAWN) == DRAWN) {
13064 if (numFlags > 0) {
13065 output += " ";
13066 }
13067 output += "DRAWN";
13068 // USELESS HERE numFlags++;
13069 }
13070 return output;
13071 }
13072
13073 /**
13074 * <p>Indicates whether or not this view's layout will be requested during
13075 * the next hierarchy layout pass.</p>
13076 *
13077 * @return true if the layout will be forced during next layout pass
13078 */
13079 public boolean isLayoutRequested() {
13080 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13081 }
13082
13083 /**
13084 * Assign a size and position to a view and all of its
13085 * descendants
13086 *
13087 * <p>This is the second phase of the layout mechanism.
13088 * (The first is measuring). In this phase, each parent calls
13089 * layout on all of its children to position them.
13090 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013091 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013092 *
Chet Haase9c087442011-01-12 16:20:16 -080013093 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013094 * Derived classes with children should override
13095 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013096 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013097 *
13098 * @param l Left position, relative to parent
13099 * @param t Top position, relative to parent
13100 * @param r Right position, relative to parent
13101 * @param b Bottom position, relative to parent
13102 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013103 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013104 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013105 int oldL = mLeft;
13106 int oldT = mTop;
13107 int oldB = mBottom;
13108 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013109 boolean changed = setFrame(l, t, r, b);
13110 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13111 if (ViewDebug.TRACE_HIERARCHY) {
13112 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13113 }
13114
13115 onLayout(changed, l, t, r, b);
13116 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013117
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013118 ListenerInfo li = mListenerInfo;
13119 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013120 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013121 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013122 int numListeners = listenersCopy.size();
13123 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013124 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013125 }
13126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013127 }
13128 mPrivateFlags &= ~FORCE_LAYOUT;
13129 }
13130
13131 /**
13132 * Called from layout when this view should
13133 * assign a size and position to each of its children.
13134 *
13135 * Derived classes with children should override
13136 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013137 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013138 * @param changed This is a new size or position for this view
13139 * @param left Left position, relative to parent
13140 * @param top Top position, relative to parent
13141 * @param right Right position, relative to parent
13142 * @param bottom Bottom position, relative to parent
13143 */
13144 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13145 }
13146
13147 /**
13148 * Assign a size and position to this view.
13149 *
13150 * This is called from layout.
13151 *
13152 * @param left Left position, relative to parent
13153 * @param top Top position, relative to parent
13154 * @param right Right position, relative to parent
13155 * @param bottom Bottom position, relative to parent
13156 * @return true if the new size and position are different than the
13157 * previous ones
13158 * {@hide}
13159 */
13160 protected boolean setFrame(int left, int top, int right, int bottom) {
13161 boolean changed = false;
13162
13163 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013164 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013165 + right + "," + bottom + ")");
13166 }
13167
13168 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13169 changed = true;
13170
13171 // Remember our drawn bit
13172 int drawn = mPrivateFlags & DRAWN;
13173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013174 int oldWidth = mRight - mLeft;
13175 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013176 int newWidth = right - left;
13177 int newHeight = bottom - top;
13178 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13179
13180 // Invalidate our old position
13181 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013182
13183 mLeft = left;
13184 mTop = top;
13185 mRight = right;
13186 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080013187 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
13188 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013190
13191 mPrivateFlags |= HAS_BOUNDS;
13192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013193
Chet Haase75755e22011-07-18 17:48:25 -070013194 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013195 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13196 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013197 if (mTransformationInfo != null) {
13198 mTransformationInfo.mMatrixDirty = true;
13199 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013201 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13202 }
13203
13204 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13205 // If we are visible, force the DRAWN bit to on so that
13206 // this invalidate will go through (at least to our parent).
13207 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013208 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013209 // the DRAWN bit.
13210 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013211 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013212 // parent display list may need to be recreated based on a change in the bounds
13213 // of any child
13214 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013215 }
13216
13217 // Reset drawn bit to original value (invalidate turns it off)
13218 mPrivateFlags |= drawn;
13219
13220 mBackgroundSizeChanged = true;
13221 }
13222 return changed;
13223 }
13224
13225 /**
13226 * Finalize inflating a view from XML. This is called as the last phase
13227 * of inflation, after all child views have been added.
13228 *
13229 * <p>Even if the subclass overrides onFinishInflate, they should always be
13230 * sure to call the super method, so that we get called.
13231 */
13232 protected void onFinishInflate() {
13233 }
13234
13235 /**
13236 * Returns the resources associated with this view.
13237 *
13238 * @return Resources object.
13239 */
13240 public Resources getResources() {
13241 return mResources;
13242 }
13243
13244 /**
13245 * Invalidates the specified Drawable.
13246 *
13247 * @param drawable the drawable to invalidate
13248 */
13249 public void invalidateDrawable(Drawable drawable) {
13250 if (verifyDrawable(drawable)) {
13251 final Rect dirty = drawable.getBounds();
13252 final int scrollX = mScrollX;
13253 final int scrollY = mScrollY;
13254
13255 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13256 dirty.right + scrollX, dirty.bottom + scrollY);
13257 }
13258 }
13259
13260 /**
13261 * Schedules an action on a drawable to occur at a specified time.
13262 *
13263 * @param who the recipient of the action
13264 * @param what the action to run on the drawable
13265 * @param when the time at which the action must occur. Uses the
13266 * {@link SystemClock#uptimeMillis} timebase.
13267 */
13268 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013269 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013270 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013271 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013272 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13273 Choreographer.CALLBACK_ANIMATION, what, who,
13274 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013275 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013276 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013278 }
13279 }
13280
13281 /**
13282 * Cancels a scheduled action on a drawable.
13283 *
13284 * @param who the recipient of the action
13285 * @param what the action to cancel
13286 */
13287 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013288 if (verifyDrawable(who) && what != null) {
13289 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013290 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13291 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013292 } else {
13293 ViewRootImpl.getRunQueue().removeCallbacks(what);
13294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013295 }
13296 }
13297
13298 /**
13299 * Unschedule any events associated with the given Drawable. This can be
13300 * used when selecting a new Drawable into a view, so that the previous
13301 * one is completely unscheduled.
13302 *
13303 * @param who The Drawable to unschedule.
13304 *
13305 * @see #drawableStateChanged
13306 */
13307 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013308 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013309 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13310 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013311 }
13312 }
13313
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013314 /**
13315 * Return the layout direction of a given Drawable.
13316 *
13317 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013318 */
13319 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013320 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013321 }
13322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013323 /**
13324 * If your view subclass is displaying its own Drawable objects, it should
13325 * override this function and return true for any Drawable it is
13326 * displaying. This allows animations for those drawables to be
13327 * scheduled.
13328 *
13329 * <p>Be sure to call through to the super class when overriding this
13330 * function.
13331 *
13332 * @param who The Drawable to verify. Return true if it is one you are
13333 * displaying, else return the result of calling through to the
13334 * super class.
13335 *
13336 * @return boolean If true than the Drawable is being displayed in the
13337 * view; else false and it is not allowed to animate.
13338 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013339 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13340 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013341 */
13342 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013343 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013344 }
13345
13346 /**
13347 * This function is called whenever the state of the view changes in such
13348 * a way that it impacts the state of drawables being shown.
13349 *
13350 * <p>Be sure to call through to the superclass when overriding this
13351 * function.
13352 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013353 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013354 */
13355 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013356 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013357 if (d != null && d.isStateful()) {
13358 d.setState(getDrawableState());
13359 }
13360 }
13361
13362 /**
13363 * Call this to force a view to update its drawable state. This will cause
13364 * drawableStateChanged to be called on this view. Views that are interested
13365 * in the new state should call getDrawableState.
13366 *
13367 * @see #drawableStateChanged
13368 * @see #getDrawableState
13369 */
13370 public void refreshDrawableState() {
13371 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13372 drawableStateChanged();
13373
13374 ViewParent parent = mParent;
13375 if (parent != null) {
13376 parent.childDrawableStateChanged(this);
13377 }
13378 }
13379
13380 /**
13381 * Return an array of resource IDs of the drawable states representing the
13382 * current state of the view.
13383 *
13384 * @return The current drawable state
13385 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013386 * @see Drawable#setState(int[])
13387 * @see #drawableStateChanged()
13388 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013389 */
13390 public final int[] getDrawableState() {
13391 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13392 return mDrawableState;
13393 } else {
13394 mDrawableState = onCreateDrawableState(0);
13395 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13396 return mDrawableState;
13397 }
13398 }
13399
13400 /**
13401 * Generate the new {@link android.graphics.drawable.Drawable} state for
13402 * this view. This is called by the view
13403 * system when the cached Drawable state is determined to be invalid. To
13404 * retrieve the current state, you should use {@link #getDrawableState}.
13405 *
13406 * @param extraSpace if non-zero, this is the number of extra entries you
13407 * would like in the returned array in which you can place your own
13408 * states.
13409 *
13410 * @return Returns an array holding the current {@link Drawable} state of
13411 * the view.
13412 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013413 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013414 */
13415 protected int[] onCreateDrawableState(int extraSpace) {
13416 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13417 mParent instanceof View) {
13418 return ((View) mParent).onCreateDrawableState(extraSpace);
13419 }
13420
13421 int[] drawableState;
13422
13423 int privateFlags = mPrivateFlags;
13424
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013425 int viewStateIndex = 0;
13426 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13427 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13428 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013429 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013430 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13431 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013432 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13433 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013434 // This is set if HW acceleration is requested, even if the current
13435 // process doesn't allow it. This is just to allow app preview
13436 // windows to better match their app.
13437 viewStateIndex |= VIEW_STATE_ACCELERATED;
13438 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013439 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013440
Christopher Tate3d4bf172011-03-28 16:16:46 -070013441 final int privateFlags2 = mPrivateFlags2;
13442 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13443 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013445 drawableState = VIEW_STATE_SETS[viewStateIndex];
13446
13447 //noinspection ConstantIfStatement
13448 if (false) {
13449 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13450 Log.i("View", toString()
13451 + " pressed=" + ((privateFlags & PRESSED) != 0)
13452 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13453 + " fo=" + hasFocus()
13454 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013455 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013456 + ": " + Arrays.toString(drawableState));
13457 }
13458
13459 if (extraSpace == 0) {
13460 return drawableState;
13461 }
13462
13463 final int[] fullState;
13464 if (drawableState != null) {
13465 fullState = new int[drawableState.length + extraSpace];
13466 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13467 } else {
13468 fullState = new int[extraSpace];
13469 }
13470
13471 return fullState;
13472 }
13473
13474 /**
13475 * Merge your own state values in <var>additionalState</var> into the base
13476 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013477 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013478 *
13479 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013480 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013481 * own additional state values.
13482 *
13483 * @param additionalState The additional state values you would like
13484 * added to <var>baseState</var>; this array is not modified.
13485 *
13486 * @return As a convenience, the <var>baseState</var> array you originally
13487 * passed into the function is returned.
13488 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013489 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013490 */
13491 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13492 final int N = baseState.length;
13493 int i = N - 1;
13494 while (i >= 0 && baseState[i] == 0) {
13495 i--;
13496 }
13497 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13498 return baseState;
13499 }
13500
13501 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013502 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13503 * on all Drawable objects associated with this view.
13504 */
13505 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013506 if (mBackground != null) {
13507 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013508 }
13509 }
13510
13511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013512 * Sets the background color for this view.
13513 * @param color the color of the background
13514 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013515 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013516 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013517 if (mBackground instanceof ColorDrawable) {
13518 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013519 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013520 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013522 }
13523
13524 /**
13525 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013526 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013527 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013528 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013529 * @attr ref android.R.styleable#View_background
13530 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013531 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013532 public void setBackgroundResource(int resid) {
13533 if (resid != 0 && resid == mBackgroundResource) {
13534 return;
13535 }
13536
13537 Drawable d= null;
13538 if (resid != 0) {
13539 d = mResources.getDrawable(resid);
13540 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013541 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013542
13543 mBackgroundResource = resid;
13544 }
13545
13546 /**
13547 * Set the background to a given Drawable, or remove the background. If the
13548 * background has padding, this View's padding is set to the background's
13549 * padding. However, when a background is removed, this View's padding isn't
13550 * touched. If setting the padding is desired, please use
13551 * {@link #setPadding(int, int, int, int)}.
13552 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013553 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013554 * background
13555 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013556 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013557 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013558 setBackgroundDrawable(background);
13559 }
13560
13561 /**
13562 * @deprecated use {@link #setBackground(Drawable)} instead
13563 */
13564 @Deprecated
13565 public void setBackgroundDrawable(Drawable background) {
13566 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013567 return;
13568 }
13569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013570 boolean requestLayout = false;
13571
13572 mBackgroundResource = 0;
13573
13574 /*
13575 * Regardless of whether we're setting a new background or not, we want
13576 * to clear the previous drawable.
13577 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013578 if (mBackground != null) {
13579 mBackground.setCallback(null);
13580 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013581 }
13582
Philip Milne6c8ea062012-04-03 17:38:43 -070013583 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013584 Rect padding = sThreadLocal.get();
13585 if (padding == null) {
13586 padding = new Rect();
13587 sThreadLocal.set(padding);
13588 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013589 if (background.getPadding(padding)) {
13590 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013591 case LAYOUT_DIRECTION_RTL:
13592 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13593 break;
13594 case LAYOUT_DIRECTION_LTR:
13595 default:
13596 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013598 }
13599
13600 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13601 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013602 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13603 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013604 requestLayout = true;
13605 }
13606
Philip Milne6c8ea062012-04-03 17:38:43 -070013607 background.setCallback(this);
13608 if (background.isStateful()) {
13609 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013610 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013611 background.setVisible(getVisibility() == VISIBLE, false);
13612 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013613
13614 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13615 mPrivateFlags &= ~SKIP_DRAW;
13616 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13617 requestLayout = true;
13618 }
13619 } else {
13620 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013621 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013622
13623 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13624 /*
13625 * This view ONLY drew the background before and we're removing
13626 * the background, so now it won't draw anything
13627 * (hence we SKIP_DRAW)
13628 */
13629 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13630 mPrivateFlags |= SKIP_DRAW;
13631 }
13632
13633 /*
13634 * When the background is set, we try to apply its padding to this
13635 * View. When the background is removed, we don't touch this View's
13636 * padding. This is noted in the Javadocs. Hence, we don't need to
13637 * requestLayout(), the invalidate() below is sufficient.
13638 */
13639
13640 // The old background's minimum size could have affected this
13641 // View's layout, so let's requestLayout
13642 requestLayout = true;
13643 }
13644
Romain Guy8f1344f52009-05-15 16:03:59 -070013645 computeOpaqueFlags();
13646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013647 if (requestLayout) {
13648 requestLayout();
13649 }
13650
13651 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013652 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013653 }
13654
13655 /**
13656 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013657 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013658 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013659 *
13660 * @see #setBackground(Drawable)
13661 *
13662 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013663 */
13664 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013665 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013666 }
13667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013668 /**
13669 * Sets the padding. The view may add on the space required to display
13670 * the scrollbars, depending on the style and visibility of the scrollbars.
13671 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13672 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13673 * from the values set in this call.
13674 *
13675 * @attr ref android.R.styleable#View_padding
13676 * @attr ref android.R.styleable#View_paddingBottom
13677 * @attr ref android.R.styleable#View_paddingLeft
13678 * @attr ref android.R.styleable#View_paddingRight
13679 * @attr ref android.R.styleable#View_paddingTop
13680 * @param left the left padding in pixels
13681 * @param top the top padding in pixels
13682 * @param right the right padding in pixels
13683 * @param bottom the bottom padding in pixels
13684 */
13685 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013686 mUserPaddingStart = -1;
13687 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013688 mUserPaddingRelative = false;
13689
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013690 internalSetPadding(left, top, right, bottom);
13691 }
13692
13693 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013694 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013695 mUserPaddingRight = right;
13696 mUserPaddingBottom = bottom;
13697
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013698 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013699 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013700
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013701 // Common case is there are no scroll bars.
13702 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013703 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013704 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013705 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013706 switch (mVerticalScrollbarPosition) {
13707 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013708 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13709 left += offset;
13710 } else {
13711 right += offset;
13712 }
13713 break;
Adam Powell20232d02010-12-08 21:08:53 -080013714 case SCROLLBAR_POSITION_RIGHT:
13715 right += offset;
13716 break;
13717 case SCROLLBAR_POSITION_LEFT:
13718 left += offset;
13719 break;
13720 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013721 }
Adam Powell20232d02010-12-08 21:08:53 -080013722 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013723 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13724 ? 0 : getHorizontalScrollbarHeight();
13725 }
13726 }
Romain Guy8506ab42009-06-11 17:35:47 -070013727
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013728 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013729 changed = true;
13730 mPaddingLeft = left;
13731 }
13732 if (mPaddingTop != top) {
13733 changed = true;
13734 mPaddingTop = top;
13735 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013736 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013737 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013738 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013739 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013740 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013741 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013742 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013743 }
13744
13745 if (changed) {
13746 requestLayout();
13747 }
13748 }
13749
13750 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013751 * Sets the relative padding. The view may add on the space required to display
13752 * the scrollbars, depending on the style and visibility of the scrollbars.
13753 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13754 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13755 * from the values set in this call.
13756 *
13757 * @attr ref android.R.styleable#View_padding
13758 * @attr ref android.R.styleable#View_paddingBottom
13759 * @attr ref android.R.styleable#View_paddingStart
13760 * @attr ref android.R.styleable#View_paddingEnd
13761 * @attr ref android.R.styleable#View_paddingTop
13762 * @param start the start padding in pixels
13763 * @param top the top padding in pixels
13764 * @param end the end padding in pixels
13765 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013766 */
13767 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013768 mUserPaddingStart = start;
13769 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013770 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013771
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013772 switch(getResolvedLayoutDirection()) {
13773 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013774 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013775 break;
13776 case LAYOUT_DIRECTION_LTR:
13777 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013778 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013779 }
13780 }
13781
13782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013783 * Returns the top padding of this view.
13784 *
13785 * @return the top padding in pixels
13786 */
13787 public int getPaddingTop() {
13788 return mPaddingTop;
13789 }
13790
13791 /**
13792 * Returns the bottom padding of this view. If there are inset and enabled
13793 * scrollbars, this value may include the space required to display the
13794 * scrollbars as well.
13795 *
13796 * @return the bottom padding in pixels
13797 */
13798 public int getPaddingBottom() {
13799 return mPaddingBottom;
13800 }
13801
13802 /**
13803 * Returns the left padding of this view. If there are inset and enabled
13804 * scrollbars, this value may include the space required to display the
13805 * scrollbars as well.
13806 *
13807 * @return the left padding in pixels
13808 */
13809 public int getPaddingLeft() {
13810 return mPaddingLeft;
13811 }
13812
13813 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013814 * Returns the start padding of this view depending on its resolved layout direction.
13815 * If there are inset and enabled scrollbars, this value may include the space
13816 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013817 *
13818 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013819 */
13820 public int getPaddingStart() {
13821 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13822 mPaddingRight : mPaddingLeft;
13823 }
13824
13825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013826 * Returns the right padding of this view. If there are inset and enabled
13827 * scrollbars, this value may include the space required to display the
13828 * scrollbars as well.
13829 *
13830 * @return the right padding in pixels
13831 */
13832 public int getPaddingRight() {
13833 return mPaddingRight;
13834 }
13835
13836 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013837 * Returns the end padding of this view depending on its resolved layout direction.
13838 * If there are inset and enabled scrollbars, this value may include the space
13839 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013840 *
13841 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013842 */
13843 public int getPaddingEnd() {
13844 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13845 mPaddingLeft : mPaddingRight;
13846 }
13847
13848 /**
13849 * Return if the padding as been set thru relative values
13850 * {@link #setPaddingRelative(int, int, int, int)} or thru
13851 * @attr ref android.R.styleable#View_paddingStart or
13852 * @attr ref android.R.styleable#View_paddingEnd
13853 *
13854 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013855 */
13856 public boolean isPaddingRelative() {
13857 return mUserPaddingRelative;
13858 }
13859
13860 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013861 * @hide
13862 */
13863 public Insets getLayoutInsets() {
13864 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070013865 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070013866 }
13867 return mLayoutInsets;
13868 }
13869
13870 /**
13871 * @hide
13872 */
13873 public void setLayoutInsets(Insets layoutInsets) {
13874 mLayoutInsets = layoutInsets;
13875 }
13876
13877 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013878 * Changes the selection state of this view. A view can be selected or not.
13879 * Note that selection is not the same as focus. Views are typically
13880 * selected in the context of an AdapterView like ListView or GridView;
13881 * the selected view is the view that is highlighted.
13882 *
13883 * @param selected true if the view must be selected, false otherwise
13884 */
13885 public void setSelected(boolean selected) {
13886 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13887 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013888 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013889 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013890 refreshDrawableState();
13891 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070013892 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
13893 notifyAccessibilityStateChanged();
13894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895 }
13896 }
13897
13898 /**
13899 * Dispatch setSelected to all of this View's children.
13900 *
13901 * @see #setSelected(boolean)
13902 *
13903 * @param selected The new selected state
13904 */
13905 protected void dispatchSetSelected(boolean selected) {
13906 }
13907
13908 /**
13909 * Indicates the selection state of this view.
13910 *
13911 * @return true if the view is selected, false otherwise
13912 */
13913 @ViewDebug.ExportedProperty
13914 public boolean isSelected() {
13915 return (mPrivateFlags & SELECTED) != 0;
13916 }
13917
13918 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013919 * Changes the activated state of this view. A view can be activated or not.
13920 * Note that activation is not the same as selection. Selection is
13921 * a transient property, representing the view (hierarchy) the user is
13922 * currently interacting with. Activation is a longer-term state that the
13923 * user can move views in and out of. For example, in a list view with
13924 * single or multiple selection enabled, the views in the current selection
13925 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13926 * here.) The activated state is propagated down to children of the view it
13927 * is set on.
13928 *
13929 * @param activated true if the view must be activated, false otherwise
13930 */
13931 public void setActivated(boolean activated) {
13932 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13933 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013934 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013935 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013936 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013937 }
13938 }
13939
13940 /**
13941 * Dispatch setActivated to all of this View's children.
13942 *
13943 * @see #setActivated(boolean)
13944 *
13945 * @param activated The new activated state
13946 */
13947 protected void dispatchSetActivated(boolean activated) {
13948 }
13949
13950 /**
13951 * Indicates the activation state of this view.
13952 *
13953 * @return true if the view is activated, false otherwise
13954 */
13955 @ViewDebug.ExportedProperty
13956 public boolean isActivated() {
13957 return (mPrivateFlags & ACTIVATED) != 0;
13958 }
13959
13960 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013961 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13962 * observer can be used to get notifications when global events, like
13963 * layout, happen.
13964 *
13965 * The returned ViewTreeObserver observer is not guaranteed to remain
13966 * valid for the lifetime of this View. If the caller of this method keeps
13967 * a long-lived reference to ViewTreeObserver, it should always check for
13968 * the return value of {@link ViewTreeObserver#isAlive()}.
13969 *
13970 * @return The ViewTreeObserver for this view's hierarchy.
13971 */
13972 public ViewTreeObserver getViewTreeObserver() {
13973 if (mAttachInfo != null) {
13974 return mAttachInfo.mTreeObserver;
13975 }
13976 if (mFloatingTreeObserver == null) {
13977 mFloatingTreeObserver = new ViewTreeObserver();
13978 }
13979 return mFloatingTreeObserver;
13980 }
13981
13982 /**
13983 * <p>Finds the topmost view in the current view hierarchy.</p>
13984 *
13985 * @return the topmost view containing this view
13986 */
13987 public View getRootView() {
13988 if (mAttachInfo != null) {
13989 final View v = mAttachInfo.mRootView;
13990 if (v != null) {
13991 return v;
13992 }
13993 }
Romain Guy8506ab42009-06-11 17:35:47 -070013994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013995 View parent = this;
13996
13997 while (parent.mParent != null && parent.mParent instanceof View) {
13998 parent = (View) parent.mParent;
13999 }
14000
14001 return parent;
14002 }
14003
14004 /**
14005 * <p>Computes the coordinates of this view on the screen. The argument
14006 * must be an array of two integers. After the method returns, the array
14007 * contains the x and y location in that order.</p>
14008 *
14009 * @param location an array of two integers in which to hold the coordinates
14010 */
14011 public void getLocationOnScreen(int[] location) {
14012 getLocationInWindow(location);
14013
14014 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014015 if (info != null) {
14016 location[0] += info.mWindowLeft;
14017 location[1] += info.mWindowTop;
14018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014019 }
14020
14021 /**
14022 * <p>Computes the coordinates of this view in its window. The argument
14023 * must be an array of two integers. After the method returns, the array
14024 * contains the x and y location in that order.</p>
14025 *
14026 * @param location an array of two integers in which to hold the coordinates
14027 */
14028 public void getLocationInWindow(int[] location) {
14029 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014030 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014031 }
14032
Gilles Debunne6583ce52011-12-06 18:09:02 -080014033 if (mAttachInfo == null) {
14034 // When the view is not attached to a window, this method does not make sense
14035 location[0] = location[1] = 0;
14036 return;
14037 }
14038
Gilles Debunnecea45132011-11-24 02:19:27 +010014039 float[] position = mAttachInfo.mTmpTransformLocation;
14040 position[0] = position[1] = 0.0f;
14041
14042 if (!hasIdentityMatrix()) {
14043 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014045
Gilles Debunnecea45132011-11-24 02:19:27 +010014046 position[0] += mLeft;
14047 position[1] += mTop;
14048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014049 ViewParent viewParent = mParent;
14050 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014051 final View view = (View) viewParent;
14052
14053 position[0] -= view.mScrollX;
14054 position[1] -= view.mScrollY;
14055
14056 if (!view.hasIdentityMatrix()) {
14057 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014058 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014059
14060 position[0] += view.mLeft;
14061 position[1] += view.mTop;
14062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014063 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014064 }
Romain Guy8506ab42009-06-11 17:35:47 -070014065
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014066 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014068 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14069 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014070 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014071
14072 location[0] = (int) (position[0] + 0.5f);
14073 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 }
14075
14076 /**
14077 * {@hide}
14078 * @param id the id of the view to be found
14079 * @return the view of the specified id, null if cannot be found
14080 */
14081 protected View findViewTraversal(int id) {
14082 if (id == mID) {
14083 return this;
14084 }
14085 return null;
14086 }
14087
14088 /**
14089 * {@hide}
14090 * @param tag the tag of the view to be found
14091 * @return the view of specified tag, null if cannot be found
14092 */
14093 protected View findViewWithTagTraversal(Object tag) {
14094 if (tag != null && tag.equals(mTag)) {
14095 return this;
14096 }
14097 return null;
14098 }
14099
14100 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014101 * {@hide}
14102 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014103 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014104 * @return The first view that matches the predicate or null.
14105 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014106 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014107 if (predicate.apply(this)) {
14108 return this;
14109 }
14110 return null;
14111 }
14112
14113 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114 * Look for a child view with the given id. If this view has the given
14115 * id, return this view.
14116 *
14117 * @param id The id to search for.
14118 * @return The view that has the given id in the hierarchy or null
14119 */
14120 public final View findViewById(int id) {
14121 if (id < 0) {
14122 return null;
14123 }
14124 return findViewTraversal(id);
14125 }
14126
14127 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014128 * Finds a view by its unuque and stable accessibility id.
14129 *
14130 * @param accessibilityId The searched accessibility id.
14131 * @return The found view.
14132 */
14133 final View findViewByAccessibilityId(int accessibilityId) {
14134 if (accessibilityId < 0) {
14135 return null;
14136 }
14137 return findViewByAccessibilityIdTraversal(accessibilityId);
14138 }
14139
14140 /**
14141 * Performs the traversal to find a view by its unuque and stable accessibility id.
14142 *
14143 * <strong>Note:</strong>This method does not stop at the root namespace
14144 * boundary since the user can touch the screen at an arbitrary location
14145 * potentially crossing the root namespace bounday which will send an
14146 * accessibility event to accessibility services and they should be able
14147 * to obtain the event source. Also accessibility ids are guaranteed to be
14148 * unique in the window.
14149 *
14150 * @param accessibilityId The accessibility id.
14151 * @return The found view.
14152 */
14153 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14154 if (getAccessibilityViewId() == accessibilityId) {
14155 return this;
14156 }
14157 return null;
14158 }
14159
14160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014161 * Look for a child view with the given tag. If this view has the given
14162 * tag, return this view.
14163 *
14164 * @param tag The tag to search for, using "tag.equals(getTag())".
14165 * @return The View that has the given tag in the hierarchy or null
14166 */
14167 public final View findViewWithTag(Object tag) {
14168 if (tag == null) {
14169 return null;
14170 }
14171 return findViewWithTagTraversal(tag);
14172 }
14173
14174 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014175 * {@hide}
14176 * Look for a child view that matches the specified predicate.
14177 * If this view matches the predicate, return this view.
14178 *
14179 * @param predicate The predicate to evaluate.
14180 * @return The first view that matches the predicate or null.
14181 */
14182 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014183 return findViewByPredicateTraversal(predicate, null);
14184 }
14185
14186 /**
14187 * {@hide}
14188 * Look for a child view that matches the specified predicate,
14189 * starting with the specified view and its descendents and then
14190 * recusively searching the ancestors and siblings of that view
14191 * until this view is reached.
14192 *
14193 * This method is useful in cases where the predicate does not match
14194 * a single unique view (perhaps multiple views use the same id)
14195 * and we are trying to find the view that is "closest" in scope to the
14196 * starting view.
14197 *
14198 * @param start The view to start from.
14199 * @param predicate The predicate to evaluate.
14200 * @return The first view that matches the predicate or null.
14201 */
14202 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14203 View childToSkip = null;
14204 for (;;) {
14205 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14206 if (view != null || start == this) {
14207 return view;
14208 }
14209
14210 ViewParent parent = start.getParent();
14211 if (parent == null || !(parent instanceof View)) {
14212 return null;
14213 }
14214
14215 childToSkip = start;
14216 start = (View) parent;
14217 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014218 }
14219
14220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014221 * Sets the identifier for this view. The identifier does not have to be
14222 * unique in this view's hierarchy. The identifier should be a positive
14223 * number.
14224 *
14225 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014226 * @see #getId()
14227 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014228 *
14229 * @param id a number used to identify the view
14230 *
14231 * @attr ref android.R.styleable#View_id
14232 */
14233 public void setId(int id) {
14234 mID = id;
14235 }
14236
14237 /**
14238 * {@hide}
14239 *
14240 * @param isRoot true if the view belongs to the root namespace, false
14241 * otherwise
14242 */
14243 public void setIsRootNamespace(boolean isRoot) {
14244 if (isRoot) {
14245 mPrivateFlags |= IS_ROOT_NAMESPACE;
14246 } else {
14247 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14248 }
14249 }
14250
14251 /**
14252 * {@hide}
14253 *
14254 * @return true if the view belongs to the root namespace, false otherwise
14255 */
14256 public boolean isRootNamespace() {
14257 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14258 }
14259
14260 /**
14261 * Returns this view's identifier.
14262 *
14263 * @return a positive integer used to identify the view or {@link #NO_ID}
14264 * if the view has no ID
14265 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014266 * @see #setId(int)
14267 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014268 * @attr ref android.R.styleable#View_id
14269 */
14270 @ViewDebug.CapturedViewProperty
14271 public int getId() {
14272 return mID;
14273 }
14274
14275 /**
14276 * Returns this view's tag.
14277 *
14278 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014279 *
14280 * @see #setTag(Object)
14281 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014282 */
14283 @ViewDebug.ExportedProperty
14284 public Object getTag() {
14285 return mTag;
14286 }
14287
14288 /**
14289 * Sets the tag associated with this view. A tag can be used to mark
14290 * a view in its hierarchy and does not have to be unique within the
14291 * hierarchy. Tags can also be used to store data within a view without
14292 * resorting to another data structure.
14293 *
14294 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014295 *
14296 * @see #getTag()
14297 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014298 */
14299 public void setTag(final Object tag) {
14300 mTag = tag;
14301 }
14302
14303 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014304 * Returns the tag associated with this view and the specified key.
14305 *
14306 * @param key The key identifying the tag
14307 *
14308 * @return the Object stored in this view as a tag
14309 *
14310 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014311 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014312 */
14313 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014314 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014315 return null;
14316 }
14317
14318 /**
14319 * Sets a tag associated with this view and a key. A tag can be used
14320 * to mark a view in its hierarchy and does not have to be unique within
14321 * the hierarchy. Tags can also be used to store data within a view
14322 * without resorting to another data structure.
14323 *
14324 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014325 * application to ensure it is unique (see the <a
14326 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14327 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014328 * the Android framework or not associated with any package will cause
14329 * an {@link IllegalArgumentException} to be thrown.
14330 *
14331 * @param key The key identifying the tag
14332 * @param tag An Object to tag the view with
14333 *
14334 * @throws IllegalArgumentException If they specified key is not valid
14335 *
14336 * @see #setTag(Object)
14337 * @see #getTag(int)
14338 */
14339 public void setTag(int key, final Object tag) {
14340 // If the package id is 0x00 or 0x01, it's either an undefined package
14341 // or a framework id
14342 if ((key >>> 24) < 2) {
14343 throw new IllegalArgumentException("The key must be an application-specific "
14344 + "resource id.");
14345 }
14346
Adam Powell2b2f6d62011-09-23 11:15:39 -070014347 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014348 }
14349
14350 /**
14351 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14352 * framework id.
14353 *
14354 * @hide
14355 */
14356 public void setTagInternal(int key, Object tag) {
14357 if ((key >>> 24) != 0x1) {
14358 throw new IllegalArgumentException("The key must be a framework-specific "
14359 + "resource id.");
14360 }
14361
Adam Powell2b2f6d62011-09-23 11:15:39 -070014362 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014363 }
14364
Adam Powell2b2f6d62011-09-23 11:15:39 -070014365 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014366 if (mKeyedTags == null) {
14367 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014368 }
14369
Adam Powell7db82ac2011-09-22 19:44:04 -070014370 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014371 }
14372
14373 /**
Romain Guy13922e02009-05-12 17:56:14 -070014374 * @param consistency The type of consistency. See ViewDebug for more information.
14375 *
14376 * @hide
14377 */
14378 protected boolean dispatchConsistencyCheck(int consistency) {
14379 return onConsistencyCheck(consistency);
14380 }
14381
14382 /**
14383 * Method that subclasses should implement to check their consistency. The type of
14384 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014385 *
Romain Guy13922e02009-05-12 17:56:14 -070014386 * @param consistency The type of consistency. See ViewDebug for more information.
14387 *
14388 * @throws IllegalStateException if the view is in an inconsistent state.
14389 *
14390 * @hide
14391 */
14392 protected boolean onConsistencyCheck(int consistency) {
14393 boolean result = true;
14394
14395 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14396 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14397
14398 if (checkLayout) {
14399 if (getParent() == null) {
14400 result = false;
14401 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14402 "View " + this + " does not have a parent.");
14403 }
14404
14405 if (mAttachInfo == null) {
14406 result = false;
14407 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14408 "View " + this + " is not attached to a window.");
14409 }
14410 }
14411
14412 if (checkDrawing) {
14413 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14414 // from their draw() method
14415
14416 if ((mPrivateFlags & DRAWN) != DRAWN &&
14417 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14418 result = false;
14419 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14420 "View " + this + " was invalidated but its drawing cache is valid.");
14421 }
14422 }
14423
14424 return result;
14425 }
14426
14427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014428 * Prints information about this view in the log output, with the tag
14429 * {@link #VIEW_LOG_TAG}.
14430 *
14431 * @hide
14432 */
14433 public void debug() {
14434 debug(0);
14435 }
14436
14437 /**
14438 * Prints information about this view in the log output, with the tag
14439 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14440 * indentation defined by the <code>depth</code>.
14441 *
14442 * @param depth the indentation level
14443 *
14444 * @hide
14445 */
14446 protected void debug(int depth) {
14447 String output = debugIndent(depth - 1);
14448
14449 output += "+ " + this;
14450 int id = getId();
14451 if (id != -1) {
14452 output += " (id=" + id + ")";
14453 }
14454 Object tag = getTag();
14455 if (tag != null) {
14456 output += " (tag=" + tag + ")";
14457 }
14458 Log.d(VIEW_LOG_TAG, output);
14459
14460 if ((mPrivateFlags & FOCUSED) != 0) {
14461 output = debugIndent(depth) + " FOCUSED";
14462 Log.d(VIEW_LOG_TAG, output);
14463 }
14464
14465 output = debugIndent(depth);
14466 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14467 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14468 + "} ";
14469 Log.d(VIEW_LOG_TAG, output);
14470
14471 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14472 || mPaddingBottom != 0) {
14473 output = debugIndent(depth);
14474 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14475 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14476 Log.d(VIEW_LOG_TAG, output);
14477 }
14478
14479 output = debugIndent(depth);
14480 output += "mMeasureWidth=" + mMeasuredWidth +
14481 " mMeasureHeight=" + mMeasuredHeight;
14482 Log.d(VIEW_LOG_TAG, output);
14483
14484 output = debugIndent(depth);
14485 if (mLayoutParams == null) {
14486 output += "BAD! no layout params";
14487 } else {
14488 output = mLayoutParams.debug(output);
14489 }
14490 Log.d(VIEW_LOG_TAG, output);
14491
14492 output = debugIndent(depth);
14493 output += "flags={";
14494 output += View.printFlags(mViewFlags);
14495 output += "}";
14496 Log.d(VIEW_LOG_TAG, output);
14497
14498 output = debugIndent(depth);
14499 output += "privateFlags={";
14500 output += View.printPrivateFlags(mPrivateFlags);
14501 output += "}";
14502 Log.d(VIEW_LOG_TAG, output);
14503 }
14504
14505 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014506 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014507 *
14508 * @param depth the indentation level
14509 * @return a String containing (depth * 2 + 3) * 2 white spaces
14510 *
14511 * @hide
14512 */
14513 protected static String debugIndent(int depth) {
14514 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14515 for (int i = 0; i < (depth * 2) + 3; i++) {
14516 spaces.append(' ').append(' ');
14517 }
14518 return spaces.toString();
14519 }
14520
14521 /**
14522 * <p>Return the offset of the widget's text baseline from the widget's top
14523 * boundary. If this widget does not support baseline alignment, this
14524 * method returns -1. </p>
14525 *
14526 * @return the offset of the baseline within the widget's bounds or -1
14527 * if baseline alignment is not supported
14528 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014529 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014530 public int getBaseline() {
14531 return -1;
14532 }
14533
14534 /**
14535 * Call this when something has changed which has invalidated the
14536 * layout of this view. This will schedule a layout pass of the view
14537 * tree.
14538 */
14539 public void requestLayout() {
14540 if (ViewDebug.TRACE_HIERARCHY) {
14541 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14542 }
14543
14544 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014545 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014546
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014547 if (mLayoutParams != null) {
14548 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14549 }
14550
14551 if (mParent != null && !mParent.isLayoutRequested()) {
14552 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014553 }
14554 }
14555
14556 /**
14557 * Forces this view to be laid out during the next layout pass.
14558 * This method does not call requestLayout() or forceLayout()
14559 * on the parent.
14560 */
14561 public void forceLayout() {
14562 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014563 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014564 }
14565
14566 /**
14567 * <p>
14568 * This is called to find out how big a view should be. The parent
14569 * supplies constraint information in the width and height parameters.
14570 * </p>
14571 *
14572 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014573 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014574 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014575 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014576 * </p>
14577 *
14578 *
14579 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14580 * parent
14581 * @param heightMeasureSpec Vertical space requirements as imposed by the
14582 * parent
14583 *
14584 * @see #onMeasure(int, int)
14585 */
14586 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14587 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14588 widthMeasureSpec != mOldWidthMeasureSpec ||
14589 heightMeasureSpec != mOldHeightMeasureSpec) {
14590
14591 // first clears the measured dimension flag
14592 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14593
14594 if (ViewDebug.TRACE_HIERARCHY) {
14595 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14596 }
14597
14598 // measure ourselves, this should set the measured dimension flag back
14599 onMeasure(widthMeasureSpec, heightMeasureSpec);
14600
14601 // flag not set, setMeasuredDimension() was not invoked, we raise
14602 // an exception to warn the developer
14603 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14604 throw new IllegalStateException("onMeasure() did not set the"
14605 + " measured dimension by calling"
14606 + " setMeasuredDimension()");
14607 }
14608
14609 mPrivateFlags |= LAYOUT_REQUIRED;
14610 }
14611
14612 mOldWidthMeasureSpec = widthMeasureSpec;
14613 mOldHeightMeasureSpec = heightMeasureSpec;
14614 }
14615
14616 /**
14617 * <p>
14618 * Measure the view and its content to determine the measured width and the
14619 * measured height. This method is invoked by {@link #measure(int, int)} and
14620 * should be overriden by subclasses to provide accurate and efficient
14621 * measurement of their contents.
14622 * </p>
14623 *
14624 * <p>
14625 * <strong>CONTRACT:</strong> When overriding this method, you
14626 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14627 * measured width and height of this view. Failure to do so will trigger an
14628 * <code>IllegalStateException</code>, thrown by
14629 * {@link #measure(int, int)}. Calling the superclass'
14630 * {@link #onMeasure(int, int)} is a valid use.
14631 * </p>
14632 *
14633 * <p>
14634 * The base class implementation of measure defaults to the background size,
14635 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14636 * override {@link #onMeasure(int, int)} to provide better measurements of
14637 * their content.
14638 * </p>
14639 *
14640 * <p>
14641 * If this method is overridden, it is the subclass's responsibility to make
14642 * sure the measured height and width are at least the view's minimum height
14643 * and width ({@link #getSuggestedMinimumHeight()} and
14644 * {@link #getSuggestedMinimumWidth()}).
14645 * </p>
14646 *
14647 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14648 * The requirements are encoded with
14649 * {@link android.view.View.MeasureSpec}.
14650 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14651 * The requirements are encoded with
14652 * {@link android.view.View.MeasureSpec}.
14653 *
14654 * @see #getMeasuredWidth()
14655 * @see #getMeasuredHeight()
14656 * @see #setMeasuredDimension(int, int)
14657 * @see #getSuggestedMinimumHeight()
14658 * @see #getSuggestedMinimumWidth()
14659 * @see android.view.View.MeasureSpec#getMode(int)
14660 * @see android.view.View.MeasureSpec#getSize(int)
14661 */
14662 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14663 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14664 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14665 }
14666
14667 /**
14668 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14669 * measured width and measured height. Failing to do so will trigger an
14670 * exception at measurement time.</p>
14671 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014672 * @param measuredWidth The measured width of this view. May be a complex
14673 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14674 * {@link #MEASURED_STATE_TOO_SMALL}.
14675 * @param measuredHeight The measured height of this view. May be a complex
14676 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14677 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014678 */
14679 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14680 mMeasuredWidth = measuredWidth;
14681 mMeasuredHeight = measuredHeight;
14682
14683 mPrivateFlags |= MEASURED_DIMENSION_SET;
14684 }
14685
14686 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014687 * Merge two states as returned by {@link #getMeasuredState()}.
14688 * @param curState The current state as returned from a view or the result
14689 * of combining multiple views.
14690 * @param newState The new view state to combine.
14691 * @return Returns a new integer reflecting the combination of the two
14692 * states.
14693 */
14694 public static int combineMeasuredStates(int curState, int newState) {
14695 return curState | newState;
14696 }
14697
14698 /**
14699 * Version of {@link #resolveSizeAndState(int, int, int)}
14700 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14701 */
14702 public static int resolveSize(int size, int measureSpec) {
14703 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14704 }
14705
14706 /**
14707 * Utility to reconcile a desired size and state, with constraints imposed
14708 * by a MeasureSpec. Will take the desired size, unless a different size
14709 * is imposed by the constraints. The returned value is a compound integer,
14710 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14711 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14712 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014713 *
14714 * @param size How big the view wants to be
14715 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014716 * @return Size information bit mask as defined by
14717 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014718 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014719 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014720 int result = size;
14721 int specMode = MeasureSpec.getMode(measureSpec);
14722 int specSize = MeasureSpec.getSize(measureSpec);
14723 switch (specMode) {
14724 case MeasureSpec.UNSPECIFIED:
14725 result = size;
14726 break;
14727 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014728 if (specSize < size) {
14729 result = specSize | MEASURED_STATE_TOO_SMALL;
14730 } else {
14731 result = size;
14732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014733 break;
14734 case MeasureSpec.EXACTLY:
14735 result = specSize;
14736 break;
14737 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014738 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014739 }
14740
14741 /**
14742 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014743 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014744 * by the MeasureSpec.
14745 *
14746 * @param size Default size for this view
14747 * @param measureSpec Constraints imposed by the parent
14748 * @return The size this view should be.
14749 */
14750 public static int getDefaultSize(int size, int measureSpec) {
14751 int result = size;
14752 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014753 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014754
14755 switch (specMode) {
14756 case MeasureSpec.UNSPECIFIED:
14757 result = size;
14758 break;
14759 case MeasureSpec.AT_MOST:
14760 case MeasureSpec.EXACTLY:
14761 result = specSize;
14762 break;
14763 }
14764 return result;
14765 }
14766
14767 /**
14768 * Returns the suggested minimum height that the view should use. This
14769 * returns the maximum of the view's minimum height
14770 * and the background's minimum height
14771 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14772 * <p>
14773 * When being used in {@link #onMeasure(int, int)}, the caller should still
14774 * ensure the returned height is within the requirements of the parent.
14775 *
14776 * @return The suggested minimum height of the view.
14777 */
14778 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014779 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014781 }
14782
14783 /**
14784 * Returns the suggested minimum width that the view should use. This
14785 * returns the maximum of the view's minimum width)
14786 * and the background's minimum width
14787 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14788 * <p>
14789 * When being used in {@link #onMeasure(int, int)}, the caller should still
14790 * ensure the returned width is within the requirements of the parent.
14791 *
14792 * @return The suggested minimum width of the view.
14793 */
14794 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014795 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014797
Philip Milne6c8ea062012-04-03 17:38:43 -070014798 /**
14799 * Returns the minimum height of the view.
14800 *
14801 * @return the minimum height the view will try to be.
14802 *
14803 * @see #setMinimumHeight(int)
14804 *
14805 * @attr ref android.R.styleable#View_minHeight
14806 */
14807 public int getMinimumHeight() {
14808 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014809 }
14810
14811 /**
14812 * Sets the minimum height of the view. It is not guaranteed the view will
14813 * be able to achieve this minimum height (for example, if its parent layout
14814 * constrains it with less available height).
14815 *
14816 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014817 *
14818 * @see #getMinimumHeight()
14819 *
14820 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014821 */
14822 public void setMinimumHeight(int minHeight) {
14823 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014824 requestLayout();
14825 }
14826
14827 /**
14828 * Returns the minimum width of the view.
14829 *
14830 * @return the minimum width the view will try to be.
14831 *
14832 * @see #setMinimumWidth(int)
14833 *
14834 * @attr ref android.R.styleable#View_minWidth
14835 */
14836 public int getMinimumWidth() {
14837 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014838 }
14839
14840 /**
14841 * Sets the minimum width of the view. It is not guaranteed the view will
14842 * be able to achieve this minimum width (for example, if its parent layout
14843 * constrains it with less available width).
14844 *
14845 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014846 *
14847 * @see #getMinimumWidth()
14848 *
14849 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014850 */
14851 public void setMinimumWidth(int minWidth) {
14852 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014853 requestLayout();
14854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014855 }
14856
14857 /**
14858 * Get the animation currently associated with this view.
14859 *
14860 * @return The animation that is currently playing or
14861 * scheduled to play for this view.
14862 */
14863 public Animation getAnimation() {
14864 return mCurrentAnimation;
14865 }
14866
14867 /**
14868 * Start the specified animation now.
14869 *
14870 * @param animation the animation to start now
14871 */
14872 public void startAnimation(Animation animation) {
14873 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14874 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014875 invalidateParentCaches();
14876 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014877 }
14878
14879 /**
14880 * Cancels any animations for this view.
14881 */
14882 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014883 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014884 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014886 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014887 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014888 }
14889
14890 /**
14891 * Sets the next animation to play for this view.
14892 * If you want the animation to play immediately, use
14893 * startAnimation. This method provides allows fine-grained
14894 * control over the start time and invalidation, but you
14895 * must make sure that 1) the animation has a start time set, and
14896 * 2) the view will be invalidated when the animation is supposed to
14897 * start.
14898 *
14899 * @param animation The next animation, or null.
14900 */
14901 public void setAnimation(Animation animation) {
14902 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014904 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014905 // If the screen is off assume the animation start time is now instead of
14906 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14907 // would cause the animation to start when the screen turns back on
14908 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14909 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14910 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014912 animation.reset();
14913 }
14914 }
14915
14916 /**
14917 * Invoked by a parent ViewGroup to notify the start of the animation
14918 * currently associated with this view. If you override this method,
14919 * always call super.onAnimationStart();
14920 *
14921 * @see #setAnimation(android.view.animation.Animation)
14922 * @see #getAnimation()
14923 */
14924 protected void onAnimationStart() {
14925 mPrivateFlags |= ANIMATION_STARTED;
14926 }
14927
14928 /**
14929 * Invoked by a parent ViewGroup to notify the end of the animation
14930 * currently associated with this view. If you override this method,
14931 * always call super.onAnimationEnd();
14932 *
14933 * @see #setAnimation(android.view.animation.Animation)
14934 * @see #getAnimation()
14935 */
14936 protected void onAnimationEnd() {
14937 mPrivateFlags &= ~ANIMATION_STARTED;
14938 }
14939
14940 /**
14941 * Invoked if there is a Transform that involves alpha. Subclass that can
14942 * draw themselves with the specified alpha should return true, and then
14943 * respect that alpha when their onDraw() is called. If this returns false
14944 * then the view may be redirected to draw into an offscreen buffer to
14945 * fulfill the request, which will look fine, but may be slower than if the
14946 * subclass handles it internally. The default implementation returns false.
14947 *
14948 * @param alpha The alpha (0..255) to apply to the view's drawing
14949 * @return true if the view can draw with the specified alpha.
14950 */
14951 protected boolean onSetAlpha(int alpha) {
14952 return false;
14953 }
14954
14955 /**
14956 * This is used by the RootView to perform an optimization when
14957 * the view hierarchy contains one or several SurfaceView.
14958 * SurfaceView is always considered transparent, but its children are not,
14959 * therefore all View objects remove themselves from the global transparent
14960 * region (passed as a parameter to this function).
14961 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014962 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014963 *
14964 * @return Returns true if the effective visibility of the view at this
14965 * point is opaque, regardless of the transparent region; returns false
14966 * if it is possible for underlying windows to be seen behind the view.
14967 *
14968 * {@hide}
14969 */
14970 public boolean gatherTransparentRegion(Region region) {
14971 final AttachInfo attachInfo = mAttachInfo;
14972 if (region != null && attachInfo != null) {
14973 final int pflags = mPrivateFlags;
14974 if ((pflags & SKIP_DRAW) == 0) {
14975 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14976 // remove it from the transparent region.
14977 final int[] location = attachInfo.mTransparentLocation;
14978 getLocationInWindow(location);
14979 region.op(location[0], location[1], location[0] + mRight - mLeft,
14980 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014981 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014982 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14983 // exists, so we remove the background drawable's non-transparent
14984 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014985 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014986 }
14987 }
14988 return true;
14989 }
14990
14991 /**
14992 * Play a sound effect for this view.
14993 *
14994 * <p>The framework will play sound effects for some built in actions, such as
14995 * clicking, but you may wish to play these effects in your widget,
14996 * for instance, for internal navigation.
14997 *
14998 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14999 * {@link #isSoundEffectsEnabled()} is true.
15000 *
15001 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15002 */
15003 public void playSoundEffect(int soundConstant) {
15004 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15005 return;
15006 }
15007 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15008 }
15009
15010 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015011 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015012 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015013 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015014 *
15015 * <p>The framework will provide haptic feedback for some built in actions,
15016 * such as long presses, but you may wish to provide feedback for your
15017 * own widget.
15018 *
15019 * <p>The feedback will only be performed if
15020 * {@link #isHapticFeedbackEnabled()} is true.
15021 *
15022 * @param feedbackConstant One of the constants defined in
15023 * {@link HapticFeedbackConstants}
15024 */
15025 public boolean performHapticFeedback(int feedbackConstant) {
15026 return performHapticFeedback(feedbackConstant, 0);
15027 }
15028
15029 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015030 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015031 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015032 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015033 *
15034 * @param feedbackConstant One of the constants defined in
15035 * {@link HapticFeedbackConstants}
15036 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15037 */
15038 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15039 if (mAttachInfo == null) {
15040 return false;
15041 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015042 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015043 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015044 && !isHapticFeedbackEnabled()) {
15045 return false;
15046 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015047 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15048 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015049 }
15050
15051 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015052 * Request that the visibility of the status bar or other screen/window
15053 * decorations be changed.
15054 *
15055 * <p>This method is used to put the over device UI into temporary modes
15056 * where the user's attention is focused more on the application content,
15057 * by dimming or hiding surrounding system affordances. This is typically
15058 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15059 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15060 * to be placed behind the action bar (and with these flags other system
15061 * affordances) so that smooth transitions between hiding and showing them
15062 * can be done.
15063 *
15064 * <p>Two representative examples of the use of system UI visibility is
15065 * implementing a content browsing application (like a magazine reader)
15066 * and a video playing application.
15067 *
15068 * <p>The first code shows a typical implementation of a View in a content
15069 * browsing application. In this implementation, the application goes
15070 * into a content-oriented mode by hiding the status bar and action bar,
15071 * and putting the navigation elements into lights out mode. The user can
15072 * then interact with content while in this mode. Such an application should
15073 * provide an easy way for the user to toggle out of the mode (such as to
15074 * check information in the status bar or access notifications). In the
15075 * implementation here, this is done simply by tapping on the content.
15076 *
15077 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15078 * content}
15079 *
15080 * <p>This second code sample shows a typical implementation of a View
15081 * in a video playing application. In this situation, while the video is
15082 * playing the application would like to go into a complete full-screen mode,
15083 * to use as much of the display as possible for the video. When in this state
15084 * the user can not interact with the application; the system intercepts
15085 * touching on the screen to pop the UI out of full screen mode.
15086 *
15087 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15088 * content}
15089 *
15090 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15091 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15092 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15093 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015094 */
15095 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015096 if (visibility != mSystemUiVisibility) {
15097 mSystemUiVisibility = visibility;
15098 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15099 mParent.recomputeViewAttributes(this);
15100 }
Joe Onorato664644d2011-01-23 17:53:23 -080015101 }
15102 }
15103
15104 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015105 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15106 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15107 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15108 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15109 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015110 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015111 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015112 return mSystemUiVisibility;
15113 }
15114
Scott Mainec6331b2011-05-24 16:55:56 -070015115 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015116 * Returns the current system UI visibility that is currently set for
15117 * the entire window. This is the combination of the
15118 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15119 * views in the window.
15120 */
15121 public int getWindowSystemUiVisibility() {
15122 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15123 }
15124
15125 /**
15126 * Override to find out when the window's requested system UI visibility
15127 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15128 * This is different from the callbacks recieved through
15129 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15130 * in that this is only telling you about the local request of the window,
15131 * not the actual values applied by the system.
15132 */
15133 public void onWindowSystemUiVisibilityChanged(int visible) {
15134 }
15135
15136 /**
15137 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15138 * the view hierarchy.
15139 */
15140 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15141 onWindowSystemUiVisibilityChanged(visible);
15142 }
15143
15144 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015145 * Set a listener to receive callbacks when the visibility of the system bar changes.
15146 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15147 */
Joe Onorato664644d2011-01-23 17:53:23 -080015148 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015149 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015150 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15151 mParent.recomputeViewAttributes(this);
15152 }
15153 }
15154
15155 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015156 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15157 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015158 */
15159 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015160 ListenerInfo li = mListenerInfo;
15161 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15162 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015163 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015164 }
15165 }
15166
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015167 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15168 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15169 if (val != mSystemUiVisibility) {
15170 setSystemUiVisibility(val);
15171 }
15172 }
15173
Joe Onorato664644d2011-01-23 17:53:23 -080015174 /**
Joe Malin32736f02011-01-19 16:14:20 -080015175 * Creates an image that the system displays during the drag and drop
15176 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15177 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15178 * appearance as the given View. The default also positions the center of the drag shadow
15179 * directly under the touch point. If no View is provided (the constructor with no parameters
15180 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15181 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15182 * default is an invisible drag shadow.
15183 * <p>
15184 * You are not required to use the View you provide to the constructor as the basis of the
15185 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15186 * anything you want as the drag shadow.
15187 * </p>
15188 * <p>
15189 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15190 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15191 * size and position of the drag shadow. It uses this data to construct a
15192 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15193 * so that your application can draw the shadow image in the Canvas.
15194 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015195 *
15196 * <div class="special reference">
15197 * <h3>Developer Guides</h3>
15198 * <p>For a guide to implementing drag and drop features, read the
15199 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15200 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015201 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015202 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015203 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015204
15205 /**
Joe Malin32736f02011-01-19 16:14:20 -080015206 * Constructs a shadow image builder based on a View. By default, the resulting drag
15207 * shadow will have the same appearance and dimensions as the View, with the touch point
15208 * over the center of the View.
15209 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015210 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015211 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015212 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015213 }
15214
Christopher Tate17ed60c2011-01-18 12:50:26 -080015215 /**
15216 * Construct a shadow builder object with no associated View. This
15217 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15218 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15219 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015220 * reference to any View object. If they are not overridden, then the result is an
15221 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015222 */
15223 public DragShadowBuilder() {
15224 mView = new WeakReference<View>(null);
15225 }
15226
15227 /**
15228 * Returns the View object that had been passed to the
15229 * {@link #View.DragShadowBuilder(View)}
15230 * constructor. If that View parameter was {@code null} or if the
15231 * {@link #View.DragShadowBuilder()}
15232 * constructor was used to instantiate the builder object, this method will return
15233 * null.
15234 *
15235 * @return The View object associate with this builder object.
15236 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015237 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015238 final public View getView() {
15239 return mView.get();
15240 }
15241
Christopher Tate2c095f32010-10-04 14:13:40 -070015242 /**
Joe Malin32736f02011-01-19 16:14:20 -080015243 * Provides the metrics for the shadow image. These include the dimensions of
15244 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015245 * be centered under the touch location while dragging.
15246 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015247 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015248 * same as the dimensions of the View itself and centers the shadow under
15249 * the touch point.
15250 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015251 *
Joe Malin32736f02011-01-19 16:14:20 -080015252 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15253 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15254 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15255 * image.
15256 *
15257 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15258 * shadow image that should be underneath the touch point during the drag and drop
15259 * operation. Your application must set {@link android.graphics.Point#x} to the
15260 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015261 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015262 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015263 final View view = mView.get();
15264 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015265 shadowSize.set(view.getWidth(), view.getHeight());
15266 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015267 } else {
15268 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15269 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015270 }
15271
15272 /**
Joe Malin32736f02011-01-19 16:14:20 -080015273 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15274 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015275 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015276 *
Joe Malin32736f02011-01-19 16:14:20 -080015277 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015278 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015279 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015280 final View view = mView.get();
15281 if (view != null) {
15282 view.draw(canvas);
15283 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015284 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015285 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015286 }
15287 }
15288
15289 /**
Joe Malin32736f02011-01-19 16:14:20 -080015290 * Starts a drag and drop operation. When your application calls this method, it passes a
15291 * {@link android.view.View.DragShadowBuilder} object to the system. The
15292 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15293 * to get metrics for the drag shadow, and then calls the object's
15294 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15295 * <p>
15296 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15297 * drag events to all the View objects in your application that are currently visible. It does
15298 * this either by calling the View object's drag listener (an implementation of
15299 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15300 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15301 * Both are passed a {@link android.view.DragEvent} object that has a
15302 * {@link android.view.DragEvent#getAction()} value of
15303 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15304 * </p>
15305 * <p>
15306 * Your application can invoke startDrag() on any attached View object. The View object does not
15307 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15308 * be related to the View the user selected for dragging.
15309 * </p>
15310 * @param data A {@link android.content.ClipData} object pointing to the data to be
15311 * transferred by the drag and drop operation.
15312 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15313 * drag shadow.
15314 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15315 * drop operation. This Object is put into every DragEvent object sent by the system during the
15316 * current drag.
15317 * <p>
15318 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15319 * to the target Views. For example, it can contain flags that differentiate between a
15320 * a copy operation and a move operation.
15321 * </p>
15322 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15323 * so the parameter should be set to 0.
15324 * @return {@code true} if the method completes successfully, or
15325 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15326 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015327 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015328 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015329 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015330 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015331 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015332 }
15333 boolean okay = false;
15334
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015335 Point shadowSize = new Point();
15336 Point shadowTouchPoint = new Point();
15337 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015338
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015339 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15340 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15341 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015342 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015343
Chris Tatea32dcf72010-10-14 12:13:50 -070015344 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015345 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15346 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015347 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015348 Surface surface = new Surface();
15349 try {
15350 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015351 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015352 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015353 + " surface=" + surface);
15354 if (token != null) {
15355 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015356 try {
Chris Tate6b391282010-10-14 15:48:59 -070015357 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015358 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015359 } finally {
15360 surface.unlockCanvasAndPost(canvas);
15361 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015362
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015363 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015364
15365 // Cache the local state object for delivery with DragEvents
15366 root.setLocalDragState(myLocalState);
15367
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015368 // repurpose 'shadowSize' for the last touch point
15369 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015370
Christopher Tatea53146c2010-09-07 11:57:52 -070015371 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015372 shadowSize.x, shadowSize.y,
15373 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015374 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015375
15376 // Off and running! Release our local surface instance; the drag
15377 // shadow surface is now managed by the system process.
15378 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015379 }
15380 } catch (Exception e) {
15381 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15382 surface.destroy();
15383 }
15384
15385 return okay;
15386 }
15387
Christopher Tatea53146c2010-09-07 11:57:52 -070015388 /**
Joe Malin32736f02011-01-19 16:14:20 -080015389 * Handles drag events sent by the system following a call to
15390 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15391 *<p>
15392 * When the system calls this method, it passes a
15393 * {@link android.view.DragEvent} object. A call to
15394 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15395 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15396 * operation.
15397 * @param event The {@link android.view.DragEvent} sent by the system.
15398 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15399 * in DragEvent, indicating the type of drag event represented by this object.
15400 * @return {@code true} if the method was successful, otherwise {@code false}.
15401 * <p>
15402 * The method should return {@code true} in response to an action type of
15403 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15404 * operation.
15405 * </p>
15406 * <p>
15407 * The method should also return {@code true} in response to an action type of
15408 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15409 * {@code false} if it didn't.
15410 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015411 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015412 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015413 return false;
15414 }
15415
15416 /**
Joe Malin32736f02011-01-19 16:14:20 -080015417 * Detects if this View is enabled and has a drag event listener.
15418 * If both are true, then it calls the drag event listener with the
15419 * {@link android.view.DragEvent} it received. If the drag event listener returns
15420 * {@code true}, then dispatchDragEvent() returns {@code true}.
15421 * <p>
15422 * For all other cases, the method calls the
15423 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15424 * method and returns its result.
15425 * </p>
15426 * <p>
15427 * This ensures that a drag event is always consumed, even if the View does not have a drag
15428 * event listener. However, if the View has a listener and the listener returns true, then
15429 * onDragEvent() is not called.
15430 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015431 */
15432 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015433 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015434 ListenerInfo li = mListenerInfo;
15435 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15436 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015437 return true;
15438 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015439 return onDragEvent(event);
15440 }
15441
Christopher Tate3d4bf172011-03-28 16:16:46 -070015442 boolean canAcceptDrag() {
15443 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15444 }
15445
Christopher Tatea53146c2010-09-07 11:57:52 -070015446 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015447 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15448 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015449 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015450 */
15451 public void onCloseSystemDialogs(String reason) {
15452 }
Joe Malin32736f02011-01-19 16:14:20 -080015453
Dianne Hackbornffa42482009-09-23 22:20:11 -070015454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015455 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015456 * update a Region being computed for
15457 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015458 * that any non-transparent parts of the Drawable are removed from the
15459 * given transparent region.
15460 *
15461 * @param dr The Drawable whose transparency is to be applied to the region.
15462 * @param region A Region holding the current transparency information,
15463 * where any parts of the region that are set are considered to be
15464 * transparent. On return, this region will be modified to have the
15465 * transparency information reduced by the corresponding parts of the
15466 * Drawable that are not transparent.
15467 * {@hide}
15468 */
15469 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15470 if (DBG) {
15471 Log.i("View", "Getting transparent region for: " + this);
15472 }
15473 final Region r = dr.getTransparentRegion();
15474 final Rect db = dr.getBounds();
15475 final AttachInfo attachInfo = mAttachInfo;
15476 if (r != null && attachInfo != null) {
15477 final int w = getRight()-getLeft();
15478 final int h = getBottom()-getTop();
15479 if (db.left > 0) {
15480 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15481 r.op(0, 0, db.left, h, Region.Op.UNION);
15482 }
15483 if (db.right < w) {
15484 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15485 r.op(db.right, 0, w, h, Region.Op.UNION);
15486 }
15487 if (db.top > 0) {
15488 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15489 r.op(0, 0, w, db.top, Region.Op.UNION);
15490 }
15491 if (db.bottom < h) {
15492 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15493 r.op(0, db.bottom, w, h, Region.Op.UNION);
15494 }
15495 final int[] location = attachInfo.mTransparentLocation;
15496 getLocationInWindow(location);
15497 r.translate(location[0], location[1]);
15498 region.op(r, Region.Op.INTERSECT);
15499 } else {
15500 region.op(db, Region.Op.DIFFERENCE);
15501 }
15502 }
15503
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015504 private void checkForLongClick(int delayOffset) {
15505 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15506 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015507
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015508 if (mPendingCheckForLongPress == null) {
15509 mPendingCheckForLongPress = new CheckForLongPress();
15510 }
15511 mPendingCheckForLongPress.rememberWindowAttachCount();
15512 postDelayed(mPendingCheckForLongPress,
15513 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015515 }
15516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015517 /**
15518 * Inflate a view from an XML resource. This convenience method wraps the {@link
15519 * LayoutInflater} class, which provides a full range of options for view inflation.
15520 *
15521 * @param context The Context object for your activity or application.
15522 * @param resource The resource ID to inflate
15523 * @param root A view group that will be the parent. Used to properly inflate the
15524 * layout_* parameters.
15525 * @see LayoutInflater
15526 */
15527 public static View inflate(Context context, int resource, ViewGroup root) {
15528 LayoutInflater factory = LayoutInflater.from(context);
15529 return factory.inflate(resource, root);
15530 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015532 /**
Adam Powell637d3372010-08-25 14:37:03 -070015533 * Scroll the view with standard behavior for scrolling beyond the normal
15534 * content boundaries. Views that call this method should override
15535 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15536 * results of an over-scroll operation.
15537 *
15538 * Views can use this method to handle any touch or fling-based scrolling.
15539 *
15540 * @param deltaX Change in X in pixels
15541 * @param deltaY Change in Y in pixels
15542 * @param scrollX Current X scroll value in pixels before applying deltaX
15543 * @param scrollY Current Y scroll value in pixels before applying deltaY
15544 * @param scrollRangeX Maximum content scroll range along the X axis
15545 * @param scrollRangeY Maximum content scroll range along the Y axis
15546 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15547 * along the X axis.
15548 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15549 * along the Y axis.
15550 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15551 * @return true if scrolling was clamped to an over-scroll boundary along either
15552 * axis, false otherwise.
15553 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015554 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015555 protected boolean overScrollBy(int deltaX, int deltaY,
15556 int scrollX, int scrollY,
15557 int scrollRangeX, int scrollRangeY,
15558 int maxOverScrollX, int maxOverScrollY,
15559 boolean isTouchEvent) {
15560 final int overScrollMode = mOverScrollMode;
15561 final boolean canScrollHorizontal =
15562 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15563 final boolean canScrollVertical =
15564 computeVerticalScrollRange() > computeVerticalScrollExtent();
15565 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15566 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15567 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15568 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15569
15570 int newScrollX = scrollX + deltaX;
15571 if (!overScrollHorizontal) {
15572 maxOverScrollX = 0;
15573 }
15574
15575 int newScrollY = scrollY + deltaY;
15576 if (!overScrollVertical) {
15577 maxOverScrollY = 0;
15578 }
15579
15580 // Clamp values if at the limits and record
15581 final int left = -maxOverScrollX;
15582 final int right = maxOverScrollX + scrollRangeX;
15583 final int top = -maxOverScrollY;
15584 final int bottom = maxOverScrollY + scrollRangeY;
15585
15586 boolean clampedX = false;
15587 if (newScrollX > right) {
15588 newScrollX = right;
15589 clampedX = true;
15590 } else if (newScrollX < left) {
15591 newScrollX = left;
15592 clampedX = true;
15593 }
15594
15595 boolean clampedY = false;
15596 if (newScrollY > bottom) {
15597 newScrollY = bottom;
15598 clampedY = true;
15599 } else if (newScrollY < top) {
15600 newScrollY = top;
15601 clampedY = true;
15602 }
15603
15604 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15605
15606 return clampedX || clampedY;
15607 }
15608
15609 /**
15610 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15611 * respond to the results of an over-scroll operation.
15612 *
15613 * @param scrollX New X scroll value in pixels
15614 * @param scrollY New Y scroll value in pixels
15615 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15616 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15617 */
15618 protected void onOverScrolled(int scrollX, int scrollY,
15619 boolean clampedX, boolean clampedY) {
15620 // Intentionally empty.
15621 }
15622
15623 /**
15624 * Returns the over-scroll mode for this view. The result will be
15625 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15626 * (allow over-scrolling only if the view content is larger than the container),
15627 * or {@link #OVER_SCROLL_NEVER}.
15628 *
15629 * @return This view's over-scroll mode.
15630 */
15631 public int getOverScrollMode() {
15632 return mOverScrollMode;
15633 }
15634
15635 /**
15636 * Set the over-scroll mode for this view. Valid over-scroll modes are
15637 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15638 * (allow over-scrolling only if the view content is larger than the container),
15639 * or {@link #OVER_SCROLL_NEVER}.
15640 *
15641 * Setting the over-scroll mode of a view will have an effect only if the
15642 * view is capable of scrolling.
15643 *
15644 * @param overScrollMode The new over-scroll mode for this view.
15645 */
15646 public void setOverScrollMode(int overScrollMode) {
15647 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15648 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15649 overScrollMode != OVER_SCROLL_NEVER) {
15650 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15651 }
15652 mOverScrollMode = overScrollMode;
15653 }
15654
15655 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015656 * Gets a scale factor that determines the distance the view should scroll
15657 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15658 * @return The vertical scroll scale factor.
15659 * @hide
15660 */
15661 protected float getVerticalScrollFactor() {
15662 if (mVerticalScrollFactor == 0) {
15663 TypedValue outValue = new TypedValue();
15664 if (!mContext.getTheme().resolveAttribute(
15665 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15666 throw new IllegalStateException(
15667 "Expected theme to define listPreferredItemHeight.");
15668 }
15669 mVerticalScrollFactor = outValue.getDimension(
15670 mContext.getResources().getDisplayMetrics());
15671 }
15672 return mVerticalScrollFactor;
15673 }
15674
15675 /**
15676 * Gets a scale factor that determines the distance the view should scroll
15677 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15678 * @return The horizontal scroll scale factor.
15679 * @hide
15680 */
15681 protected float getHorizontalScrollFactor() {
15682 // TODO: Should use something else.
15683 return getVerticalScrollFactor();
15684 }
15685
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015686 /**
15687 * Return the value specifying the text direction or policy that was set with
15688 * {@link #setTextDirection(int)}.
15689 *
15690 * @return the defined text direction. It can be one of:
15691 *
15692 * {@link #TEXT_DIRECTION_INHERIT},
15693 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15694 * {@link #TEXT_DIRECTION_ANY_RTL},
15695 * {@link #TEXT_DIRECTION_LTR},
15696 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015697 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015698 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015699 @ViewDebug.ExportedProperty(category = "text", mapping = {
15700 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15701 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15702 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15703 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15704 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15705 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15706 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015707 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015708 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015709 }
15710
15711 /**
15712 * Set the text direction.
15713 *
15714 * @param textDirection the direction to set. Should be one of:
15715 *
15716 * {@link #TEXT_DIRECTION_INHERIT},
15717 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15718 * {@link #TEXT_DIRECTION_ANY_RTL},
15719 * {@link #TEXT_DIRECTION_LTR},
15720 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015721 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015722 */
15723 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015724 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015725 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015726 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015727 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015728 // Set the new text direction
15729 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015730 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015731 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015732 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015733 }
15734 }
15735
15736 /**
15737 * Return the resolved text direction.
15738 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015739 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15740 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15741 * up the parent chain of the view. if there is no parent, then it will return the default
15742 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15743 *
15744 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015745 *
Doug Feltcb3791202011-07-07 11:57:48 -070015746 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15747 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015748 * {@link #TEXT_DIRECTION_LTR},
15749 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015750 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015751 */
15752 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015753 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015754 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015755 resolveTextDirection();
15756 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015757 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015758 }
15759
15760 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015761 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15762 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015763 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015764 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015765 // Reset any previous text direction resolution
15766 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15767
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015768 if (hasRtlSupport()) {
15769 // Set resolved text direction flag depending on text direction flag
15770 final int textDirection = getTextDirection();
15771 switch(textDirection) {
15772 case TEXT_DIRECTION_INHERIT:
15773 if (canResolveTextDirection()) {
15774 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015775
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015776 // Set current resolved direction to the same value as the parent's one
15777 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15778 switch (parentResolvedDirection) {
15779 case TEXT_DIRECTION_FIRST_STRONG:
15780 case TEXT_DIRECTION_ANY_RTL:
15781 case TEXT_DIRECTION_LTR:
15782 case TEXT_DIRECTION_RTL:
15783 case TEXT_DIRECTION_LOCALE:
15784 mPrivateFlags2 |=
15785 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15786 break;
15787 default:
15788 // Default resolved direction is "first strong" heuristic
15789 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15790 }
15791 } else {
15792 // We cannot do the resolution if there is no parent, so use the default one
15793 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015794 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015795 break;
15796 case TEXT_DIRECTION_FIRST_STRONG:
15797 case TEXT_DIRECTION_ANY_RTL:
15798 case TEXT_DIRECTION_LTR:
15799 case TEXT_DIRECTION_RTL:
15800 case TEXT_DIRECTION_LOCALE:
15801 // Resolved direction is the same as text direction
15802 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15803 break;
15804 default:
15805 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015806 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015807 }
15808 } else {
15809 // Default resolved direction is "first strong" heuristic
15810 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015811 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015812
15813 // Set to resolved
15814 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015815 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015816 }
15817
15818 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015819 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015820 * resolution should override this method.
15821 *
15822 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015823 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015824 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015825 }
15826
15827 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015828 * Check if text direction resolution can be done.
15829 *
15830 * @return true if text direction resolution can be done otherwise return false.
15831 */
15832 public boolean canResolveTextDirection() {
15833 switch (getTextDirection()) {
15834 case TEXT_DIRECTION_INHERIT:
15835 return (mParent != null) && (mParent instanceof ViewGroup);
15836 default:
15837 return true;
15838 }
15839 }
15840
15841 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015842 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015843 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015844 * reset is done.
15845 */
15846 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015847 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015848 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015849 }
15850
15851 /**
15852 * Called when text direction is reset. Subclasses that care about text direction reset should
15853 * override this method and do a reset of the text direction of their children. The default
15854 * implementation does nothing.
15855 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015856 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015857 }
15858
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015859 /**
15860 * Return the value specifying the text alignment or policy that was set with
15861 * {@link #setTextAlignment(int)}.
15862 *
15863 * @return the defined text alignment. It can be one of:
15864 *
15865 * {@link #TEXT_ALIGNMENT_INHERIT},
15866 * {@link #TEXT_ALIGNMENT_GRAVITY},
15867 * {@link #TEXT_ALIGNMENT_CENTER},
15868 * {@link #TEXT_ALIGNMENT_TEXT_START},
15869 * {@link #TEXT_ALIGNMENT_TEXT_END},
15870 * {@link #TEXT_ALIGNMENT_VIEW_START},
15871 * {@link #TEXT_ALIGNMENT_VIEW_END}
15872 */
15873 @ViewDebug.ExportedProperty(category = "text", mapping = {
15874 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15875 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15876 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15877 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15878 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15879 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15880 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15881 })
15882 public int getTextAlignment() {
15883 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15884 }
15885
15886 /**
15887 * Set the text alignment.
15888 *
15889 * @param textAlignment The text alignment to set. Should be one of
15890 *
15891 * {@link #TEXT_ALIGNMENT_INHERIT},
15892 * {@link #TEXT_ALIGNMENT_GRAVITY},
15893 * {@link #TEXT_ALIGNMENT_CENTER},
15894 * {@link #TEXT_ALIGNMENT_TEXT_START},
15895 * {@link #TEXT_ALIGNMENT_TEXT_END},
15896 * {@link #TEXT_ALIGNMENT_VIEW_START},
15897 * {@link #TEXT_ALIGNMENT_VIEW_END}
15898 *
15899 * @attr ref android.R.styleable#View_textAlignment
15900 */
15901 public void setTextAlignment(int textAlignment) {
15902 if (textAlignment != getTextAlignment()) {
15903 // Reset the current and resolved text alignment
15904 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15905 resetResolvedTextAlignment();
15906 // Set the new text alignment
15907 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15908 // Refresh
15909 requestLayout();
15910 invalidate(true);
15911 }
15912 }
15913
15914 /**
15915 * Return the resolved text alignment.
15916 *
15917 * The resolved text alignment. This needs resolution if the value is
15918 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15919 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15920 *
15921 * @return the resolved text alignment. Returns one of:
15922 *
15923 * {@link #TEXT_ALIGNMENT_GRAVITY},
15924 * {@link #TEXT_ALIGNMENT_CENTER},
15925 * {@link #TEXT_ALIGNMENT_TEXT_START},
15926 * {@link #TEXT_ALIGNMENT_TEXT_END},
15927 * {@link #TEXT_ALIGNMENT_VIEW_START},
15928 * {@link #TEXT_ALIGNMENT_VIEW_END}
15929 */
15930 @ViewDebug.ExportedProperty(category = "text", mapping = {
15931 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15932 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15933 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15934 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15935 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15936 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15937 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15938 })
15939 public int getResolvedTextAlignment() {
15940 // If text alignment is not resolved, then resolve it
15941 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15942 resolveTextAlignment();
15943 }
15944 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15945 }
15946
15947 /**
15948 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15949 * resolution is done.
15950 */
15951 public void resolveTextAlignment() {
15952 // Reset any previous text alignment resolution
15953 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15954
15955 if (hasRtlSupport()) {
15956 // Set resolved text alignment flag depending on text alignment flag
15957 final int textAlignment = getTextAlignment();
15958 switch (textAlignment) {
15959 case TEXT_ALIGNMENT_INHERIT:
15960 // Check if we can resolve the text alignment
15961 if (canResolveLayoutDirection() && mParent instanceof View) {
15962 View view = (View) mParent;
15963
15964 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15965 switch (parentResolvedTextAlignment) {
15966 case TEXT_ALIGNMENT_GRAVITY:
15967 case TEXT_ALIGNMENT_TEXT_START:
15968 case TEXT_ALIGNMENT_TEXT_END:
15969 case TEXT_ALIGNMENT_CENTER:
15970 case TEXT_ALIGNMENT_VIEW_START:
15971 case TEXT_ALIGNMENT_VIEW_END:
15972 // Resolved text alignment is the same as the parent resolved
15973 // text alignment
15974 mPrivateFlags2 |=
15975 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15976 break;
15977 default:
15978 // Use default resolved text alignment
15979 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15980 }
15981 }
15982 else {
15983 // We cannot do the resolution if there is no parent so use the default
15984 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15985 }
15986 break;
15987 case TEXT_ALIGNMENT_GRAVITY:
15988 case TEXT_ALIGNMENT_TEXT_START:
15989 case TEXT_ALIGNMENT_TEXT_END:
15990 case TEXT_ALIGNMENT_CENTER:
15991 case TEXT_ALIGNMENT_VIEW_START:
15992 case TEXT_ALIGNMENT_VIEW_END:
15993 // Resolved text alignment is the same as text alignment
15994 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15995 break;
15996 default:
15997 // Use default resolved text alignment
15998 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15999 }
16000 } else {
16001 // Use default resolved text alignment
16002 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16003 }
16004
16005 // Set the resolved
16006 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16007 onResolvedTextAlignmentChanged();
16008 }
16009
16010 /**
16011 * Check if text alignment resolution can be done.
16012 *
16013 * @return true if text alignment resolution can be done otherwise return false.
16014 */
16015 public boolean canResolveTextAlignment() {
16016 switch (getTextAlignment()) {
16017 case TEXT_DIRECTION_INHERIT:
16018 return (mParent != null);
16019 default:
16020 return true;
16021 }
16022 }
16023
16024 /**
16025 * Called when text alignment has been resolved. Subclasses that care about text alignment
16026 * resolution should override this method.
16027 *
16028 * The default implementation does nothing.
16029 */
16030 public void onResolvedTextAlignmentChanged() {
16031 }
16032
16033 /**
16034 * Reset resolved text alignment. Text alignment can be resolved with a call to
16035 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16036 * reset is done.
16037 */
16038 public void resetResolvedTextAlignment() {
16039 // Reset any previous text alignment resolution
16040 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16041 onResolvedTextAlignmentReset();
16042 }
16043
16044 /**
16045 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16046 * override this method and do a reset of the text alignment of their children. The default
16047 * implementation does nothing.
16048 */
16049 public void onResolvedTextAlignmentReset() {
16050 }
16051
Chet Haaseb39f0512011-05-24 14:36:40 -070016052 //
16053 // Properties
16054 //
16055 /**
16056 * A Property wrapper around the <code>alpha</code> functionality handled by the
16057 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16058 */
Chet Haased47f1532011-12-16 11:18:52 -080016059 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016060 @Override
16061 public void setValue(View object, float value) {
16062 object.setAlpha(value);
16063 }
16064
16065 @Override
16066 public Float get(View object) {
16067 return object.getAlpha();
16068 }
16069 };
16070
16071 /**
16072 * A Property wrapper around the <code>translationX</code> functionality handled by the
16073 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16074 */
Chet Haased47f1532011-12-16 11:18:52 -080016075 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016076 @Override
16077 public void setValue(View object, float value) {
16078 object.setTranslationX(value);
16079 }
16080
16081 @Override
16082 public Float get(View object) {
16083 return object.getTranslationX();
16084 }
16085 };
16086
16087 /**
16088 * A Property wrapper around the <code>translationY</code> functionality handled by the
16089 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16090 */
Chet Haased47f1532011-12-16 11:18:52 -080016091 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016092 @Override
16093 public void setValue(View object, float value) {
16094 object.setTranslationY(value);
16095 }
16096
16097 @Override
16098 public Float get(View object) {
16099 return object.getTranslationY();
16100 }
16101 };
16102
16103 /**
16104 * A Property wrapper around the <code>x</code> functionality handled by the
16105 * {@link View#setX(float)} and {@link View#getX()} methods.
16106 */
Chet Haased47f1532011-12-16 11:18:52 -080016107 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016108 @Override
16109 public void setValue(View object, float value) {
16110 object.setX(value);
16111 }
16112
16113 @Override
16114 public Float get(View object) {
16115 return object.getX();
16116 }
16117 };
16118
16119 /**
16120 * A Property wrapper around the <code>y</code> functionality handled by the
16121 * {@link View#setY(float)} and {@link View#getY()} methods.
16122 */
Chet Haased47f1532011-12-16 11:18:52 -080016123 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016124 @Override
16125 public void setValue(View object, float value) {
16126 object.setY(value);
16127 }
16128
16129 @Override
16130 public Float get(View object) {
16131 return object.getY();
16132 }
16133 };
16134
16135 /**
16136 * A Property wrapper around the <code>rotation</code> functionality handled by the
16137 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16138 */
Chet Haased47f1532011-12-16 11:18:52 -080016139 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016140 @Override
16141 public void setValue(View object, float value) {
16142 object.setRotation(value);
16143 }
16144
16145 @Override
16146 public Float get(View object) {
16147 return object.getRotation();
16148 }
16149 };
16150
16151 /**
16152 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16153 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16154 */
Chet Haased47f1532011-12-16 11:18:52 -080016155 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016156 @Override
16157 public void setValue(View object, float value) {
16158 object.setRotationX(value);
16159 }
16160
16161 @Override
16162 public Float get(View object) {
16163 return object.getRotationX();
16164 }
16165 };
16166
16167 /**
16168 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16169 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16170 */
Chet Haased47f1532011-12-16 11:18:52 -080016171 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016172 @Override
16173 public void setValue(View object, float value) {
16174 object.setRotationY(value);
16175 }
16176
16177 @Override
16178 public Float get(View object) {
16179 return object.getRotationY();
16180 }
16181 };
16182
16183 /**
16184 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16185 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16186 */
Chet Haased47f1532011-12-16 11:18:52 -080016187 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016188 @Override
16189 public void setValue(View object, float value) {
16190 object.setScaleX(value);
16191 }
16192
16193 @Override
16194 public Float get(View object) {
16195 return object.getScaleX();
16196 }
16197 };
16198
16199 /**
16200 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16201 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16202 */
Chet Haased47f1532011-12-16 11:18:52 -080016203 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016204 @Override
16205 public void setValue(View object, float value) {
16206 object.setScaleY(value);
16207 }
16208
16209 @Override
16210 public Float get(View object) {
16211 return object.getScaleY();
16212 }
16213 };
16214
Jeff Brown33bbfd22011-02-24 20:55:35 -080016215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016216 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16217 * Each MeasureSpec represents a requirement for either the width or the height.
16218 * A MeasureSpec is comprised of a size and a mode. There are three possible
16219 * modes:
16220 * <dl>
16221 * <dt>UNSPECIFIED</dt>
16222 * <dd>
16223 * The parent has not imposed any constraint on the child. It can be whatever size
16224 * it wants.
16225 * </dd>
16226 *
16227 * <dt>EXACTLY</dt>
16228 * <dd>
16229 * The parent has determined an exact size for the child. The child is going to be
16230 * given those bounds regardless of how big it wants to be.
16231 * </dd>
16232 *
16233 * <dt>AT_MOST</dt>
16234 * <dd>
16235 * The child can be as large as it wants up to the specified size.
16236 * </dd>
16237 * </dl>
16238 *
16239 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16240 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16241 */
16242 public static class MeasureSpec {
16243 private static final int MODE_SHIFT = 30;
16244 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16245
16246 /**
16247 * Measure specification mode: The parent has not imposed any constraint
16248 * on the child. It can be whatever size it wants.
16249 */
16250 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16251
16252 /**
16253 * Measure specification mode: The parent has determined an exact size
16254 * for the child. The child is going to be given those bounds regardless
16255 * of how big it wants to be.
16256 */
16257 public static final int EXACTLY = 1 << MODE_SHIFT;
16258
16259 /**
16260 * Measure specification mode: The child can be as large as it wants up
16261 * to the specified size.
16262 */
16263 public static final int AT_MOST = 2 << MODE_SHIFT;
16264
16265 /**
16266 * Creates a measure specification based on the supplied size and mode.
16267 *
16268 * The mode must always be one of the following:
16269 * <ul>
16270 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16271 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16272 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16273 * </ul>
16274 *
16275 * @param size the size of the measure specification
16276 * @param mode the mode of the measure specification
16277 * @return the measure specification based on size and mode
16278 */
16279 public static int makeMeasureSpec(int size, int mode) {
16280 return size + mode;
16281 }
16282
16283 /**
16284 * Extracts the mode from the supplied measure specification.
16285 *
16286 * @param measureSpec the measure specification to extract the mode from
16287 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16288 * {@link android.view.View.MeasureSpec#AT_MOST} or
16289 * {@link android.view.View.MeasureSpec#EXACTLY}
16290 */
16291 public static int getMode(int measureSpec) {
16292 return (measureSpec & MODE_MASK);
16293 }
16294
16295 /**
16296 * Extracts the size from the supplied measure specification.
16297 *
16298 * @param measureSpec the measure specification to extract the size from
16299 * @return the size in pixels defined in the supplied measure specification
16300 */
16301 public static int getSize(int measureSpec) {
16302 return (measureSpec & ~MODE_MASK);
16303 }
16304
16305 /**
16306 * Returns a String representation of the specified measure
16307 * specification.
16308 *
16309 * @param measureSpec the measure specification to convert to a String
16310 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16311 */
16312 public static String toString(int measureSpec) {
16313 int mode = getMode(measureSpec);
16314 int size = getSize(measureSpec);
16315
16316 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16317
16318 if (mode == UNSPECIFIED)
16319 sb.append("UNSPECIFIED ");
16320 else if (mode == EXACTLY)
16321 sb.append("EXACTLY ");
16322 else if (mode == AT_MOST)
16323 sb.append("AT_MOST ");
16324 else
16325 sb.append(mode).append(" ");
16326
16327 sb.append(size);
16328 return sb.toString();
16329 }
16330 }
16331
16332 class CheckForLongPress implements Runnable {
16333
16334 private int mOriginalWindowAttachCount;
16335
16336 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016337 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016338 && mOriginalWindowAttachCount == mWindowAttachCount) {
16339 if (performLongClick()) {
16340 mHasPerformedLongPress = true;
16341 }
16342 }
16343 }
16344
16345 public void rememberWindowAttachCount() {
16346 mOriginalWindowAttachCount = mWindowAttachCount;
16347 }
16348 }
Joe Malin32736f02011-01-19 16:14:20 -080016349
Adam Powelle14579b2009-12-16 18:39:52 -080016350 private final class CheckForTap implements Runnable {
16351 public void run() {
16352 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016353 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016354 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016355 }
16356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016357
Adam Powella35d7682010-03-12 14:48:13 -080016358 private final class PerformClick implements Runnable {
16359 public void run() {
16360 performClick();
16361 }
16362 }
16363
Dianne Hackborn63042d62011-01-26 18:56:29 -080016364 /** @hide */
16365 public void hackTurnOffWindowResizeAnim(boolean off) {
16366 mAttachInfo.mTurnOffWindowResizeAnim = off;
16367 }
Joe Malin32736f02011-01-19 16:14:20 -080016368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016369 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016370 * This method returns a ViewPropertyAnimator object, which can be used to animate
16371 * specific properties on this View.
16372 *
16373 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16374 */
16375 public ViewPropertyAnimator animate() {
16376 if (mAnimator == null) {
16377 mAnimator = new ViewPropertyAnimator(this);
16378 }
16379 return mAnimator;
16380 }
16381
16382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016383 * Interface definition for a callback to be invoked when a key event is
16384 * dispatched to this view. The callback will be invoked before the key
16385 * event is given to the view.
16386 */
16387 public interface OnKeyListener {
16388 /**
16389 * Called when a key is dispatched to a view. This allows listeners to
16390 * get a chance to respond before the target view.
16391 *
16392 * @param v The view the key has been dispatched to.
16393 * @param keyCode The code for the physical key that was pressed
16394 * @param event The KeyEvent object containing full information about
16395 * the event.
16396 * @return True if the listener has consumed the event, false otherwise.
16397 */
16398 boolean onKey(View v, int keyCode, KeyEvent event);
16399 }
16400
16401 /**
16402 * Interface definition for a callback to be invoked when a touch event is
16403 * dispatched to this view. The callback will be invoked before the touch
16404 * event is given to the view.
16405 */
16406 public interface OnTouchListener {
16407 /**
16408 * Called when a touch event is dispatched to a view. This allows listeners to
16409 * get a chance to respond before the target view.
16410 *
16411 * @param v The view the touch event has been dispatched to.
16412 * @param event The MotionEvent object containing full information about
16413 * the event.
16414 * @return True if the listener has consumed the event, false otherwise.
16415 */
16416 boolean onTouch(View v, MotionEvent event);
16417 }
16418
16419 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016420 * Interface definition for a callback to be invoked when a hover event is
16421 * dispatched to this view. The callback will be invoked before the hover
16422 * event is given to the view.
16423 */
16424 public interface OnHoverListener {
16425 /**
16426 * Called when a hover event is dispatched to a view. This allows listeners to
16427 * get a chance to respond before the target view.
16428 *
16429 * @param v The view the hover event has been dispatched to.
16430 * @param event The MotionEvent object containing full information about
16431 * the event.
16432 * @return True if the listener has consumed the event, false otherwise.
16433 */
16434 boolean onHover(View v, MotionEvent event);
16435 }
16436
16437 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016438 * Interface definition for a callback to be invoked when a generic motion event is
16439 * dispatched to this view. The callback will be invoked before the generic motion
16440 * event is given to the view.
16441 */
16442 public interface OnGenericMotionListener {
16443 /**
16444 * Called when a generic motion event is dispatched to a view. This allows listeners to
16445 * get a chance to respond before the target view.
16446 *
16447 * @param v The view the generic motion event has been dispatched to.
16448 * @param event The MotionEvent object containing full information about
16449 * the event.
16450 * @return True if the listener has consumed the event, false otherwise.
16451 */
16452 boolean onGenericMotion(View v, MotionEvent event);
16453 }
16454
16455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016456 * Interface definition for a callback to be invoked when a view has been clicked and held.
16457 */
16458 public interface OnLongClickListener {
16459 /**
16460 * Called when a view has been clicked and held.
16461 *
16462 * @param v The view that was clicked and held.
16463 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016464 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016465 */
16466 boolean onLongClick(View v);
16467 }
16468
16469 /**
Chris Tate32affef2010-10-18 15:29:21 -070016470 * Interface definition for a callback to be invoked when a drag is being dispatched
16471 * to this view. The callback will be invoked before the hosting view's own
16472 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16473 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016474 *
16475 * <div class="special reference">
16476 * <h3>Developer Guides</h3>
16477 * <p>For a guide to implementing drag and drop features, read the
16478 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16479 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016480 */
16481 public interface OnDragListener {
16482 /**
16483 * Called when a drag event is dispatched to a view. This allows listeners
16484 * to get a chance to override base View behavior.
16485 *
Joe Malin32736f02011-01-19 16:14:20 -080016486 * @param v The View that received the drag event.
16487 * @param event The {@link android.view.DragEvent} object for the drag event.
16488 * @return {@code true} if the drag event was handled successfully, or {@code false}
16489 * if the drag event was not handled. Note that {@code false} will trigger the View
16490 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016491 */
16492 boolean onDrag(View v, DragEvent event);
16493 }
16494
16495 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016496 * Interface definition for a callback to be invoked when the focus state of
16497 * a view changed.
16498 */
16499 public interface OnFocusChangeListener {
16500 /**
16501 * Called when the focus state of a view has changed.
16502 *
16503 * @param v The view whose state has changed.
16504 * @param hasFocus The new focus state of v.
16505 */
16506 void onFocusChange(View v, boolean hasFocus);
16507 }
16508
16509 /**
16510 * Interface definition for a callback to be invoked when a view is clicked.
16511 */
16512 public interface OnClickListener {
16513 /**
16514 * Called when a view has been clicked.
16515 *
16516 * @param v The view that was clicked.
16517 */
16518 void onClick(View v);
16519 }
16520
16521 /**
16522 * Interface definition for a callback to be invoked when the context menu
16523 * for this view is being built.
16524 */
16525 public interface OnCreateContextMenuListener {
16526 /**
16527 * Called when the context menu for this view is being built. It is not
16528 * safe to hold onto the menu after this method returns.
16529 *
16530 * @param menu The context menu that is being built
16531 * @param v The view for which the context menu is being built
16532 * @param menuInfo Extra information about the item for which the
16533 * context menu should be shown. This information will vary
16534 * depending on the class of v.
16535 */
16536 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16537 }
16538
Joe Onorato664644d2011-01-23 17:53:23 -080016539 /**
16540 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016541 * visibility. This reports <strong>global</strong> changes to the system UI
16542 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016543 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016544 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016545 */
16546 public interface OnSystemUiVisibilityChangeListener {
16547 /**
16548 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016549 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016550 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016551 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016552 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16553 * <strong>global</strong> state of the UI visibility flags, not what your
16554 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016555 */
16556 public void onSystemUiVisibilityChange(int visibility);
16557 }
16558
Adam Powell4afd62b2011-02-18 15:02:18 -080016559 /**
16560 * Interface definition for a callback to be invoked when this view is attached
16561 * or detached from its window.
16562 */
16563 public interface OnAttachStateChangeListener {
16564 /**
16565 * Called when the view is attached to a window.
16566 * @param v The view that was attached
16567 */
16568 public void onViewAttachedToWindow(View v);
16569 /**
16570 * Called when the view is detached from a window.
16571 * @param v The view that was detached
16572 */
16573 public void onViewDetachedFromWindow(View v);
16574 }
16575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016576 private final class UnsetPressedState implements Runnable {
16577 public void run() {
16578 setPressed(false);
16579 }
16580 }
16581
16582 /**
16583 * Base class for derived classes that want to save and restore their own
16584 * state in {@link android.view.View#onSaveInstanceState()}.
16585 */
16586 public static class BaseSavedState extends AbsSavedState {
16587 /**
16588 * Constructor used when reading from a parcel. Reads the state of the superclass.
16589 *
16590 * @param source
16591 */
16592 public BaseSavedState(Parcel source) {
16593 super(source);
16594 }
16595
16596 /**
16597 * Constructor called by derived classes when creating their SavedState objects
16598 *
16599 * @param superState The state of the superclass of this view
16600 */
16601 public BaseSavedState(Parcelable superState) {
16602 super(superState);
16603 }
16604
16605 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16606 new Parcelable.Creator<BaseSavedState>() {
16607 public BaseSavedState createFromParcel(Parcel in) {
16608 return new BaseSavedState(in);
16609 }
16610
16611 public BaseSavedState[] newArray(int size) {
16612 return new BaseSavedState[size];
16613 }
16614 };
16615 }
16616
16617 /**
16618 * A set of information given to a view when it is attached to its parent
16619 * window.
16620 */
16621 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016622 interface Callbacks {
16623 void playSoundEffect(int effectId);
16624 boolean performHapticFeedback(int effectId, boolean always);
16625 }
16626
16627 /**
16628 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16629 * to a Handler. This class contains the target (View) to invalidate and
16630 * the coordinates of the dirty rectangle.
16631 *
16632 * For performance purposes, this class also implements a pool of up to
16633 * POOL_LIMIT objects that get reused. This reduces memory allocations
16634 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016635 */
Romain Guyd928d682009-03-31 17:52:16 -070016636 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016637 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016638 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16639 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016640 public InvalidateInfo newInstance() {
16641 return new InvalidateInfo();
16642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016643
Romain Guyd928d682009-03-31 17:52:16 -070016644 public void onAcquired(InvalidateInfo element) {
16645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016646
Romain Guyd928d682009-03-31 17:52:16 -070016647 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016648 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016649 }
16650 }, POOL_LIMIT)
16651 );
16652
16653 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016654 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016655
16656 View target;
16657
16658 int left;
16659 int top;
16660 int right;
16661 int bottom;
16662
Romain Guyd928d682009-03-31 17:52:16 -070016663 public void setNextPoolable(InvalidateInfo element) {
16664 mNext = element;
16665 }
16666
16667 public InvalidateInfo getNextPoolable() {
16668 return mNext;
16669 }
16670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016671 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016672 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016673 }
16674
16675 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016676 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016677 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016678
16679 public boolean isPooled() {
16680 return mIsPooled;
16681 }
16682
16683 public void setPooled(boolean isPooled) {
16684 mIsPooled = isPooled;
16685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016686 }
16687
16688 final IWindowSession mSession;
16689
16690 final IWindow mWindow;
16691
16692 final IBinder mWindowToken;
16693
16694 final Callbacks mRootCallbacks;
16695
Romain Guy59a12ca2011-06-09 17:48:21 -070016696 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016698 /**
16699 * The top view of the hierarchy.
16700 */
16701 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016703 IBinder mPanelParentWindowToken;
16704 Surface mSurface;
16705
Romain Guyb051e892010-09-28 19:09:36 -070016706 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016707 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016708 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016709
Romain Guy7e4e5612012-03-05 14:37:29 -080016710 boolean mScreenOn;
16711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016712 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016713 * Scale factor used by the compatibility mode
16714 */
16715 float mApplicationScale;
16716
16717 /**
16718 * Indicates whether the application is in compatibility mode
16719 */
16720 boolean mScalingRequired;
16721
16722 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016723 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016724 */
16725 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016726
Dianne Hackborn63042d62011-01-26 18:56:29 -080016727 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016728 * Left position of this view's window
16729 */
16730 int mWindowLeft;
16731
16732 /**
16733 * Top position of this view's window
16734 */
16735 int mWindowTop;
16736
16737 /**
Adam Powell26153a32010-11-08 15:22:27 -080016738 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016739 */
Adam Powell26153a32010-11-08 15:22:27 -080016740 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016741
16742 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016743 * For windows that are full-screen but using insets to layout inside
16744 * of the screen decorations, these are the current insets for the
16745 * content of the window.
16746 */
16747 final Rect mContentInsets = new Rect();
16748
16749 /**
16750 * For windows that are full-screen but using insets to layout inside
16751 * of the screen decorations, these are the current insets for the
16752 * actual visible parts of the window.
16753 */
16754 final Rect mVisibleInsets = new Rect();
16755
16756 /**
16757 * The internal insets given by this window. This value is
16758 * supplied by the client (through
16759 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16760 * be given to the window manager when changed to be used in laying
16761 * out windows behind it.
16762 */
16763 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16764 = new ViewTreeObserver.InternalInsetsInfo();
16765
16766 /**
16767 * All views in the window's hierarchy that serve as scroll containers,
16768 * used to determine if the window can be resized or must be panned
16769 * to adjust for a soft input area.
16770 */
16771 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16772
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016773 final KeyEvent.DispatcherState mKeyDispatchState
16774 = new KeyEvent.DispatcherState();
16775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016776 /**
16777 * Indicates whether the view's window currently has the focus.
16778 */
16779 boolean mHasWindowFocus;
16780
16781 /**
16782 * The current visibility of the window.
16783 */
16784 int mWindowVisibility;
16785
16786 /**
16787 * Indicates the time at which drawing started to occur.
16788 */
16789 long mDrawingTime;
16790
16791 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016792 * Indicates whether or not ignoring the DIRTY_MASK flags.
16793 */
16794 boolean mIgnoreDirtyState;
16795
16796 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016797 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16798 * to avoid clearing that flag prematurely.
16799 */
16800 boolean mSetIgnoreDirtyState = false;
16801
16802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016803 * Indicates whether the view's window is currently in touch mode.
16804 */
16805 boolean mInTouchMode;
16806
16807 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016808 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016809 * the next time it performs a traversal
16810 */
16811 boolean mRecomputeGlobalAttributes;
16812
16813 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016814 * Always report new attributes at next traversal.
16815 */
16816 boolean mForceReportNewAttributes;
16817
16818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016819 * Set during a traveral if any views want to keep the screen on.
16820 */
16821 boolean mKeepScreenOn;
16822
16823 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016824 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16825 */
16826 int mSystemUiVisibility;
16827
16828 /**
16829 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16830 * attached.
16831 */
16832 boolean mHasSystemUiListeners;
16833
16834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016835 * Set if the visibility of any views has changed.
16836 */
16837 boolean mViewVisibilityChanged;
16838
16839 /**
16840 * Set to true if a view has been scrolled.
16841 */
16842 boolean mViewScrollChanged;
16843
16844 /**
16845 * Global to the view hierarchy used as a temporary for dealing with
16846 * x/y points in the transparent region computations.
16847 */
16848 final int[] mTransparentLocation = new int[2];
16849
16850 /**
16851 * Global to the view hierarchy used as a temporary for dealing with
16852 * x/y points in the ViewGroup.invalidateChild implementation.
16853 */
16854 final int[] mInvalidateChildLocation = new int[2];
16855
Chet Haasec3aa3612010-06-17 08:50:37 -070016856
16857 /**
16858 * Global to the view hierarchy used as a temporary for dealing with
16859 * x/y location when view is transformed.
16860 */
16861 final float[] mTmpTransformLocation = new float[2];
16862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016863 /**
16864 * The view tree observer used to dispatch global events like
16865 * layout, pre-draw, touch mode change, etc.
16866 */
16867 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16868
16869 /**
16870 * A Canvas used by the view hierarchy to perform bitmap caching.
16871 */
16872 Canvas mCanvas;
16873
16874 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016875 * The view root impl.
16876 */
16877 final ViewRootImpl mViewRootImpl;
16878
16879 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016880 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016881 * handler can be used to pump events in the UI events queue.
16882 */
16883 final Handler mHandler;
16884
16885 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016886 * Temporary for use in computing invalidate rectangles while
16887 * calling up the hierarchy.
16888 */
16889 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016890
16891 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016892 * Temporary for use in computing hit areas with transformed views
16893 */
16894 final RectF mTmpTransformRect = new RectF();
16895
16896 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016897 * Temporary list for use in collecting focusable descendents of a view.
16898 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070016899 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070016900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016901 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016902 * The id of the window for accessibility purposes.
16903 */
16904 int mAccessibilityWindowId = View.NO_ID;
16905
16906 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070016907 * Whether to ingore not exposed for accessibility Views when
16908 * reporting the view tree to accessibility services.
16909 */
16910 boolean mIncludeNotImportantViews;
16911
16912 /**
16913 * The drawable for highlighting accessibility focus.
16914 */
16915 Drawable mAccessibilityFocusDrawable;
16916
16917 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070016918 * Show where the margins, bounds and layout bounds are for each view.
16919 */
16920 final boolean mDebugLayout = SystemProperties.getBoolean("debug.layout", false);
16921
16922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016923 * Creates a new set of attachment information with the specified
16924 * events handler and thread.
16925 *
16926 * @param handler the events handler the view must use
16927 */
16928 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016929 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016930 mSession = session;
16931 mWindow = window;
16932 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016933 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016934 mHandler = handler;
16935 mRootCallbacks = effectPlayer;
16936 }
16937 }
16938
16939 /**
16940 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16941 * is supported. This avoids keeping too many unused fields in most
16942 * instances of View.</p>
16943 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016944 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016945
Mike Cleronf116bf82009-09-27 19:14:12 -070016946 /**
16947 * Scrollbars are not visible
16948 */
16949 public static final int OFF = 0;
16950
16951 /**
16952 * Scrollbars are visible
16953 */
16954 public static final int ON = 1;
16955
16956 /**
16957 * Scrollbars are fading away
16958 */
16959 public static final int FADING = 2;
16960
16961 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016963 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016964 public int scrollBarDefaultDelayBeforeFade;
16965 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016966
16967 public int scrollBarSize;
16968 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016969 public float[] interpolatorValues;
16970 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016971
16972 public final Paint paint;
16973 public final Matrix matrix;
16974 public Shader shader;
16975
Mike Cleronf116bf82009-09-27 19:14:12 -070016976 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16977
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016978 private static final float[] OPAQUE = { 255 };
16979 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016980
Mike Cleronf116bf82009-09-27 19:14:12 -070016981 /**
16982 * When fading should start. This time moves into the future every time
16983 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16984 */
16985 public long fadeStartTime;
16986
16987
16988 /**
16989 * The current state of the scrollbars: ON, OFF, or FADING
16990 */
16991 public int state = OFF;
16992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016993 private int mLastColor;
16994
Mike Cleronf116bf82009-09-27 19:14:12 -070016995 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016996 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16997 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016998 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16999 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017000
17001 paint = new Paint();
17002 matrix = new Matrix();
17003 // use use a height of 1, and then wack the matrix each time we
17004 // actually use it.
17005 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017007 paint.setShader(shader);
17008 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017009 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017010 }
Romain Guy8506ab42009-06-11 17:35:47 -070017011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017012 public void setFadeColor(int color) {
17013 if (color != 0 && color != mLastColor) {
17014 mLastColor = color;
17015 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017016
Romain Guye55e1a72009-08-27 10:42:26 -070017017 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17018 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017020 paint.setShader(shader);
17021 // Restore the default transfer mode (src_over)
17022 paint.setXfermode(null);
17023 }
17024 }
Joe Malin32736f02011-01-19 16:14:20 -080017025
Mike Cleronf116bf82009-09-27 19:14:12 -070017026 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017027 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017028 if (now >= fadeStartTime) {
17029
17030 // the animation fades the scrollbars out by changing
17031 // the opacity (alpha) from fully opaque to fully
17032 // transparent
17033 int nextFrame = (int) now;
17034 int framesCount = 0;
17035
17036 Interpolator interpolator = scrollBarInterpolator;
17037
17038 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017039 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017040
17041 // End transparent
17042 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017043 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017044
17045 state = FADING;
17046
17047 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017048 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017049 }
17050 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017051 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017052
Svetoslav Ganova0156172011-06-26 17:55:44 -070017053 /**
17054 * Resuable callback for sending
17055 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17056 */
17057 private class SendViewScrolledAccessibilityEvent implements Runnable {
17058 public volatile boolean mIsPending;
17059
17060 public void run() {
17061 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17062 mIsPending = false;
17063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017064 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017065
17066 /**
17067 * <p>
17068 * This class represents a delegate that can be registered in a {@link View}
17069 * to enhance accessibility support via composition rather via inheritance.
17070 * It is specifically targeted to widget developers that extend basic View
17071 * classes i.e. classes in package android.view, that would like their
17072 * applications to be backwards compatible.
17073 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017074 * <div class="special reference">
17075 * <h3>Developer Guides</h3>
17076 * <p>For more information about making applications accessible, read the
17077 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17078 * developer guide.</p>
17079 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017080 * <p>
17081 * A scenario in which a developer would like to use an accessibility delegate
17082 * is overriding a method introduced in a later API version then the minimal API
17083 * version supported by the application. For example, the method
17084 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17085 * in API version 4 when the accessibility APIs were first introduced. If a
17086 * developer would like his application to run on API version 4 devices (assuming
17087 * all other APIs used by the application are version 4 or lower) and take advantage
17088 * of this method, instead of overriding the method which would break the application's
17089 * backwards compatibility, he can override the corresponding method in this
17090 * delegate and register the delegate in the target View if the API version of
17091 * the system is high enough i.e. the API version is same or higher to the API
17092 * version that introduced
17093 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17094 * </p>
17095 * <p>
17096 * Here is an example implementation:
17097 * </p>
17098 * <code><pre><p>
17099 * if (Build.VERSION.SDK_INT >= 14) {
17100 * // If the API version is equal of higher than the version in
17101 * // which onInitializeAccessibilityNodeInfo was introduced we
17102 * // register a delegate with a customized implementation.
17103 * View view = findViewById(R.id.view_id);
17104 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17105 * public void onInitializeAccessibilityNodeInfo(View host,
17106 * AccessibilityNodeInfo info) {
17107 * // Let the default implementation populate the info.
17108 * super.onInitializeAccessibilityNodeInfo(host, info);
17109 * // Set some other information.
17110 * info.setEnabled(host.isEnabled());
17111 * }
17112 * });
17113 * }
17114 * </code></pre></p>
17115 * <p>
17116 * This delegate contains methods that correspond to the accessibility methods
17117 * in View. If a delegate has been specified the implementation in View hands
17118 * off handling to the corresponding method in this delegate. The default
17119 * implementation the delegate methods behaves exactly as the corresponding
17120 * method in View for the case of no accessibility delegate been set. Hence,
17121 * to customize the behavior of a View method, clients can override only the
17122 * corresponding delegate method without altering the behavior of the rest
17123 * accessibility related methods of the host view.
17124 * </p>
17125 */
17126 public static class AccessibilityDelegate {
17127
17128 /**
17129 * Sends an accessibility event of the given type. If accessibility is not
17130 * enabled this method has no effect.
17131 * <p>
17132 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17133 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17134 * been set.
17135 * </p>
17136 *
17137 * @param host The View hosting the delegate.
17138 * @param eventType The type of the event to send.
17139 *
17140 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17141 */
17142 public void sendAccessibilityEvent(View host, int eventType) {
17143 host.sendAccessibilityEventInternal(eventType);
17144 }
17145
17146 /**
17147 * Sends an accessibility event. This method behaves exactly as
17148 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17149 * empty {@link AccessibilityEvent} and does not perform a check whether
17150 * accessibility is enabled.
17151 * <p>
17152 * The default implementation behaves as
17153 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17154 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17155 * the case of no accessibility delegate been set.
17156 * </p>
17157 *
17158 * @param host The View hosting the delegate.
17159 * @param event The event to send.
17160 *
17161 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17162 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17163 */
17164 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17165 host.sendAccessibilityEventUncheckedInternal(event);
17166 }
17167
17168 /**
17169 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17170 * to its children for adding their text content to the event.
17171 * <p>
17172 * The default implementation behaves as
17173 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17174 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17175 * the case of no accessibility delegate been set.
17176 * </p>
17177 *
17178 * @param host The View hosting the delegate.
17179 * @param event The event.
17180 * @return True if the event population was completed.
17181 *
17182 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17183 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17184 */
17185 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17186 return host.dispatchPopulateAccessibilityEventInternal(event);
17187 }
17188
17189 /**
17190 * Gives a chance to the host View to populate the accessibility event with its
17191 * text content.
17192 * <p>
17193 * The default implementation behaves as
17194 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17195 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17196 * the case of no accessibility delegate been set.
17197 * </p>
17198 *
17199 * @param host The View hosting the delegate.
17200 * @param event The accessibility event which to populate.
17201 *
17202 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17203 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17204 */
17205 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17206 host.onPopulateAccessibilityEventInternal(event);
17207 }
17208
17209 /**
17210 * Initializes an {@link AccessibilityEvent} with information about the
17211 * the host View which is the event source.
17212 * <p>
17213 * The default implementation behaves as
17214 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17215 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17216 * the case of no accessibility delegate been set.
17217 * </p>
17218 *
17219 * @param host The View hosting the delegate.
17220 * @param event The event to initialize.
17221 *
17222 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17223 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17224 */
17225 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17226 host.onInitializeAccessibilityEventInternal(event);
17227 }
17228
17229 /**
17230 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17231 * <p>
17232 * The default implementation behaves as
17233 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17234 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17235 * the case of no accessibility delegate been set.
17236 * </p>
17237 *
17238 * @param host The View hosting the delegate.
17239 * @param info The instance to initialize.
17240 *
17241 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17242 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17243 */
17244 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17245 host.onInitializeAccessibilityNodeInfoInternal(info);
17246 }
17247
17248 /**
17249 * Called when a child of the host View has requested sending an
17250 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17251 * to augment the event.
17252 * <p>
17253 * The default implementation behaves as
17254 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17255 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17256 * the case of no accessibility delegate been set.
17257 * </p>
17258 *
17259 * @param host The View hosting the delegate.
17260 * @param child The child which requests sending the event.
17261 * @param event The event to be sent.
17262 * @return True if the event should be sent
17263 *
17264 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17265 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17266 */
17267 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17268 AccessibilityEvent event) {
17269 return host.onRequestSendAccessibilityEventInternal(child, event);
17270 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017271
17272 /**
17273 * Gets the provider for managing a virtual view hierarchy rooted at this View
17274 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17275 * that explore the window content.
17276 * <p>
17277 * The default implementation behaves as
17278 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17279 * the case of no accessibility delegate been set.
17280 * </p>
17281 *
17282 * @return The provider.
17283 *
17284 * @see AccessibilityNodeProvider
17285 */
17286 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17287 return null;
17288 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017290}